@@ -36,6 +36,7 @@ const mocks = vi.hoisted(() => ({
3636 ) ,
3737 list : vi . fn ( async ( ) => AsyncResult . success ( emptyList ) ) ,
3838 resize : vi . fn ( ) ,
39+ close : vi . fn ( async ( ) => AsyncResult . success ( undefined ) ) ,
3940 respond :
4041 vi . fn <
4142 ( target : { environmentId : EnvironmentId ; input : PreviewAutomationResponse } ) => Promise < void >
@@ -56,6 +57,7 @@ vi.mock("~/state/preview", () => ({
5657 list : ( ) => listAtom ,
5758 open : mocks . open ,
5859 resize : mocks . resize ,
60+ close : mocks . close ,
5961 respondToAutomation : mocks . respond ,
6062 focusAutomationHost : mocks . focus ,
6163 } ,
@@ -211,6 +213,158 @@ describe("PreviewAutomationHosts open", () => {
211213 } ) ;
212214} ) ;
213215
216+ describe ( "PreviewAutomationHosts timeout cleanup" , ( ) => {
217+ it ( "closes a hidden tab created after its open request already timed out" , async ( ) => {
218+ const opened = deferred < Awaited < ReturnType < typeof mocks . open > > > ( ) ;
219+ mocks . open . mockImplementationOnce ( ( ) => opened . promise ) ;
220+ vi . useFakeTimers ( ) ;
221+ try {
222+ await act ( async ( ) => {
223+ appAtomRegistry . set (
224+ requestsAtom ,
225+ AsyncResult . success ( {
226+ ...requestEvent ,
227+ request : { ...requestEvent . request , timeoutMs : 100 } ,
228+ } ) ,
229+ ) ;
230+ await vi . advanceTimersByTimeAsync ( 0 ) ;
231+ } ) ;
232+ expect ( mocks . open ) . toHaveBeenCalledTimes ( 1 ) ;
233+ await act ( async ( ) => vi . advanceTimersByTimeAsync ( 100 ) ) ;
234+ expect ( mocks . close ) . not . toHaveBeenCalled ( ) ;
235+
236+ await act ( async ( ) => {
237+ opened . resolve ( AsyncResult . success ( snapshot ) ) ;
238+ await opened . promise ;
239+ } ) ;
240+ expect ( mocks . close ) . toHaveBeenCalledTimes ( 1 ) ;
241+ expect ( readThreadPreviewState ( threadRef ) . sessions [ snapshot . tabId ] ) . toBeUndefined ( ) ;
242+ } finally {
243+ vi . useRealTimers ( ) ;
244+ }
245+ } ) ;
246+
247+ it ( "preserves an agent tab after it has been explicitly presented" , async ( ) => {
248+ await act ( async ( ) => {
249+ appAtomRegistry . set ( requestsAtom , AsyncResult . success ( requestEvent ) ) ;
250+ await vi . waitFor ( ( ) => expect ( mocks . respond ) . toHaveBeenCalledTimes ( 1 ) ) ;
251+ } ) ;
252+ const runtimeTabId = previewRuntimeTabId (
253+ threadRef ,
254+ readThreadPreviewState ( threadRef ) . serverEpoch ,
255+ snapshot . tabId ,
256+ ) ;
257+ const owner = Symbol ( ) ;
258+ await act ( ( ) => {
259+ useBrowserSurfaceStore . getState ( ) . claim ( runtimeTabId , owner , false ) ;
260+ useBrowserSurfaceStore
261+ . getState ( )
262+ . present ( runtimeTabId , owner , { x : 0 , y : 0 , width : 800 , height : 600 } , true , 0 , 1 ) ;
263+ } ) ;
264+ await act ( async ( ) => {
265+ appAtomRegistry . set (
266+ requestsAtom ,
267+ AsyncResult . success ( {
268+ ...requestEvent ,
269+ request : {
270+ ...requestEvent . request ,
271+ requestId : "present-existing" ,
272+ tabId : snapshot . tabId ,
273+ input : { open : true } ,
274+ } ,
275+ } ) ,
276+ ) ;
277+ await vi . waitFor ( ( ) => expect ( mocks . respond ) . toHaveBeenCalledTimes ( 2 ) ) ;
278+ } ) ;
279+ await act ( ( ) => useBrowserSurfaceStore . getState ( ) . release ( runtimeTabId , owner ) ) ;
280+
281+ const pendingResponse = deferred < void > ( ) ;
282+ mocks . respond . mockImplementationOnce ( ( ) => pendingResponse . promise ) ;
283+ vi . useFakeTimers ( ) ;
284+ try {
285+ await act ( async ( ) => {
286+ appAtomRegistry . set (
287+ requestsAtom ,
288+ AsyncResult . success ( {
289+ ...requestEvent ,
290+ request : {
291+ ...requestEvent . request ,
292+ requestId : "stalled-status" ,
293+ operation : "status" ,
294+ tabId : snapshot . tabId ,
295+ input : { } ,
296+ } ,
297+ } ) ,
298+ ) ;
299+ } ) ;
300+ await act ( async ( ) => vi . advanceTimersByTimeAsync ( 15_000 ) ) ;
301+ expect ( mocks . close ) . not . toHaveBeenCalled ( ) ;
302+ expect ( readThreadPreviewState ( threadRef ) . sessions [ snapshot . tabId ] ) . toEqual ( snapshot ) ;
303+ } finally {
304+ pendingResponse . resolve ( ) ;
305+ await act ( async ( ) => pendingResponse . promise ) ;
306+ vi . useRealTimers ( ) ;
307+ }
308+ } ) ;
309+
310+ it . each ( [ false , true ] ) (
311+ "closes only a hidden agent tab that was never presented (presented: %s)" ,
312+ async ( wasPresented ) => {
313+ await act ( async ( ) => {
314+ appAtomRegistry . set ( requestsAtom , AsyncResult . success ( requestEvent ) ) ;
315+ await vi . waitFor ( ( ) => expect ( mocks . respond ) . toHaveBeenCalledTimes ( 1 ) ) ;
316+ } ) ;
317+ const runtimeTabId = previewRuntimeTabId (
318+ threadRef ,
319+ readThreadPreviewState ( threadRef ) . serverEpoch ,
320+ snapshot . tabId ,
321+ ) ;
322+ if ( wasPresented ) {
323+ const owner = Symbol ( ) ;
324+ await act ( ( ) => {
325+ useBrowserSurfaceStore . getState ( ) . claim ( runtimeTabId , owner , false ) ;
326+ useBrowserSurfaceStore
327+ . getState ( )
328+ . present ( runtimeTabId , owner , { x : 0 , y : 0 , width : 800 , height : 600 } , true , 0 , 1 ) ;
329+ } ) ;
330+ await act ( ( ) => useBrowserSurfaceStore . getState ( ) . release ( runtimeTabId , owner ) ) ;
331+ }
332+ expect ( useBrowserSurfaceStore . getState ( ) . byTabId [ runtimeTabId ] ?. visible ?? false ) . toBe ( false ) ;
333+ expect ( mocks . close ) . not . toHaveBeenCalled ( ) ;
334+
335+ const pendingResponse = deferred < void > ( ) ;
336+ mocks . respond . mockImplementationOnce ( ( ) => pendingResponse . promise ) ;
337+ vi . useFakeTimers ( ) ;
338+ try {
339+ await act ( async ( ) => {
340+ appAtomRegistry . set (
341+ requestsAtom ,
342+ AsyncResult . success ( {
343+ ...requestEvent ,
344+ request : {
345+ ...requestEvent . request ,
346+ requestId : "stalled-status" ,
347+ operation : "status" ,
348+ tabId : snapshot . tabId ,
349+ input : { } ,
350+ } ,
351+ } ) ,
352+ ) ;
353+ } ) ;
354+ await act ( async ( ) => vi . advanceTimersByTimeAsync ( 15_000 ) ) ;
355+ expect ( mocks . close ) . toHaveBeenCalledTimes ( wasPresented ? 0 : 1 ) ;
356+ expect ( readThreadPreviewState ( threadRef ) . sessions [ snapshot . tabId ] ) . toBe (
357+ wasPresented ? snapshot : undefined ,
358+ ) ;
359+ } finally {
360+ pendingResponse . resolve ( ) ;
361+ await act ( async ( ) => pendingResponse . promise ) ;
362+ vi . useRealTimers ( ) ;
363+ }
364+ } ,
365+ ) ;
366+ } ) ;
367+
214368describe ( "PreviewAutomationHosts ownership" , ( ) => {
215369 it ( "reports only local live tabs and removes ownership when their web contents close" , async ( ) => {
216370 await act ( ( ) => {
@@ -360,4 +514,50 @@ describe("PreviewAutomationHosts ownership", () => {
360514 result : { available : false , tabId : snapshot . tabId } ,
361515 } ) ;
362516 } ) ;
517+
518+ it ( "preserves an existing user tab reused by a hidden automation request" , async ( ) => {
519+ const pendingResponse = deferred < void > ( ) ;
520+ applyPreviewServerSnapshot ( threadRef , snapshot ) ;
521+ await act ( async ( ) => {
522+ appAtomRegistry . set (
523+ requestsAtom ,
524+ AsyncResult . success ( {
525+ ...requestEvent ,
526+ request : {
527+ ...requestEvent . request ,
528+ tabId : snapshot . tabId ,
529+ input : { open : false } ,
530+ } ,
531+ } ) ,
532+ ) ;
533+ await vi . waitFor ( ( ) => expect ( mocks . respond ) . toHaveBeenCalledTimes ( 1 ) ) ;
534+ } ) ;
535+ expect ( mocks . open ) . not . toHaveBeenCalled ( ) ;
536+ mocks . respond . mockImplementationOnce ( ( ) => pendingResponse . promise ) ;
537+ vi . useFakeTimers ( ) ;
538+ try {
539+ await act ( async ( ) => {
540+ appAtomRegistry . set (
541+ requestsAtom ,
542+ AsyncResult . success ( {
543+ ...requestEvent ,
544+ request : {
545+ ...requestEvent . request ,
546+ requestId : "stalled-status" ,
547+ operation : "status" ,
548+ tabId : snapshot . tabId ,
549+ input : { } ,
550+ } ,
551+ } ) ,
552+ ) ;
553+ } ) ;
554+ await act ( async ( ) => vi . advanceTimersByTimeAsync ( 15_000 ) ) ;
555+ expect ( mocks . close ) . not . toHaveBeenCalled ( ) ;
556+ expect ( readThreadPreviewState ( threadRef ) . sessions [ snapshot . tabId ] ) . toEqual ( snapshot ) ;
557+ } finally {
558+ pendingResponse . resolve ( ) ;
559+ await act ( async ( ) => pendingResponse . promise ) ;
560+ vi . useRealTimers ( ) ;
561+ }
562+ } ) ;
363563} ) ;
0 commit comments