@@ -7,7 +7,10 @@ import {
7
7
createMockWebSocket ,
8
8
} from "testHelpers/websockets" ;
9
9
import { OneWayWebSocket } from "utils/OneWayWebSocket" ;
10
- import { type OnError , createUseAgentLogs } from "./useAgentLogs" ;
10
+ import {
11
+ type CreateUseAgentLogsOptions ,
12
+ createUseAgentLogs ,
13
+ } from "./useAgentLogs" ;
11
14
12
15
const millisecondsInOneMinute = 60_000 ;
13
16
@@ -30,15 +33,15 @@ function generateMockLogs(
30
33
} ) ;
31
34
}
32
35
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
36
39
type PublisherResult = { current : MockWebSocketPublisher | undefined } ;
37
40
38
41
type MountHookOptions = Readonly < {
39
42
initialAgentId : string ;
40
43
enabled ?: boolean ;
41
- onError ?: OnError ;
44
+ onError ?: CreateUseAgentLogsOptions [ "onError" ] ;
42
45
} > ;
43
46
44
47
type MountHookResult = Readonly < {
@@ -54,20 +57,23 @@ function mountHook(options: MountHookOptions): MountHookResult {
54
57
const { initialAgentId, enabled = true , onError = jest . fn ( ) } = options ;
55
58
56
59
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
+ } ) ;
71
77
72
78
const { result, rerender } = renderHook (
73
79
( { agentId, enabled } ) => useAgentLogs ( agentId , enabled ) ,
@@ -116,10 +122,10 @@ describe("useAgentLogs", () => {
116
122
initialAgentId : MockWorkspaceAgent . id ,
117
123
} ) ;
118
124
119
- expect ( publisherResult . current ?. isConnectionOpen ( ) ) . toBe ( true ) ;
125
+ expect ( publisherResult . current ?. isConnectionOpen ) . toBe ( true ) ;
120
126
rerender ( { agentId : MockWorkspaceAgent . id , enabled : false } ) ;
121
127
await waitFor ( ( ) => {
122
- expect ( publisherResult . current ?. isConnectionOpen ( ) ) . toBe ( false ) ;
128
+ expect ( publisherResult . current ?. isConnectionOpen ) . toBe ( false ) ;
123
129
} ) ;
124
130
} ) ;
125
131
@@ -129,14 +135,17 @@ describe("useAgentLogs", () => {
129
135
} ) ;
130
136
131
137
const publisher1 = publisherResult . current ;
132
- expect ( publisher1 ?. isConnectionOpen ( ) ) . toBe ( true ) ;
138
+ expect ( publisher1 ?. isConnectionOpen ) . toBe ( true ) ;
133
139
134
- const newAgentId = `${ MockWorkspaceAgent . id } -2` ;
135
- rerender ( { agentId : newAgentId , enabled : true } ) ;
140
+ rerender ( {
141
+ enabled : true ,
142
+ agentId : `${ MockWorkspaceAgent . id } -new-value` ,
143
+ } ) ;
136
144
137
145
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 ) ;
140
149
} ) ;
141
150
142
151
it ( "Calls error callback when error is received (but only while hook is enabled)" , async ( ) => {
0 commit comments