@@ -131,7 +131,7 @@ function makeSession(
131131 return { result : { value } } ;
132132 }
133133 if ( method === "Runtime.evaluate" && ( input as { contextId ?: number } ) . contextId === 9 )
134- return { result : { value : { ...viewport } } } ;
134+ return desktopSizeAnswer ?. ( ) ?? { result : { value : { ...viewport } } } ;
135135 return { result : { value : "evaluated" } } ;
136136 } ) ,
137137 } ;
@@ -285,6 +285,8 @@ let focusedFrame: ReturnType<typeof makeFrame> | null = null;
285285let contextFailure : Error | null = null ;
286286let initialNavigation : ( ( ) => Promise < void > ) | null = null ;
287287let desktopRendersNext = false ;
288+ /** Replaces the answer to the desktop page's size reads. */
289+ let desktopSizeAnswer : ( ( ) => Promise < Record < string , unknown > > ) | null = null ;
288290const desktopDetaches = new NodeEvents . EventEmitter ( ) ;
289291let desktopDetachReady = Promise . withResolvers < void > ( ) ;
290292let desktopReleased = Promise . withResolvers < void > ( ) ;
@@ -415,9 +417,6 @@ const openDesktopViewer = Effect.gen(function* () {
415417 Object . assign ( context . cssViewport , { width, height } ) ;
416418 return Effect . promise ( ( ) => cdp . emitAsync ( "Page.frameResized" ) ) ;
417419 } ;
418- // Ready once the size measured on attach and on a first resize both reached the viewer.
419- yield * resize ( 1920 , 1080 ) ;
420- expect ( viewportSizes ( yield * Queue . takeAll ( viewer . output ) ) ) . toEqual ( [ "1920x1080" , "1920x1080" ] ) ;
421420 return { tabId, viewer, context, cdp, resize } ;
422421} ) ;
423422
@@ -433,6 +432,7 @@ beforeEach(() => {
433432 focusedFrame = null ;
434433 desktopTabs . clear ( ) ;
435434 desktopRendersNext = false ;
435+ desktopSizeAnswer = null ;
436436 desktopDetachReady = Promise . withResolvers < void > ( ) ;
437437 desktopReleased = Promise . withResolvers < void > ( ) ;
438438 releasedDesktopTabs . length = 0 ;
@@ -1204,21 +1204,41 @@ it.effect(
12041204 ( ) =>
12051205 Effect . scoped (
12061206 Effect . gen ( function * ( ) {
1207- const { viewer, cdp } = yield * openDesktopViewer ;
12081207 // Each page size read waits here for its answer.
12091208 const reads = yield * Queue . unbounded < PromiseWithResolvers < Record < string , unknown > > > ( ) ;
1210- const send = cdp . send . getMockImplementation ( ) ! ;
1211- cdp . send . mockImplementation ( ( method , input ) => {
1212- if ( method !== "Runtime.evaluate" || ( input as { contextId ?: number } ) . contextId !== 9 )
1213- return send ( method , input ) ;
1214- const read = Promise . withResolvers < Record < string , unknown > > ( ) ;
1215- Queue . offerUnsafe ( reads , read ) ;
1216- return read . promise ;
1217- } ) ;
1209+ const deferReads = ( ) => {
1210+ desktopSizeAnswer = ( ) => {
1211+ const read = Promise . withResolvers < Record < string , unknown > > ( ) ;
1212+ Queue . offerUnsafe ( reads , read ) ;
1213+ return read . promise ;
1214+ } ;
1215+ } ;
1216+ deferReads ( ) ;
1217+ const { viewer, cdp } = yield * openDesktopViewer ;
12181218 const answer = ( width : number , height : number ) => ( {
12191219 result : { value : { width, height } } ,
12201220 } ) ;
12211221 const resized = ( ) => cdp . emitAsync ( "Page.frameResized" ) ;
1222+ // An alert open on attach blocks the first read, which gives up with no size to send.
1223+ const blocked = yield * Queue . take ( reads ) ;
1224+ yield * TestClock . adjust ( "1 second" ) ;
1225+ // Closing it resizes nothing, so the close itself remeasures.
1226+ desktopSizeAnswer = null ;
1227+ yield * Effect . promise ( ( ) => cdp . emitAsync ( "Page.javascriptDialogClosed" ) ) ;
1228+ expect ( viewportSizes ( yield * Queue . takeAll ( viewer . output ) ) ) . toEqual ( [ "1920x1080" ] ) ;
1229+ // The late answer, a read a navigation breaks and a hidden webview keep the last size.
1230+ blocked . resolve ( answer ( 100 , 100 ) ) ;
1231+ deferReads ( ) ;
1232+ const navigated = resized ( ) ;
1233+ ( yield * Queue . take ( reads ) ) . reject ( new Error ( "Cannot find context with specified id" ) ) ;
1234+ yield * Effect . promise ( ( ) => navigated ) ;
1235+ const hidden = resized ( ) ;
1236+ ( yield * Queue . take ( reads ) ) . resolve ( answer ( 0 , 0 ) ) ;
1237+ yield * Effect . promise ( ( ) => hidden ) ;
1238+ expect ( viewportSizes ( yield * Queue . takeAll ( viewer . output ) ) ) . toEqual ( [
1239+ "1920x1080" ,
1240+ "1920x1080" ,
1241+ ] ) ;
12221242 // Resizes during a read share one more read, so the newest size arrives last.
12231243 const first = resized ( ) ;
12241244 const firstRead = yield * Queue . take ( reads ) ;
@@ -1232,24 +1252,6 @@ it.effect(
12321252 "1000x600" ,
12331253 "1000x600" ,
12341254 ] ) ;
1235- // A dialog blocks page script, so a read that never answers gives up.
1236- const blocked = resized ( ) ;
1237- const stuck = yield * Queue . take ( reads ) ;
1238- yield * TestClock . adjust ( "1 second" ) ;
1239- yield * Effect . promise ( ( ) => blocked ) ;
1240- // Its late answer, a read a navigation breaks and a hidden webview keep the last size.
1241- stuck . resolve ( answer ( 100 , 100 ) ) ;
1242- const navigated = resized ( ) ;
1243- ( yield * Queue . take ( reads ) ) . reject ( new Error ( "Cannot find context with specified id" ) ) ;
1244- yield * Effect . promise ( ( ) => navigated ) ;
1245- const hidden = resized ( ) ;
1246- ( yield * Queue . take ( reads ) ) . resolve ( answer ( 0 , 0 ) ) ;
1247- yield * Effect . promise ( ( ) => hidden ) ;
1248- expect ( viewportSizes ( yield * Queue . takeAll ( viewer . output ) ) ) . toEqual ( [
1249- "1000x600" ,
1250- "1000x600" ,
1251- "1000x600" ,
1252- ] ) ;
12531255 } ) ,
12541256 ) . pipe ( Effect . provide ( layer ) ) ,
12551257) ;
@@ -1258,6 +1260,14 @@ it.effect("a stalled desktop viewer still gets its size, control and reconnect",
12581260 Effect . scoped (
12591261 Effect . gen ( function * ( ) {
12601262 const { tabId, viewer, context, resize } = yield * openDesktopViewer ;
1263+ const output =
1264+ viewer . output as unknown as Queue . Queue < ServerBrowser . ServerBrowserViewerOutput > ;
1265+ // Ready once the sizes measured on attach and on a first resize both arrived.
1266+ yield * resize ( 1920 , 1080 ) ;
1267+ expect ( viewportSizes ( yield * Queue . takeAll ( viewer . output ) ) ) . toEqual ( [
1268+ "1920x1080" ,
1269+ "1920x1080" ,
1270+ ] ) ;
12611271 yield * Effect . promise ( ( ) => desktopDetachReady . promise ) ;
12621272 yield * viewer . input ( { type : "takeControl" } ) ;
12631273 context . page . emit ( "filechooser" , {
@@ -1268,18 +1278,26 @@ it.effect("a stalled desktop viewer still gets its size, control and reconnect",
12681278 let chooser = yield * Queue . take ( viewer . output ) ;
12691279 while ( chooser . _tag !== "fileChooser" ) chooser = yield * Queue . take ( viewer . output ) ;
12701280 yield * Queue . clear ( viewer . output ) ;
1271- const control = expect . objectContaining ( { _tag : " control" , controller : "you" } ) ;
1272- // A resize replaces a full backlog but keeps control and the controller's open picker .
1281+ // A resize compacts a full backlog but keeps control, the open picker and
1282+ // one-time notifications in order .
12731283 yield * viewer . input ( { type : "takeControl" } ) ;
1284+ const notifications : Array < ServerBrowser . ServerBrowserViewerOutput > = [
1285+ { _tag : "fileChooserClosed" , id : "chooser-0" } ,
1286+ { _tag : "popup" , tabId : "popup-1" } ,
1287+ { _tag : "download" , id : "download-1" , fileName : "report.csv" , sizeBytes : 3 } ,
1288+ { _tag : "clipboard" , text : "copied" } ,
1289+ ] ;
1290+ for ( const item of notifications ) Queue . offerUnsafe ( output , item ) ;
12741291 let acked = stallViewer ( viewer ) ;
12751292 yield * resize ( 1000 , 600 ) ;
12761293 expect ( yield * Queue . takeAll ( viewer . output ) ) . toEqual ( [
1277- { _tag : "viewport" , width : 1000 , height : 600 } ,
1278- control ,
12791294 chooser ,
1295+ ...notifications ,
1296+ expect . objectContaining ( { _tag : "control" , controller : "you" } ) ,
1297+ { _tag : "viewport" , width : 1000 , height : 600 } ,
12801298 ] ) ;
12811299 yield * acked ;
1282- // A control change keeps the newest size.
1300+ // A control change keeps only the newest size.
12831301 yield * resize ( 900 , 500 ) ;
12841302 yield * resize ( 1200 , 700 ) ;
12851303 acked = stallViewer ( viewer ) ;
@@ -1289,6 +1307,13 @@ it.effect("a stalled desktop viewer still gets its size, control and reconnect",
12891307 { _tag : "viewport" , width : 1200 , height : 700 } ,
12901308 ] ) ;
12911309 yield * acked ;
1310+ // Notifications that no longer fit go oldest first, never the size.
1311+ let copies = 0 ;
1312+ while ( Queue . offerUnsafe ( output , { _tag : "clipboard" , text : `copy ${ copies } ` } ) ) copies ++ ;
1313+ yield * resize ( 1000 , 600 ) ;
1314+ const compacted = yield * Queue . takeAll ( viewer . output ) ;
1315+ expect ( compacted . at ( 0 ) ) . toEqual ( { _tag : "clipboard" , text : "copy 1" } ) ;
1316+ expect ( compacted . at ( - 1 ) ) . toEqual ( { _tag : "viewport" , width : 1000 , height : 600 } ) ;
12921317 // The desktop takes the page back; a resize after it cannot bury the reconnect.
12931318 acked = stallViewer ( viewer ) ;
12941319 desktopDetaches . emit ( "detach" , { threadId : scope . thread . threadId , tabId } ) ;
0 commit comments