@@ -4,23 +4,19 @@ import type { WorkspaceAgentListContainersResponse } from "api/typesGenerated";
4
4
import * as GlobalSnackbar from "components/GlobalSnackbar/utils" ;
5
5
import { http , HttpResponse } from "msw" ;
6
6
import type { FC , PropsWithChildren } from "react" ;
7
- import { QueryClient , QueryClientProvider } from "react-query" ;
7
+ import { act } from "react" ;
8
+ import { QueryClientProvider } from "react-query" ;
8
9
import {
9
10
MockWorkspaceAgent ,
10
11
MockWorkspaceAgentDevcontainer ,
11
12
} from "testHelpers/entities" ;
13
+ import { createTestQueryClient } from "testHelpers/renderHelpers" ;
12
14
import { server } from "testHelpers/server" ;
13
15
import type { OneWayWebSocket } from "utils/OneWayWebSocket" ;
14
16
import { useAgentContainers } from "./useAgentContainers" ;
15
17
16
18
const createWrapper = ( ) :FC < PropsWithChildren > => {
17
- const queryClient = new QueryClient ( {
18
- defaultOptions :{
19
- queries :{
20
- retry :false ,
21
- } ,
22
- } ,
23
- } ) ;
19
+ const queryClient = createTestQueryClient ( ) ;
24
20
return ( { children} ) => (
25
21
< QueryClientProvider client = { queryClient } > { children } </ QueryClientProvider >
26
22
) ;
@@ -111,22 +107,29 @@ describe("useAgentContainers", () => {
111
107
) ,
112
108
) ;
113
109
114
- const { unmount} = renderHook (
110
+ const { result , unmount} = renderHook (
115
111
( ) => useAgentContainers ( MockWorkspaceAgent ) ,
116
112
{
117
113
wrapper :createWrapper ( ) ,
118
114
} ,
119
115
) ;
120
116
121
- // Simulate message event with parsing error
117
+ // Wait for initial query to complete
118
+ await waitFor ( ( ) => {
119
+ expect ( result . current ) . toEqual ( [ MockWorkspaceAgentDevcontainer ] ) ;
120
+ } ) ;
121
+
122
+ // Now simulate message event with parsing error
122
123
const messageHandler = mockSocket . addEventListener . mock . calls . find (
123
124
( call ) => call [ 0 ] === "message" ,
124
125
) ?. [ 1 ] ;
125
126
126
127
if ( messageHandler ) {
127
- messageHandler ( {
128
- parseError :new Error ( "Parse error" ) ,
129
- parsedMessage :null ,
128
+ act ( ( ) => {
129
+ messageHandler ( {
130
+ parseError :new Error ( "Parse error" ) ,
131
+ parsedMessage :null ,
132
+ } ) ;
130
133
} ) ;
131
134
}
132
135
@@ -166,20 +169,27 @@ describe("useAgentContainers", () => {
166
169
) ,
167
170
) ;
168
171
169
- const { unmount} = renderHook (
172
+ const { result , unmount} = renderHook (
170
173
( ) => useAgentContainers ( MockWorkspaceAgent ) ,
171
174
{
172
175
wrapper :createWrapper ( ) ,
173
176
} ,
174
177
) ;
175
178
176
- // Simulate error event
179
+ // Wait for initial query to complete
180
+ await waitFor ( ( ) => {
181
+ expect ( result . current ) . toEqual ( [ MockWorkspaceAgentDevcontainer ] ) ;
182
+ } ) ;
183
+
184
+ // Now simulate error event
177
185
const errorHandler = mockSocket . addEventListener . mock . calls . find (
178
186
( call ) => call [ 0 ] === "error" ,
179
187
) ?. [ 1 ] ;
180
188
181
189
if ( errorHandler ) {
182
- errorHandler ( new Error ( "WebSocket error" ) ) ;
190
+ act ( ( ) => {
191
+ errorHandler ( new Error ( "WebSocket error" ) ) ;
192
+ } ) ;
183
193
}
184
194
185
195
await waitFor ( ( ) => {
@@ -211,4 +221,36 @@ describe("useAgentContainers", () => {
211
221
212
222
watchAgentContainersSpy . mockRestore ( ) ;
213
223
} ) ;
224
+
225
+ it ( "does not establish WebSocket connection when dev container feature is not enabled" , async ( ) => {
226
+ const watchAgentContainersSpy = jest . spyOn ( API , "watchAgentContainers" ) ;
227
+
228
+ server . use (
229
+ http . get (
230
+ `/api/v2/workspaceagents/${ MockWorkspaceAgent . id } /containers` ,
231
+ ( ) => {
232
+ return HttpResponse . json (
233
+ { message :"Dev Container feature not enabled." } ,
234
+ { status :403 } ,
235
+ ) ;
236
+ } ,
237
+ ) ,
238
+ ) ;
239
+
240
+ const { result} = renderHook (
241
+ ( ) => useAgentContainers ( MockWorkspaceAgent ) ,
242
+ {
243
+ wrapper :createWrapper ( ) ,
244
+ } ,
245
+ ) ;
246
+
247
+ // Wait for the query to complete and error to be processed
248
+ await waitFor ( ( ) => {
249
+ expect ( result . current ) . toBeUndefined ( ) ;
250
+ } ) ;
251
+
252
+ expect ( watchAgentContainersSpy ) . not . toHaveBeenCalled ( ) ;
253
+
254
+ watchAgentContainersSpy . mockRestore ( ) ;
255
+ } ) ;
214
256
} ) ;