Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

device-stream.browser.test.ts

117 lines3.5 KB

1import { afterEach, expect, it, vi } from "vite-plus/test";
2import { start, stop } from "./device-stream.browser";
3​
4class Element extends EventTarget {
5 readonly style = {};
6 naturalWidth = 0;
7 naturalHeight = 0;
8 src = "";
9 readonly tag: string;
10 constructor(tag: string) {
11 super();
12 this.tag = tag;
13 }
14 setAttribute() {}
15 removeAttribute(name: string) {
16 if (name === "src") this.src = "";
17 }
18 append() {}
20​
21async function setup() {
22 vi.useFakeTimers();
23 const elements: Element[] = [];
24 vi.stubGlobal("document", {
25 documentElement: { style: {} },
26 body: { style: {}, replaceChildren() {} },
27 createElement: (tag: string) => {
28 const element = new Element(tag);
29 elements.push(element);
30 return element;
31 },
32 });
33 const postMessage = vi.fn();
34 vi.stubGlobal("window", { ReactNativeWebView: { postMessage }, addEventListener() {} });
35 vi.stubGlobal("fetch", () => Promise.resolve(new Response("prime")));
36 const sockets: Socket[] = [];
37 class Socket {
38 static OPEN = 1;
39 readyState = 1;
40 onopen: (() => void) | null = null;
41 close = vi.fn();
42 send = vi.fn();
43 constructor() {
44 sockets.push(this);
45 }
46 }
47 vi.stubGlobal("WebSocket", Socket);
48 const configuration = {
49 platform: "ios" as const,
50 deviceId: "fixture-device",
51 access: {
52 httpBase: "https://device.test",
53 wsBase: "wss://device.test",
54 credentials: false,
55 query: {},
56 },
57 colors: {
58 background: "white",
59 foreground: "black",
60 muted: "gray",
61 buttonBackground: "gray",
62 buttonForeground: "black",
63 buttonBorder: "gray",
64 },
65 };
66 start(configuration);
67 await vi.advanceTimersByTimeAsync(0);
68 return {
69 configuration,
70 elements,
71 sockets,
72 messages: () =>
73 postMessage.mock.calls.map(
74 ([message]) => JSON.parse(message as string) as { type: string; status?: string },
75 ),
76 };
78​
79afterEach(() => {
80 stop();
81 vi.useRealTimers();
82 vi.unstubAllGlobals();
83});
84​
85it("bridges shared first-frame readiness, image failure, and a successful fresh attempt to native", async () => {
86 const { elements, messages, sockets, configuration } = await setup();
87 sockets[0]!.onopen?.();
88 expect(messages()).not.toContainEqual({ type: "status", status: "streaming" });
89 expect(messages()).toContainEqual({ type: "input", connected: true });
90 const image = elements.find((element) => element.tag === "img")!;
91 image.naturalWidth = 400;
92 image.naturalHeight = 800;
93 await vi.advanceTimersByTimeAsync(250);
94 expect(messages()).toContainEqual({ type: "status", status: "streaming" });
95 image.dispatchEvent(new Event("error"));
96 expect(messages()).toContainEqual({
97 type: "status",
98 status: "error",
99 detail: "Could not receive the device stream. Reconnect to try again.",
100 });
101 expect(messages()).toContainEqual({ type: "input", connected: false });
102 expect(messages()).not.toContainEqual({ type: "unauthorized" });
103 expect(image.src).toBe("");
104 expect(sockets[0]!.close).toHaveBeenCalledOnce();
105 start(configuration);
106 await vi.advanceTimersByTimeAsync(0);
107 const replacement = elements.findLast((element) => element.tag === "img")!;
108 replacement.naturalWidth = 400;
109 replacement.naturalHeight = 800;
110 replacement.dispatchEvent(new Event("load"));
111 expect(messages().at(-1)).toEqual({ type: "status", status: "streaming" });
112 image.dispatchEvent(new Event("error"));
113 expect(sockets[1]!.close).not.toHaveBeenCalled();
114 stop();
115 expect(replacement.src).toBe("");
116 expect(vi.getTimerCount()).toBe(0);
117});