@@ -256,31 +256,64 @@ function makeContext(onClose?: (context: BrowserContext) => void, initialUrl = "
256256
257257// Runs the real encoder script against fake canvas and MediaRecorder globals.
258258function makeEncoderPage ( ) {
259+ let paintedFrame = "" ;
259260 const canvas = {
260261 width : 0 ,
261262 height : 0 ,
262- getContext : ( ) => ( { drawImage ( ) { } , fillRect ( ) { } } ) ,
263- captureStream : ( frameRate ?: number ) => ( { frameRate, getTracks : ( ) => [ ] } ) ,
263+ getContext : ( ) => ( {
264+ drawImage ( bitmap : { pixels : string } ) {
265+ paintedFrame = bitmap . pixels ;
266+ } ,
267+ fillRect ( ) { } ,
268+ } ) ,
269+ captureStream : ( frameRate ?: number ) => ( {
270+ frameRate,
271+ getTracks : ( ) => [ ] ,
272+ getVideoTracks : ( ) => [ { requestFrame ( ) { } } ] ,
273+ } ) ,
274+ } ;
275+ const video = {
276+ play : async ( ) => { } ,
277+ pause ( ) { } ,
278+ requestVideoFrameCallback : ( callback : ( ) => void ) => queueMicrotask ( callback ) ,
264279 } ;
265280 class FakeMediaRecorder {
266281 static isTypeSupported = ( type : string ) => type === "video/webm" ;
282+ mimeType = "video/webm" ;
283+ state = "inactive" ;
267284 onstart : ( ( ) => void ) | null = null ;
285+ onstop : ( ( ) => void ) | null = null ;
286+ ondataavailable : ( ( event : { data : Blob } ) => void ) | null = null ;
287+ readonly stream : { frameRate ?: number } ;
268288 constructor ( stream : { frameRate ?: number } , options : { videoBitsPerSecond : number } ) {
289+ this . stream = stream ;
269290 encoderStreams . push ( {
270291 frameRate : stream . frameRate ,
271292 bitsPerSecond : options . videoBitsPerSecond ,
272293 } ) ;
273294 }
274295 start ( ) {
296+ this . state = "recording" ;
275297 this . onstart ?.( ) ;
276298 }
299+ stop ( ) {
300+ this . state = "inactive" ;
301+ this . ondataavailable ?.( { data : new Blob ( [ paintedFrame ] ) } ) ;
302+ this . onstop ?.( ) ;
303+ }
277304 }
278305 const sandbox : Record < string , unknown > = {
279- document : { createElement : ( ) => canvas } ,
306+ document : { createElement : ( tag : string ) => ( tag === "video" ? video : canvas ) } ,
280307 Path2D : function Path2D ( ) { } ,
281308 MediaRecorder : FakeMediaRecorder ,
282- createImageBitmap : async ( ) => ( { width : 1280 , height : 800 , close ( ) { } } ) ,
309+ createImageBitmap : async ( blob : Blob ) => ( {
310+ width : 1280 ,
311+ height : 800 ,
312+ pixels : await blob . text ( ) ,
313+ close ( ) { } ,
314+ } ) ,
283315 atob,
316+ btoa,
284317 Blob,
285318 performance,
286319 } ;
@@ -818,6 +851,37 @@ it.live.each([
818851 ) . pipe ( Effect . provide ( layer ) ) ,
819852) ;
820853
854+ it . live . each ( [
855+ { refreshFails : false , expected : "final frame" } ,
856+ { refreshFails : true , expected : "frame" } ,
857+ ] ) (
858+ "saves the last page state when stopping a recording ($refreshFails)" ,
859+ ( { refreshFails, expected } ) =>
860+ Effect . scoped (
861+ Effect . gen ( function * ( ) {
862+ const { broker, tabId } = yield * ready ;
863+ const fs = yield * FileSystem . FileSystem ;
864+ yield * broker . invoke ( { scope, tabId, operation : "recordingStart" , input : { } } ) ;
865+
866+ const cdp = contexts [ 0 ] ! . sessions [ 0 ] ! ;
867+ const send = cdp . send . getMockImplementation ( ) ! ;
868+ cdp . send . mockImplementation ( ( method , input ) => {
869+ if ( method !== "Page.captureScreenshot" ) return send ( method , input ) ;
870+ return refreshFails
871+ ? Promise . reject ( new Error ( "Page cannot capture a frame" ) )
872+ : Promise . resolve ( { data : Buffer . from ( "final frame" ) . toString ( "base64" ) } ) ;
873+ } ) ;
874+ const artifact = yield * broker . invoke < { path : string } > ( {
875+ scope,
876+ tabId,
877+ operation : "recordingStop" ,
878+ input : { } ,
879+ } ) ;
880+ expect ( yield * fs . readFileString ( artifact . path ) ) . toBe ( expected ) ;
881+ } ) ,
882+ ) . pipe ( Effect . provide ( layer ) ) ,
883+ ) ;
884+
821885it . live ( "enforces provider ownership and explicit targets when a session has multiple tabs" , ( ) =>
822886 Effect . scoped (
823887 Effect . gen ( function * ( ) {
0 commit comments