@@ -3,6 +3,7 @@ import { describe, expect, it } from "@effect/vitest";
33import { DesktopBrowserEvent } from "@t3tools/contracts" ;
44import * as Effect from "effect/Effect" ;
55import * as Fiber from "effect/Fiber" ;
6+ import * as Queue from "effect/Queue" ;
67import * as Schema from "effect/Schema" ;
78import * as Stream from "effect/Stream" ;
89import * as NodeEvents from "node:events" ;
@@ -15,32 +16,54 @@ const decodeCdpReply = Schema.decodeUnknownSync(
1516 Schema . fromJsonString ( Schema . Struct ( { id : Schema . Number } ) ) ,
1617) ;
1718const key = { threadId : "thread-1" , tabId : "tab-1" } ;
19+ /** Sends a page command whose id the fake debugger records as it receives it. */
20+ const command = (
21+ host : DesktopBrowserHost . DesktopBrowserHost [ "Service" ] ,
22+ id : number ,
23+ method : string ,
24+ ) =>
25+ host . handleCommandLine (
26+ encodeJson ( {
27+ type : "cdp" ,
28+ ...key ,
29+ message : encodeJson ( { id, method, params : { id } , sessionId : "t3-preview-page" } ) ,
30+ } ) ,
31+ ) ;
1832
1933const makeDebuggee = ( ) => {
2034 const emitter = new NodeEvents . EventEmitter ( ) ;
2135 const pending : Array < ( ) => void > = [ ] ;
36+ const sent : Array < unknown > = [ ] ;
37+ const sendWaiters : Array < ( ) => void > = [ ] ;
2238 const debuggee = Object . assign ( emitter , {
23- sendCommand : ( method : string ) =>
39+ sendCommand : ( method : string , params ?: { id ?: number } ) =>
2440 new Promise ( ( resolve ) => {
2541 if ( method === "Target.getTargetInfo" ) {
2642 resolve ( { targetInfo : { targetId : "GUEST" } } ) ;
2743 return ;
2844 }
45+ sent . push ( params ?. id ) ;
2946 pending . push ( ( ) => resolve ( { method } ) ) ;
47+ sendWaiters . shift ( ) ?.( ) ;
3048 } ) ,
3149 } ) ;
32- const webContents = {
50+ const webContents = Object . assign ( new NodeEvents . EventEmitter ( ) , {
3351 getURL : ( ) => "http://localhost/" ,
3452 getTitle : ( ) => "Page" ,
3553 getUserAgent : ( ) => "Electron" ,
36- } ;
54+ } ) ;
3755 return {
3856 tab : {
3957 webContents : webContents as unknown as Electron . WebContents ,
4058 debugger : debuggee as unknown as Electron . Debugger ,
4159 } ,
4260 emit : ( method : string , params : unknown ) => emitter . emit ( "message" , { } , method , params , "" ) ,
61+ navigate : ( ) => webContents . emit ( "did-navigate" ) ,
62+ /** Ids of the commands the debugger received, in order. */
63+ sent,
64+ nextSend : ( ) => new Promise < void > ( ( resolve ) => sendWaiters . push ( resolve ) ) ,
4365 release : ( ) => pending . splice ( 0 ) . forEach ( ( resolve ) => resolve ( ) ) ,
66+ releaseFirst : ( ) => pending . shift ( ) ?.( ) ,
4467 } ;
4568} ;
4669
@@ -51,6 +74,25 @@ const takeEvents = (host: DesktopBrowserHost.DesktopBrowserHost["Service"], coun
5174 Effect . map ( ( lines ) => lines . map ( ( line ) => decodeEvent ( new TextDecoder ( ) . decode ( line ) ) ) ) ,
5275 ) ;
5376
77+ /**
78+ * Ids of the replies the host sends, in order. A screenshot's reply leaves after its turn
79+ * has passed on, so by then the next waiting screenshot, if any, has been sent. Subscribe
80+ * before sending commands: a new subscriber releases the tab's relay.
81+ */
82+ const watchReplies = ( host : DesktopBrowserHost . DesktopBrowserHost [ "Service" ] ) =>
83+ Effect . gen ( function * ( ) {
84+ const replies = yield * Queue . unbounded < number > ( ) ;
85+ yield * host . events . pipe (
86+ Stream . map ( ( line ) => decodeEvent ( new TextDecoder ( ) . decode ( line ) ) ) ,
87+ Stream . runForEach ( ( event ) =>
88+ event . type === "cdp" ? Queue . offer ( replies , decodeCdpReply ( event . message ) . id ) : Effect . void ,
89+ ) ,
90+ Effect . forkScoped ,
91+ ) ;
92+ yield * Effect . yieldNow ;
93+ return replies ;
94+ } ) ;
95+
5496describe ( "DesktopBrowserHost" , ( ) => {
5597 it . effect ( "announces tabs already attached to a backend that starts later" , ( ) =>
5698 Effect . gen ( function * ( ) {
@@ -89,6 +131,67 @@ describe("DesktopBrowserHost", () => {
89131 } ) . pipe ( Effect . scoped ) ,
90132 ) ;
91133
134+ it . effect ( "sends a page one screenshot at a time" , ( ) =>
135+ Effect . gen ( function * ( ) {
136+ const host = yield * DesktopBrowserHost . make ;
137+ const debuggee = makeDebuggee ( ) ;
138+ host . attach ( key , debuggee . tab ) ;
139+ const replies = yield * watchReplies ( host ) ;
140+
141+ yield * command ( host , 1 , "Page.captureScreenshot" ) ;
142+ yield * command ( host , 2 , "Page.captureScreenshot" ) ;
143+ yield * command ( host , 3 , "DOM.enable" ) ;
144+ expect ( debuggee . sent ) . toEqual ( [ 1 , 3 ] ) ;
145+
146+ debuggee . releaseFirst ( ) ;
147+ expect ( yield * Queue . take ( replies ) ) . toBe ( 1 ) ;
148+ expect ( debuggee . sent ) . toEqual ( [ 1 , 3 , 2 ] ) ;
149+ } ) . pipe ( Effect . scoped ) ,
150+ ) ;
151+
152+ it . effect ( "stops waiting on a screenshot the page navigated away from" , ( ) =>
153+ Effect . gen ( function * ( ) {
154+ const host = yield * DesktopBrowserHost . make ;
155+ const debuggee = makeDebuggee ( ) ;
156+ host . attach ( key , debuggee . tab ) ;
157+ const replies = yield * watchReplies ( host ) ;
158+
159+ yield * command ( host , 1 , "Page.captureScreenshot" ) ;
160+ yield * command ( host , 2 , "Page.captureScreenshot" ) ;
161+ yield * command ( host , 3 , "Page.captureScreenshot" ) ;
162+ debuggee . navigate ( ) ;
163+ expect ( debuggee . sent ) . toEqual ( [ 1 , 2 ] ) ;
164+
165+ // The superseded screenshot answering late must not start a third beside the second.
166+ debuggee . releaseFirst ( ) ;
167+ expect ( yield * Queue . take ( replies ) ) . toBe ( 1 ) ;
168+ expect ( debuggee . sent ) . toEqual ( [ 1 , 2 ] ) ;
169+
170+ debuggee . releaseFirst ( ) ;
171+ expect ( yield * Queue . take ( replies ) ) . toBe ( 2 ) ;
172+ expect ( debuggee . sent ) . toEqual ( [ 1 , 2 , 3 ] ) ;
173+ } ) . pipe ( Effect . scoped ) ,
174+ ) ;
175+
176+ it . effect ( "drops screenshots a released relay left waiting" , ( ) =>
177+ Effect . gen ( function * ( ) {
178+ const host = yield * DesktopBrowserHost . make ;
179+ const debuggee = makeDebuggee ( ) ;
180+ host . attach ( key , debuggee . tab ) ;
181+
182+ yield * command ( host , 1 , "Page.captureScreenshot" ) ;
183+ yield * command ( host , 2 , "Page.captureScreenshot" ) ;
184+ yield * host . handleCommandLine ( encodeJson ( { type : "release" , ...key } ) ) ;
185+ yield * command ( host , 3 , "Page.captureScreenshot" ) ;
186+ expect ( debuggee . sent ) . toEqual ( [ 1 ] ) ;
187+
188+ const sent = debuggee . nextSend ( ) ;
189+ debuggee . release ( ) ;
190+ yield * Effect . promise ( ( ) => sent ) ;
191+ expect ( debuggee . sent ) . toEqual ( [ 1 , 3 ] ) ;
192+ } ) ,
193+ ) ;
194+
92195 it . effect ( "saves a server tab's download under its CDP guid where the server asked" , ( ) =>
93196 Effect . gen ( function * ( ) {
94197 const host = yield * DesktopBrowserHost . make ;
0 commit comments