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

previewStateStore.test.ts

433 lines13.9 KB

1import { scopedThreadKey, scopeThreadRef } from "@v12/client-runtime/environment";
2import { type EnvironmentId, type PreviewSessionSnapshot, ThreadId } from "@v12/contracts";
3import { beforeEach, describe, expect, it } from "vite-plus/test";
4​
5import {
6 __testing,
7 applyPreviewDesktopState,
8 applyPreviewServerEvent,
9 applyPreviewServerSnapshot,
10 beginPreviewSessionClose,
11 cancelPreviewSessionClose,
12 previewStateAtom,
13 readThreadPreviewState,
14 reconcilePreviewServerSessions,
15 rememberPreviewUrl,
16 removePreviewThread,
17 resetPreviewStateForTests,
18 setActivePreviewTab,
19 updatePreviewServerSnapshot,
20} from "./previewStateStore";
21​
22const environmentId = "env-1" as EnvironmentId;
23const ref = scopeThreadRef(environmentId, ThreadId.make("thread-1"));
24const otherRef = scopeThreadRef(environmentId, ThreadId.make("thread-2"));
25​
26const makeSnapshot = (overrides: Partial<PreviewSessionSnapshot> = {}): PreviewSessionSnapshot => ({
27 threadId: "thread-1",
28 tabId: "tab_a",
29 navStatus: { _tag: "Loading", url: "http://localhost:5173/", title: "" },
30 canGoBack: false,
31 canGoForward: false,
32 updatedAt: "2026-01-01T00:00:00.000Z",
33 ...overrides,
34});
35​
36beforeEach(() => {
37 resetPreviewStateForTests();
38});
39​
40describe("previewStateStore (single-tab)", () => {
41 it("keeps independent state atoms for each thread", () => {
42 expect(previewStateAtom(scopedThreadKey(ref))).toBe(previewStateAtom(scopedThreadKey(ref)));
43 expect(previewStateAtom(scopedThreadKey(ref))).not.toBe(
44 previewStateAtom(scopedThreadKey(otherRef)),
45 );
46​
47 applyPreviewServerSnapshot(ref, makeSnapshot());
48 expect(readThreadPreviewState(ref).snapshot?.tabId).toBe("tab_a");
49 expect(readThreadPreviewState(otherRef)).toEqual(__testing.EMPTY_THREAD_PREVIEW_STATE);
50 });
51​
52 it("opened event seeds the snapshot and remembers the URL", () => {
53 const snapshot = makeSnapshot();
54 applyPreviewServerEvent(ref, {
55 type: "opened",
56 threadId: "thread-1",
57 tabId: snapshot.tabId,
58 createdAt: snapshot.updatedAt,
59 snapshot,
60 });
61 const state = readThreadPreviewState(ref);
62 expect(state.snapshot?.tabId).toBe(snapshot.tabId);
63 expect(state.recentlySeenUrls).toContain("http://localhost:5173/");
64 });
65​
66 it("a second `opened` for a different tab replaces the rendered snapshot", () => {
67 const a = makeSnapshot({ tabId: "tab_a" });
68 const b = makeSnapshot({ tabId: "tab_b" });
69 applyPreviewServerEvent(ref, {
70 type: "opened",
71 threadId: "thread-1",
72 tabId: a.tabId,
73 createdAt: a.updatedAt,
74 snapshot: a,
75 });
76 applyPreviewServerEvent(ref, {
77 type: "opened",
78 threadId: "thread-1",
79 tabId: b.tabId,
80 createdAt: b.updatedAt,
81 snapshot: b,
82 });
83 const state = readThreadPreviewState(ref);
84 expect(state.snapshot?.tabId).toBe(b.tabId);
85 });
86​
87 it("navigated event updates the snapshot URL", () => {
88 const snapshot = makeSnapshot();
89 applyPreviewServerEvent(ref, {
90 type: "opened",
91 threadId: "thread-1",
92 tabId: snapshot.tabId,
93 createdAt: snapshot.updatedAt,
94 snapshot,
95 });
96 applyPreviewServerEvent(ref, {
97 type: "navigated",
98 threadId: "thread-1",
99 tabId: snapshot.tabId,
100 createdAt: "2026-01-01T00:00:01.000Z",
101 snapshot: {
102 ...snapshot,
103 navStatus: { _tag: "Success", url: "http://localhost:5173/about", title: "About" },
104 },
105 });
106 const state = readThreadPreviewState(ref);
107 expect(state.snapshot?.navStatus._tag).toBe("Success");
108 if (state.snapshot?.navStatus._tag === "Success") {
109 expect(state.snapshot.navStatus.url).toBe("http://localhost:5173/about");
110 }
111 });
112​
113 it("resized event updates tab viewport without changing the active tab", () => {
114 const active = makeSnapshot({ tabId: "tab_a" });
115 const background = makeSnapshot({ tabId: "tab_b" });
116 applyPreviewServerSnapshot(ref, background);
117 applyPreviewServerSnapshot(ref, active);
118​
119 applyPreviewServerEvent(ref, {
120 type: "resized",
121 threadId: "thread-1",
122 tabId: background.tabId,
123 createdAt: "2026-01-01T00:00:01.000Z",
124 snapshot: {
125 ...background,
126 viewport: { _tag: "preset", presetId: "pixel-8", width: 412, height: 915 },
127 updatedAt: "2026-01-01T00:00:01.000Z",
128 },
129 });
130​
131 const state = readThreadPreviewState(ref);
132 expect(state.activeTabId).toBe(active.tabId);
133 expect(state.sessions[background.tabId]?.viewport).toEqual({
134 _tag: "preset",
135 presetId: "pixel-8",
136 width: 412,
137 height: 915,
138 });
139 });
140​
141 it("failed event flips the snapshot to LoadFailed when tabId matches", () => {
142 const snapshot = makeSnapshot();
143 applyPreviewServerEvent(ref, {
144 type: "opened",
145 threadId: "thread-1",
146 tabId: snapshot.tabId,
147 createdAt: snapshot.updatedAt,
148 snapshot,
149 });
150 applyPreviewServerEvent(ref, {
151 type: "failed",
152 threadId: "thread-1",
153 tabId: snapshot.tabId,
154 createdAt: "2026-01-01T00:00:01.000Z",
155 url: "http://localhost:5173/",
156 title: "",
157 code: -105,
158 description: "ERR_NAME_NOT_RESOLVED",
159 });
160 const state = readThreadPreviewState(ref);
161 expect(state.snapshot?.navStatus._tag).toBe("LoadFailed");
162 });
163​
164 it("failed event for a non-active tab is ignored", () => {
165 const snapshot = makeSnapshot({ tabId: "tab_a" });
166 applyPreviewServerEvent(ref, {
167 type: "opened",
168 threadId: "thread-1",
169 tabId: snapshot.tabId,
170 createdAt: snapshot.updatedAt,
171 snapshot,
172 });
173 applyPreviewServerEvent(ref, {
174 type: "failed",
175 threadId: "thread-1",
176 tabId: "tab_b",
177 createdAt: "2026-01-01T00:00:01.000Z",
178 url: "http://localhost:9999/",
179 title: "",
180 code: -105,
181 description: "ERR_NAME_NOT_RESOLVED",
182 });
183 const state = readThreadPreviewState(ref);
184 expect(state.snapshot?.navStatus._tag).toBe("Loading");
185 });
186​
187 it("closed event clears snapshot but retains recently-seen URLs", () => {
188 const snapshot = makeSnapshot();
189 applyPreviewServerEvent(ref, {
190 type: "opened",
191 threadId: "thread-1",
192 tabId: snapshot.tabId,
193 createdAt: snapshot.updatedAt,
194 snapshot,
195 });
196 applyPreviewServerEvent(ref, {
197 type: "closed",
198 threadId: "thread-1",
199 tabId: snapshot.tabId,
200 createdAt: "2026-01-01T00:00:01.000Z",
201 });
202 const state = readThreadPreviewState(ref);
203 expect(state.snapshot).toBeNull();
204 expect(state.recentlySeenUrls).toContain("http://localhost:5173/");
205 });
206​
207 it("optimistically removes a session before the server close event arrives", () => {
208 const first = makeSnapshot({ tabId: "tab_a" });
209 const second = makeSnapshot({
210 tabId: "tab_b",
211 updatedAt: "2026-01-01T00:00:01.000Z",
212 });
213 applyPreviewServerSnapshot(ref, first);
214 applyPreviewServerSnapshot(ref, second);
215​
216 beginPreviewSessionClose(ref, second.tabId);
217​
218 const state = readThreadPreviewState(ref);
219 expect(Object.keys(state.sessions)).toEqual([first.tabId]);
220 expect(state.activeTabId).toBe(first.tabId);
221 expect(state.snapshot?.tabId).toBe(first.tabId);
222 });
223​
224 it("treats a late server close event after optimistic removal as a no-op", () => {
225 const snapshot = makeSnapshot();
226 applyPreviewServerSnapshot(ref, snapshot);
227 beginPreviewSessionClose(ref, snapshot.tabId);
228​
229 applyPreviewServerEvent(ref, {
230 type: "closed",
231 threadId: "thread-1",
232 tabId: snapshot.tabId,
233 createdAt: "2026-01-01T00:00:01.000Z",
234 });
235​
236 const state = readThreadPreviewState(ref);
237 expect(state.sessions).toEqual({});
238 expect(state.snapshot).toBeNull();
239 });
240​
241 it("does not resurrect an intentionally closed tab from a stale list snapshot", () => {
242 const snapshot = makeSnapshot();
243 applyPreviewServerSnapshot(ref, snapshot);
244 beginPreviewSessionClose(ref, snapshot.tabId);
245​
246 applyPreviewServerSnapshot(ref, snapshot);
247​
248 const state = readThreadPreviewState(ref);
249 expect(state.sessions).toEqual({});
250 expect(state.snapshot).toBeNull();
251 });
252​
253 it("can restore a suppressed tab after a failed close", () => {
254 const snapshot = makeSnapshot();
255 applyPreviewServerSnapshot(ref, snapshot);
256 beginPreviewSessionClose(ref, snapshot.tabId);
257​
258 cancelPreviewSessionClose(ref, snapshot, snapshot.tabId);
259​
260 const state = readThreadPreviewState(ref);
261 expect(state.sessions).toEqual({ [snapshot.tabId]: snapshot });
262 expect(state.snapshot).toEqual(snapshot);
263 });
264​
265 it("closed event for a different tab is a no-op", () => {
266 const snapshot = makeSnapshot({ tabId: "tab_a" });
267 applyPreviewServerEvent(ref, {
268 type: "opened",
269 threadId: "thread-1",
270 tabId: snapshot.tabId,
271 createdAt: snapshot.updatedAt,
272 snapshot,
273 });
274 applyPreviewServerEvent(ref, {
275 type: "closed",
276 threadId: "thread-1",
277 tabId: "tab_b",
278 createdAt: "2026-01-01T00:00:01.000Z",
279 });
280 const state = readThreadPreviewState(ref);
281 expect(state.snapshot?.tabId).toBe(snapshot.tabId);
282 });
283​
284 it("desktopOverlay updates independently of snapshot", () => {
285 const snapshot = makeSnapshot();
286 applyPreviewServerEvent(ref, {
287 type: "opened",
288 threadId: "thread-1",
289 tabId: snapshot.tabId,
290 createdAt: snapshot.updatedAt,
291 snapshot,
292 });
293 applyPreviewDesktopState(ref, snapshot.tabId, {
294 canGoBack: true,
295 canGoForward: false,
296 loading: false,
297 zoomFactor: 1,
298 controller: "none",
299 });
300 const state = readThreadPreviewState(ref);
301 expect(state.desktopOverlay?.canGoBack).toBe(true);
302 expect(state.snapshot?.canGoBack).toBe(false);
303 });
304​
305 it("retains multiple tabs and switches active desktop state", () => {
306 const first = makeSnapshot();
307 const second = { ...makeSnapshot(), tabId: "tab_2", updatedAt: "2026-01-02T00:00:00.000Z" };
308 applyPreviewServerSnapshot(ref, first);
309 applyPreviewServerSnapshot(ref, second);
310 applyPreviewDesktopState(ref, first.tabId, {
311 canGoBack: true,
312 canGoForward: false,
313 loading: false,
314 zoomFactor: 1,
315 controller: "none",
316 });
317 setActivePreviewTab(ref, first.tabId);
318​
319 const state = readThreadPreviewState(ref);
320 expect(Object.keys(state.sessions)).toEqual([first.tabId, second.tabId]);
321 expect(state.snapshot?.tabId).toBe(first.tabId);
322 expect(state.desktopOverlay?.canGoBack).toBe(true);
323 });
324​
325 it("updates a background snapshot without changing the active tab", () => {
326 const background = makeSnapshot({ tabId: "tab_a" });
327 const active = makeSnapshot({
328 tabId: "tab_b",
329 updatedAt: "2026-01-01T00:00:01.000Z",
330 });
331 applyPreviewServerSnapshot(ref, background);
332 applyPreviewServerSnapshot(ref, active);
333​
334 const resized = {
335 ...background,
336 viewport: { _tag: "freeform" as const, width: 900, height: 700 },
337 updatedAt: "2026-01-01T00:00:02.000Z",
338 };
339 updatePreviewServerSnapshot(ref, resized);
340​
341 const state = readThreadPreviewState(ref);
342 expect(state.activeTabId).toBe(active.tabId);
343 expect(state.snapshot?.tabId).toBe(active.tabId);
344 expect(state.sessions[background.tabId]).toEqual(resized);
345 });
346​
347 it("reconciles an authoritative session list without focusing a background tab", () => {
348 const active = makeSnapshot({ tabId: "tab_a" });
349 const stale = makeSnapshot({
350 tabId: "tab_stale",
351 updatedAt: "2026-01-01T00:00:01.000Z",
352 });
353 applyPreviewServerSnapshot(ref, stale);
354 applyPreviewServerSnapshot(ref, active);
355 applyPreviewDesktopState(ref, stale.tabId, {
356 canGoBack: false,
357 canGoForward: false,
358 loading: false,
359 zoomFactor: 1,
360 controller: "none",
361 });
362​
363 reconcilePreviewServerSessions(ref, [active]);
364​
365 const state = readThreadPreviewState(ref);
366 expect(Object.keys(state.sessions)).toEqual([active.tabId]);
367 expect(state.activeTabId).toBe(active.tabId);
368 expect(state.snapshot).toEqual(active);
369 expect(state.desktopByTabId[stale.tabId]).toBeUndefined();
370 });
371​
372 it("clears stale sessions when an authoritative list is empty", () => {
373 applyPreviewServerSnapshot(ref, makeSnapshot());
374​
375 reconcilePreviewServerSessions(ref, []);
376​
377 const state = readThreadPreviewState(ref);
378 expect(state.sessions).toEqual({});
379 expect(state.activeTabId).toBeNull();
380 expect(state.snapshot).toBeNull();
381 });
382​
383 it("applyServerSnapshot null clears snapshot for a thread that had one", () => {
384 const snapshot = makeSnapshot();
385 applyPreviewServerSnapshot(ref, snapshot);
386 applyPreviewServerSnapshot(ref, null);
387 const state = readThreadPreviewState(ref);
388 expect(state.snapshot).toBeNull();
389 });
390​
391 it("does not replace a streamed snapshot with older SWR data", () => {
392 applyPreviewServerSnapshot(
393 ref,
394 makeSnapshot({
395 navStatus: { _tag: "Success", url: "http://localhost:5173/new", title: "New" },
396 updatedAt: "2026-01-01T00:00:02.000Z",
397 }),
398 );
399 applyPreviewServerSnapshot(
400 ref,
401 makeSnapshot({
402 navStatus: { _tag: "Success", url: "http://localhost:5173/old", title: "Old" },
403 updatedAt: "2026-01-01T00:00:01.000Z",
404 }),
405 );
406​
407 const state = readThreadPreviewState(ref);
408 expect(state.snapshot?.navStatus).toEqual({
409 _tag: "Success",
410 url: "http://localhost:5173/new",
411 title: "New",
412 });
413 });
414​
415 it("rememberUrl dedupes and caps at limit", () => {
416 for (let i = 0; i < __testing.RECENT_URL_LIMIT + 5; i += 1) {
417 rememberPreviewUrl(ref, `http://localhost:${5000 + i}/`);
418 }
419 const state = readThreadPreviewState(ref);
420 expect(state.recentlySeenUrls.length).toBeLessThanOrEqual(__testing.RECENT_URL_LIMIT);
421 expect(state.recentlySeenUrls[0]).toBe(
422 `http://localhost:${5000 + __testing.RECENT_URL_LIMIT + 4}/`,
423 );
424 });
425​
426 it("removeThread strips the entry", () => {
427 const snapshot = makeSnapshot();
428 applyPreviewServerSnapshot(ref, snapshot);
429 removePreviewThread(ref);
430 const state = readThreadPreviewState(ref);
431 expect(state).toEqual(__testing.EMPTY_THREAD_PREVIEW_STATE);
432 });
433});