-
Notifications
You must be signed in to change notification settings - Fork 963
fix(site): update useAgentLogs
to make it more testable and add more tests
#19126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…mes/logs-flake
const stableMetadataResult = useMemo<UseEmbeddedMetadataResult>(() => { | ||
return { | ||
metadata, | ||
clearMetadataByKey: manager.clearMetadataByKey, | ||
}; | ||
}, [metadata]); | ||
}, [manager, metadata]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A case where realistically, the value of manager
won't ever change, but it technically can, since function parameters are reassignable. So better to add it to the dependencies for correctness
const logListRef = useRef<List>(null); | ||
const logListDivRef = useRef<HTMLDivElement>(null); | ||
const startupLogs = useMemo(() => { | ||
const allLogs = agentLogs || []; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agentLogs
is no longer nullable, so a bunch of logic can be removed
@@ -1,60 +1,147 @@ | |||
import { renderHook, waitFor } from "@testing-library/react"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enough changed with the test file that it honestly might not even be worth looking at the old version of the code
useAgentLogs
to make it more testable and add more testsuseAgentLogs
to make it more testable and add more tests
site/src/testHelpers/websockets.ts
Outdated
publishError: (event: Event) => void; | ||
publishClose: (event: CloseEvent) => void; | ||
publishOpen: (event: Event) => void; | ||
readonly isConnectionOpen: boolean; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setup is 99% the same from the old file, just with the addition of this getter method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this mock WebSocket implementation needs some tests of its own, and is probably worth its own PR as well. @jaaydenh has been working on a similar refactor of some other tests that I think uses a copy of this WebSocket mock. It'd be nice to sync on that so that we don't duplicate effort, and then we can get everyones tests reviewed. 😄
@aslilac Yeah, totally fair. I'll check with Jaayden |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd love to have a story for the logs_overflowed
state
(sorry if this is asking a lot, I just keep seeing things that'd be nice to clean up everywhere)
} | ||
return [ | ||
...agentLogs, | ||
{ | ||
id: -1, | ||
level: "error", | ||
output: | ||
"Startup logs exceeded the max size of 1MB, and will not continue to be written to the database! Logs will continue to be written to the /tmp/coder-startup-script.log file in the workspace.", | ||
created_at: new Date().toISOString(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we're leaking in here. I feel like this should be...
created_at: new Date().toISOString(), | |
created_at: agentLogs.at(-1).created_at, |
since that's probably more "correct" and deterministic
|
||
function generateMockLogs( | ||
logCount: number, | ||
baseDate = new Date(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
baseDate = new Date(), | |
baseDate = new Date("Thu, 07 Aug 2025 21:38:12 GMT"), |
as an example, how about just right now
} | ||
return [ | ||
...agentLogs, | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also I know it was already like this, but is shoving a log entry on the end really the best way to do this?
this startupLogs
value eventually gets passed to AgentLogs
below. what if we just passed agentLogs
in unmodified and added an overflowed
prop that shows the warning?
import type { WorkspaceAgent, WorkspaceAgentLog } from "api/typesGenerated"; | ||
import { | ||
type WatchWorkspaceAgentLogsParams, | ||
watchWorkspaceAgentLogs, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if rather than all of this factory stuff we could just mock watchWorkspaceAgentLogs
. I think that might make it easier to get the publisher
value too, but can't say for sure.
return <Loader />; | ||
} | ||
|
||
const logs = useAgentLogs(agent.id, true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
another "I know it was already like this" moment but nondescript booleans like this are dreadful. I'd love to see this argument go from boolean
to { enabled: boolean = true }
(similar to what react-query does, default to on, optimized for the happy path)
function mountHook(options: MountHookOptions): MountHookResult { | ||
const { initialAgentId, enabled = true, onError = jest.fn() } = options; | ||
|
||
const serverResult: ServerResult = { current: undefined }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we stick with this we could just use createRef
and React.RefObject<MockWebSocketServer>
instead of making our own type here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those are not the same. createRef
is for interfacing with class components, and creates a mutable object that's still part of React's data flow
This setup is ejecting a value created during React's lifecycle into an outside function. React has absolutely no idea that this exists, but it's how a lot of React testing tools work under the hood. I get the aversion to having more bespoke types, but switching to the built-in type would be lying about it actually is
@aslilac I think I have this pretty close to done:
The only thing I'm not super clear on is how to style it. I really think that the overflow styling should be represented as a tooltip. The actual message is super short, so we want to constrain the width pretty aggressively for good typography, and a tooltip naturally lends itself well to having those constraints without affecting the layout of the main container The only thing is, I don't really know how to style it. I tried going through Storybook, but their search is so bad that it made it hard to see if we have any components that have the "alert tooltip" behavior already. Do you know of any existing components that fit the bill? |
Take 2
Closes coder/internal#644
Changes made
useAgentLogs
was defined to make it easier to inject specific data dependencies (basically making the hook more unit-testable)