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

preview.test.ts

166 lines4.2 KB

1import { Schema } from "effect";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 DiscoveredLocalServer,
6 PreviewEvent,
7 PreviewNavStatus,
8 PreviewSessionSnapshot,
9} from "./preview.ts";
10​
11const decodePreviewEvent = Schema.decodeUnknownSync(PreviewEvent);
12const decodeSnapshot = Schema.decodeUnknownSync(PreviewSessionSnapshot);
13const decodeNavStatus = Schema.decodeUnknownSync(PreviewNavStatus);
14const decodeServer = Schema.decodeUnknownSync(DiscoveredLocalServer);
15​
16describe("PreviewNavStatus", () => {
17 it("decodes Idle", () => {
18 expect(decodeNavStatus({ _tag: "Idle" })).toEqual({ _tag: "Idle" });
19 });
20​
21 it("decodes Loading with title", () => {
22 expect(decodeNavStatus({ _tag: "Loading", url: "http://localhost:5173/", title: "" })).toEqual({
23 _tag: "Loading",
24 url: "http://localhost:5173/",
25 title: "",
26 });
27 });
28​
29 it("decodes LoadFailed with code/description", () => {
30 expect(
31 decodeNavStatus({
32 _tag: "LoadFailed",
33 url: "https://example.com/",
34 title: "Example",
35 code: -105,
36 description: "ERR_NAME_NOT_RESOLVED",
37 }),
38 ).toEqual({
39 _tag: "LoadFailed",
40 url: "https://example.com/",
41 title: "Example",
42 code: -105,
43 description: "ERR_NAME_NOT_RESOLVED",
44 });
45 });
46​
47 it("rejects empty url", () => {
48 expect(() => decodeNavStatus({ _tag: "Loading", url: "", title: "" })).toThrow();
49 });
50});
51​
52describe("PreviewSessionSnapshot", () => {
53 it("round-trips a Success snapshot", () => {
54 const snapshot = decodeSnapshot({
55 threadId: "thread-1",
56 tabId: "preview-thread-1",
57 navStatus: {
58 _tag: "Success",
59 url: "http://localhost:5173/",
60 title: "Vite App",
61 },
62 canGoBack: false,
63 canGoForward: false,
64 updatedAt: "2026-01-01T00:00:00.000Z",
65 });
66 expect(snapshot.tabId).toBe("preview-thread-1");
67 expect(snapshot.navStatus._tag).toBe("Success");
68 });
69});
70​
71describe("PreviewEvent", () => {
72 it("decodes opened", () => {
73 const event = decodePreviewEvent({
74 type: "opened",
75 threadId: "t",
76 tabId: "preview-t",
77 createdAt: "2026-01-01T00:00:00.000Z",
78 snapshot: {
79 threadId: "t",
80 tabId: "preview-t",
81 navStatus: { _tag: "Idle" },
82 canGoBack: false,
83 canGoForward: false,
84 updatedAt: "2026-01-01T00:00:00.000Z",
85 },
86 });
87 expect(event.type).toBe("opened");
88 });
89​
90 it("decodes failed with code/description", () => {
91 const event = decodePreviewEvent({
92 type: "failed",
93 threadId: "t",
94 tabId: "preview-t",
95 createdAt: "2026-01-01T00:00:00.000Z",
96 url: "https://example.com/",
97 title: "",
98 code: -105,
99 description: "ERR_NAME_NOT_RESOLVED",
100 });
101 expect(event.type).toBe("failed");
102 if (event.type === "failed") {
103 expect(event.code).toBe(-105);
104 }
105 });
106​
107 it("decodes closed without snapshot", () => {
108 const event = decodePreviewEvent({
109 type: "closed",
110 threadId: "t",
111 tabId: "preview-t",
112 createdAt: "2026-01-01T00:00:00.000Z",
113 });
114 expect(event.type).toBe("closed");
115 });
116});
117​
118describe("DiscoveredLocalServer", () => {
119 it("decodes a server with process metadata", () => {
120 const server = decodeServer({
121 host: "localhost",
122 port: 5173,
123 url: "http://localhost:5173",
124 processName: "node",
125 pid: 12345,
126 terminal: null,
127 });
128 expect(server.port).toBe(5173);
129 expect(server.processName).toBe("node");
130 });
131​
132 it("decodes a server without process metadata", () => {
133 const server = decodeServer({
134 host: "localhost",
135 port: 3000,
136 url: "http://localhost:3000",
137 processName: null,
138 pid: null,
139 terminal: null,
140 });
141 expect(server.processName).toBeNull();
142 });
143​
144 it("rejects invalid ports", () => {
145 expect(() =>
146 decodeServer({
147 host: "localhost",
148 port: 0,
149 url: "http://localhost:0",
150 processName: null,
151 pid: null,
152 terminal: null,
153 }),
154 ).toThrow();
155 expect(() =>
156 decodeServer({
157 host: "localhost",
158 port: 70000,
159 url: "http://localhost:70000",
160 processName: null,
161 pid: null,
162 terminal: null,
163 }),
164 ).toThrow();
165 });
166});