@@ -23,6 +23,7 @@ import * as PubSub from "effect/PubSub";
2323import * as Queue from "effect/Queue" ;
2424import * as Scope from "effect/Scope" ;
2525import * as Stream from "effect/Stream" ;
26+ import * as TestClock from "effect/testing/TestClock" ;
2627import type { BrowserContext , Page } from "playwright-core" ;
2728import { beforeEach , expect , vi } from "vite-plus/test" ;
2829
@@ -32,6 +33,7 @@ import * as Broker from "../mcp/PreviewAutomationBroker.ts";
3233import * as DesktopChannel from "./DesktopBrowserChannel.ts" ;
3334import * as Manager from "./Manager.ts" ;
3435import * as ServerBrowser from "./ServerBrowser.ts" ;
36+ import * as ServerBrowserContexts from "./ServerBrowserContexts.ts" ;
3537import * as PreviewBrowser from "./PreviewBrowser.ts" ;
3638
3739vi . mock ( "./ServerBrowserContexts.ts" , ( ) => ( {
@@ -152,6 +154,9 @@ function makeContext(onClose?: (context: BrowserContext) => void) {
152154 goto : vi . fn ( async ( next : string ) => {
153155 url = next ;
154156 events . emit ( "load" ) ;
157+ const pending = initialNavigation ;
158+ initialNavigation = null ;
159+ await pending ?.( ) ;
155160 } ) ,
156161 goBack : vi . fn ( async ( ) => { } ) ,
157162 goForward : vi . fn ( async ( ) => { } ) ,
@@ -206,6 +211,7 @@ let pageCopy = "";
206211let keyCopy = "" ;
207212let focusedFrame : ReturnType < typeof makeFrame > | null = null ;
208213let contextFailure : Error | null = null ;
214+ let initialNavigation : ( ( ) => Promise < void > ) | null = null ;
209215let desktopRendersNext = false ;
210216const desktopDetaches = new NodeEvents . EventEmitter ( ) ;
211217const desktopTabs = new Set < string > ( ) ;
@@ -316,6 +322,7 @@ beforeEach(() => {
316322 contextRequests . length = 0 ;
317323 contextGate = null ;
318324 contextFailure = null ;
325+ initialNavigation = null ;
319326 pageCopy = "" ;
320327 keyCopy = "" ;
321328 focusedFrame = null ;
@@ -325,6 +332,71 @@ beforeEach(() => {
325332 desktopConnections . length = 0 ;
326333} ) ;
327334
335+ it . live ( "an expired open still drains its initial navigation before human input" , ( ) =>
336+ Effect . scoped (
337+ Effect . gen ( function * ( ) {
338+ const browser = yield * ServerBrowser . ServerBrowser ;
339+ const broker = yield * Broker . PreviewAutomationBroker ;
340+ const manager = yield * Manager . PreviewManager ;
341+ yield * Effect . yieldNow ;
342+ const committed = Promise . withResolvers < void > ( ) ;
343+ const events : string [ ] = [ ] ;
344+ const clock = vi . spyOn ( performance , "now" ) . mockReturnValue ( 0 ) ;
345+ yield * Effect . addFinalizer ( ( ) => Effect . sync ( ( ) => clock . mockRestore ( ) ) ) ;
346+ yield * Effect . addFinalizer ( ( ) => Effect . sync ( ( ) => committed . resolve ( ) ) ) ;
347+ const original = ServerBrowserContexts . ServerBrowserContexts . prototype . contextFor ;
348+ const create = vi
349+ . spyOn ( ServerBrowserContexts . ServerBrowserContexts . prototype , "contextFor" )
350+ . mockImplementation ( async function (
351+ this : ServerBrowserContexts . ServerBrowserContexts ,
352+ profileId ,
353+ isolationKey ,
354+ ) {
355+ const context = await original . call ( this , profileId , isolationKey ) ;
356+ contexts . at ( - 1 ) ! . page . goto . mockImplementationOnce ( async ( ) => {
357+ clock . mockReturnValue ( 1000 ) ;
358+ await committed . promise ;
359+ events . push ( "navigation committed" ) ;
360+ } ) ;
361+ return context ;
362+ } ) ;
363+ yield * Effect . addFinalizer ( ( ) => Effect . sync ( ( ) => create . mockRestore ( ) ) ) ;
364+ const expired = yield * broker
365+ . invoke < PreviewAutomationStatus > ( {
366+ scope,
367+ operation : "open" ,
368+ input : { url : "http://localhost:5173/slow" , reuseExistingTab : false , show : false } ,
369+ timeoutMs : 1000 ,
370+ } )
371+ . pipe ( Effect . flip ) ;
372+ expect ( expired ) . toMatchObject ( { _tag : "PreviewAutomationTimeoutError" } ) ;
373+ const { sessions } = yield * manager . list ( { threadId : scope . thread . threadId } ) ;
374+ expect ( sessions ) . toHaveLength ( 1 ) ;
375+ const tabId = PreviewTabId . make ( sessions [ 0 ] ! . tabId ) ;
376+ const viewer = yield * browser . attachViewer ( viewerInput ( tabId , true ) ) ;
377+ yield * Queue . clear ( viewer . output ) ;
378+ const takeover = yield * viewer . input ( { type : "takeControl" } ) . pipe ( Effect . forkScoped ) ;
379+ let control = yield * Queue . take ( viewer . output ) ;
380+ while ( control . _tag !== "control" || control . controller !== "you" ) {
381+ control = yield * Queue . take ( viewer . output ) ;
382+ }
383+ const cdp = contexts [ 0 ] ! . sessions . at ( - 1 ) ! ;
384+ const send = cdp . send . getMockImplementation ( ) ! ;
385+ cdp . send . mockImplementation ( async ( operation , input ) => {
386+ if ( operation === "Input.insertText" ) events . push ( "human typed" ) ;
387+ return send ( operation , input ) ;
388+ } ) ;
389+ const typing = yield * viewer . input ( { type : "text" , text : "hello" } ) . pipe ( Effect . forkScoped ) ;
390+ yield * broker . invoke ( { scope, tabId, operation : "status" , input : { } } ) ;
391+ expect ( events ) . toEqual ( [ ] ) ;
392+ committed . resolve ( ) ;
393+ yield * Fiber . join ( takeover ) ;
394+ yield * Fiber . join ( typing ) ;
395+ expect ( events ) . toEqual ( [ "navigation committed" , "human typed" ] ) ;
396+ } ) ,
397+ ) . pipe ( Effect . provide ( layer ) ) ,
398+ ) ;
399+
328400it . live . each ( [ "desktop" as const , undefined ] ) (
329401 "explicit desktop targets ($0 runtime) fail without creating a server tab" ,
330402 ( runtime ) =>
@@ -595,6 +667,193 @@ it.live.each([
595667 ) . pipe ( Effect . provide ( layer ) ) ,
596668) ;
597669
670+ it . live ( "a user-opened tab allows a corrected URL before its initial navigation commits" , ( ) =>
671+ Effect . scoped (
672+ Effect . gen ( function * ( ) {
673+ const { browser } = yield * ready ;
674+ const manager = yield * Manager . PreviewManager ;
675+ yield * Effect . yieldNow ;
676+ const started = Promise . withResolvers < void > ( ) ;
677+ const committed = Promise . withResolvers < void > ( ) ;
678+ yield * Effect . addFinalizer ( ( ) => Effect . sync ( ( ) => committed . resolve ( ) ) ) ;
679+ initialNavigation = ( ) => {
680+ started . resolve ( ) ;
681+ return committed . promise ;
682+ } ;
683+ const opened = yield * manager . open ( {
684+ threadId : scope . thread . threadId ,
685+ url : "http://localhost:5173/stalled" ,
686+ runtime : "server" ,
687+ } ) ;
688+ yield * Effect . promise ( ( ) => started . promise ) ;
689+ const viewer = yield * browser . attachViewer ( viewerInput ( opened . tabId , true ) ) ;
690+ const correction = yield * viewer
691+ . input ( { type : "navigate" , url : "http://localhost:5173/fixed" } )
692+ . pipe ( Effect . forkScoped ) ;
693+ yield * Effect . promise ( ( ) => new Promise < void > ( ( resolve ) => setImmediate ( resolve ) ) ) ;
694+ const lastNavigationBeforeCommit = contexts [ 1 ] ! . page . goto . mock . calls . at ( - 1 ) ;
695+ committed . resolve ( ) ;
696+ yield * Fiber . join ( correction ) ;
697+ expect ( lastNavigationBeforeCommit ) . toEqual ( [
698+ "http://localhost:5173/fixed" ,
699+ expect . objectContaining ( { waitUntil : "commit" } ) ,
700+ ] ) ;
701+ } ) ,
702+ ) . pipe ( Effect . provide ( layer ) ) ,
703+ ) ;
704+
705+ it . live ( "an expired queued click never reaches the page" , ( ) =>
706+ Effect . scoped (
707+ Effect . gen ( function * ( ) {
708+ const { broker, tabId } = yield * ready ;
709+ const page = contexts [ 0 ] ! . page ;
710+ const started = Promise . withResolvers < void > ( ) ;
711+ const release = Promise . withResolvers < Awaited < ReturnType < typeof page . evaluate > > > ( ) ;
712+ const metadata = {
713+ url : "about:blank" ,
714+ title : "test page" ,
715+ loading : false ,
716+ visibleText : "" ,
717+ interactiveElements : [ ] ,
718+ } ;
719+ yield * Effect . addFinalizer ( ( ) => Effect . sync ( ( ) => release . resolve ( metadata ) ) ) ;
720+ page . evaluate . mockImplementationOnce ( async ( ) => {
721+ started . resolve ( ) ;
722+ return release . promise ;
723+ } ) ;
724+ const click = vi . fn ( async ( ) => { } ) ;
725+ page . locator . mockReturnValue ( {
726+ scrollIntoViewIfNeeded : async ( ) => { } ,
727+ boundingBox : async ( ) => ( { x : 0 , y : 0 , width : 10 , height : 10 } ) ,
728+ click,
729+ } as never ) ;
730+ let now = performance . now ( ) ;
731+ const clock = vi . spyOn ( performance , "now" ) . mockImplementation ( ( ) => now ) ;
732+ yield * Effect . addFinalizer ( ( ) => Effect . sync ( ( ) => clock . mockRestore ( ) ) ) ;
733+ const snapshot = yield * broker
734+ . invoke < PreviewAutomationSnapshot > ( {
735+ scope,
736+ tabId,
737+ operation : "snapshot" ,
738+ input : { } ,
739+ timeoutMs : 5000 ,
740+ } )
741+ . pipe ( Effect . forkScoped ) ;
742+ yield * Effect . promise ( ( ) => started . promise ) ;
743+ const action = yield * broker
744+ . invoke < void > ( {
745+ scope,
746+ tabId,
747+ operation : "click" ,
748+ input : { selector : "button" } ,
749+ timeoutMs : 1000 ,
750+ } )
751+ . pipe ( Effect . exit , Effect . forkScoped ) ;
752+ // The later status reply marks delivery through the same request stream.
753+ yield * broker . invoke ( { scope, tabId, operation : "status" , input : { } } ) ;
754+ now += 1000 ;
755+ release . resolve ( metadata ) ;
756+ yield * Fiber . join ( snapshot ) ;
757+ const result = yield * Fiber . join ( action ) ;
758+ expect ( click ) . not . toHaveBeenCalled ( ) ;
759+ const error = yield * Effect . flip ( result ) ;
760+ expect ( error ) . toMatchObject ( {
761+ _tag : "PreviewAutomationTimeoutError" ,
762+ remoteTag : "PreviewAutomationTimeoutError" ,
763+ } ) ;
764+ } ) ,
765+ ) . pipe ( Effect . provide ( layer ) ) ,
766+ ) ;
767+
768+ it . effect (
769+ "a queued recording stop still runs after its deadline when a snapshot caller times out" ,
770+ ( ) =>
771+ Effect . scoped (
772+ Effect . gen ( function * ( ) {
773+ const { broker, tabId } = yield * ready ;
774+ const page = contexts [ 0 ] ! . page ;
775+ const encoder = {
776+ evaluate : vi
777+ . fn ( )
778+ . mockResolvedValueOnce ( undefined )
779+ . mockResolvedValueOnce ( true )
780+ . mockResolvedValueOnce ( { mimeType : "video/webm" , count : 1 , bytes : 5 } )
781+ . mockResolvedValueOnce ( "ZnJhbWU=" ) ,
782+ close : vi . fn ( async ( ) => { } ) ,
783+ } ;
784+ const scratchPage = vi
785+ . spyOn ( ServerBrowserContexts . ServerBrowserContexts . prototype , "scratchPage" )
786+ . mockResolvedValue ( encoder as unknown as Page ) ;
787+ yield * Effect . addFinalizer ( ( ) => Effect . sync ( ( ) => scratchPage . mockRestore ( ) ) ) ;
788+ const recording = yield * broker . invoke ( {
789+ scope,
790+ tabId,
791+ operation : "recordingStart" ,
792+ input : { } ,
793+ } ) ;
794+ expect ( recording ) . toMatchObject ( { recording : true } ) ;
795+ const session = contexts [ 0 ] ! . sessions . at ( - 1 ) ! ;
796+ expect ( session . send ) . toHaveBeenCalledWith ( "Page.startScreencast" , expect . anything ( ) ) ;
797+
798+ const started = Promise . withResolvers < void > ( ) ;
799+ const release = Promise . withResolvers < Awaited < ReturnType < typeof page . evaluate > > > ( ) ;
800+ const metadata = {
801+ url : "about:blank" ,
802+ title : "test page" ,
803+ loading : false ,
804+ visibleText : "" ,
805+ interactiveElements : [ ] ,
806+ } ;
807+ yield * Effect . addFinalizer ( ( ) => Effect . sync ( ( ) => release . resolve ( metadata ) ) ) ;
808+ page . evaluate . mockImplementationOnce ( async ( ) => {
809+ started . resolve ( ) ;
810+ return release . promise ;
811+ } ) ;
812+ let now = performance . now ( ) ;
813+ const clock = vi . spyOn ( performance , "now" ) . mockImplementation ( ( ) => now ) ;
814+ yield * Effect . addFinalizer ( ( ) => Effect . sync ( ( ) => clock . mockRestore ( ) ) ) ;
815+ const snapshot = yield * broker
816+ . invoke < PreviewAutomationSnapshot > ( {
817+ scope,
818+ tabId,
819+ operation : "snapshot" ,
820+ input : { } ,
821+ timeoutMs : 150 ,
822+ updateCurrentTab : false ,
823+ } )
824+ . pipe ( Effect . flip , Effect . forkScoped ) ;
825+ yield * Effect . promise ( ( ) => started . promise ) ;
826+ const stop = yield * broker
827+ . invoke < void > ( {
828+ scope,
829+ tabId,
830+ operation : "recordingStop" ,
831+ input : { } ,
832+ timeoutMs : 1000 ,
833+ } )
834+ . pipe ( Effect . exit , Effect . forkScoped ) ;
835+ // The status reply marks delivery while the snapshot still holds the tab queue.
836+ yield * broker . invoke ( { scope, tabId, operation : "status" , input : { } } ) ;
837+ yield * TestClock . adjust ( 150 ) ;
838+ expect ( yield * Fiber . join ( snapshot ) ) . toMatchObject ( {
839+ _tag : "PreviewAutomationTimeoutError" ,
840+ } ) ;
841+ now += 1000 ;
842+ expect ( session . detach ) . not . toHaveBeenCalled ( ) ;
843+ release . resolve ( metadata ) ;
844+ yield * yield * Fiber . join ( stop ) ;
845+ expect ( session . detach ) . toHaveBeenCalledOnce ( ) ;
846+ const stopped = yield * broker
847+ . invoke < void > ( { scope, tabId, operation : "recordingStop" , input : { } } )
848+ . pipe ( Effect . flip ) ;
849+ expect ( stopped ) . toMatchObject ( {
850+ _tag : "PreviewAutomationExecutionError" ,
851+ cause : { _tag : "PreviewAutomationRecordingNotActiveError" } ,
852+ } ) ;
853+ } ) ,
854+ ) . pipe ( Effect . provide ( layer ) ) ,
855+ ) ;
856+
598857it . live ( "enforces provider ownership and explicit targets when a session has multiple tabs" , ( ) =>
599858 Effect . scoped (
600859 Effect . gen ( function * ( ) {
0 commit comments