Open History to see the last commit
1import * as NodeVM from "node:vm";
2import { describe, expect, it, vi } from "vite-plus/test";
3
4import {
5 deviceStreamBaseUrl,
6 deviceStreamDocument,
7 deviceStreamMessage,
8} from "./device-stream-document";
9
10describe("native device stream document", () => {
11 it.each([
12 ["android", "ios", "http://192.168.1.10:3773/api/device-hub", "http://192.168.1.10:3773/"],
13 ["android", "ios", "https://env.example/t3/api/device-hub", "https://env.example/"],
14 ["android", "android", "http://192.168.1.10:3773/api/device-hub", "https://localhost/"],
15 ["ios", "ios", "http://192.168.1.10:3773/api/device-hub", "file:///"],
16 ] as const)(
17 "uses a %s viewer base URL for %s targets on %s",
18 (viewer, target, httpBase, expected) => {
19 expect(deviceStreamBaseUrl(viewer, target, httpBase)).toBe(expected);
20 },
21 );
22
23 it("keeps ticket and device values from terminating the embedded script", () => {
24 const deviceId = '</script><script>alert("device")</script>';
25 const configuration = JSON.stringify({ deviceId, ticket: "<ticket>" });
26 const html = deviceStreamDocument(
27 configuration,
28 "var T3DeviceStream={start(input){return input}};",
29 );
30 const script = html.match(/<script>([\s\S]*)<\/script>/)?.[1];
31 expect(html.match(/<script>/g)).toHaveLength(1);
32 const input: unknown = NodeVM.runInNewContext(script!, { window: { addEventListener() {} } });
33 expect(input).toEqual({ deviceId, ticket: "<ticket>" });
34 });
35
36 it("preserves script strings containing an HTML closing delimiter", () => {
37 const html = deviceStreamDocument("{}", 'var text="</script>";');
38 expect(html.match(/<\/script>/g)).toHaveLength(1);
39 expect(html).toContain('var text="<\\/script>";');
40 });
41
42 it("reports a bootstrap exception to the native recovery UI", () => {
43 const postMessage = vi.fn();
44 const html = deviceStreamDocument(
45 "{}",
46 'var T3DeviceStream={start(){throw new Error("startup")}};',
47 );
48 NodeVM.runInNewContext(html.match(/<script>([\s\S]*)<\/script>/)![1]!, {
49 window: { addEventListener() {}, ReactNativeWebView: { postMessage } },
50 });
51 expect(JSON.parse(postMessage.mock.calls[0]![0] as string)).toEqual({
52 type: "status",
53 status: "error",
54 detail: "Device viewer stopped unexpectedly.",
55 });
56 });
57
58 it.each(["error", "unhandledrejection"])("reports later %s failures to native", (event) => {
59 const postMessage = vi.fn();
60 const listeners = new Map<string, () => void>();
61 const html = deviceStreamDocument("{}", "var T3DeviceStream={start(){}};");
62 NodeVM.runInNewContext(html.match(/<script>([\s\S]*)<\/script>/)![1]!, {
63 window: {
64 addEventListener: (name: string, callback: () => void) => listeners.set(name, callback),
65 ReactNativeWebView: { postMessage },
66 },
67 });
68 listeners.get(event)!();
69 expect(JSON.parse(postMessage.mock.calls[0]![0] as string)).toMatchObject({
70 type: "status",
71 status: "error",
72 });
73 });
74});
75
76describe("native device stream messages", () => {
77 it("accepts authentication renewal and input connection changes", () => {
78 expect(deviceStreamMessage('{"type":"unauthorized"}')).toEqual({ type: "unauthorized" });
79 expect(deviceStreamMessage('{"type":"input","connected":true}')).toEqual({
80 type: "input",
81 connected: true,
82 });
83 expect(deviceStreamMessage('{"type":"input","connected":false}')).toEqual({
84 type: "input",
85 connected: false,
86 });
87 expect(deviceStreamMessage('{"type":"retry"}')).toEqual({ type: "retry" });
88 });
89
90 it.each(["connecting", "streaming", "error"])("accepts stream %s feedback", (status) => {
91 expect(
92 deviceStreamMessage(JSON.stringify({ type: "status", status, detail: "Stream feedback" })),
93 ).toEqual({ type: "status", status, detail: "Stream feedback" });
94 });
95
96 it.each([
97 "invalid JSON",
98 "null",
99 '"input"',
100 "{}",
101 '{"type":"input","connected":"yes"}',
102 '{"type":"unknown"}',
103 '{"type":"status","status":"unknown"}',
104 '{"type":"status","status":"error","detail":42}',
105 '{"type":"status"}',
106 ])("ignores invalid bridge messages: %s", (data) => expect(deviceStreamMessage(data)).toBeNull());
107});