@@ -68,8 +68,9 @@ vi.mock("./ServerBrowserContexts.ts", () => ({
6868 async connectDesktopPage ( endpoint : string ) {
6969 const context = makeContext ( undefined , desktopPageUrl ) ;
7070 context . page . viewportSize . mockReturnValue ( null ) ;
71- desktopConnections . push ( { endpoint, context } ) ;
72- return { browser : { close : async ( ) => { } } , page : context . page as unknown as Page } ;
71+ const browser = { close : vi . fn ( async ( ) => { } ) } ;
72+ desktopConnections . push ( { endpoint, context, browser } ) ;
73+ return { browser, page : context . page as unknown as Page } ;
7374 }
7475 async close ( ) {
7576 for ( const context of contexts ) await context . close ( ) ;
@@ -228,11 +229,50 @@ function makeContext(onClose?: (context: BrowserContext) => void, initialUrl = "
228229 throw new Error ( "Unexpected locator action" ) ;
229230 } ) ,
230231 isClosed : ( ) => closed ,
231- close : vi . fn ( async ( ) => {
232- if ( ! closed ) {
232+ // Models Playwright: unload handlers always run, but `beforeunload` only with
233+ // `runBeforeUnload`, and a guarding page stays open behind a `beforeunload` dialog.
234+ beforeUnload : "none" as "none" | "listen" | "guard" ,
235+ unloadEvents : [ ] as string [ ] ,
236+ beforeUnloadDialog : null as null | {
237+ accept : ReturnType < typeof vi . fn > ;
238+ dismiss : ReturnType < typeof vi . fn > ;
239+ } ,
240+ // Set to hold the page open after `beforeunload` until resolved, as Playwright's
241+ // `runBeforeUnload` close resolves before the page actually closes.
242+ unloadGate : null as PromiseWithResolvers < void > | null ,
243+ removed : false ,
244+ pagehideBinding : null as ( ( ) => void ) | null ,
245+ pagehideWatched : Promise . withResolvers < void > ( ) ,
246+ exposeFunction : vi . fn ( async ( _name : string , callback : ( ) => void ) => {
247+ page . pagehideBinding = callback ;
248+ page . pagehideWatched . resolve ( ) ;
249+ } ) ,
250+ close : vi . fn ( async ( options ?: { runBeforeUnload ?: boolean } ) => {
251+ if ( closed ) return ;
252+ const finish = ( ) => {
253+ if ( closed || page . removed ) return ;
254+ page . unloadEvents . push ( "pagehide" ) ;
255+ page . pagehideBinding ?.( ) ;
256+ page . unloadEvents . push ( "visibilitychange" ) ;
233257 closed = true ;
234258 events . emit ( "close" ) ;
259+ } ;
260+ if ( ! options ?. runBeforeUnload || page . beforeUnload === "none" ) return finish ( ) ;
261+ page . unloadEvents . push ( "beforeunload" ) ;
262+ if ( page . beforeUnload === "listen" && page . unloadGate ) {
263+ void page . unloadGate . promise . then ( finish ) ;
264+ return ;
235265 }
266+ if ( page . beforeUnload === "listen" ) return finish ( ) ;
267+ const dialog = {
268+ type : ( ) => "beforeunload" ,
269+ message : ( ) => "" ,
270+ defaultValue : ( ) => "" ,
271+ accept : vi . fn ( async ( ) => finish ( ) ) ,
272+ dismiss : vi . fn ( async ( ) => { } ) ,
273+ } ;
274+ page . beforeUnloadDialog = dialog ;
275+ events . emit ( "dialog" , dialog ) ;
236276 } ) ,
237277 } ;
238278 const context = {
@@ -249,9 +289,11 @@ function makeContext(onClose?: (context: BrowserContext) => void, initialUrl = "
249289 sessions . push ( session ) ;
250290 return session ;
251291 } ,
292+ closing : Promise . withResolvers < void > ( ) ,
252293 close : vi . fn ( async ( ) => {
253294 if ( contextClosed ) return ;
254295 contextClosed = true ;
296+ context . closing . resolve ( ) ;
255297 await page . close ( ) ;
256298 onClose ?.( context as unknown as BrowserContext ) ;
257299 } ) ,
@@ -366,7 +408,11 @@ const shownDesktopTabs = new Set<string>();
366408const releasedDesktopTabs : Array < string > = [ ] ;
367409const desktopLoadFailures = new Map < string , PreviewAutomationLoadError > ( ) ;
368410const desktopTurns = new NodeEvents . EventEmitter ( ) ;
369- const desktopConnections : Array < { endpoint : string ; context : ReturnType < typeof makeContext > } > = [ ] ;
411+ const desktopConnections : Array < {
412+ endpoint : string ;
413+ context : ReturnType < typeof makeContext > ;
414+ browser : { close : ReturnType < typeof vi . fn > } ;
415+ } > = [ ] ;
370416const testThread = {
371417 threadId : ThreadId . make ( "browser-test-thread" ) ,
372418 providerSessionId : "agent-a" ,
@@ -1179,6 +1225,117 @@ it.live("reports a pending dialog without evaluating the page and resolves it ex
11791225 ) . pipe ( Effect . provide ( layer ) ) ,
11801226) ;
11811227
1228+ it . live ( "closing an isolated tab closes its context only after the page closes" , ( ) =>
1229+ Effect . scoped (
1230+ Effect . gen ( function * ( ) {
1231+ const { broker, tabId } = yield * ready ;
1232+ const context = contexts [ 0 ] ! ;
1233+ const page = context . page ;
1234+ page . beforeUnload = "listen" ;
1235+ page . unloadGate = Promise . withResolvers < void > ( ) ;
1236+ yield * broker . invoke ( { scope, tabId, operation : "close" , input : { } } ) ;
1237+ expect ( context . close ) . not . toHaveBeenCalled ( ) ;
1238+ expect ( page . isClosed ( ) ) . toBe ( false ) ;
1239+ page . unloadGate . resolve ( ) ;
1240+ yield * Effect . promise ( ( ) => context . closing . promise ) ;
1241+ expect ( page . isClosed ( ) ) . toBe ( true ) ;
1242+ expect ( page . unloadEvents ) . toEqual ( [ "beforeunload" , "pagehide" , "visibilitychange" ] ) ;
1243+ } ) ,
1244+ ) . pipe ( Effect . provide ( layer ) ) ,
1245+ ) ;
1246+
1247+ it . live ( "closing a tab force-closes a page that never finishes unloading" , ( ) =>
1248+ Effect . scoped (
1249+ Effect . gen ( function * ( ) {
1250+ const { broker, tabId } = yield * ready ;
1251+ const context = contexts [ 0 ] ! ;
1252+ const page = context . page ;
1253+ page . beforeUnload = "listen" ;
1254+ page . unloadGate = Promise . withResolvers < void > ( ) ;
1255+ vi . useFakeTimers ( { toFake : [ "setTimeout" , "clearTimeout" ] } ) ;
1256+ yield * broker . invoke ( { scope, tabId, operation : "close" , input : { } } ) ;
1257+ vi . advanceTimersByTime ( 4_999 ) ;
1258+ expect ( page . isClosed ( ) ) . toBe ( false ) ;
1259+ expect ( context . close ) . not . toHaveBeenCalled ( ) ;
1260+ vi . advanceTimersByTime ( 1 ) ;
1261+ expect ( page . isClosed ( ) ) . toBe ( true ) ;
1262+ yield * Effect . promise ( ( ) => context . closing . promise ) ;
1263+ expect ( vi . getTimerCount ( ) ) . toBe ( 0 ) ;
1264+ } ) . pipe ( Effect . ensuring ( Effect . sync ( ( ) => vi . useRealTimers ( ) ) ) ) ,
1265+ ) . pipe ( Effect . provide ( layer ) ) ,
1266+ ) ;
1267+
1268+ it . live ( "closing a desktop tab waits for its page to unload before the desktop removes it" , ( ) =>
1269+ Effect . scoped (
1270+ Effect . gen ( function * ( ) {
1271+ const { broker } = yield * ready ;
1272+ const manager = yield * Manager . PreviewManager ;
1273+ desktopRendersNext = true ;
1274+ const opened = yield * broker . invoke < PreviewAutomationStatus > ( {
1275+ scope,
1276+ operation : "open" ,
1277+ input : { reuseExistingTab : false , open : false } ,
1278+ } ) ;
1279+ const tabId = PreviewTabId . make ( opened . tabId ! ) ;
1280+ const { context, browser } = desktopConnections [ 0 ] ! ;
1281+ const page = context . page ;
1282+ // The desktop removes the webview once the session closes, dropping an unload in progress.
1283+ const events = yield * manager . subscribeEvents ;
1284+ const sessionClosed = Promise . withResolvers < void > ( ) ;
1285+ yield * Effect . gen ( function * ( ) {
1286+ while ( ( yield * PubSub . take ( events ) ) . type !== "closed" ) ;
1287+ page . removed = true ;
1288+ sessionClosed . resolve ( ) ;
1289+ } ) . pipe ( Effect . forkScoped ) ;
1290+ page . beforeUnload = "listen" ;
1291+ page . unloadGate = Promise . withResolvers < void > ( ) ;
1292+ const closing = yield * broker
1293+ . invoke ( { scope, tabId, operation : "close" , input : { } } )
1294+ . pipe ( Effect . forkScoped ) ;
1295+ // The page finishes unloading once the server listens for it, or too late if it never does.
1296+ yield * Effect . promise ( ( ) =>
1297+ Promise . race ( [ page . pagehideWatched . promise , sessionClosed . promise ] ) ,
1298+ ) ;
1299+ expect ( browser . close ) . not . toHaveBeenCalled ( ) ;
1300+ page . unloadGate . resolve ( ) ;
1301+ yield * Fiber . join ( closing ) ;
1302+ expect ( page . unloadEvents ) . toEqual ( [ "beforeunload" , "pagehide" , "visibilitychange" ] ) ;
1303+ expect ( browser . close ) . toHaveBeenCalledTimes ( 1 ) ;
1304+ } ) ,
1305+ ) . pipe ( Effect . provide ( layer ) ) ,
1306+ ) ;
1307+
1308+ it . live ( "closing a tab with an unsaved-changes guard still closes it" , ( ) =>
1309+ Effect . scoped (
1310+ Effect . gen ( function * ( ) {
1311+ const browser = yield * ServerBrowser . ServerBrowser ;
1312+ const broker = yield * Broker . PreviewAutomationBroker ;
1313+ yield * Effect . yieldNow ;
1314+ // A profile context is shared, so closing the tab cannot fall back to closing its context.
1315+ const reportProfiles = yield * browser . profileReporter ;
1316+ yield * reportProfiles ( {
1317+ profiles : [ { id : "profile-work" , name : "Work" , kind : "persistent" } ] ,
1318+ defaultProfileId : "profile-work" ,
1319+ } ) ;
1320+ const opened = yield * broker . invoke < PreviewAutomationStatus > ( {
1321+ scope,
1322+ operation : "open" ,
1323+ input : { reuseExistingTab : false , show : false , profileId : "profile-work" } ,
1324+ } ) ;
1325+ const tabId = PreviewTabId . make ( opened . tabId ! ) ;
1326+ expect ( contextRequests ) . toEqual ( [ { profileId : "profile-work" , isolated : false } ] ) ;
1327+ const page = contexts [ 0 ] ! . page ;
1328+ page . beforeUnload = "guard" ;
1329+ yield * broker . invoke ( { scope, tabId, operation : "close" , input : { } } ) ;
1330+ // The mock raises the dialog inside `close`, so an unanswered guard leaves the page open here.
1331+ expect ( page . beforeUnloadDialog ) . not . toBeNull ( ) ;
1332+ expect ( page . beforeUnloadDialog ! . accept ) . toHaveBeenCalledTimes ( 1 ) ;
1333+ expect ( page . isClosed ( ) ) . toBe ( true ) ;
1334+ expect ( page . unloadEvents ) . toEqual ( [ "beforeunload" , "pagehide" , "visibilitychange" ] ) ;
1335+ } ) ,
1336+ ) . pipe ( Effect . provide ( layer ) ) ,
1337+ ) ;
1338+
11821339it . live ( "a tab opened to a file the browser cannot show reports the file to download" , ( ) =>
11831340 Effect . scoped (
11841341 Effect . gen ( function * ( ) {
0 commit comments