@@ -2560,6 +2560,114 @@ public async Task Capture_UsesMonitorAlias_ForScreenIndex()
25602560 Assert . Equal ( 2 , receivedArgs ! . MonitorIndex ) ;
25612561 }
25622562
2563+ [ Fact ]
2564+ public async Task Capture_RejectsUnsupportedFormat ( )
2565+ {
2566+ // Reject before the capture handler runs so a caller-supplied format
2567+ // cannot reach the data URI MIME type.
2568+ var cap = new ScreenCapability ( NullLogger . Instance ) ;
2569+ var handlerCalled = false ;
2570+ cap . CaptureRequested += ( _ ) =>
2571+ {
2572+ handlerCalled = true ;
2573+ return Task . FromResult ( new ScreenCaptureResult { Format = "png" , Base64 = "x" } ) ;
2574+ } ;
2575+
2576+ var req = new NodeInvokeRequest
2577+ {
2578+ Id = "sfmt1" ,
2579+ Command = "screen.snapshot" ,
2580+ Args = Parse ( """{"format":"svg+xml"}""" )
2581+ } ;
2582+
2583+ var res = await cap . ExecuteAsync ( req ) ;
2584+ Assert . False ( res . Ok ) ;
2585+ Assert . False ( handlerCalled ) ;
2586+ Assert . Contains ( "Unsupported screen snapshot format" , res . Error ) ;
2587+ }
2588+
2589+ [ Fact ]
2590+ public async Task Capture_NormalizesJpgToJpeg ( )
2591+ {
2592+ // Normalize the alias before invoking the capture handler.
2593+ var cap = new ScreenCapability ( NullLogger . Instance ) ;
2594+ ScreenCaptureArgs ? received = null ;
2595+ cap . CaptureRequested += ( args ) =>
2596+ {
2597+ received = args ;
2598+ return Task . FromResult ( new ScreenCaptureResult { Format = args . Format , Width = 10 , Height = 10 , Base64 = "data" } ) ;
2599+ } ;
2600+
2601+ var req = new NodeInvokeRequest
2602+ {
2603+ Id = "sfmt2" ,
2604+ Command = "screen.snapshot" ,
2605+ Args = Parse ( """{"format":"jpg"}""" )
2606+ } ;
2607+
2608+ var res = await cap . ExecuteAsync ( req ) ;
2609+ Assert . True ( res . Ok ) ;
2610+ Assert . NotNull ( received ) ;
2611+ Assert . Equal ( "jpeg" , received ! . Format ) ;
2612+
2613+ var json = JsonSerializer . Serialize ( res . Payload ) ;
2614+ using var doc = JsonDocument . Parse ( json ) ;
2615+ var root = doc . RootElement ;
2616+ Assert . Equal ( "jpeg" , root . GetProperty ( "format" ) . GetString ( ) ) ;
2617+ Assert . StartsWith ( "data:image/jpeg;base64," , root . GetProperty ( "image" ) . GetString ( ) ) ;
2618+ }
2619+
2620+ [ Fact ]
2621+ public async Task Capture_DataUri_IgnoresHandlerEchoedFormat ( )
2622+ {
2623+ // The response MIME type comes from the validated request format.
2624+ var cap = new ScreenCapability ( NullLogger . Instance ) ;
2625+ cap . CaptureRequested += ( _ ) => Task . FromResult ( new ScreenCaptureResult
2626+ {
2627+ Format = "svg+xml\" ;base64,evil" ,
2628+ Width = 1 ,
2629+ Height = 1 ,
2630+ Base64 = "abc123"
2631+ } ) ;
2632+
2633+ var req = new NodeInvokeRequest
2634+ {
2635+ Id = "sfmt3" ,
2636+ Command = "screen.snapshot" ,
2637+ Args = Parse ( """{"format":"png"}""" )
2638+ } ;
2639+
2640+ var res = await cap . ExecuteAsync ( req ) ;
2641+ Assert . True ( res . Ok ) ;
2642+
2643+ var json = JsonSerializer . Serialize ( res . Payload ) ;
2644+ using var doc = JsonDocument . Parse ( json ) ;
2645+ var root = doc . RootElement ;
2646+ Assert . Equal ( "png" , root . GetProperty ( "format" ) . GetString ( ) ) ;
2647+ Assert . Equal ( "data:image/png;base64,abc123" , root . GetProperty ( "image" ) . GetString ( ) ) ;
2648+ }
2649+
2650+ [ Fact ]
2651+ public void TryNormalizeSnapshotFormat_AllowsKnownFormats_RejectsOthers ( )
2652+ {
2653+ Assert . True ( ScreenCapability . TryNormalizeSnapshotFormat ( "png" , out var png ) ) ;
2654+ Assert . Equal ( "png" , png ) ;
2655+ Assert . True ( ScreenCapability . TryNormalizeSnapshotFormat ( "PNG" , out var pngUpper ) ) ;
2656+ Assert . Equal ( "png" , pngUpper ) ;
2657+ Assert . True ( ScreenCapability . TryNormalizeSnapshotFormat ( "jpeg" , out var jpeg ) ) ;
2658+ Assert . Equal ( "jpeg" , jpeg ) ;
2659+ Assert . True ( ScreenCapability . TryNormalizeSnapshotFormat ( " JPG " , out var jpg ) ) ;
2660+ Assert . Equal ( "jpeg" , jpg ) ;
2661+ Assert . True ( ScreenCapability . TryNormalizeSnapshotFormat ( null , out var def ) ) ;
2662+ Assert . Equal ( "png" , def ) ;
2663+ Assert . True ( ScreenCapability . TryNormalizeSnapshotFormat ( "" , out var empty ) ) ;
2664+ Assert . Equal ( "png" , empty ) ;
2665+
2666+ Assert . False ( ScreenCapability . TryNormalizeSnapshotFormat ( "webp" , out _ ) ) ;
2667+ Assert . False ( ScreenCapability . TryNormalizeSnapshotFormat ( "gif" , out _ ) ) ;
2668+ Assert . False ( ScreenCapability . TryNormalizeSnapshotFormat ( "png;base64,x" , out _ ) ) ;
2669+ }
2670+
25632671 [ Fact ]
25642672 public async Task Record_ReturnsError_WhenNoHandler ( )
25652673 {
0 commit comments