33 AuthOrchestrationReadScope ,
44 AuthOrchestrationOperateScope ,
55 AuthSessionId ,
6+ DeviceOperationError ,
7+ type DeviceSummary ,
68 LOCAL_DEVICE_HOST_ID ,
79 type AuthEnvironmentScope ,
810} from "@t3tools/contracts" ;
@@ -24,6 +26,8 @@ const fixture = (
2426 fail = false ,
2527 authError ?: EnvironmentAuth . ServerAuthCredentialError | EnvironmentAuth . ServerAuthInternalError ,
2628 upstream ?: DeviceHost . DeviceHostReady [ "upstream" ] ,
29+ screenshot : DeviceService . DeviceService [ "Service" ] [ "screenshot" ] = ( ) =>
30+ Effect . die ( new Error ( "unexpected screenshot" ) ) ,
2731) => {
2832 let finalized = 0 ;
2933 const requests : string [ ] = [ ] ;
@@ -65,6 +69,7 @@ const fixture = (
6569 hub : { origin : "http://hub.test" } ,
6670 ...( upstream ? { upstream } : { } ) ,
6771 } ) ,
72+ screenshot,
6873 } as DeviceService . DeviceService [ "Service" ] ) ,
6974 ) ,
7075 Layer . provideMerge ( Layer . succeed ( HttpClient . HttpClient , client ) ) ,
@@ -202,7 +207,7 @@ describe("device hub proxy", () => {
202207 undefined ,
203208 ( hubPath , search ) =>
204209 Effect . succeed (
205- hubPath === "/vendor/serve-sim/api"
210+ hubPath === "/vendor/serve-sim/api" || hubPath === "/vendor/serve-sim/api/screenshot"
206211 ? {
207212 url : `https://session.test${ hubPath . slice ( "/vendor/serve-sim" . length ) } ?${ search } ` ,
208213 headers : { authorization : "Bearer session-token" } ,
@@ -221,11 +226,72 @@ describe("device hub proxy", () => {
221226 await routed . text ( ) ;
222227 expect ( requests ) . toEqual ( [ "https://session.test/api?device=udid-1" ] ) ;
223228 expect ( headers [ 0 ] ?. authorization ) . toBe ( "Bearer session-token" ) ;
229+ const screenshot = await handler (
230+ new Request (
231+ "http://t3.test/api/device-hub/vendor/serve-sim/api/screenshot?device=udid-1&hostId=eas" ,
232+ { method : "POST" } ,
233+ ) ,
234+ ) ;
235+ expect ( screenshot . status ) . toBe ( 200 ) ;
236+ await screenshot . text ( ) ;
237+ expect ( requests ) . toEqual ( [
238+ "https://session.test/api?device=udid-1" ,
239+ "https://session.test/api/screenshot?device=udid-1" ,
240+ ] ) ;
224241 const unrouted = await handler (
225242 new Request ( "http://t3.test/api/device-hub/vendor/serve-sim/helper/udid-2/stream.mjpeg" ) ,
226243 ) ;
227244 expect ( unrouted . status ) . toBe ( 404 ) ;
228- expect ( requests ) . toHaveLength ( 1 ) ;
245+ expect ( requests ) . toHaveLength ( 2 ) ;
246+ } ) ;
247+
248+ it ( "captures iOS screenshots through the device service on hub hosts" , async ( ) => {
249+ const captured : Array < { readonly hostId ?: string | undefined ; readonly deviceId : string } > = [ ] ;
250+ const png = new Uint8Array ( [ 0x89 , 0x50 , 0x4e , 0x47 ] ) ;
251+ const { handler, requests } = fixture (
252+ [ AuthOrchestrationReadScope ] ,
253+ false ,
254+ undefined ,
255+ undefined ,
256+ ( input ) => {
257+ captured . push ( input ) ;
258+ return input . deviceId === "udid-1"
259+ ? Effect . succeed ( { device : { } as DeviceSummary , png } )
260+ : Effect . fail (
261+ new DeviceOperationError ( {
262+ operation : "screenshot" ,
263+ reason : "command_failed" ,
264+ cause : "simctl failed" ,
265+ } ) ,
266+ ) ;
267+ } ,
268+ ) ;
269+ const hub = "http://t3.test/api/device-hub/vendor" ;
270+ const ios = await handler (
271+ new Request ( `${ hub } /serve-sim/api/screenshot?device=udid-1&hostId=local` , {
272+ method : "POST" ,
273+ } ) ,
274+ ) ;
275+ expect ( ios . status ) . toBe ( 200 ) ;
276+ expect ( ios . headers . get ( "content-type" ) ) . toBe ( "image/png" ) ;
277+ expect ( new Uint8Array ( await ios . arrayBuffer ( ) ) ) . toEqual ( png ) ;
278+ const failed = await handler (
279+ new Request ( `${ hub } /serve-sim/api/screenshot?device=udid-2` , { method : "POST" } ) ,
280+ ) ;
281+ expect ( failed . status ) . toBe ( 502 ) ;
282+ expect ( await failed . text ( ) ) . not . toContain ( "simctl failed" ) ;
283+ expect ( captured ) . toEqual ( [
284+ { hostId : LOCAL_DEVICE_HOST_ID , deviceId : "udid-1" } ,
285+ { hostId : LOCAL_DEVICE_HOST_ID , deviceId : "udid-2" } ,
286+ ] ) ;
287+ const android = await handler (
288+ new Request ( `${ hub } /serve-emu/api/screenshot?device=emulator-5554` , { method : "POST" } ) ,
289+ ) ;
290+ expect ( android . status ) . toBe ( 200 ) ;
291+ await android . text ( ) ;
292+ expect ( requests ) . toEqual ( [
293+ "http://hub.test/vendor/serve-emu/api/screenshot?device=emulator-5554" ,
294+ ] ) ;
229295 } ) ;
230296
231297 it ( "never forwards the vendor shell endpoint" , async ( ) => {
0 commit comments