@@ -28,6 +28,7 @@ import * as Broker from "../mcp/PreviewAutomationBroker.ts";
2828import * as DesktopChannel from "./DesktopBrowserChannel.ts" ;
2929import * as Manager from "./Manager.ts" ;
3030import * as ServerBrowser from "./ServerBrowser.ts" ;
31+ import * as ServerBrowserContexts from "./ServerBrowserContexts.ts" ;
3132import * as PreviewBrowser from "./PreviewBrowser.ts" ;
3233
3334// Keep the manager, broker, ownership, refs, and viewer paths real; replace Chromium I/O only.
@@ -557,6 +558,105 @@ it.live("a caller timeout keeps the in-flight capture reserved until Chromium an
557558 ) . pipe ( Effect . provide ( layer ) ) ,
558559) ;
559560
561+ it . live (
562+ "a queued recording stop still runs after its deadline when a snapshot caller times out" ,
563+ ( ) =>
564+ Effect . scoped (
565+ Effect . gen ( function * ( ) {
566+ const { broker, tabId } = yield * ready ;
567+ const manager = yield * Manager . PreviewManager ;
568+ const page = contexts [ 0 ] ! . page ;
569+ const encoder = {
570+ evaluate : vi
571+ . fn ( )
572+ . mockResolvedValueOnce ( undefined )
573+ . mockResolvedValueOnce ( true )
574+ . mockResolvedValueOnce ( { mimeType : "video/webm" , count : 1 , bytes : 5 } )
575+ . mockResolvedValueOnce ( "ZnJhbWU=" ) ,
576+ close : vi . fn ( async ( ) => { } ) ,
577+ } ;
578+ const scratchPage = vi
579+ . spyOn ( ServerBrowserContexts . ServerBrowserContexts . prototype , "scratchPage" )
580+ . mockResolvedValue ( encoder as unknown as Page ) ;
581+ yield * Effect . addFinalizer ( ( ) => Effect . sync ( ( ) => scratchPage . mockRestore ( ) ) ) ;
582+ const recording = yield * broker . invoke ( {
583+ scope,
584+ tabId,
585+ operation : "recordingStart" ,
586+ input : { } ,
587+ } ) ;
588+ expect ( recording ) . toMatchObject ( { recording : true } ) ;
589+ const session = contexts [ 0 ] ! . sessions . at ( - 1 ) ! ;
590+ expect ( session . send ) . toHaveBeenCalledWith ( "Page.startScreencast" , expect . anything ( ) ) ;
591+
592+ const started = Promise . withResolvers < void > ( ) ;
593+ const release = Promise . withResolvers < Awaited < ReturnType < typeof page . evaluate > > > ( ) ;
594+ const drained = Promise . withResolvers < void > ( ) ;
595+ const metadata = {
596+ url : "about:blank" ,
597+ title : "test page" ,
598+ loading : false ,
599+ visibleText : "" ,
600+ interactiveElements : [ ] ,
601+ } ;
602+ yield * Effect . addFinalizer ( ( ) => Effect . sync ( ( ) => release . resolve ( metadata ) ) ) ;
603+ page . evaluate . mockImplementationOnce ( async ( ) => {
604+ started . resolve ( ) ;
605+ return release . promise ;
606+ } ) ;
607+ page . setViewportSize . mockImplementationOnce ( async ( ) => {
608+ drained . resolve ( ) ;
609+ } ) ;
610+ let now = performance . now ( ) ;
611+ const clock = vi . spyOn ( performance , "now" ) . mockImplementation ( ( ) => now ) ;
612+ yield * Effect . addFinalizer ( ( ) => Effect . sync ( ( ) => clock . mockRestore ( ) ) ) ;
613+ const snapshot = yield * broker
614+ . invoke < PreviewAutomationSnapshot > ( {
615+ scope,
616+ tabId,
617+ operation : "snapshot" ,
618+ input : { } ,
619+ timeoutMs : 150 ,
620+ updateCurrentTab : false ,
621+ } )
622+ . pipe ( Effect . flip , Effect . forkScoped ) ;
623+ yield * Effect . promise ( ( ) => started . promise ) ;
624+ const stop = yield * broker
625+ . invoke < void > ( {
626+ scope,
627+ tabId,
628+ operation : "recordingStop" ,
629+ input : { } ,
630+ timeoutMs : 1000 ,
631+ } )
632+ . pipe ( Effect . exit , Effect . forkScoped ) ;
633+ // The status reply marks delivery while the snapshot still holds the tab queue.
634+ yield * broker . invoke ( { scope, tabId, operation : "status" , input : { } } ) ;
635+ expect ( yield * Fiber . join ( snapshot ) ) . toMatchObject ( {
636+ _tag : "PreviewAutomationTimeoutError" ,
637+ } ) ;
638+ now += 1000 ;
639+ release . resolve ( metadata ) ;
640+ // A system resize runs after the queued stop without changing control ownership.
641+ yield * manager . resize ( {
642+ threadId : scope . thread . threadId ,
643+ tabId,
644+ viewport : { _tag : "freeform" , width : 900 , height : 700 } ,
645+ } ) ;
646+ yield * Effect . promise ( ( ) => drained . promise ) ;
647+ expect ( session . detach ) . toHaveBeenCalledOnce ( ) ;
648+ yield * yield * Fiber . join ( stop ) ;
649+ const stopped = yield * broker
650+ . invoke < void > ( { scope, tabId, operation : "recordingStop" , input : { } } )
651+ . pipe ( Effect . flip ) ;
652+ expect ( stopped ) . toMatchObject ( {
653+ _tag : "PreviewAutomationExecutionError" ,
654+ cause : { _tag : "PreviewAutomationRecordingNotActiveError" } ,
655+ } ) ;
656+ } ) ,
657+ ) . pipe ( Effect . provide ( layer ) ) ,
658+ ) ;
659+
560660it . live ( "enforces provider ownership and explicit targets when a session has multiple tabs" , ( ) =>
561661 Effect . scoped (
562662 Effect . gen ( function * ( ) {
0 commit comments