@@ -1026,6 +1026,129 @@ it.live("viewers see the agent's pointer move to its target and click there", ()
10261026 ) . pipe ( Effect . provide ( layer ) ) ,
10271027) ;
10281028
1029+ /** Makes a fake desktop page report its ratio and CSS viewport, as a real one would. */
1030+ const reportPage = (
1031+ context : ReturnType < typeof makeContext > ,
1032+ page : { readonly ratio : number ; readonly width : number ; readonly height : number } ,
1033+ ) => {
1034+ const evaluate = context . page . evaluate . getMockImplementation ( ) ! ;
1035+ context . page . evaluate . mockImplementation ( ( ( script ?: unknown ) =>
1036+ typeof script === "string" && script . includes ( "devicePixelRatio" )
1037+ ? Promise . resolve ( page )
1038+ : evaluate ( ) ) as typeof evaluate ) ;
1039+ } ;
1040+
1041+ /** The clip of each scaled capture a context's sessions were asked for. Viewer stills are full size. */
1042+ const captureClips = ( context : ReturnType < typeof makeContext > ) =>
1043+ context . sessions . flatMap ( ( session ) =>
1044+ session . send . mock . calls . flatMap ( ( [ method , input ] ) => {
1045+ const clip = (
1046+ input as
1047+ | {
1048+ readonly clip ?: {
1049+ readonly width : number ;
1050+ readonly height : number ;
1051+ readonly scale : number ;
1052+ } ;
1053+ }
1054+ | undefined
1055+ ) ?. clip ;
1056+ return method === "Page.captureScreenshot" && clip !== undefined
1057+ ? [ { width : clip . width , height : clip . height , scale : clip . scale } ]
1058+ : [ ] ;
1059+ } ) ,
1060+ ) ;
1061+
1062+ it . live ( "sizes a snapshot by the display's ratio for a tab the desktop renders" , ( ) =>
1063+ Effect . scoped (
1064+ Effect . gen ( function * ( ) {
1065+ desktopRendersNext = true ;
1066+ const { browser, broker, tabId } = yield * ready ;
1067+ yield * browser . attachViewer ( viewerInput ( tabId , false ) ) ;
1068+ const context = desktopConnections [ 0 ] ! . context ;
1069+ // A 150% display, with the page at the panel's default 1280x800.
1070+ reportPage ( context , { ratio : 1.5 , width : 1280 , height : 800 } ) ;
1071+ const snapshot = yield * broker . invoke < PreviewAutomationSnapshot > ( {
1072+ scope,
1073+ tabId,
1074+ operation : "snapshot" ,
1075+ input : { } ,
1076+ } ) ;
1077+ expect ( captureClips ( context ) ) . toEqual ( [ { width : 1280 , height : 800 , scale : 1280 / 1920 } ] ) ;
1078+ expect ( snapshot . screenshot ) . toMatchObject ( { width : 1280 , height : 800 } ) ;
1079+ } ) ,
1080+ ) . pipe ( Effect . provide ( layer ) ) ,
1081+ ) ;
1082+
1083+ it . live ( "captures the viewport a desktop-drawn page actually has" , ( ) =>
1084+ Effect . scoped (
1085+ Effect . gen ( function * ( ) {
1086+ desktopRendersNext = true ;
1087+ const { browser, broker, tabId } = yield * ready ;
1088+ yield * browser . attachViewer ( viewerInput ( tabId , false ) ) ;
1089+ const context = desktopConnections [ 0 ] ! . context ;
1090+ // The desktop window was zoomed out, so the page lays out smaller than 1280x800.
1091+ reportPage ( context , { ratio : 1.5 , width : 1067 , height : 667 } ) ;
1092+ const snapshot = yield * broker . invoke < PreviewAutomationSnapshot > ( {
1093+ scope,
1094+ tabId,
1095+ operation : "snapshot" ,
1096+ input : { } ,
1097+ } ) ;
1098+ expect ( captureClips ( context ) ) . toEqual ( [
1099+ { width : 1067 , height : 667 , scale : 1280 / ( 1067 * 1.5 ) } ,
1100+ ] ) ;
1101+ expect ( snapshot . screenshot ) . toMatchObject ( { width : 1280 , height : 800 } ) ;
1102+ } ) ,
1103+ ) . pipe ( Effect . provide ( layer ) ) ,
1104+ ) ;
1105+
1106+ it . live ( "leaves page zoom out of a desktop-drawn tab's snapshot scale" , ( ) =>
1107+ Effect . scoped (
1108+ Effect . gen ( function * ( ) {
1109+ desktopRendersNext = true ;
1110+ const { browser, broker, tabId } = yield * ready ;
1111+ const manager = yield * Manager . PreviewManager ;
1112+ yield * browser . attachViewer ( viewerInput ( tabId , false ) ) ;
1113+ const context = desktopConnections [ 0 ] ! . context ;
1114+ yield * manager . adjust ( { threadId : scope . thread . threadId , tabId, zoomFactor : 2 } ) ;
1115+ // A 150% display with the page at 200% zoom: it lays out at half size in CSS pixels.
1116+ reportPage ( context , { ratio : 3 , width : 640 , height : 400 } ) ;
1117+ const snapshot = yield * broker . invoke < PreviewAutomationSnapshot > ( {
1118+ scope,
1119+ tabId,
1120+ operation : "snapshot" ,
1121+ input : { } ,
1122+ } ) ;
1123+ expect ( captureClips ( context ) ) . toEqual ( [ { width : 1280 , height : 800 , scale : 1280 / 1920 } ] ) ;
1124+ expect ( snapshot . screenshot ) . toMatchObject ( { width : 1280 , height : 800 } ) ;
1125+ // The desktop zooms its own page; the server sends it no emulation.
1126+ expect (
1127+ context . sessions . flatMap ( ( session ) => session . send . mock . calls . map ( ( [ method ] ) => method ) ) ,
1128+ ) . not . toContain ( "Emulation.setDeviceMetricsOverride" ) ;
1129+ } ) ,
1130+ ) . pipe ( Effect . provide ( layer ) ) ,
1131+ ) ;
1132+
1133+ it . live ( "keeps a headless tab's snapshot at its own render scale" , ( ) =>
1134+ Effect . scoped (
1135+ Effect . gen ( function * ( ) {
1136+ const { broker, tabId } = yield * ready ;
1137+ const snapshot = yield * broker . invoke < PreviewAutomationSnapshot > ( {
1138+ scope,
1139+ tabId,
1140+ operation : "snapshot" ,
1141+ input : { } ,
1142+ } ) ;
1143+ expect ( captureClips ( contexts [ 0 ] ! ) ) . toEqual ( [ { width : 1280 , height : 800 , scale : 0.5 } ] ) ;
1144+ expect ( contexts [ 0 ] ! . page . evaluate ) . not . toHaveBeenCalledWith (
1145+ expect . stringContaining ( "devicePixelRatio" ) ,
1146+ ) ;
1147+ expect ( snapshot . screenshot ) . toMatchObject ( { width : 1280 , height : 800 } ) ;
1148+ } ) ,
1149+ ) . pipe ( Effect . provide ( layer ) ) ,
1150+ ) ;
1151+
10291152it . live ( "drives the desktop's own page for a tab the desktop renders" , ( ) =>
10301153 Effect . scoped (
10311154 Effect . gen ( function * ( ) {
0 commit comments