Skip to content

Commit 5160e28

Browse files
committed
Fix tests
1 parent 4e4c405 commit 5160e28

File tree

5 files changed

+56
-117
lines changed

5 files changed

+56
-117
lines changed

site/src/modules/resources/AgentLogs/useAgentLogs.test.tsx

Lines changed: 0 additions & 115 deletions
This file was deleted.

site/src/modules/resources/AgentRow.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { AgentDevcontainerCard } from "./AgentDevcontainerCard";
3030
import { AgentLatency } from "./AgentLatency";
3131
import { AGENT_LOG_LINE_HEIGHT } from "./AgentLogs/AgentLogLine";
3232
import { AgentLogs } from "./AgentLogs/AgentLogs";
33-
import { useAgentLogs } from "./AgentLogs/useAgentLogs";
33+
import { useAgentLogs } from "./useAgentLogs";
3434
import { AgentMetadata } from "./AgentMetadata";
3535
import { AgentStatus } from "./AgentStatus";
3636
import { AgentVersion } from "./AgentVersion";
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { renderHook } from "@testing-library/react";
2+
import type { WorkspaceAgentLog } from "api/typesGenerated";
3+
import WS from "jest-websocket-mock";
4+
import { MockWorkspaceAgent } from "testHelpers/entities";
5+
import { useAgentLogs } from "./useAgentLogs";
6+
7+
/**
8+
* TODO: WS does not support multiple tests running at once in isolation so we
9+
* have one single test that test the most common scenario.
10+
* Issue: https://github.com/romgain/jest-websocket-mock/issues/172
11+
*/
12+
13+
describe("useAgentLogs", () => {
14+
afterEach(() => {
15+
WS.clean();
16+
});
17+
18+
it("clear logs when disabled to avoid duplicates", async () => {
19+
const server = new WS(
20+
`ws://localhost/api/v2/workspaceagents/${
21+
MockWorkspaceAgent.id
22+
}/logs?follow&after=0`,
23+
);
24+
const { result, rerender } = renderHook(
25+
({ enabled }) => useAgentLogs(MockWorkspaceAgent, enabled),
26+
{ initialProps: { enabled: true } },
27+
);
28+
await server.connected;
29+
30+
// Send 3 logs
31+
server.send(JSON.stringify(generateLogs(3)));
32+
expect(result.current).toHaveLength(3);
33+
34+
// Disable the hook
35+
rerender({ enabled: false });
36+
expect(result.current).toBeUndefined();
37+
38+
// Enable the hook again
39+
rerender({ enabled: true });
40+
await server.connected;
41+
server.send(JSON.stringify(generateLogs(3)));
42+
expect(result.current).toHaveLength(3);
43+
});
44+
});
45+
46+
function generateLogs(count: number): WorkspaceAgentLog[] {
47+
return Array.from({ length: count }, (_, i) => ({
48+
id: i,
49+
created_at: new Date().toISOString(),
50+
level: "info",
51+
output: `Log ${i}`,
52+
source_id: "",
53+
}));
54+
}

site/src/pages/WorkspaceBuildPage/WorkspaceBuildPageView.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { useSearchParamsKey } from "hooks/useSearchParamsKey";
2121
import { BuildAvatar } from "modules/builds/BuildAvatar/BuildAvatar";
2222
import { DashboardFullPage } from "modules/dashboard/DashboardLayout";
2323
import { AgentLogs } from "modules/resources/AgentLogs/AgentLogs";
24-
import { useAgentLogs } from "modules/resources/AgentLogs/useAgentLogs";
24+
import { useAgentLogs } from "modules/resources/useAgentLogs";
2525
import {
2626
WorkspaceBuildData,
2727
WorkspaceBuildDataSkeleton,

0 commit comments

Comments
 (0)