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

preview.test.ts

344 lines10.3 KB

1import { Schema } from "effect";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 DiscoveredLocalServer,
6 PreviewEvent,
7 PreviewNavStatus,
8 PreviewSessionSnapshot,
9 PreviewViewportSetting,
10} from "./preview.ts";
11import {
12 PreviewAutomationHost,
13 PreviewAutomationError,
14 PreviewAutomationOpenInput,
15 PreviewAutomationResizeInput,
16 PreviewAutomationResizeResult,
17 PreviewAutomationStatus,
18} from "./previewAutomation.ts";
19​
20const decodePreviewEvent = Schema.decodeUnknownSync(PreviewEvent);
21const decodeSnapshot = Schema.decodeUnknownSync(PreviewSessionSnapshot);
22const decodeNavStatus = Schema.decodeUnknownSync(PreviewNavStatus);
23const decodeServer = Schema.decodeUnknownSync(DiscoveredLocalServer);
24const decodeViewport = Schema.decodeUnknownSync(PreviewViewportSetting);
25const decodeResizeInput = Schema.decodeUnknownSync(PreviewAutomationResizeInput);
26const decodeOpenInput = Schema.decodeUnknownSync(PreviewAutomationOpenInput);
27const decodeResizeResult = Schema.decodeUnknownSync(PreviewAutomationResizeResult);
28const decodeAutomationHost = Schema.decodeUnknownSync(PreviewAutomationHost);
29const decodeAutomationError = Schema.decodeUnknownSync(PreviewAutomationError);
30const decodeAutomationStatus = Schema.decodeUnknownSync(PreviewAutomationStatus);
31​
32describe("PreviewAutomationOpenInput", () => {
33 it("accepts the inline preview visibility flag", () => {
34 expect(decodeOpenInput({ open: false })).toEqual({ open: false });
35 });
36​
37 it("retains the legacy show visibility alias", () => {
38 expect(decodeOpenInput({ show: false })).toEqual({ show: false });
39 });
40});
41​
42describe("PreviewNavStatus", () => {
43 it("decodes Idle", () => {
44 expect(decodeNavStatus({ _tag: "Idle" })).toEqual({ _tag: "Idle" });
45 });
46​
47 it("decodes Loading with title", () => {
48 expect(decodeNavStatus({ _tag: "Loading", url: "http://localhost:5173/", title: "" })).toEqual({
49 _tag: "Loading",
50 url: "http://localhost:5173/",
51 title: "",
52 });
53 });
54​
55 it("decodes LoadFailed with code/description", () => {
56 expect(
57 decodeNavStatus({
58 _tag: "LoadFailed",
59 url: "https://example.com/",
60 title: "Example",
61 code: -105,
62 description: "ERR_NAME_NOT_RESOLVED",
63 }),
64 ).toEqual({
65 _tag: "LoadFailed",
66 url: "https://example.com/",
67 title: "Example",
68 code: -105,
69 description: "ERR_NAME_NOT_RESOLVED",
70 });
71 });
72​
73 it("rejects empty url", () => {
74 expect(() => decodeNavStatus({ _tag: "Loading", url: "", title: "" })).toThrow();
75 });
76});
77​
78describe("PreviewSessionSnapshot", () => {
79 it("round-trips a Success snapshot", () => {
80 const snapshot = decodeSnapshot({
81 threadId: "thread-1",
82 tabId: "preview-thread-1",
83 navStatus: {
84 _tag: "Success",
85 url: "http://localhost:5173/",
86 title: "Vite App",
87 },
88 canGoBack: false,
89 canGoForward: false,
90 updatedAt: "2026-01-01T00:00:00.000Z",
91 });
92 expect(snapshot.tabId).toBe("preview-thread-1");
93 expect(snapshot.navStatus._tag).toBe("Success");
94 });
95});
96​
97describe("PreviewViewportSetting", () => {
98 it("decodes fill, freeform, and preset modes", () => {
99 expect(decodeViewport({ _tag: "fill" })).toEqual({ _tag: "fill" });
100 expect(decodeViewport({ _tag: "freeform", width: 1024, height: 768 })).toEqual({
101 _tag: "freeform",
102 width: 1024,
103 height: 768,
104 });
105 expect(
106 decodeViewport({
107 _tag: "preset",
108 presetId: "iphone-15-pro",
109 width: 393,
110 height: 852,
111 }),
112 ).toMatchObject({ _tag: "preset", presetId: "iphone-15-pro" });
113 });
114​
115 it("rejects unsafe dimensions and oversized render areas", () => {
116 expect(() => decodeViewport({ _tag: "freeform", width: 100, height: 800 })).toThrow();
117 expect(() => decodeViewport({ _tag: "freeform", width: 3840, height: 3840 })).toThrow();
118 });
119});
120​
121describe("PreviewAutomationResizeInput", () => {
122 it("requires fields that match the selected mode", () => {
123 expect(decodeResizeInput({ mode: "fill" })).toEqual({ mode: "fill" });
124 expect(
125 decodeResizeInput({ mode: "preset", preset: "pixel-7", orientation: "landscape" }),
126 ).toMatchObject({ mode: "preset", preset: "pixel-7" });
127 expect(() => decodeResizeInput({ mode: "preset", preset: "pixel-8" })).toThrow();
128 expect(() => decodeResizeInput({ mode: "freeform", width: 1024 })).toThrow();
129 expect(() => decodeResizeInput({ mode: "fill", width: 1024, height: 768 })).toThrow();
130 });
131​
132 it("allows fill-mode measurements below the minimum selectable fixed size", () => {
133 expect(
134 decodeResizeResult({
135 tabId: "preview-t",
136 setting: { _tag: "fill" },
137 viewport: { width: 180, height: 120 },
138 }).viewport,
139 ).toEqual({ width: 180, height: 120 });
140 });
141});
142​
143describe("preview automation tab targeting", () => {
144 it("accepts an explicit tab and rejects contradictory open behavior", () => {
145 expect(decodeResizeInput({ tabId: "tab-app", mode: "fill" })).toMatchObject({
146 tabId: "tab-app",
147 mode: "fill",
148 });
149 expect(decodeOpenInput({ tabId: "tab-app", reuseExistingTab: true })).toMatchObject({
150 tabId: "tab-app",
151 reuseExistingTab: true,
152 });
153 expect(() => decodeOpenInput({ tabId: "tab-app", reuseExistingTab: false })).toThrow();
154 });
155});
156​
157describe("PreviewAutomationHost", () => {
158 it("accepts legacy hosts and current operation advertisements", () => {
159 expect(decodeAutomationHost({ clientId: "legacy", environmentId: "environment-1" })).toEqual({
160 clientId: "legacy",
161 environmentId: "environment-1",
162 });
163 expect(
164 decodeAutomationHost({
165 clientId: "current",
166 environmentId: "environment-1",
167 supportedOperations: ["status", "resize"],
168 }).supportedOperations,
169 ).toEqual(["status", "resize"]);
170 });
171});
172​
173describe("PreviewAutomationError", () => {
174 it("preserves a typed non-editable target failure", () => {
175 const error = decodeAutomationError({
176 _tag: "PreviewAutomationTargetNotEditableError",
177 operation: "type",
178 environmentId: "environment-1",
179 threadId: "thread-1",
180 providerSessionId: "provider-session-1",
181 providerInstanceId: "codex",
182 clientId: "client-1",
183 connectionId: "connection-1",
184 requestId: "request-1",
185 tabId: "tab-1",
186 timeoutMs: 1_000,
187 remoteTag: "PreviewAutomationTargetNotEditableError",
188 remoteMessageLength: 12,
189 cause: {},
190 selectorKind: "focused-element",
191 });
192​
193 expect(error._tag).toBe("PreviewAutomationTargetNotEditableError");
194 if (error._tag === "PreviewAutomationTargetNotEditableError") {
195 expect(error.selectorKind).toBe("focused-element");
196 expect(error.message).toBe("Preview automation type requires an editable focused element.");
197 }
198 });
199});
200​
201describe("PreviewAutomationStatus", () => {
202 it("accepts old hosts without viewport data and exposes it from current hosts", () => {
203 const base = {
204 available: true,
205 visible: false,
206 tabId: "preview-t",
207 url: "https://example.com",
208 title: "Example",
209 loading: false,
210 };
211 expect(decodeAutomationStatus(base)).toEqual(base);
212 expect(
213 decodeAutomationStatus({
214 ...base,
215 viewportSetting: { _tag: "preset", presetId: "pixel-8", width: 412, height: 915 },
216 viewport: { width: 412, height: 915 },
217 }).viewport,
218 ).toEqual({ width: 412, height: 915 });
219 });
220});
221​
222describe("PreviewEvent", () => {
223 it("decodes opened", () => {
224 const event = decodePreviewEvent({
225 type: "opened",
226 threadId: "t",
227 tabId: "preview-t",
228 createdAt: "2026-01-01T00:00:00.000Z",
229 serverEpoch: "server-a",
230 revision: 1,
231 snapshot: {
232 threadId: "t",
233 tabId: "preview-t",
234 navStatus: { _tag: "Idle" },
235 canGoBack: false,
236 canGoForward: false,
237 updatedAt: "2026-01-01T00:00:00.000Z",
238 },
239 });
240 expect(event.type).toBe("opened");
241 });
242​
243 it("decodes failed with code/description", () => {
244 const event = decodePreviewEvent({
245 type: "failed",
246 threadId: "t",
247 tabId: "preview-t",
248 createdAt: "2026-01-01T00:00:00.000Z",
249 serverEpoch: "server-a",
250 revision: 1,
251 url: "https://example.com/",
252 title: "",
253 code: -105,
254 description: "ERR_NAME_NOT_RESOLVED",
255 });
256 expect(event.type).toBe("failed");
257 if (event.type === "failed") {
258 expect(event.code).toBe(-105);
259 }
260 });
261​
262 it("decodes resized with tab viewport state", () => {
263 const event = decodePreviewEvent({
264 type: "resized",
265 threadId: "t",
266 tabId: "preview-t",
267 createdAt: "2026-01-01T00:00:00.000Z",
268 serverEpoch: "server-a",
269 revision: 1,
270 snapshot: {
271 threadId: "t",
272 tabId: "preview-t",
273 navStatus: { _tag: "Idle" },
274 canGoBack: false,
275 canGoForward: false,
276 viewport: { _tag: "freeform", width: 1024, height: 768 },
277 updatedAt: "2026-01-01T00:00:00.000Z",
278 },
279 });
280 expect(event.type).toBe("resized");
281 });
282​
283 it("decodes closed without snapshot", () => {
284 const event = decodePreviewEvent({
285 type: "closed",
286 threadId: "t",
287 tabId: "preview-t",
288 createdAt: "2026-01-01T00:00:00.000Z",
289 serverEpoch: "server-a",
290 revision: 1,
291 });
292 expect(event.type).toBe("closed");
293 });
294});
295​
296describe("DiscoveredLocalServer", () => {
297 it("decodes a server with process metadata", () => {
298 const server = decodeServer({
299 host: "localhost",
300 port: 5173,
301 url: "http://localhost:5173",
302 processName: "node",
303 pid: 12345,
304 terminal: null,
305 });
306 expect(server.port).toBe(5173);
307 expect(server.processName).toBe("node");
308 });
309​
310 it("decodes a server without process metadata", () => {
311 const server = decodeServer({
312 host: "localhost",
313 port: 3000,
314 url: "http://localhost:3000",
315 processName: null,
316 pid: null,
317 terminal: null,
318 });
319 expect(server.processName).toBeNull();
320 });
321​
322 it("rejects invalid ports", () => {
323 expect(() =>
324 decodeServer({
325 host: "localhost",
326 port: 0,
327 url: "http://localhost:0",
328 processName: null,
329 pid: null,
330 terminal: null,
331 }),
332 ).toThrow();
333 expect(() =>
334 decodeServer({
335 host: "localhost",
336 port: 70000,
337 url: "http://localhost:70000",
338 processName: null,
339 pid: null,
340 terminal: null,
341 }),
342 ).toThrow();
343 });
344});