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

previewStateStore.test.ts

587 lines18.1 KB

1import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime/environment";
2import {
3 type EnvironmentId,
4 type PreviewEvent,
5 type PreviewSessionSnapshot,
6 ThreadId,
7} from "@t3tools/contracts";
8import { beforeEach, describe, expect, it } from "vite-plus/test";
9​
10import {
11 __testing,
12 applyPreviewDesktopState,
13 applyPreviewServerEvent as applyPreviewServerEventImpl,
14 applyPreviewServerSnapshot,
15 beginPreviewSessionClose,
16 cancelPreviewSessionClose,
17 previewStateAtom,
18 readThreadPreviewState,
19 reconcilePreviewServerSessions,
20 rememberPreviewUrl,
21 removePreviewThread,
22 resetPreviewStateForTests,
23 setActivePreviewTab,
24 updatePreviewServerSnapshot,
25} from "./previewStateStore";
26​
27const environmentId = "env-1" as EnvironmentId;
28const ref = scopeThreadRef(environmentId, ThreadId.make("thread-1"));
29const otherRef = scopeThreadRef(environmentId, ThreadId.make("thread-2"));
30​
31const makeSnapshot = (overrides: Partial<PreviewSessionSnapshot> = {}): PreviewSessionSnapshot => ({
32 threadId: "thread-1",
33 tabId: "tab_a",
34 navStatus: { _tag: "Loading", url: "http://localhost:5173/", title: "" },
35 canGoBack: false,
36 canGoForward: false,
37 updatedAt: "2026-01-01T00:00:00.000Z",
38 ...overrides,
39});
40​
41type PreviewEventDraft = PreviewEvent extends infer Event
42 ? Event extends { readonly revision: number }
43 ? Omit<Event, "revision" | "serverEpoch">
44 : never
45 : never;
46​
47const serverEpoch = "server-a";
48let nextServerRevision = 0;
49const applyPreviewServerEvent = (eventRef: typeof ref, event: PreviewEventDraft): void => {
50 nextServerRevision += 1;
51 applyPreviewServerEventImpl(eventRef, {
52 ...event,
53 serverEpoch,
54 revision: nextServerRevision,
55 } as PreviewEvent);
56};
57​
58beforeEach(() => {
59 nextServerRevision = 0;
60 resetPreviewStateForTests();
61});
62​
63describe("previewStateStore (single-tab)", () => {
64 it("keeps independent state atoms for each thread", () => {
65 expect(previewStateAtom(scopedThreadKey(ref))).toBe(previewStateAtom(scopedThreadKey(ref)));
66 expect(previewStateAtom(scopedThreadKey(ref))).not.toBe(
67 previewStateAtom(scopedThreadKey(otherRef)),
68 );
69​
70 applyPreviewServerSnapshot(ref, makeSnapshot());
71 expect(readThreadPreviewState(ref).snapshot?.tabId).toBe("tab_a");
72 expect(readThreadPreviewState(otherRef)).toEqual(__testing.EMPTY_THREAD_PREVIEW_STATE);
73 });
74​
75 it("opened event seeds the snapshot and remembers the URL", () => {
76 const snapshot = makeSnapshot();
77 applyPreviewServerEvent(ref, {
78 type: "opened",
79 threadId: "thread-1",
80 tabId: snapshot.tabId,
81 createdAt: snapshot.updatedAt,
82 snapshot,
83 });
84 const state = readThreadPreviewState(ref);
85 expect(state.snapshot?.tabId).toBe(snapshot.tabId);
86 expect(state.recentlySeenUrls).toContain("http://localhost:5173/");
87 });
88​
89 it("a second `opened` for a different tab replaces the rendered snapshot", () => {
90 const a = makeSnapshot({ tabId: "tab_a" });
91 const b = makeSnapshot({ tabId: "tab_b" });
92 applyPreviewServerEvent(ref, {
93 type: "opened",
94 threadId: "thread-1",
95 tabId: a.tabId,
96 createdAt: a.updatedAt,
97 snapshot: a,
98 });
99 applyPreviewServerEvent(ref, {
100 type: "opened",
101 threadId: "thread-1",
102 tabId: b.tabId,
103 createdAt: b.updatedAt,
104 snapshot: b,
105 });
106 const state = readThreadPreviewState(ref);
107 expect(state.snapshot?.tabId).toBe(b.tabId);
108 });
109​
110 it("navigated event updates the snapshot URL", () => {
111 const snapshot = makeSnapshot();
112 applyPreviewServerEvent(ref, {
113 type: "opened",
114 threadId: "thread-1",
115 tabId: snapshot.tabId,
116 createdAt: snapshot.updatedAt,
117 snapshot,
118 });
119 applyPreviewServerEvent(ref, {
120 type: "navigated",
121 threadId: "thread-1",
122 tabId: snapshot.tabId,
123 createdAt: "2026-01-01T00:00:01.000Z",
124 snapshot: {
125 ...snapshot,
126 navStatus: { _tag: "Success", url: "http://localhost:5173/about", title: "About" },
127 },
128 });
129 const state = readThreadPreviewState(ref);
130 expect(state.snapshot?.navStatus._tag).toBe("Success");
131 if (state.snapshot?.navStatus._tag === "Success") {
132 expect(state.snapshot.navStatus.url).toBe("http://localhost:5173/about");
133 }
134 });
135​
136 it("resized event updates tab viewport without changing the active tab", () => {
137 const active = makeSnapshot({ tabId: "tab_a" });
138 const background = makeSnapshot({ tabId: "tab_b" });
139 applyPreviewServerSnapshot(ref, background);
140 applyPreviewServerSnapshot(ref, active);
141​
142 applyPreviewServerEvent(ref, {
143 type: "resized",
144 threadId: "thread-1",
145 tabId: background.tabId,
146 createdAt: "2026-01-01T00:00:01.000Z",
147 snapshot: {
148 ...background,
149 viewport: { _tag: "preset", presetId: "pixel-8", width: 412, height: 915 },
150 updatedAt: "2026-01-01T00:00:01.000Z",
151 },
152 });
153​
154 const state = readThreadPreviewState(ref);
155 expect(state.activeTabId).toBe(active.tabId);
156 expect(state.sessions[background.tabId]?.viewport).toEqual({
157 _tag: "preset",
158 presetId: "pixel-8",
159 width: 412,
160 height: 915,
161 });
162 });
163​
164 it("failed event flips the snapshot to LoadFailed when tabId matches", () => {
165 const snapshot = makeSnapshot();
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: snapshot.tabId,
177 createdAt: "2026-01-01T00:00:01.000Z",
178 url: "http://localhost:5173/",
179 title: "",
180 code: -105,
181 description: "ERR_NAME_NOT_RESOLVED",
182 });
183 const state = readThreadPreviewState(ref);
184 expect(state.snapshot?.navStatus._tag).toBe("LoadFailed");
185 });
186​
187 it("failed event for a non-active tab is ignored", () => {
188 const snapshot = makeSnapshot({ tabId: "tab_a" });
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: "failed",
198 threadId: "thread-1",
199 tabId: "tab_b",
200 createdAt: "2026-01-01T00:00:01.000Z",
201 url: "http://localhost:9999/",
202 title: "",
203 code: -105,
204 description: "ERR_NAME_NOT_RESOLVED",
205 });
206 const state = readThreadPreviewState(ref);
207 expect(state.snapshot?.navStatus._tag).toBe("Loading");
208 });
209​
210 it("closed event clears snapshot but retains recently-seen URLs", () => {
211 const snapshot = makeSnapshot();
212 applyPreviewServerEvent(ref, {
213 type: "opened",
214 threadId: "thread-1",
215 tabId: snapshot.tabId,
216 createdAt: snapshot.updatedAt,
217 snapshot,
218 });
219 applyPreviewServerEvent(ref, {
220 type: "closed",
221 threadId: "thread-1",
222 tabId: snapshot.tabId,
223 createdAt: "2026-01-01T00:00:01.000Z",
224 });
225 const state = readThreadPreviewState(ref);
226 expect(state.snapshot).toBeNull();
227 expect(state.recentlySeenUrls).toContain("http://localhost:5173/");
228 });
229​
230 it("optimistically removes a session before the server close event arrives", () => {
231 const first = makeSnapshot({ tabId: "tab_a" });
232 const second = makeSnapshot({
233 tabId: "tab_b",
234 updatedAt: "2026-01-01T00:00:01.000Z",
235 });
236 applyPreviewServerSnapshot(ref, first);
237 applyPreviewServerSnapshot(ref, second);
238​
239 beginPreviewSessionClose(ref, second.tabId);
240​
241 const state = readThreadPreviewState(ref);
242 expect(Object.keys(state.sessions)).toEqual([first.tabId]);
243 expect(state.activeTabId).toBe(first.tabId);
244 expect(state.snapshot?.tabId).toBe(first.tabId);
245 });
246​
247 it("treats a late server close event after optimistic removal as a no-op", () => {
248 const snapshot = makeSnapshot();
249 applyPreviewServerSnapshot(ref, snapshot);
250 beginPreviewSessionClose(ref, snapshot.tabId);
251​
252 applyPreviewServerEvent(ref, {
253 type: "closed",
254 threadId: "thread-1",
255 tabId: snapshot.tabId,
256 createdAt: "2026-01-01T00:00:01.000Z",
257 });
258​
259 const state = readThreadPreviewState(ref);
260 expect(state.sessions).toEqual({});
261 expect(state.snapshot).toBeNull();
262 });
263​
264 it("does not resurrect an intentionally closed tab from a stale list snapshot", () => {
265 const snapshot = makeSnapshot();
266 applyPreviewServerSnapshot(ref, snapshot);
267 beginPreviewSessionClose(ref, snapshot.tabId);
268​
269 applyPreviewServerSnapshot(ref, snapshot);
270​
271 const state = readThreadPreviewState(ref);
272 expect(state.sessions).toEqual({});
273 expect(state.snapshot).toBeNull();
274 });
275​
276 it("can restore a suppressed tab after a failed close", () => {
277 const snapshot = makeSnapshot();
278 applyPreviewServerSnapshot(ref, snapshot);
279 beginPreviewSessionClose(ref, snapshot.tabId);
280​
281 cancelPreviewSessionClose(ref, snapshot, snapshot.tabId);
282​
283 const state = readThreadPreviewState(ref);
284 expect(state.sessions).toEqual({ [snapshot.tabId]: snapshot });
285 expect(state.snapshot).toEqual(snapshot);
286 });
287​
288 it("closed event for a different tab is a no-op", () => {
289 const snapshot = makeSnapshot({ tabId: "tab_a" });
290 applyPreviewServerEvent(ref, {
291 type: "opened",
292 threadId: "thread-1",
293 tabId: snapshot.tabId,
294 createdAt: snapshot.updatedAt,
295 snapshot,
296 });
297 applyPreviewServerEvent(ref, {
298 type: "closed",
299 threadId: "thread-1",
300 tabId: "tab_b",
301 createdAt: "2026-01-01T00:00:01.000Z",
302 });
303 const state = readThreadPreviewState(ref);
304 expect(state.snapshot?.tabId).toBe(snapshot.tabId);
305 });
306​
307 it("desktopOverlay updates independently of snapshot", () => {
308 const snapshot = makeSnapshot();
309 applyPreviewServerEvent(ref, {
310 type: "opened",
311 threadId: "thread-1",
312 tabId: snapshot.tabId,
313 createdAt: snapshot.updatedAt,
314 snapshot,
315 });
316 applyPreviewDesktopState(ref, snapshot.tabId, {
317 hasWebContents: true,
318 canGoBack: true,
319 canGoForward: false,
320 loading: false,
321 zoomFactor: 1,
322 pictureInPicture: false,
323 colorScheme: "system",
324 audioMuted: false,
325 audible: false,
326 controller: "none",
327 favicon: null,
328 });
329 const state = readThreadPreviewState(ref);
330 expect(state.desktopOverlay?.canGoBack).toBe(true);
331 expect(state.snapshot?.canGoBack).toBe(false);
332 });
333​
334 it("retains multiple tabs and switches active desktop state", () => {
335 const first = makeSnapshot();
336 const second = { ...makeSnapshot(), tabId: "tab_2", updatedAt: "2026-01-02T00:00:00.000Z" };
337 applyPreviewServerSnapshot(ref, first);
338 applyPreviewServerSnapshot(ref, second);
339 applyPreviewDesktopState(ref, first.tabId, {
340 hasWebContents: true,
341 canGoBack: true,
342 canGoForward: false,
343 loading: false,
344 zoomFactor: 1,
345 pictureInPicture: false,
346 colorScheme: "system",
347 audioMuted: false,
348 audible: false,
349 controller: "none",
350 favicon: null,
351 });
352 setActivePreviewTab(ref, first.tabId);
353​
354 const state = readThreadPreviewState(ref);
355 expect(Object.keys(state.sessions)).toEqual([first.tabId, second.tabId]);
356 expect(state.snapshot?.tabId).toBe(first.tabId);
357 expect(state.desktopOverlay?.canGoBack).toBe(true);
358 });
359​
360 it("updates a background snapshot without changing the active tab", () => {
361 const background = makeSnapshot({ tabId: "tab_a" });
362 const active = makeSnapshot({
363 tabId: "tab_b",
364 updatedAt: "2026-01-01T00:00:01.000Z",
365 });
366 applyPreviewServerSnapshot(ref, background);
367 applyPreviewServerSnapshot(ref, active);
368​
369 const resized = {
370 ...background,
371 viewport: { _tag: "freeform" as const, width: 900, height: 700 },
372 updatedAt: "2026-01-01T00:00:02.000Z",
373 };
374 updatePreviewServerSnapshot(ref, resized);
375​
376 const state = readThreadPreviewState(ref);
377 expect(state.activeTabId).toBe(active.tabId);
378 expect(state.snapshot?.tabId).toBe(active.tabId);
379 expect(state.sessions[background.tabId]).toEqual(resized);
380 });
381​
382 it("reconciles an authoritative session list without focusing a background tab", () => {
383 const active = makeSnapshot({ tabId: "tab_a" });
384 const stale = makeSnapshot({
385 tabId: "tab_stale",
386 updatedAt: "2026-01-01T00:00:01.000Z",
387 });
388 applyPreviewServerSnapshot(ref, stale);
389 applyPreviewServerSnapshot(ref, active);
390 applyPreviewDesktopState(ref, stale.tabId, {
391 hasWebContents: true,
392 canGoBack: false,
393 canGoForward: false,
394 loading: false,
395 zoomFactor: 1,
396 pictureInPicture: false,
397 colorScheme: "system",
398 audioMuted: false,
399 audible: false,
400 controller: "none",
401 favicon: null,
402 });
403​
404 reconcilePreviewServerSessions(ref, { sessions: [active], serverEpoch, revision: 1 });
405​
406 const state = readThreadPreviewState(ref);
407 expect(Object.keys(state.sessions)).toEqual([active.tabId]);
408 expect(state.activeTabId).toBe(active.tabId);
409 expect(state.snapshot).toEqual(active);
410 expect(state.desktopByTabId[stale.tabId]).toBeUndefined();
411 });
412​
413 it("clears stale sessions when an authoritative list is empty", () => {
414 applyPreviewServerSnapshot(ref, makeSnapshot());
415​
416 reconcilePreviewServerSessions(ref, { sessions: [], serverEpoch, revision: 1 });
417​
418 const state = readThreadPreviewState(ref);
419 expect(state.sessions).toEqual({});
420 expect(state.activeTabId).toBeNull();
421 expect(state.snapshot).toBeNull();
422 });
423​
424 it("ignores a list response older than the latest server event", () => {
425 const snapshot = makeSnapshot();
426 applyPreviewServerEvent(ref, {
427 type: "opened",
428 threadId: "thread-1",
429 tabId: snapshot.tabId,
430 createdAt: snapshot.updatedAt,
431 snapshot,
432 });
433​
434 reconcilePreviewServerSessions(ref, { sessions: [], serverEpoch, revision: 0 });
435​
436 expect(readThreadPreviewState(ref).sessions).toEqual({ [snapshot.tabId]: snapshot });
437 });
438​
439 it("does not resurrect a tab from an event older than its close", () => {
440 const snapshot = makeSnapshot();
441 applyPreviewServerEvent(ref, {
442 type: "opened",
443 threadId: "thread-1",
444 tabId: snapshot.tabId,
445 createdAt: snapshot.updatedAt,
446 snapshot,
447 });
448 applyPreviewServerEvent(ref, {
449 type: "closed",
450 threadId: "thread-1",
451 tabId: snapshot.tabId,
452 createdAt: "2026-01-01T00:00:01.000Z",
453 });
454​
455 applyPreviewServerEventImpl(ref, {
456 type: "opened",
457 threadId: "thread-1",
458 tabId: snapshot.tabId,
459 createdAt: snapshot.updatedAt,
460 serverEpoch,
461 revision: 1,
462 snapshot,
463 });
464​
465 expect(readThreadPreviewState(ref).sessions).toEqual({});
466 });
467​
468 it("accepts a lower revision from a newly restarted server", () => {
469 const snapshot = makeSnapshot();
470 applyPreviewServerEventImpl(ref, {
471 type: "opened",
472 threadId: "thread-1",
473 tabId: snapshot.tabId,
474 createdAt: snapshot.updatedAt,
475 serverEpoch,
476 revision: 12,
477 snapshot,
478 });
479​
480 reconcilePreviewServerSessions(ref, {
481 sessions: [],
482 serverEpoch: "server-b",
483 revision: 0,
484 });
485​
486 const state = readThreadPreviewState(ref);
487 expect(state.sessions).toEqual({});
488 expect(state.serverEpoch).toBe("server-b");
489 expect(state.serverRevision).toBe(0);
490 });
491​
492 it("does not carry raw-tab state across a server restart", () => {
493 const previous = makeSnapshot({
494 navStatus: { _tag: "Success", url: "https://old.example", title: "Old" },
495 updatedAt: "2026-01-01T00:00:02.000Z",
496 });
497 applyPreviewServerEventImpl(ref, {
498 type: "opened",
499 threadId: "thread-1",
500 tabId: previous.tabId,
501 createdAt: previous.updatedAt,
502 serverEpoch,
503 revision: 12,
504 snapshot: previous,
505 });
506 beginPreviewSessionClose(ref, previous.tabId);
507 applyPreviewDesktopState(ref, previous.tabId, {
508 hasWebContents: true,
509 canGoBack: false,
510 canGoForward: false,
511 loading: false,
512 zoomFactor: 1,
513 pictureInPicture: false,
514 colorScheme: "system",
515 audioMuted: false,
516 audible: false,
517 controller: "none",
518 favicon: null,
519 });
520 const restarted = makeSnapshot({
521 navStatus: { _tag: "Success", url: "https://new.example", title: "New" },
522 updatedAt: "2026-01-01T00:00:01.000Z",
523 });
524 reconcilePreviewServerSessions(ref, {
525 sessions: [restarted],
526 serverEpoch: "server-b",
527 revision: 0,
528 });
529​
530 const state = readThreadPreviewState(ref);
531 expect(state.sessions[restarted.tabId]).toEqual(restarted);
532 expect(state.suppressedTabIds).toEqual(new Set());
533 expect(state.desktopByTabId).toEqual({});
534 expect(state.desktopOverlay).toBeNull();
535 });
536​
537 it("applyServerSnapshot null clears snapshot for a thread that had one", () => {
538 const snapshot = makeSnapshot();
539 applyPreviewServerSnapshot(ref, snapshot);
540 applyPreviewServerSnapshot(ref, null);
541 const state = readThreadPreviewState(ref);
542 expect(state.snapshot).toBeNull();
543 });
544​
545 it("does not replace a streamed snapshot with older SWR data", () => {
546 applyPreviewServerSnapshot(
547 ref,
548 makeSnapshot({
549 navStatus: { _tag: "Success", url: "http://localhost:5173/new", title: "New" },
550 updatedAt: "2026-01-01T00:00:02.000Z",
551 }),
552 );
553 applyPreviewServerSnapshot(
554 ref,
555 makeSnapshot({
556 navStatus: { _tag: "Success", url: "http://localhost:5173/old", title: "Old" },
557 updatedAt: "2026-01-01T00:00:01.000Z",
558 }),
559 );
560​
561 const state = readThreadPreviewState(ref);
562 expect(state.snapshot?.navStatus).toEqual({
563 _tag: "Success",
564 url: "http://localhost:5173/new",
565 title: "New",
566 });
567 });
568​
569 it("rememberUrl dedupes and caps at limit", () => {
570 for (let i = 0; i < __testing.RECENT_URL_LIMIT + 5; i += 1) {
571 rememberPreviewUrl(ref, `http://localhost:${5000 + i}/`);
572 }
573 const state = readThreadPreviewState(ref);
574 expect(state.recentlySeenUrls.length).toBeLessThanOrEqual(__testing.RECENT_URL_LIMIT);
575 expect(state.recentlySeenUrls[0]).toBe(
576 `http://localhost:${5000 + __testing.RECENT_URL_LIMIT + 4}/`,
577 );
578 });
579​
580 it("removeThread strips the entry", () => {
581 const snapshot = makeSnapshot();
582 applyPreviewServerSnapshot(ref, snapshot);
583 removePreviewThread(ref);
584 const state = readThreadPreviewState(ref);
585 expect(state).toEqual(__testing.EMPTY_THREAD_PREVIEW_STATE);
586 });
587});