@@ -6,6 +6,7 @@ import * as Fiber from "effect/Fiber";
66import * as Queue from "effect/Queue" ;
77import * as Schema from "effect/Schema" ;
88import * as Stream from "effect/Stream" ;
9+ import * as TestClock from "effect/testing/TestClock" ;
910import * as NodeEvents from "node:events" ;
1011
1112import * as DesktopBrowserHost from "./DesktopBrowserHost.ts" ;
@@ -30,7 +31,10 @@ const command = (
3031 } ) ,
3132 ) ;
3233
33- const makeDebuggee = ( prepareSurface : ( ) => Promise < unknown > = async ( ) => true ) => {
34+ const makeDebuggee = (
35+ prepareSurface : ( ) => Promise < unknown > = async ( ) => true ,
36+ restoreSurface : ( ) => Promise < unknown > = async ( ) => true ,
37+ ) => {
3438 const emitter = new NodeEvents . EventEmitter ( ) ;
3539 const pending : Array < ( cause ?: Error ) => void > = [ ] ;
3640 const sent : Array < unknown > = [ ] ;
@@ -55,7 +59,7 @@ const makeDebuggee = (prepareSurface: () => Promise<unknown> = async () => true)
5559 isDestroyed : ( ) => false ,
5660 executeJavaScript : ( expression : string ) => {
5761 surfaceRequests . push ( expression ) ;
58- return surfaceRequests . length === 1 ? prepareSurface ( ) : Promise . resolve ( true ) ;
62+ return expression . endsWith ( ", true)" ) ? prepareSurface ( ) : restoreSurface ( ) ;
5963 } ,
6064 } ,
6165 getURL : ( ) => "http://localhost/" ,
@@ -172,6 +176,39 @@ describe("DesktopBrowserHost", () => {
172176 } ) . pipe ( Effect . scoped ) ,
173177 ) ;
174178
179+ it . effect ( "sends the screenshot when the renderer never answers readiness" , ( ) =>
180+ Effect . gen ( function * ( ) {
181+ const host = yield * DesktopBrowserHost . make ;
182+ const debuggee = makeDebuggee ( ( ) => new Promise ( ( ) => { } ) ) ;
183+ host . attach ( key , debuggee . tab ) ;
184+ const replies = yield * watchReplies ( host ) ;
185+
186+ const sent = debuggee . nextSend ( ) ;
187+ yield * command ( host , 1 , "Page.captureScreenshot" ) ;
188+ yield * TestClock . adjust ( "2 seconds" ) ;
189+ yield * Effect . promise ( ( ) => sent ) ;
190+ debuggee . releaseFirst ( ) ;
191+ expect ( yield * Queue . take ( replies ) ) . toBe ( 1 ) ;
192+ } ) . pipe ( Effect . scoped ) ,
193+ ) ;
194+
195+ it . effect ( "answers and moves on when the renderer never answers restoring the surface" , ( ) =>
196+ Effect . gen ( function * ( ) {
197+ const host = yield * DesktopBrowserHost . make ;
198+ const debuggee = makeDebuggee ( undefined , ( ) => new Promise ( ( ) => { } ) ) ;
199+ host . attach ( key , debuggee . tab ) ;
200+ const replies = yield * watchReplies ( host ) ;
201+
202+ yield * sendScreenshot ( host , debuggee , 1 ) ;
203+ yield * command ( host , 2 , "Page.captureScreenshot" ) ;
204+ const secondSent = debuggee . nextSend ( ) ;
205+ debuggee . releaseFirst ( ) ;
206+ expect ( yield * Queue . take ( replies ) ) . toBe ( 1 ) ;
207+ yield * Effect . promise ( ( ) => secondSent ) ;
208+ expect ( debuggee . sent ) . toEqual ( [ 1 , 2 ] ) ;
209+ } ) . pipe ( Effect . scoped ) ,
210+ ) ;
211+
175212 it . effect ( "announces tabs already attached to a backend that starts later" , ( ) =>
176213 Effect . gen ( function * ( ) {
177214 const host = yield * DesktopBrowserHost . make ;
0 commit comments