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

Manager.test.ts

286 lines10.7 KB

1import { it } from "@effect/vitest";
2import { type PreviewEvent, ThreadId } from "@t3tools/contracts";
3import { PreviewUrlNormalizationError } from "@t3tools/shared/preview";
4import { Effect, PubSub } from "effect";
5import { expect } from "vite-plus/test";
6​
7import * as PreviewManager from "./Manager.ts";
8​
9const DRAIN_LIMIT = 100;
10​
11interface EventCollector {
12 /** Drain everything published since the last call (or since subscribe). */
13 readonly drain: Effect.Effect<ReadonlyArray<PreviewEvent>>;
15​
16/**
17 * Each `it.effect` shares the live PreviewManager layer across the whole
18 * `it.layer` block, so tests that assert per-thread counts must use a unique
19 * thread id to avoid bleeding state from earlier tests.
20 */
21let nextThreadId = 0;
22const freshThreadId = () => ThreadId.make(`thread-${++nextThreadId}`);
23​
24/**
25 * Subscribe to the manager's event stream BEFORE the test publishes. We
26 * use `subscribeEvents` (synchronous PubSub.subscribe under the hood) so
27 * no event can land between subscribe and the consumer drain.
28 */
29const collectEvents = Effect.gen(function* () {
30 const manager = yield* PreviewManager.PreviewManager;
31 const subscription = yield* manager.subscribeEvents;
32 const collector: EventCollector = {
33 drain: PubSub.takeUpTo(subscription, DRAIN_LIMIT),
34 };
35 return collector;
36}).pipe(Effect.withSpan("preview.test.collectEvents"));
37​
38it.layer(PreviewManager.layer)("PreviewManager", (it) => {
39 it.effect("opens a session and emits opened with normalized URL", () =>
40 Effect.gen(function* () {
41 const threadId = freshThreadId();
42 const manager = yield* PreviewManager.PreviewManager;
43 const collector = yield* collectEvents;
44​
45 const snapshot = yield* manager.open({ threadId, url: "localhost:5173" });
46 expect(snapshot.tabId.startsWith("tab_")).toBe(true);
47 expect(snapshot.navStatus._tag).toBe("Loading");
48 if (snapshot.navStatus._tag === "Loading") {
49 expect(snapshot.navStatus.url).toBe("http://localhost:5173/");
50 }
51​
52 const events = yield* collector.drain;
53 expect(events).toHaveLength(1);
54 expect(events[0]?.type).toBe("opened");
55 if (events[0]?.type === "opened") {
56 expect(events[0].tabId).toBe(snapshot.tabId);
57 }
58 }),
59 );
60​
61 it.effect("opens an Idle tab when no URL is supplied", () =>
62 Effect.gen(function* () {
63 const threadId = freshThreadId();
64 const manager = yield* PreviewManager.PreviewManager;
65 const snapshot = yield* manager.open({ threadId });
66 expect(snapshot.navStatus._tag).toBe("Idle");
67 }),
68 );
69​
70 it.effect("treats bare hosts as https", () =>
71 Effect.gen(function* () {
72 const threadId = freshThreadId();
73 const manager = yield* PreviewManager.PreviewManager;
74 const snapshot = yield* manager.open({ threadId, url: "example.com" });
75 if (snapshot.navStatus._tag === "Loading") {
76 expect(snapshot.navStatus.url).toBe("https://example.com/");
77 }
78 }),
79 );
80​
81 it.effect("rejects empty URL with PreviewInvalidUrlError", () =>
82 Effect.gen(function* () {
83 const threadId = freshThreadId();
84 const manager = yield* PreviewManager.PreviewManager;
85 const error = yield* Effect.flip(manager.open({ threadId, url: " " }));
86 expect(error._tag).toBe("PreviewInvalidUrlError");
87 expect(error).toMatchObject({ inputLength: 3, reason: "empty" });
88 expect(error).not.toHaveProperty("rawUrl");
89 expect(error.cause).toBeInstanceOf(PreviewUrlNormalizationError);
90 expect((error.cause as PreviewUrlNormalizationError).reason).toBe("empty");
91 }),
92 );
93​
94 it.effect("preserves URL parser failures as the invalid URL cause chain", () =>
95 Effect.gen(function* () {
96 const threadId = freshThreadId();
97 const manager = yield* PreviewManager.PreviewManager;
98 const rawUrl = "https://user:password@example.com:bad/path?access_token=secret#fragment";
99 const error = yield* Effect.flip(manager.open({ threadId, url: rawUrl }));
100​
101 expect(error).toMatchObject({
102 inputLength: rawUrl.length,
103 reason: "parse",
104 protocol: "https:",
105 });
106 expect(error).not.toHaveProperty("rawUrl");
107 expect(error.cause).toBeInstanceOf(PreviewUrlNormalizationError);
108 const normalizationError = error.cause as PreviewUrlNormalizationError;
109 expect(normalizationError.cause).toBeInstanceOf(Error);
110 expect(error.message).not.toContain((normalizationError.cause as Error).message);
111 expect(error.message).not.toMatch(/user|password|access_token|secret|fragment/);
112 }),
113 );
114​
115 it.effect("navigate updates snapshot and emits navigated", () =>
116 Effect.gen(function* () {
117 const threadId = freshThreadId();
118 const manager = yield* PreviewManager.PreviewManager;
119 const collector = yield* collectEvents;
120​
121 const opened = yield* manager.open({ threadId, url: "http://localhost:5173" });
122 const snapshot = yield* manager.navigate({
123 threadId,
124 tabId: opened.tabId,
125 url: "http://localhost:5173/about",
126 resolvedTitle: "About",
127 });
128​
129 expect(snapshot.navStatus._tag).toBe("Success");
130 if (snapshot.navStatus._tag === "Success") {
131 expect(snapshot.navStatus.url).toBe("http://localhost:5173/about");
132 expect(snapshot.navStatus.title).toBe("About");
133 }
134 const events = yield* collector.drain;
135 expect(events.map((e) => e.type)).toEqual(["opened", "navigated"]);
136 }),
137 );
138​
139 it.effect("navigate fails for unknown tab", () =>
140 Effect.gen(function* () {
141 const threadId = freshThreadId();
142 const manager = yield* PreviewManager.PreviewManager;
143 const error = yield* Effect.flip(
144 manager.navigate({
145 threadId,
146 tabId: "tab_missing",
147 url: "http://localhost:5173",
148 }),
149 );
150 expect(error._tag).toBe("PreviewSessionLookupError");
151 }),
152 );
153​
154 it.effect("reportStatus emits failed for LoadFailed nav", () =>
155 Effect.gen(function* () {
156 const threadId = freshThreadId();
157 const manager = yield* PreviewManager.PreviewManager;
158 const collector = yield* collectEvents;
159​
160 const opened = yield* manager.open({ threadId, url: "http://localhost:5173" });
161 yield* manager.reportStatus({
162 threadId,
163 tabId: opened.tabId,
164 navStatus: {
165 _tag: "LoadFailed",
166 url: "http://localhost:5173",
167 title: "",
168 code: -105,
169 description: "ERR_NAME_NOT_RESOLVED",
170 },
171 canGoBack: false,
172 canGoForward: false,
173 });
174​
175 const events = yield* collector.drain;
176 const failed = events.find((e) => e.type === "failed");
177 expect(failed?.type).toBe("failed");
178 if (failed?.type === "failed") {
179 expect(failed.code).toBe(-105);
180 expect(failed.description).toBe("ERR_NAME_NOT_RESOLVED");
181 }
182 }),
183 );
184​
185 it.effect("close removes the session and emits closed", () =>
186 Effect.gen(function* () {
187 const threadId = freshThreadId();
188 const manager = yield* PreviewManager.PreviewManager;
189 const collector = yield* collectEvents;
190​
191 yield* manager.open({ threadId, url: "http://localhost:5173" });
192 yield* manager.close({ threadId });
193​
194 const result = yield* manager.list({ threadId });
195 expect(result.sessions).toHaveLength(0);
196 const events = yield* collector.drain;
197 const closed = events.find((e) => e.type === "closed");
198 expect(closed?.type).toBe("closed");
199 }),
200 );
201​
202 it.effect("close is idempotent for unknown threads", () =>
203 Effect.gen(function* () {
204 const threadId = freshThreadId();
205 const manager = yield* PreviewManager.PreviewManager;
206 yield* manager.close({ threadId });
207 const result = yield* manager.list({ threadId });
208 expect(result.sessions).toHaveLength(0);
209 }),
210 );
211​
212 it.effect("list returns every snapshot for the thread sorted by updatedAt", () =>
213 Effect.gen(function* () {
214 const threadId = freshThreadId();
215 const manager = yield* PreviewManager.PreviewManager;
216 const first = yield* manager.open({ threadId, url: "http://localhost:5173" });
217 const second = yield* manager.open({ threadId, url: "http://localhost:3000" });
218 const result = yield* manager.list({ threadId });
219 expect(result.sessions).toHaveLength(2);
220 const ids = result.sessions.map((s) => s.tabId);
221 expect(ids).toContain(first.tabId);
222 expect(ids).toContain(second.tabId);
223 }),
224 );
225​
226 it.effect("open creates an independent tab on every call", () =>
227 Effect.gen(function* () {
228 const threadId = freshThreadId();
229 const manager = yield* PreviewManager.PreviewManager;
230 const collector = yield* collectEvents;
231​
232 const a = yield* manager.open({ threadId, url: "http://localhost:5173" });
233 const b = yield* manager.open({ threadId, url: "http://localhost:3000/path" });
234​
235 expect(a.tabId).not.toBe(b.tabId);
236 const list = yield* manager.list({ threadId });
237 expect(list.sessions).toHaveLength(2);
238​
239 const events = yield* collector.drain;
240 expect(events.map((e) => e.type)).toEqual(["opened", "opened"]);
241 }),
242 );
243​
244 it.effect("close with mismatching tabId is a no-op", () =>
245 Effect.gen(function* () {
246 const threadId = freshThreadId();
247 const manager = yield* PreviewManager.PreviewManager;
248 yield* manager.open({ threadId, url: "http://localhost:5173" });
249 yield* manager.close({ threadId, tabId: "tab_missing" });
250​
251 const list = yield* manager.list({ threadId });
252 expect(list.sessions).toHaveLength(1);
253 }),
254 );
255​
256 it.effect("close with explicit tabId removes only that tab", () =>
257 Effect.gen(function* () {
258 const threadId = freshThreadId();
259 const manager = yield* PreviewManager.PreviewManager;
260 const a = yield* manager.open({ threadId, url: "http://localhost:5173" });
261 const b = yield* manager.open({ threadId, url: "http://localhost:3000" });
262​
263 yield* manager.close({ threadId, tabId: a.tabId });
264​
265 const list = yield* manager.list({ threadId });
266 expect(list.sessions.map((s) => s.tabId)).toEqual([b.tabId]);
267 }),
268 );
269​
270 it.effect("multiple subscribers receive every event independently", () =>
271 Effect.gen(function* () {
272 const threadId = freshThreadId();
273 const manager = yield* PreviewManager.PreviewManager;
274 const aSub = yield* manager.subscribeEvents;
275 const bSub = yield* manager.subscribeEvents;
276​
277 yield* manager.open({ threadId, url: "http://localhost:5173" });
278 yield* manager.open({ threadId, url: "http://localhost:3000" });
279​
280 const aEvents = yield* PubSub.takeUpTo(aSub, DRAIN_LIMIT);
281 const bEvents = yield* PubSub.takeUpTo(bSub, DRAIN_LIMIT);
282 expect(aEvents.map((e) => e.type)).toEqual(["opened", "opened"]);
283 expect(bEvents.map((e) => e.type)).toEqual(["opened", "opened"]);
284 }),
285 );
286});