Skip to content

Commit 41c5a12

Browse files
committed
fix: update setup
1 parent 982d3e1 commit 41c5a12

File tree

3 files changed

+54
-43
lines changed

3 files changed

+54
-43
lines changed

site/src/modules/resources/useAgentLogs.test.ts

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import {
77
createMockWebSocket,
88
} from "testHelpers/websockets";
99
import { OneWayWebSocket } from "utils/OneWayWebSocket";
10-
import { type OnError, createUseAgentLogs } from "./useAgentLogs";
10+
import {
11+
type CreateUseAgentLogsOptions,
12+
createUseAgentLogs,
13+
} from "./useAgentLogs";
1114

1215
const millisecondsInOneMinute = 60_000;
1316

@@ -30,15 +33,15 @@ function generateMockLogs(
3033
});
3134
}
3235

33-
// A mutable object holding the most recent mock WebSocket publisher. Inner
34-
// value will be undefined if the hook is disabled on mount, but will otherwise
35-
// have some kind of value
36+
// A mutable object holding the most recent mock WebSocket publisher that was
37+
// created when initializing a mock WebSocket. Inner value will be undefined if
38+
// the hook is disabled on mount, but will always be defined otherwise
3639
type PublisherResult = { current: MockWebSocketPublisher | undefined };
3740

3841
type MountHookOptions = Readonly<{
3942
initialAgentId: string;
4043
enabled?: boolean;
41-
onError?: OnError;
44+
onError?: CreateUseAgentLogsOptions["onError"];
4245
}>;
4346

4447
type MountHookResult = Readonly<{
@@ -54,20 +57,23 @@ function mountHook(options: MountHookOptions): MountHookResult {
5457
const { initialAgentId, enabled = true, onError = jest.fn() } = options;
5558

5659
const publisherResult: PublisherResult = { current: undefined };
57-
const useAgentLogs = createUseAgentLogs((agentId, params) => {
58-
return new OneWayWebSocket({
59-
apiRoute: `/api/v2/workspaceagents/${agentId}/logs`,
60-
searchParams: new URLSearchParams({
61-
follow: "true",
62-
after: params?.after?.toString() || "0",
63-
}),
64-
websocketInit: (url) => {
65-
const [mockSocket, mockPublisher] = createMockWebSocket(url);
66-
publisherResult.current = mockPublisher;
67-
return mockSocket;
68-
},
69-
});
70-
}, onError);
60+
const useAgentLogs = createUseAgentLogs({
61+
onError,
62+
createSocket: (agentId, params) => {
63+
return new OneWayWebSocket({
64+
apiRoute: `/api/v2/workspaceagents/${agentId}/logs`,
65+
searchParams: new URLSearchParams({
66+
follow: "true",
67+
after: params?.after?.toString() || "0",
68+
}),
69+
websocketInit: (url) => {
70+
const [mockSocket, mockPublisher] = createMockWebSocket(url);
71+
publisherResult.current = mockPublisher;
72+
return mockSocket;
73+
},
74+
});
75+
},
76+
});
7177

7278
const { result, rerender } = renderHook(
7379
({ agentId, enabled }) => useAgentLogs(agentId, enabled),
@@ -116,10 +122,10 @@ describe("useAgentLogs", () => {
116122
initialAgentId: MockWorkspaceAgent.id,
117123
});
118124

119-
expect(publisherResult.current?.isConnectionOpen()).toBe(true);
125+
expect(publisherResult.current?.isConnectionOpen).toBe(true);
120126
rerender({ agentId: MockWorkspaceAgent.id, enabled: false });
121127
await waitFor(() => {
122-
expect(publisherResult.current?.isConnectionOpen()).toBe(false);
128+
expect(publisherResult.current?.isConnectionOpen).toBe(false);
123129
});
124130
});
125131

@@ -129,14 +135,17 @@ describe("useAgentLogs", () => {
129135
});
130136

131137
const publisher1 = publisherResult.current;
132-
expect(publisher1?.isConnectionOpen()).toBe(true);
138+
expect(publisher1?.isConnectionOpen).toBe(true);
133139

134-
const newAgentId = `${MockWorkspaceAgent.id}-2`;
135-
rerender({ agentId: newAgentId, enabled: true });
140+
rerender({
141+
enabled: true,
142+
agentId: `${MockWorkspaceAgent.id}-new-value`,
143+
});
136144

137145
const publisher2 = publisherResult.current;
138-
expect(publisher1?.isConnectionOpen()).toBe(false);
139-
expect(publisher2?.isConnectionOpen()).toBe(true);
146+
expect(publisher1).not.toBe(publisher2);
147+
expect(publisher1?.isConnectionOpen).toBe(false);
148+
expect(publisher2?.isConnectionOpen).toBe(true);
140149
});
141150

142151
it("Calls error callback when error is received (but only while hook is enabled)", async () => {

site/src/modules/resources/useAgentLogs.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@ import { displayError } from "components/GlobalSnackbar/utils";
77
import { useEffect, useState } from "react";
88
import type { OneWayWebSocket } from "utils/OneWayWebSocket";
99

10-
type CreateSocket = (
11-
agentId: string,
12-
params?: WatchWorkspaceAgentLogsParams,
13-
) => OneWayWebSocket<WorkspaceAgentLog[]>;
14-
15-
export type OnError = (errorEvent: Event) => void;
10+
export type CreateUseAgentLogsOptions = Readonly<{
11+
onError: (errorEvent: Event) => void;
12+
createSocket: (
13+
agentId: string,
14+
params?: WatchWorkspaceAgentLogsParams,
15+
) => OneWayWebSocket<WorkspaceAgentLog[]>;
16+
}>;
1617

17-
export function createUseAgentLogs(
18-
createSocket: CreateSocket,
19-
onError: OnError,
20-
) {
18+
export function createUseAgentLogs(options: CreateUseAgentLogsOptions) {
19+
const { createSocket, onError } = options;
2120
return function useAgentLogs(
2221
agentId: string,
2322
enabled: boolean,
@@ -70,20 +69,20 @@ export function createUseAgentLogs(
7069
});
7170

7271
return () => socket.close();
73-
}, [createSocket, onError, agentId, enabled]);
72+
}, [agentId, enabled]);
7473

7574
return logs;
7675
};
7776
}
7877

7978
// The baseline implementation to use for production
80-
export const useAgentLogs = createUseAgentLogs(
81-
watchWorkspaceAgentLogs,
82-
(errorEvent) => {
79+
export const useAgentLogs = createUseAgentLogs({
80+
createSocket: watchWorkspaceAgentLogs,
81+
onError: (errorEvent) => {
8382
console.error("Error in agent log socket: ", errorEvent);
8483
displayError(
8584
"Unable to watch agent logs",
8685
"Please try refreshing the browser",
8786
);
8887
},
89-
);
88+
});

site/src/testHelpers/websockets.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export type MockWebSocketPublisher = Readonly<{
55
publishError: (event: Event) => void;
66
publishClose: (event: CloseEvent) => void;
77
publishOpen: (event: Event) => void;
8-
isConnectionOpen: () => boolean;
8+
readonly isConnectionOpen: boolean;
99
}>;
1010

1111
export function createMockWebSocket(
@@ -95,7 +95,10 @@ export function createMockWebSocket(
9595
};
9696

9797
const publisher: MockWebSocketPublisher = {
98-
isConnectionOpen: () => isOpen,
98+
get isConnectionOpen() {
99+
return isOpen;
100+
},
101+
99102
publishOpen: (event) => {
100103
if (!isOpen) {
101104
return;

0 commit comments

Comments
 (0)