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

serverBrowserStream.test.ts

258 lines8.4 KB

1import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
2​
3import { createPreviewStreamClient, type PreviewStreamControl } from "./serverBrowserStream.ts";
4​
5class FakeSocket extends EventTarget {
6 static readonly OPEN = 1;
7 static current: FakeSocket;
8 readyState = 1;
9 binaryType = "";
10 readonly sent: string[] = [];
11 readonly url: string;
12 constructor(url: string) {
13 super();
14 this.url = url;
15 FakeSocket.current = this;
16 }
17 send(value: string) {
18 this.sent.push(value);
19 }
20 close() {
21 this.readyState = 3;
22 }
23 message(data: unknown) {
24 this.dispatchEvent(new MessageEvent("message", { data }));
25 }
26 control(value: PreviewStreamControl) {
27 this.message(JSON.stringify({ type: "control", ...value }));
28 }
30​
31const target = {
32 access: {
33 httpBase: "http://preview.test/api/preview-stream",
34 wsBase: "ws://preview.test/api/preview-stream",
35 query: {},
36 credentials: true,
37 },
38 threadId: "thread",
39 tabId: "tab",
40 maxWidth: 800,
41 maxHeight: 600,
42};
43​
44describe("preview stream control", () => {
45 beforeEach(() => vi.stubGlobal("WebSocket", FakeSocket));
46 afterEach(() => vi.unstubAllGlobals());
47 const connect = () => {
48 const onFrame = vi.fn();
49 const onControl = vi.fn();
50 const client = createPreviewStreamClient(target, {
51 onFrame,
52 onControl,
53 onViewport: vi.fn(),
54 onConnectedChange: vi.fn(),
55 onUnauthorized: vi.fn(),
56 });
57 return { client, socket: FakeSocket.current, onFrame, onControl };
58 };
59 const agent: PreviewStreamControl = {
60 canOperate: true,
61 controller: "agent",
62 generation: 1,
63 dialog: null,
64 };
65​
66 it("does not grant control from malformed dialog metadata", () => {
67 const { client, socket, onControl } = connect();
68 socket.message(
69 JSON.stringify({
70 type: "control",
71 ...agent,
72 controller: "you",
73 dialog: { type: "prompt", message: 42, defaultValue: "" },
74 }),
75 );
76 expect(onControl).not.toHaveBeenCalled();
77 expect(client.send({ type: "reload" })).toBe(false);
78 client.stop();
79 });
80​
81 it("keeps read-only frames flowing while refusing takeover and every mutation", () => {
82 const { client, socket, onFrame } = connect();
83 socket.control({ ...agent, canOperate: false });
84 const frame = new ArrayBuffer(3);
85 socket.message(frame);
86 expect(onFrame).toHaveBeenCalledWith(frame);
87 expect(client.send({ type: "takeControl" })).toBe(false);
88 expect(client.send({ type: "resize", width: 390, height: 844 })).toBe(false);
89 expect(client.send({ type: "reload" })).toBe(false);
90 expect(socket.sent).toEqual(['{"type":"ack"}']);
91 client.stop();
92 });
93​
94 it("acks an empty message, which times the link, without painting it", () => {
95 const { client, socket, onFrame } = connect();
96 socket.message(new ArrayBuffer(0));
97 expect(onFrame).not.toHaveBeenCalled();
98 expect(socket.sent).toEqual(['{"type":"ack"}']);
99 client.stop();
100 });
101​
102 it("revokes input immediately on release and exposes pending dialog state", () => {
103 const { client, socket, onControl } = connect();
104 const owned = {
105 ...agent,
106 controller: "you" as const,
107 dialog: { type: "prompt", message: "Name?", defaultValue: "initial" },
108 };
109 socket.control(owned);
110 expect(onControl).toHaveBeenLastCalledWith(owned);
111 expect(client.send({ type: "dialog", accept: true, promptText: "answer" })).toBe(true);
112 expect(client.send({ type: "releaseControl" })).toBe(true);
113 socket.control({ ...agent, generation: 2 });
114 expect(client.send({ type: "text", text: "late input" })).toBe(false);
115 expect(client.send({ type: "dialog", accept: false })).toBe(false);
116 client.stop();
117 });
118​
119 it("withdraws an offered file picker when the stream drops or stops", () => {
120 const onFileChooser = vi.fn();
121 const events = {
122 onFrame: vi.fn(),
123 onFileChooser,
124 onViewport: vi.fn(),
125 onConnectedChange: vi.fn(),
126 onUnauthorized: vi.fn(),
127 };
128 const client = createPreviewStreamClient(target, events);
129 const socket = FakeSocket.current;
130 socket.dispatchEvent(new Event("open"));
131 socket.message(JSON.stringify({ type: "fileChooser", id: "c1", multiple: false, accept: "" }));
132 socket.dispatchEvent(Object.assign(new Event("close"), { code: 1012 }));
133 expect(onFileChooser).toHaveBeenLastCalledWith(null);
134 client.stop();
135​
136 const restarted = createPreviewStreamClient(target, events);
137 FakeSocket.current.message(
138 JSON.stringify({ type: "fileChooser", id: "c2", multiple: false, accept: "" }),
139 );
140 restarted.stop();
141 expect(onFileChooser).toHaveBeenLastCalledWith(null);
142 });
143​
144 it("withdraws an offered select menu when the page closes it or the stream drops", () => {
145 const onSelectMenu = vi.fn();
146 const client = createPreviewStreamClient(target, {
147 onFrame: vi.fn(),
148 onSelectMenu,
149 onViewport: vi.fn(),
150 onConnectedChange: vi.fn(),
151 onUnauthorized: vi.fn(),
152 });
153 const socket = FakeSocket.current;
154 socket.dispatchEvent(new Event("open"));
155 const menu = {
156 id: "m1",
157 options: [{ label: "One", disabled: false }],
158 selectedIndex: 0,
159 x: 1,
160 y: 2,
161 width: 3,
162 height: 4,
163 };
164 socket.message(JSON.stringify({ type: "selectMenu", ...menu }));
165 expect(onSelectMenu).toHaveBeenLastCalledWith(menu);
166 socket.message(JSON.stringify({ type: "selectMenuClosed", id: "older" }));
167 expect(onSelectMenu).toHaveBeenLastCalledWith(menu);
168 socket.message(JSON.stringify({ type: "selectMenuClosed", id: "m1" }));
169 expect(onSelectMenu).toHaveBeenLastCalledWith(null);
170 socket.message(JSON.stringify({ type: "selectMenu", ...menu, id: "m2" }));
171 socket.dispatchEvent(Object.assign(new Event("close"), { code: 1012 }));
172 expect(onSelectMenu).toHaveBeenLastCalledWith(null);
173 client.stop();
174 });
175});
176​
177describe("preview stream downloads", () => {
178 beforeEach(() => vi.stubGlobal("WebSocket", FakeSocket));
179 afterEach(() => vi.unstubAllGlobals());
180​
181 it("offers a download at a URL carrying the stream's ticket", () => {
182 const onDownload = vi.fn();
183 createPreviewStreamClient(
184 {
185 ...target,
186 access: { ...target.access, query: { wsTicket: "ticket" }, credentials: false },
187 },
188 {
189 onFrame: vi.fn(),
190 onDownload,
191 onViewport: vi.fn(),
192 onConnectedChange: vi.fn(),
193 onUnauthorized: vi.fn(),
194 },
195 );
196 FakeSocket.current.message(
197 JSON.stringify({ type: "download", id: "d1", fileName: "a b.csv", sizeBytes: 3 }),
198 );
199 expect(onDownload).toHaveBeenCalledExactlyOnceWith({
200 fileName: "a b.csv",
201 sizeBytes: 3,
202 url: "http://preview.test/api/preview-stream/download?threadId=thread&tabId=tab&id=d1&wsTicket=ticket",
203 });
204 });
205});
206​
207describe("preview stream agent pointer", () => {
208 beforeEach(() => vi.stubGlobal("WebSocket", FakeSocket));
209 afterEach(() => vi.unstubAllGlobals());
210​
211 it("reports where the agent moves and clicks, ignoring malformed points", () => {
212 const onPointer = vi.fn();
213 createPreviewStreamClient(target, {
214 onFrame: vi.fn(),
215 onPointer,
216 onViewport: vi.fn(),
217 onConnectedChange: vi.fn(),
218 onUnauthorized: vi.fn(),
219 });
220 FakeSocket.current.message(
221 JSON.stringify({ type: "pointer", phase: "drag", x: 1, y: 2, sequence: 1 }),
222 );
223 FakeSocket.current.message(
224 JSON.stringify({ type: "pointer", phase: "click", x: 140, y: 50, sequence: 2 }),
225 );
226 expect(onPointer).toHaveBeenCalledExactlyOnceWith({
227 phase: "click",
228 x: 140,
229 y: 50,
230 sequence: 2,
231 });
232 });
233});
234​
235describe("preview stream host setup", () => {
236 beforeEach(() => vi.stubGlobal("WebSocket", FakeSocket));
237 afterEach(() => vi.unstubAllGlobals());
238​
239 it("shows the full setup command sent ahead of the bounded close reason", () => {
240 const onHostSetup = vi.fn();
241 createPreviewStreamClient(target, {
242 onFrame: vi.fn(),
243 onHostSetup,
244 onViewport: vi.fn(),
245 onConnectedChange: vi.fn(),
246 onUnauthorized: vi.fn(),
247 });
248 const command = "sudo /opt/t3code/bin/t3 browser setup --base-dir /srv/a-long-server-home/.t3";
249 FakeSocket.current.message(JSON.stringify({ type: "hostSetup", need: "libraries", command }));
250 FakeSocket.current.dispatchEvent(
251 Object.assign(new Event("close"), {
252 code: 4503,
253 reason: JSON.stringify({ need: "libraries", command: "sudo npx t3 browser setup" }),
254 }),
255 );
256 expect(onHostSetup).toHaveBeenCalledExactlyOnceWith({ need: "libraries", command });
257 });
258});