Skip to content

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

Open
wants to merge 34 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
d6e00c3
wip: commit progress on test update
Parkreiner May 23, 2025
43d0ca8
refactor: update useAgentLogs tests as unit tests
Parkreiner May 23, 2025
727bddd
docs: rewrite comment for clarity
Parkreiner May 23, 2025
d46d144
fix: remove unnecessary type
Parkreiner May 23, 2025
91a6fc1
fix: make sure logs have different timestamps
Parkreiner May 23, 2025
ecbe7b0
fix: add different dates to reduce risk of false positives
Parkreiner May 23, 2025
d13bcdc
Merge branch 'main' into mes/logs-flake
Parkreiner May 23, 2025
0f21097
Merge branch 'main' into mes/logs-flake
Parkreiner Aug 2, 2025
abd6553
refactor: decrease coupling
Parkreiner Aug 2, 2025
bade97a
wip: commit progress on updating flake
Parkreiner Aug 2, 2025
e11fefd
Merge branch 'mes/logs-flake' of https://github.com/coder/coder into …
Parkreiner Aug 2, 2025
bc3d095
fix: get all tests passing
Parkreiner Aug 2, 2025
550d09e
chore: add one more test case
Parkreiner Aug 2, 2025
cc7e632
fix: update type mismatches
Parkreiner Aug 2, 2025
79c7ffd
refactor: clean up some code
Parkreiner Aug 2, 2025
43a0d3a
fix: make testing boundaries more formal
Parkreiner Aug 2, 2025
982d3e1
fix: remove premature optimization
Parkreiner Aug 2, 2025
41c5a12
fix: update setup
Parkreiner Aug 4, 2025
42cb73b
fix: update state sync logic
Parkreiner Aug 4, 2025
3a5f7bb
Merge branch 'main' into mes/logs-flake
Parkreiner Aug 4, 2025
35a40df
fix: update wonky types
Parkreiner Aug 4, 2025
306dbc7
Merge branch 'main' into mes/logs-flake
Parkreiner Aug 4, 2025
f49e55a
Merge branch 'main' into mes/logs-flake
Parkreiner Aug 7, 2025
c2fc772
fix: update tests
Parkreiner Aug 7, 2025
2cabd85
fix: format
Parkreiner Aug 7, 2025
855f3ca
Merge branch 'main' into mes/logs-flake
Parkreiner Aug 9, 2025
453894b
fix: apply initial feedback
Parkreiner Aug 9, 2025
c9f2b12
wip: commit refactoring progress
Parkreiner Aug 9, 2025
80865fe
refactor: update assignment
Parkreiner Aug 9, 2025
f930b29
wip: prepare to change indents
Parkreiner Aug 9, 2025
a311ac9
fix: update keygen logic
Parkreiner Aug 9, 2025
5657536
chore: add basic overflow message
Parkreiner Aug 9, 2025
6547a2f
chore: swap to tailwind
Parkreiner Aug 9, 2025
c818aec
wip: commit progress
Parkreiner Aug 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: get all tests passing
  • Loading branch information
Parkreiner committed Aug 2, 2025
commit bc3d095aaa15e3695b2ee5e735dca7c407b8aa9a
20 changes: 11 additions & 9 deletions site/src/modules/resources/useAgentLogs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from "testHelpers/websockets";
import { OneWayWebSocket } from "utils/OneWayWebSocket";
import { createUseAgentLogs } from "./useAgentLogs";
import { act } from "react";

const millisecondsInOneMinute = 60_000;

Expand Down Expand Up @@ -82,14 +83,15 @@ describe("useAgentLogs", () => {
it("Automatically sorts logs that are received out of order", async () => {
const { hookResult, publisherResult } = mountHook();
const logs = generateMockLogs(10, new Date("september 9, 1999"));
const reversed = logs.toReversed();

for (const log of reversed) {
publisherResult.current.publishMessage(
new MessageEvent<string>("message", {
data: JSON.stringify(log),
}),
)
for (const log of logs.toReversed()) {
act(() => {
publisherResult.current.publishMessage(
new MessageEvent<string>("message", {
data: JSON.stringify([log]),
}),
)
})
}
await waitFor(() => expect(hookResult.current).toEqual(logs));
});
Expand All @@ -111,7 +113,7 @@ describe("useAgentLogs", () => {
const initialEvent = new MessageEvent<string>("message", {
data: JSON.stringify(initialLogs),
});
publisherResult.current.publishMessage(initialEvent);
act(() => publisherResult.current.publishMessage(initialEvent));
await waitFor(() => expect(hookResult.current).toEqual(initialLogs));

// Disable the hook (and have the hook close the connection behind the
Expand All @@ -126,7 +128,7 @@ describe("useAgentLogs", () => {
const newEvent = new MessageEvent<string>("message", {
data: JSON.stringify(newLogs),
});
publisherResult.current.publishMessage(newEvent);
act(() => publisherResult.current.publishMessage(newEvent));
await waitFor(() => expect(hookResult.current).toEqual(newLogs));
});
});
27 changes: 25 additions & 2 deletions site/src/modules/resources/useAgentLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,42 @@ export function createUseAgentLogs(createSocket: CreateSocket) {
// Always fetch the logs from the beginning. We may want to optimize
// this in the future, but it would add some complexity in the code
// that might not be worth it.
const createdAtMap = new Map<string, number>();
const socket = createSocket(agent.id, { after: 0 });
socket.addEventListener("message", (e) => {
if (e.parseError) {
console.warn("Error parsing agent log: ", e.parseError);
return;
}
setLogs((logs) => [...logs, ...e.parsedMessage]);

if (e.parsedMessage.length === 0) {
return;
}

setLogs((logs) => {
const newLogs = [...logs, ...e.parsedMessage];
newLogs.sort((l1, l2) => {
let d1 = createdAtMap.get(l1.created_at);
if (d1 === undefined) {
d1 = (new Date(l1.created_at)).getTime();
createdAtMap.set(l1.created_at, d1);
}
let d2 = createdAtMap.get(l2.created_at);
if (d2 === undefined) {
d2 = (new Date(l2.created_at)).getTime();
createdAtMap.set(l2.created_at, d2);
}
return d1 - d2;
});

return newLogs;
});
});

socket.addEventListener("error", (e) => {
console.error("Error in agent log socket: ", e);
displayError(
"Unable to watch the agent logs",
"Unable to watch agent logs",
"Please try refreshing the browser",
);
socket.close();
Expand Down