@@ -182,6 +182,7 @@ describe("previewWindowOpenAction", () => {
182182const {
183183 browserWindowConstructor,
184184 clipboardItemConstructor,
185+ createFromBuffer,
185186 createFromPath,
186187 fromId,
187188 getFocusedWebContents,
@@ -193,6 +194,11 @@ const {
193194} = vi . hoisted ( ( ) => ( {
194195 browserWindowConstructor : vi . fn ( ) ,
195196 clipboardItemConstructor : vi . fn ( ) ,
197+ createFromBuffer : vi . fn ( ( data : Buffer ) => ( {
198+ isEmpty : ( ) => data . length === 0 ,
199+ getSize : ( ) => ( { width : 800 , height : 600 } ) ,
200+ toPNG : ( ) => data ,
201+ } ) ) ,
196202 createFromPath : vi . fn ( ( ) : { readonly isEmpty : ( ) => boolean ; readonly toPNG : ( ) => Buffer } => ( {
197203 isEmpty : ( ) => false ,
198204 toPNG : ( ) => Buffer . from ( "png" ) ,
@@ -217,6 +223,7 @@ vi.mock("electron", () => ({
217223 write : writeClipboard ,
218224 } ,
219225 nativeImage : {
226+ createFromBuffer,
220227 createFromPath,
221228 } ,
222229 shell : {
@@ -2309,6 +2316,102 @@ describe("PreviewManager", () => {
23092316 ) ,
23102317 ) ;
23112318
2319+ effectIt . effect (
2320+ "snapshots a frame redrawn after reading the page, not the guest's last frame" ,
2321+ ( ) =>
2322+ withManager ( ( manager ) =>
2323+ Effect . gen ( function * ( ) {
2324+ const setBackgroundThrottling = vi . fn < ( enabled : boolean ) => void > ( ) ;
2325+ // capturePage only nudges the window to draw; its stale pixels must not be the snapshot.
2326+ const copies : Array < PromiseWithResolvers < TestCapturedPreviewImage > > = [ ] ;
2327+ const capturePage = vi . fn ( ( ..._args : ReadonlyArray < unknown > ) => {
2328+ const copy = Promise . withResolvers < TestCapturedPreviewImage > ( ) ;
2329+ copies . push ( copy ) ;
2330+ return copy . promise ;
2331+ } ) ;
2332+ const drawnCopy = {
2333+ isEmpty : ( ) => false ,
2334+ toJPEG : ( ) => Buffer . from ( "stale" ) ,
2335+ getSize : ( ) => ( { width : 1 , height : 1 } ) ,
2336+ } ;
2337+ const settlePromises = Effect . promise (
2338+ ( ) => new Promise < void > ( ( resolve ) => setImmediate ( resolve ) ) ,
2339+ ) ;
2340+ const redrawnPng = Buffer . from ( "png drawn after the page changed" ) ;
2341+ const pendingRedraw = Promise . withResolvers < { data : string } > ( ) ;
2342+ const screenshotResults = [ Promise . resolve ( { } ) , pendingRedraw . promise ] ;
2343+ const sendCommand = vi . fn ( async ( method : string ) : Promise < unknown > => {
2344+ if ( method === "Page.captureScreenshot" ) return screenshotResults . shift ( ) ;
2345+ if ( method === "Runtime.evaluate" ) {
2346+ return {
2347+ result : {
2348+ value : {
2349+ url : "https://example.com" ,
2350+ title : "Example" ,
2351+ loading : false ,
2352+ visibleText : "QA new marker" ,
2353+ interactiveElements : [ ] ,
2354+ } ,
2355+ } ,
2356+ } ;
2357+ }
2358+ return method === "Accessibility.getFullAXTree" ? { nodes : [ ] } : undefined ;
2359+ } ) ;
2360+ const wc = makeTestPreviewWebContents ( capturePage ) ;
2361+ Object . assign ( wc , { isDevToolsOpened : ( ) => false } ) ;
2362+ Object . assign ( wc . debugger , { sendCommand } ) ;
2363+ fromId . mockReturnValue ( wc ) ;
2364+ yield * manager . createTab ( "tab_1" ) ;
2365+ yield * manager . registerWebview ( "tab_1" , 42 ) ;
2366+ yield * manager . setMainWindow ( {
2367+ isDestroyed : ( ) => false ,
2368+ once : vi . fn ( ) ,
2369+ webContents : { setBackgroundThrottling } ,
2370+ } as never ) ;
2371+
2372+ const snapshot = yield * manager
2373+ . automationSnapshot ( "tab_1" )
2374+ . pipe ( Effect . forkChild ( { startImmediately : true } ) ) ;
2375+ // The first capture returned no image; the retry waits on Chromium's redraw.
2376+ yield * TestClock . adjust ( 200 ) ;
2377+ const screenshotCalls = sendCommand . mock . calls
2378+ . map ( ( [ method ] , index ) => ( {
2379+ method,
2380+ order : sendCommand . mock . invocationCallOrder [ index ] ! ,
2381+ } ) )
2382+ . filter ( ( { method } ) => method === "Page.captureScreenshot" ) ;
2383+ expect ( screenshotCalls ) . toHaveLength ( 2 ) ;
2384+ const pageRead = sendCommand . mock . calls . findIndex (
2385+ ( [ method ] ) => method === "Runtime.evaluate" ,
2386+ ) ;
2387+ expect ( screenshotCalls [ 0 ] ! . order ) . toBeGreaterThan (
2388+ sendCommand . mock . invocationCallOrder [ pageRead ] ! ,
2389+ ) ;
2390+ // Presenting the redrawn frame needs the main window to keep drawing.
2391+ expect ( setBackgroundThrottling . mock . calls ) . toEqual ( [ [ false ] ] ) ;
2392+ // Whole-view copies that leave the page hidden make the window draw the guest.
2393+ expect ( capturePage ) . toHaveBeenLastCalledWith ( undefined , { stayHidden : true } ) ;
2394+
2395+ // Every drawn copy is followed by another until the redraw arrives.
2396+ const copiesBeforeDraw = capturePage . mock . calls . length ;
2397+ copies . at ( - 1 ) ! . resolve ( drawnCopy ) ;
2398+ yield * settlePromises ;
2399+ expect ( capturePage ) . toHaveBeenCalledTimes ( copiesBeforeDraw + 1 ) ;
2400+
2401+ pendingRedraw . resolve ( { data : redrawnPng . toString ( "base64" ) } ) ;
2402+ const result = yield * Fiber . join ( snapshot ) ;
2403+ expect ( result . visibleText ) . toBe ( "QA new marker" ) ;
2404+ expect ( result . screenshot . data ) . toBe ( redrawnPng . toString ( "base64" ) ) ;
2405+ expect ( setBackgroundThrottling . mock . calls ) . toEqual ( [ [ false ] , [ true ] ] ) ;
2406+
2407+ // Once the screenshot settles, the copy in flight is the last one.
2408+ for ( const copy of copies ) copy . resolve ( drawnCopy ) ;
2409+ yield * settlePromises ;
2410+ expect ( capturePage ) . toHaveBeenCalledTimes ( copiesBeforeDraw + 1 ) ;
2411+ } ) ,
2412+ ) ,
2413+ ) ;
2414+
23122415 effectIt . effect ( "keeps every recorded guest unthrottled until its frame capture stops" , ( ) =>
23132416 withManager ( ( manager ) =>
23142417 Effect . gen ( function * ( ) {
@@ -2694,11 +2797,26 @@ describe("PreviewManager", () => {
26942797 effectIt . effect ( "releases snapshot control when every capture attempt stalls" , ( ) =>
26952798 withManager ( ( manager ) =>
26962799 Effect . gen ( function * ( ) {
2697- const capturePage = vi . fn ( ( ) => new Promise < TestCapturedPreviewImage > ( ( ) => { } ) ) ;
2800+ const copies : Array < PromiseWithResolvers < TestCapturedPreviewImage > > = [ ] ;
2801+ const capturePage = vi . fn ( ( ) => {
2802+ const copy = Promise . withResolvers < TestCapturedPreviewImage > ( ) ;
2803+ copies . push ( copy ) ;
2804+ return copy . promise ;
2805+ } ) ;
2806+ const copyImage = ( empty : boolean ) => ( {
2807+ isEmpty : ( ) => empty ,
2808+ toJPEG : ( ) => Buffer . from ( "copy" ) ,
2809+ getSize : ( ) => ( { width : empty ? 0 : 1 , height : empty ? 0 : 1 } ) ,
2810+ } ) ;
2811+ const settlePromises = Effect . promise (
2812+ ( ) => new Promise < void > ( ( resolve ) => setImmediate ( resolve ) ) ,
2813+ ) ;
2814+ const captureScreenshot = vi . fn ( ( ) => new Promise < never > ( ( ) => { } ) ) ;
26982815 const wc = makeTestPreviewWebContents ( capturePage ) ;
26992816 Object . assign ( wc , { isDevToolsOpened : ( ) => false } ) ;
27002817 Object . assign ( wc . debugger , {
27012818 sendCommand : vi . fn ( async ( method : string , params ?: Record < string , unknown > ) => {
2819+ if ( method === "Page.captureScreenshot" ) return captureScreenshot ( ) ;
27022820 if ( method === "Runtime.evaluate" ) {
27032821 return {
27042822 result : {
@@ -2726,21 +2844,37 @@ describe("PreviewManager", () => {
27262844 Effect . forkChild ( { startImmediately : true } ) ,
27272845 ) ;
27282846 yield * TestClock . adjust ( 100 ) ;
2847+ expect ( captureScreenshot ) . toHaveBeenCalledOnce ( ) ;
27292848 expect ( capturePage ) . toHaveBeenCalledOnce ( ) ;
27302849 const evaluate = yield * manager
27312850 . automationEvaluate ( "tab_1" , { expression : "42" } )
27322851 . pipe ( Effect . forkChild ( { startImmediately : true } ) ) ;
27332852 expect ( evaluate . pollUnsafe ( ) ) . toBeUndefined ( ) ;
27342853
2854+ // The first attempt timed out; its copy landing afterwards must not request another.
2855+ yield * TestClock . adjust ( 950 ) ;
2856+ copies [ 0 ] ! . resolve ( copyImage ( false ) ) ;
2857+ yield * settlePromises ;
2858+ expect ( capturePage ) . toHaveBeenCalledOnce ( ) ;
2859+
2860+ // An empty copy did not make the window draw, so the retry stops requesting copies.
2861+ yield * TestClock . adjust ( 100 ) ;
2862+ expect ( captureScreenshot ) . toHaveBeenCalledTimes ( 2 ) ;
2863+ expect ( capturePage ) . toHaveBeenCalledTimes ( 2 ) ;
2864+ copies [ 1 ] ! . resolve ( copyImage ( true ) ) ;
2865+ yield * settlePromises ;
2866+ expect ( capturePage ) . toHaveBeenCalledTimes ( 2 ) ;
2867+
27352868 yield * TestClock . adjust ( 4_000 ) ;
27362869 const exit = yield * Fiber . join ( snapshot ) ;
27372870 expect ( Exit . isFailure ( exit ) ) . toBe ( true ) ;
2871+ expect ( captureScreenshot ) . toHaveBeenCalledTimes ( 3 ) ;
27382872 expect ( capturePage ) . toHaveBeenCalledTimes ( 3 ) ;
27392873 if ( Exit . isSuccess ( exit ) ) return ;
27402874 const error = Option . getOrThrow ( Cause . findErrorOption ( exit . cause ) ) ;
27412875 expect ( error ) . toMatchObject ( {
27422876 _tag : "PreviewOperationError" ,
2743- operation : "automationSnapshot.capturePage " ,
2877+ operation : "automationSnapshot.captureScreenshot " ,
27442878 tabId : "tab_1" ,
27452879 webContentsId : 42 ,
27462880 cause : { _tag : "TimeoutError" } ,
0 commit comments