-
Notifications
You must be signed in to change notification settings - Fork 956
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?
for (const log of reversed) { | ||
act(() => { | ||
serverResult.current?.publishMessage( | ||
new MessageEvent("message", { data: JSON.stringify([log]) }), | ||
); | ||
}); | ||
} |
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.
for (const log of reversed) { | |
act(() => { | |
serverResult.current?.publishMessage( | |
new MessageEvent("message", { data: JSON.stringify([log]) }), | |
); | |
}); | |
} | |
await act(async () => { | |
for (const log of reversed) { | |
serverResult.current?.publishMessage( | |
new MessageEvent("message", { data: JSON.stringify([log]) }), | |
); | |
} | |
}); |
I'm still trying to build up an intuition for act
, but I noticed while looking at the docs that they now promote calling it with an async
function and await
ing so I'm wondering if this patch makes sense.
reference: https://react.dev/reference/react/act
}); | ||
|
||
const errorEvent = new Event("error"); | ||
act(() => serverResult.current?.publishError(errorEvent)); |
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.
act(() => serverResult.current?.publishError(errorEvent)); | |
await act(() => serverResult.current?.publishError(errorEvent)); |
expect(onError).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("Clears logs when hook becomes disabled (protection to avoid duplicate logs when hook goes back to being re-enabled)", async () => { |
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.
it("Clears logs when hook becomes disabled (protection to avoid duplicate logs when hook goes back to being re-enabled)", async () => { | |
// This is a protection to avoid duplicate logs when the hook is re-enabled | |
it("Clears logs when hook becomes disabled", async () => { |
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.
const initialEvent = new MessageEvent("message", { | ||
data: JSON.stringify(initialLogs), | ||
}); | ||
act(() => serverResult.current?.publishMessage(initialEvent)); |
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.
act(() => serverResult.current?.publishMessage(initialEvent)); | |
await act(async () => serverResult.current?.publishMessage(initialEvent)); |
const newEvent = new MessageEvent("message", { | ||
data: JSON.stringify(newLogs), | ||
}); | ||
act(() => serverResult.current?.publishMessage(newEvent)); |
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.
act(() => serverResult.current?.publishMessage(newEvent)); | |
await act(async () => serverResult.current?.publishMessage(newEvent)); |
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
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)