@@ -261,31 +261,64 @@ function makeContext(onClose?: (context: BrowserContext) => void, initialUrl = "
261261
262262// Runs the real encoder script against fake canvas and MediaRecorder globals.
263263function makeEncoderPage ( ) {
264+ let paintedFrame = "" ;
264265 const canvas = {
265266 width : 0 ,
266267 height : 0 ,
267- getContext : ( ) => ( { drawImage ( ) { } , fillRect ( ) { } } ) ,
268- captureStream : ( frameRate ?: number ) => ( { frameRate, getTracks : ( ) => [ ] } ) ,
268+ getContext : ( ) => ( {
269+ drawImage ( bitmap : { pixels : string } ) {
270+ paintedFrame = bitmap . pixels ;
271+ } ,
272+ fillRect ( ) { } ,
273+ } ) ,
274+ captureStream : ( frameRate ?: number ) => ( {
275+ frameRate,
276+ getTracks : ( ) => [ ] ,
277+ getVideoTracks : ( ) => [ { requestFrame ( ) { } } ] ,
278+ } ) ,
279+ } ;
280+ const video = {
281+ play : async ( ) => { } ,
282+ pause ( ) { } ,
283+ requestVideoFrameCallback : ( callback : ( ) => void ) => queueMicrotask ( callback ) ,
269284 } ;
270285 class FakeMediaRecorder {
271286 static isTypeSupported = ( type : string ) => type === "video/webm" ;
287+ mimeType = "video/webm" ;
288+ state = "inactive" ;
272289 onstart : ( ( ) => void ) | null = null ;
290+ onstop : ( ( ) => void ) | null = null ;
291+ ondataavailable : ( ( event : { data : Blob } ) => void ) | null = null ;
292+ readonly stream : { frameRate ?: number } ;
273293 constructor ( stream : { frameRate ?: number } , options : { videoBitsPerSecond : number } ) {
294+ this . stream = stream ;
274295 encoderStreams . push ( {
275296 frameRate : stream . frameRate ,
276297 bitsPerSecond : options . videoBitsPerSecond ,
277298 } ) ;
278299 }
279300 start ( ) {
301+ this . state = "recording" ;
280302 this . onstart ?.( ) ;
281303 }
304+ stop ( ) {
305+ this . state = "inactive" ;
306+ this . ondataavailable ?.( { data : new Blob ( [ paintedFrame ] ) } ) ;
307+ this . onstop ?.( ) ;
308+ }
282309 }
283310 const sandbox : Record < string , unknown > = {
284- document : { createElement : ( ) => canvas } ,
311+ document : { createElement : ( tag : string ) => ( tag === "video" ? video : canvas ) } ,
285312 Path2D : function Path2D ( ) { } ,
286313 MediaRecorder : FakeMediaRecorder ,
287- createImageBitmap : async ( ) => ( { width : 1280 , height : 800 , close ( ) { } } ) ,
314+ createImageBitmap : async ( blob : Blob ) => ( {
315+ width : 1280 ,
316+ height : 800 ,
317+ pixels : await blob . text ( ) ,
318+ close ( ) { } ,
319+ } ) ,
288320 atob,
321+ btoa,
289322 Blob,
290323 performance,
291324 } ;
@@ -826,6 +859,37 @@ it.live.each([
826859 ) . pipe ( Effect . provide ( layer ) ) ,
827860) ;
828861
862+ it . live . each ( [
863+ { refreshFails : false , expected : "final frame" } ,
864+ { refreshFails : true , expected : "frame" } ,
865+ ] ) (
866+ "saves the last page state when stopping a recording ($refreshFails)" ,
867+ ( { refreshFails, expected } ) =>
868+ Effect . scoped (
869+ Effect . gen ( function * ( ) {
870+ const { broker, tabId } = yield * ready ;
871+ const fs = yield * FileSystem . FileSystem ;
872+ yield * broker . invoke ( { scope, tabId, operation : "recordingStart" , input : { } } ) ;
873+
874+ const cdp = contexts [ 0 ] ! . sessions [ 0 ] ! ;
875+ const send = cdp . send . getMockImplementation ( ) ! ;
876+ cdp . send . mockImplementation ( ( method , input ) => {
877+ if ( method !== "Page.captureScreenshot" ) return send ( method , input ) ;
878+ return refreshFails
879+ ? Promise . reject ( new Error ( "Page cannot capture a frame" ) )
880+ : Promise . resolve ( { data : Buffer . from ( "final frame" ) . toString ( "base64" ) } ) ;
881+ } ) ;
882+ const artifact = yield * broker . invoke < { path : string } > ( {
883+ scope,
884+ tabId,
885+ operation : "recordingStop" ,
886+ input : { } ,
887+ } ) ;
888+ expect ( yield * fs . readFileString ( artifact . path ) ) . toBe ( expected ) ;
889+ } ) ,
890+ ) . pipe ( Effect . provide ( layer ) ) ,
891+ ) ;
892+
829893it . live ( "enforces provider ownership and explicit targets when a session has multiple tabs" , ( ) =>
830894 Effect . scoped (
831895 Effect . gen ( function * ( ) {
0 commit comments