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

previewStateStore.test.ts

636 lines19.9 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 resetPreviewStateForTests,
22 setActivePreviewTab,
23 updatePreviewServerSnapshot,
24} from "./previewStateStore";
25import { appAtomRegistry } from "./rpc/atomRegistry";
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("does not publish duplicate desktop browser state", () => {
335 const snapshot = makeSnapshot();
336 applyPreviewServerSnapshot(ref, snapshot);
337 const overlay = {
338 hasWebContents: true,
339 canGoBack: true,
340 canGoForward: false,
341 loading: false,
342 zoomFactor: 1,
343 pictureInPicture: false,
344 colorScheme: "system" as const,
345 audioMuted: false,
346 audible: false,
347 controller: "none" as const,
348 favicon: {
349 dataUrl: "data:image/png;base64,AA==",
350 pageUrl: "https://example.com",
351 capturedAt: 1,
352 },
353 };
354 let updateCount = 0;
355 const unsubscribe = appAtomRegistry.subscribe(previewStateAtom(scopedThreadKey(ref)), () => {
356 updateCount += 1;
357 });
358​
359 applyPreviewDesktopState(ref, snapshot.tabId, overlay);
360 applyPreviewDesktopState(ref, snapshot.tabId, { ...overlay, favicon: { ...overlay.favicon } });
361 unsubscribe();
362​
363 expect(updateCount).toBe(1);
364 });
365​
366 it("retains multiple tabs and switches active desktop state", () => {
367 const first = makeSnapshot();
368 const second = { ...makeSnapshot(), tabId: "tab_2", updatedAt: "2026-01-02T00:00:00.000Z" };
369 applyPreviewServerSnapshot(ref, first);
370 applyPreviewServerSnapshot(ref, second);
371 applyPreviewDesktopState(ref, first.tabId, {
372 hasWebContents: true,
373 canGoBack: true,
374 canGoForward: false,
375 loading: false,
376 zoomFactor: 1,
377 pictureInPicture: false,
378 colorScheme: "system",
379 audioMuted: false,
380 audible: false,
381 controller: "none",
382 favicon: null,
383 });
384 setActivePreviewTab(ref, first.tabId);
385​
386 const state = readThreadPreviewState(ref);
387 expect(Object.keys(state.sessions)).toEqual([first.tabId, second.tabId]);
388 expect(state.snapshot?.tabId).toBe(first.tabId);
389 expect(state.desktopOverlay?.canGoBack).toBe(true);
390 });
391​
392 it("updates a background snapshot without changing the active tab", () => {
393 const background = makeSnapshot({ tabId: "tab_a" });
394 const active = makeSnapshot({
395 tabId: "tab_b",
396 updatedAt: "2026-01-01T00:00:01.000Z",
397 });
398 applyPreviewServerSnapshot(ref, background);
399 applyPreviewServerSnapshot(ref, active);
400​
401 const resized = {
402 ...background,
403 viewport: { _tag: "freeform" as const, width: 900, height: 700 },
404 updatedAt: "2026-01-01T00:00:02.000Z",
405 };
406 updatePreviewServerSnapshot(ref, resized);
407​
408 const state = readThreadPreviewState(ref);
409 expect(state.activeTabId).toBe(active.tabId);
410 expect(state.snapshot?.tabId).toBe(active.tabId);
411 expect(state.sessions[background.tabId]).toEqual(resized);
412 });
413​
414 it("reconciles an authoritative session list without focusing a background tab", () => {
415 const active = makeSnapshot({ tabId: "tab_a" });
416 const stale = makeSnapshot({
417 tabId: "tab_stale",
418 updatedAt: "2026-01-01T00:00:01.000Z",
419 });
420 applyPreviewServerSnapshot(ref, stale);
421 applyPreviewServerSnapshot(ref, active);
422 applyPreviewDesktopState(ref, stale.tabId, {
423 hasWebContents: true,
424 canGoBack: false,
425 canGoForward: false,
426 loading: false,
427 zoomFactor: 1,
428 pictureInPicture: false,
429 colorScheme: "system",
430 audioMuted: false,
431 audible: false,
432 controller: "none",
433 favicon: null,
434 });
435​
436 reconcilePreviewServerSessions(ref, { sessions: [active], serverEpoch, revision: 1 });
437​
438 const state = readThreadPreviewState(ref);
439 expect(Object.keys(state.sessions)).toEqual([active.tabId]);
440 expect(state.activeTabId).toBe(active.tabId);
441 expect(state.snapshot).toEqual(active);
442 expect(state.desktopByTabId[stale.tabId]).toBeUndefined();
443 });
444​
445 it("clears stale sessions when an authoritative list is empty", () => {
446 applyPreviewServerSnapshot(ref, makeSnapshot());
447​
448 reconcilePreviewServerSessions(ref, { sessions: [], serverEpoch, revision: 1 });
449​
450 const state = readThreadPreviewState(ref);
451 expect(state.sessions).toEqual({});
452 expect(state.activeTabId).toBeNull();
453 expect(state.snapshot).toBeNull();
454 });
455​
456 it("keeps the state object when a refetched list changes nothing", () => {
457 reconcilePreviewServerSessions(ref, { sessions: [], serverEpoch, revision: 1 });
458 const loaded = readThreadPreviewState(ref);
459​
460 reconcilePreviewServerSessions(ref, { sessions: [], serverEpoch, revision: 1 });
461 expect(readThreadPreviewState(ref)).toBe(loaded);
462​
463 const snapshot = makeSnapshot();
464 reconcilePreviewServerSessions(ref, { sessions: [snapshot], serverEpoch, revision: 2 });
465 expect(readThreadPreviewState(ref).sessions).toEqual({ [snapshot.tabId]: snapshot });
466 });
467​
468 it("ignores a list response older than the latest server event", () => {
469 const snapshot = makeSnapshot();
470 applyPreviewServerEvent(ref, {
471 type: "opened",
472 threadId: "thread-1",
473 tabId: snapshot.tabId,
474 createdAt: snapshot.updatedAt,
475 snapshot,
476 });
477​
478 reconcilePreviewServerSessions(ref, { sessions: [], serverEpoch, revision: 0 });
479​
480 expect(readThreadPreviewState(ref).sessions).toEqual({ [snapshot.tabId]: snapshot });
481 expect(readThreadPreviewState(ref).listLoaded).toBe(false);
482​
483 const olderTab = makeSnapshot({ tabId: "tab_before_event" });
484 reconcilePreviewServerSessions(ref, {
485 sessions: [olderTab, snapshot],
486 serverEpoch,
487 revision: 2,
488 });
489 expect(readThreadPreviewState(ref).listLoaded).toBe(true);
490 expect(readThreadPreviewState(ref).sessions).toEqual({
491 [olderTab.tabId]: olderTab,
492 [snapshot.tabId]: snapshot,
493 });
494 });
495​
496 it("does not resurrect a tab from an event older than its close", () => {
497 const snapshot = makeSnapshot();
498 applyPreviewServerEvent(ref, {
499 type: "opened",
500 threadId: "thread-1",
501 tabId: snapshot.tabId,
502 createdAt: snapshot.updatedAt,
503 snapshot,
504 });
505 applyPreviewServerEvent(ref, {
506 type: "closed",
507 threadId: "thread-1",
508 tabId: snapshot.tabId,
509 createdAt: "2026-01-01T00:00:01.000Z",
510 });
511​
512 applyPreviewServerEventImpl(ref, {
513 type: "opened",
514 threadId: "thread-1",
515 tabId: snapshot.tabId,
516 createdAt: snapshot.updatedAt,
517 serverEpoch,
518 revision: 1,
519 snapshot,
520 });
521​
522 expect(readThreadPreviewState(ref).sessions).toEqual({});
523 });
524​
525 it("accepts a lower revision from a newly restarted server", () => {
526 const snapshot = makeSnapshot();
527 applyPreviewServerEventImpl(ref, {
528 type: "opened",
529 threadId: "thread-1",
530 tabId: snapshot.tabId,
531 createdAt: snapshot.updatedAt,
532 serverEpoch,
533 revision: 12,
534 snapshot,
535 });
536​
537 reconcilePreviewServerSessions(ref, {
538 sessions: [],
539 serverEpoch: "server-b",
540 revision: 0,
541 });
542​
543 const state = readThreadPreviewState(ref);
544 expect(state.sessions).toEqual({});
545 expect(state.serverEpoch).toBe("server-b");
546 expect(state.serverRevision).toBe(0);
547 });
548​
549 it("does not carry raw-tab state across a server restart", () => {
550 const previous = makeSnapshot({
551 navStatus: { _tag: "Success", url: "https://old.example", title: "Old" },
552 updatedAt: "2026-01-01T00:00:02.000Z",
553 });
554 applyPreviewServerEventImpl(ref, {
555 type: "opened",
556 threadId: "thread-1",
557 tabId: previous.tabId,
558 createdAt: previous.updatedAt,
559 serverEpoch,
560 revision: 12,
561 snapshot: previous,
562 });
563 beginPreviewSessionClose(ref, previous.tabId);
564 applyPreviewDesktopState(ref, previous.tabId, {
565 hasWebContents: true,
566 canGoBack: false,
567 canGoForward: false,
568 loading: false,
569 zoomFactor: 1,
570 pictureInPicture: false,
571 colorScheme: "system",
572 audioMuted: false,
573 audible: false,
574 controller: "none",
575 favicon: null,
576 });
577 const restarted = makeSnapshot({
578 navStatus: { _tag: "Success", url: "https://new.example", title: "New" },
579 updatedAt: "2026-01-01T00:00:01.000Z",
580 });
581 reconcilePreviewServerSessions(ref, {
582 sessions: [restarted],
583 serverEpoch: "server-b",
584 revision: 0,
585 });
586​
587 const state = readThreadPreviewState(ref);
588 expect(state.sessions[restarted.tabId]).toEqual(restarted);
589 expect(state.suppressedTabIds).toEqual(new Set());
590 expect(state.desktopByTabId).toEqual({});
591 expect(state.desktopOverlay).toBeNull();
592 });
593​
594 it("applyServerSnapshot null clears snapshot for a thread that had one", () => {
595 const snapshot = makeSnapshot();
596 applyPreviewServerSnapshot(ref, snapshot);
597 applyPreviewServerSnapshot(ref, null);
598 const state = readThreadPreviewState(ref);
599 expect(state.snapshot).toBeNull();
600 });
601​
602 it("does not replace a streamed snapshot with older SWR data", () => {
603 applyPreviewServerSnapshot(
604 ref,
605 makeSnapshot({
606 navStatus: { _tag: "Success", url: "http://localhost:5173/new", title: "New" },
607 updatedAt: "2026-01-01T00:00:02.000Z",
608 }),
609 );
610 applyPreviewServerSnapshot(
611 ref,
612 makeSnapshot({
613 navStatus: { _tag: "Success", url: "http://localhost:5173/old", title: "Old" },
614 updatedAt: "2026-01-01T00:00:01.000Z",
615 }),
616 );
617​
618 const state = readThreadPreviewState(ref);
619 expect(state.snapshot?.navStatus).toEqual({
620 _tag: "Success",
621 url: "http://localhost:5173/new",
622 title: "New",
623 });
624 });
625​
626 it("rememberUrl dedupes and caps at limit", () => {
627 for (let i = 0; i < __testing.RECENT_URL_LIMIT + 5; i += 1) {
628 rememberPreviewUrl(ref, `http://localhost:${5000 + i}/`);
629 }
630 const state = readThreadPreviewState(ref);
631 expect(state.recentlySeenUrls.length).toBeLessThanOrEqual(__testing.RECENT_URL_LIMIT);
632 expect(state.recentlySeenUrls[0]).toBe(
633 `http://localhost:${5000 + __testing.RECENT_URL_LIMIT + 4}/`,
634 );
635 });
636});