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