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

preview.test.ts

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