The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 2 | |
| 3 | import { start, stop } from "./device-stream.browser"; |
| 4 | |
| 5 | const envelope = (tag: number, payload: number[]) => { |
| 6 | const length = 1 + payload.length; |
| 7 | return [ |
| 8 | length >>> 24, |
| 9 | (length >>> 16) & 0xff, |
| 10 | (length >>> 8) & 0xff, |
| 11 | length & 0xff, |
| 12 | tag, |
| 13 | ...payload, |
| 14 | ]; |
| 15 | }; |
| 16 | |
| 17 | function viewerFixture(supported: boolean) { |
| 18 | vi.useFakeTimers(); |
| 19 | const configured: string[] = []; |
| 20 | vi.stubGlobal( |
| 21 | "VideoDecoder", |
| 22 | class { |
| 23 | static isConfigSupported = async () => ({ supported }); |
| 24 | state = "unconfigured"; |
| 25 | decodeQueueSize = 0; |
| 26 | configure(config: VideoDecoderConfig) { |
| 27 | configured.push(config.codec); |
| 28 | this.state = "configured"; |
| 29 | } |
| 30 | decode() {} |
| 31 | close() { |
| 32 | this.state = "closed"; |
| 33 | } |
| 34 | }, |
| 35 | ); |
| 36 | vi.stubGlobal("EncodedVideoChunk", vi.fn()); |
| 37 | vi.stubGlobal( |
| 38 | "WebSocket", |
| 39 | class { |
| 40 | static OPEN = 1; |
| 41 | readyState = 0; |
| 42 | send() {} |
| 43 | close() {} |
| 44 | }, |
| 45 | ); |
| 46 | const videoBodies: ReadableStreamDefaultController<Uint8Array>[] = []; |
| 47 | const requested: string[] = []; |
| 48 | vi.stubGlobal("fetch", (url: string, init: RequestInit) => { |
| 49 | requested.push(new URL(url).pathname); |
| 50 | if (!url.includes("stream.avcc")) return Promise.resolve(new Response(null)); |
| 51 | return Promise.resolve( |
| 52 | new Response( |
| 53 | new ReadableStream<Uint8Array>({ |
| 54 | start(body) { |
| 55 | videoBodies.push(body); |
| 56 | init.signal?.addEventListener("abort", () => body.error(new Error("aborted"))); |
| 57 | }, |
| 58 | }), |
| 59 | ), |
| 60 | ); |
| 61 | }); |
| 62 | vi.stubGlobal("ReactNativeWebView", { postMessage() {} }); |
| 63 | start({ |
| 64 | platform: "ios", |
| 65 | deviceId: "sim", |
| 66 | access: { |
| 67 | httpBase: "https://environment.test/api/device-hub", |
| 68 | wsBase: "wss://environment.test/api/device-hub", |
| 69 | credentials: false, |
| 70 | query: {}, |
| 71 | }, |
| 72 | colors: { |
| 73 | background: "black", |
| 74 | foreground: "white", |
| 75 | muted: "gray", |
| 76 | buttonBackground: "black", |
| 77 | buttonForeground: "white", |
| 78 | buttonBorder: "gray", |
| 79 | }, |
| 80 | labels: { |
| 81 | screen: "iOS Simulator screen", |
| 82 | reconnectingControls: "Reconnecting device controls...", |
| 83 | }, |
| 84 | }); |
| 85 | return { configured, requested, videoBodies }; |
| 86 | } |
| 87 | |
| 88 | describe("native iOS simulator viewer", () => { |
| 89 | it.each([ |
| 90 | { supported: true, image: "" }, |
| 91 | { supported: false, image: "/api/device-hub/vendor/serve-sim/helper/sim/stream.mjpeg" }, |
| 92 | ])( |
| 93 | "streams H.264 when WebCodecs decodes it, else MJPEG (supported: $supported)", |
| 94 | async ({ supported, image }) => { |
| 95 | try { |
| 96 | const { configured, requested, videoBodies } = viewerFixture(supported); |
| 97 | await vi.advanceTimersByTimeAsync(0); |
| 98 | expect(requested).toContain("/api/device-hub/vendor/serve-sim/helper/sim/stream.avcc"); |
| 99 | videoBodies[0]!.enqueue(new Uint8Array(envelope(1, [1, 0x64, 0x00, 0x33]))); |
| 100 | await vi.advanceTimersByTimeAsync(0); |
| 101 | expect(configured).toEqual(supported ? ["avc1.640033"] : []); |
| 102 | const img = document.querySelector("img")!; |
| 103 | expect(img.src && new URL(img.src).pathname).toBe(image); |
| 104 | expect(img.style.display).toBe(supported ? "none" : "block"); |
| 105 | } finally { |
| 106 | stop(); |
| 107 | vi.useRealTimers(); |
| 108 | vi.unstubAllGlobals(); |
| 109 | } |
| 110 | }, |
| 111 | ); |
| 112 | }); |