Open History to see the last commit
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 resetPreviewStateForTests,
21 setActivePreviewTab,
22} from "./previewStateStore";
23import { appAtomRegistry } from "./rpc/atomRegistry";
24
25const environmentId = "env-1" as EnvironmentId;
26const ref = scopeThreadRef(environmentId, ThreadId.make("thread-1"));
27
28const makeSnapshot = (overrides: Partial<PreviewSessionSnapshot> = {}): PreviewSessionSnapshot => ({
29 threadId: "thread-1",
30 tabId: "tab_a",
31 navStatus: { _tag: "Loading", url: "http://localhost:5173/", title: "" },
32 canGoBack: false,
33 canGoForward: false,
34 updatedAt: "2026-01-01T00:00:00.000Z",
35 ...overrides,
36});
37
38type PreviewEventDraft = PreviewEvent extends infer Event
39 ? Event extends { readonly revision: number }
40 ? Omit<Event, "revision" | "serverEpoch">
41 : never
42 : never;
43
44const serverEpoch = "server-a";
45let nextServerRevision = 0;
46const applyPreviewServerEvent = (eventRef: typeof ref, event: PreviewEventDraft): void => {
47 nextServerRevision += 1;
48 applyPreviewServerEventImpl(eventRef, {
49 ...event,
50 serverEpoch,
51 revision: nextServerRevision,
52 } as PreviewEvent);
53};
54
55beforeEach(() => {
56 nextServerRevision = 0;
57 resetPreviewStateForTests();
58});
59
60describe("previewStateStore (single-tab)", () => {
61 it("resized event updates tab viewport without changing the active tab", () => {
62 const active = makeSnapshot({ tabId: "tab_a" });
63 const background = makeSnapshot({ tabId: "tab_b" });
64 applyPreviewServerSnapshot(ref, background);
65 applyPreviewServerSnapshot(ref, active);
66
67 applyPreviewServerEvent(ref, {
68 type: "resized",
69 threadId: "thread-1",
70 tabId: background.tabId,
71 createdAt: "2026-01-01T00:00:01.000Z",
72 snapshot: {
73 ...background,
74 viewport: { _tag: "preset", presetId: "pixel-8", width: 412, height: 915 },
75 updatedAt: "2026-01-01T00:00:01.000Z",
76 },
77 });
78
79 const state = readThreadPreviewState(ref);
80 expect(state.activeTabId).toBe(active.tabId);
81 expect(state.sessions[background.tabId]?.viewport).toEqual({
82 _tag: "preset",
83 presetId: "pixel-8",
84 width: 412,
85 height: 915,
86 });
87 });
88
89 it("failed event for a non-active tab is ignored", () => {
90 const snapshot = makeSnapshot({ tabId: "tab_a" });
91 applyPreviewServerEvent(ref, {
92 type: "opened",
93 threadId: "thread-1",
94 tabId: snapshot.tabId,
95 createdAt: snapshot.updatedAt,
96 snapshot,
97 });
98 applyPreviewServerEvent(ref, {
99 type: "failed",
100 threadId: "thread-1",
101 tabId: "tab_b",
102 createdAt: "2026-01-01T00:00:01.000Z",
103 url: "http://localhost:9999/",
104 title: "",
105 code: -105,
106 description: "ERR_NAME_NOT_RESOLVED",
107 });
108 const state = readThreadPreviewState(ref);
109 expect(state.snapshot?.navStatus._tag).toBe("Loading");
110 });
111
112 it("optimistically removes a session before the server close event arrives", () => {
113 const first = makeSnapshot({ tabId: "tab_a" });
114 const second = makeSnapshot({
115 tabId: "tab_b",
116 updatedAt: "2026-01-01T00:00:01.000Z",
117 });
118 applyPreviewServerSnapshot(ref, first);
119 applyPreviewServerSnapshot(ref, second);
120
121 beginPreviewSessionClose(ref, second.tabId);
122
123 const state = readThreadPreviewState(ref);
124 expect(Object.keys(state.sessions)).toEqual([first.tabId]);
125 expect(state.activeTabId).toBe(first.tabId);
126 expect(state.snapshot?.tabId).toBe(first.tabId);
127 });
128
129 it("treats a late server close event after optimistic removal as a no-op", () => {
130 const snapshot = makeSnapshot();
131 applyPreviewServerSnapshot(ref, snapshot);
132 beginPreviewSessionClose(ref, snapshot.tabId);
133
134 applyPreviewServerEvent(ref, {
135 type: "closed",
136 threadId: "thread-1",
137 tabId: snapshot.tabId,
138 createdAt: "2026-01-01T00:00:01.000Z",
139 });
140
141 const state = readThreadPreviewState(ref);
142 expect(state.sessions).toEqual({});
143 expect(state.snapshot).toBeNull();
144 });
145
146 it("does not resurrect an intentionally closed tab from a stale list snapshot", () => {
147 const snapshot = makeSnapshot();
148 applyPreviewServerSnapshot(ref, snapshot);
149 beginPreviewSessionClose(ref, snapshot.tabId);
150
151 applyPreviewServerSnapshot(ref, snapshot);
152
153 const state = readThreadPreviewState(ref);
154 expect(state.sessions).toEqual({});
155 expect(state.snapshot).toBeNull();
156 });
157
158 it("can restore a suppressed tab after a failed close", () => {
159 const snapshot = makeSnapshot();
160 applyPreviewServerSnapshot(ref, snapshot);
161 beginPreviewSessionClose(ref, snapshot.tabId);
162
163 cancelPreviewSessionClose(ref, snapshot, snapshot.tabId);
164
165 const state = readThreadPreviewState(ref);
166 expect(state.sessions).toEqual({ [snapshot.tabId]: snapshot });
167 expect(state.snapshot).toEqual(snapshot);
168 });
169
170 it("does not publish duplicate desktop browser state", () => {
171 const snapshot = makeSnapshot();
172 applyPreviewServerSnapshot(ref, snapshot);
173 const overlay = {
174 hasWebContents: true,
175 navStatus: { _tag: "Idle" as const },
176 canGoBack: true,
177 canGoForward: false,
178 loading: false,
179 zoomFactor: 1,
180 pictureInPicture: false,
181 colorScheme: "system" as const,
182 audioMuted: false,
183 audible: false,
184 controller: "none" as const,
185 viewerSize: null,
186 favicon: {
187 dataUrl: "data:image/png;base64,AA==",
188 pageUrl: "https://example.com",
189 capturedAt: 1,
190 },
191 };
192 let updateCount = 0;
193 const unsubscribe = appAtomRegistry.subscribe(previewStateAtom(scopedThreadKey(ref)), () => {
194 updateCount += 1;
195 });
196
197 applyPreviewDesktopState(ref, snapshot.tabId, overlay);
198 applyPreviewDesktopState(ref, snapshot.tabId, { ...overlay, favicon: { ...overlay.favicon } });
199 applyPreviewDesktopState(ref, snapshot.tabId, {
200 ...overlay,
201 viewerSize: { width: 1200, height: 900 },
202 });
203 applyPreviewDesktopState(ref, snapshot.tabId, {
204 ...overlay,
205 viewerSize: { width: 1200, height: 900 },
206 });
207 const loaded = {
208 ...overlay,
209 navStatus: { _tag: "Success" as const, url: "https://example.com/", title: "Example" },
210 };
211 applyPreviewDesktopState(ref, snapshot.tabId, loaded);
212 applyPreviewDesktopState(ref, snapshot.tabId, {
213 ...loaded,
214 navStatus: { ...loaded.navStatus },
215 });
216 applyPreviewDesktopState(ref, snapshot.tabId, {
217 ...loaded,
218 navStatus: { ...loaded.navStatus, url: "https://example.com/next" },
219 });
220 unsubscribe();
221
222 expect(updateCount).toBe(4);
223 });
224
225 it("retains multiple tabs and switches active desktop state", () => {
226 const first = makeSnapshot();
227 const second = { ...makeSnapshot(), tabId: "tab_2", updatedAt: "2026-01-02T00:00:00.000Z" };
228 applyPreviewServerSnapshot(ref, first);
229 applyPreviewServerSnapshot(ref, second);
230 applyPreviewDesktopState(ref, first.tabId, {
231 hasWebContents: true,
232 navStatus: { _tag: "Idle" as const },
233 canGoBack: true,
234 canGoForward: false,
235 loading: false,
236 zoomFactor: 1,
237 pictureInPicture: false,
238 colorScheme: "system",
239 audioMuted: false,
240 audible: false,
241 controller: "none",
242 viewerSize: null,
243 favicon: null,
244 });
245 setActivePreviewTab(ref, first.tabId);
246
247 const state = readThreadPreviewState(ref);
248 expect(Object.keys(state.sessions)).toEqual([first.tabId, second.tabId]);
249 expect(state.snapshot?.tabId).toBe(first.tabId);
250 expect(state.desktopOverlay?.canGoBack).toBe(true);
251 });
252
253 it("reconciles an authoritative session list without focusing a background tab", () => {
254 const active = makeSnapshot({ tabId: "tab_a" });
255 const stale = makeSnapshot({
256 tabId: "tab_stale",
257 updatedAt: "2026-01-01T00:00:01.000Z",
258 });
259 applyPreviewServerSnapshot(ref, stale);
260 applyPreviewServerSnapshot(ref, active);
261 applyPreviewDesktopState(ref, stale.tabId, {
262 hasWebContents: true,
263 navStatus: { _tag: "Idle" as const },
264 canGoBack: false,
265 canGoForward: false,
266 loading: false,
267 zoomFactor: 1,
268 pictureInPicture: false,
269 colorScheme: "system",
270 audioMuted: false,
271 audible: false,
272 controller: "none",
273 viewerSize: null,
274 favicon: null,
275 });
276
277 reconcilePreviewServerSessions(ref, { sessions: [active], serverEpoch, revision: 1 });
278
279 const state = readThreadPreviewState(ref);
280 expect(Object.keys(state.sessions)).toEqual([active.tabId]);
281 expect(state.activeTabId).toBe(active.tabId);
282 expect(state.snapshot).toEqual(active);
283 expect(state.desktopByTabId[stale.tabId]).toBeUndefined();
284 });
285
286 it("keeps the state object when a refetched list changes nothing", () => {
287 reconcilePreviewServerSessions(ref, { sessions: [], serverEpoch, revision: 1 });
288 const loaded = readThreadPreviewState(ref);
289
290 reconcilePreviewServerSessions(ref, { sessions: [], serverEpoch, revision: 1 });
291 expect(readThreadPreviewState(ref)).toBe(loaded);
292
293 const snapshot = makeSnapshot();
294 reconcilePreviewServerSessions(ref, { sessions: [snapshot], serverEpoch, revision: 2 });
295 expect(readThreadPreviewState(ref).sessions).toEqual({ [snapshot.tabId]: snapshot });
296 });
297
298 it("ignores a list response older than the latest server event", () => {
299 const snapshot = makeSnapshot();
300 applyPreviewServerEvent(ref, {
301 type: "opened",
302 threadId: "thread-1",
303 tabId: snapshot.tabId,
304 createdAt: snapshot.updatedAt,
305 snapshot,
306 });
307
308 reconcilePreviewServerSessions(ref, { sessions: [], serverEpoch, revision: 0 });
309
310 expect(readThreadPreviewState(ref).sessions).toEqual({ [snapshot.tabId]: snapshot });
311 expect(readThreadPreviewState(ref).listLoaded).toBe(false);
312
313 const olderTab = makeSnapshot({ tabId: "tab_before_event" });
314 reconcilePreviewServerSessions(ref, {
315 sessions: [olderTab, snapshot],
316 serverEpoch,
317 revision: 2,
318 });
319 expect(readThreadPreviewState(ref).listLoaded).toBe(true);
320 expect(readThreadPreviewState(ref).sessions).toEqual({
321 [olderTab.tabId]: olderTab,
322 [snapshot.tabId]: snapshot,
323 });
324 });
325
326 it("does not resurrect a tab from an event older than its close", () => {
327 const snapshot = makeSnapshot();
328 applyPreviewServerEvent(ref, {
329 type: "opened",
330 threadId: "thread-1",
331 tabId: snapshot.tabId,
332 createdAt: snapshot.updatedAt,
333 snapshot,
334 });
335 applyPreviewServerEvent(ref, {
336 type: "closed",
337 threadId: "thread-1",
338 tabId: snapshot.tabId,
339 createdAt: "2026-01-01T00:00:01.000Z",
340 });
341
342 applyPreviewServerEventImpl(ref, {
343 type: "opened",
344 threadId: "thread-1",
345 tabId: snapshot.tabId,
346 createdAt: snapshot.updatedAt,
347 serverEpoch,
348 revision: 1,
349 snapshot,
350 });
351
352 expect(readThreadPreviewState(ref).sessions).toEqual({});
353 });
354
355 it("accepts a lower revision from a newly restarted server", () => {
356 const snapshot = makeSnapshot();
357 applyPreviewServerEventImpl(ref, {
358 type: "opened",
359 threadId: "thread-1",
360 tabId: snapshot.tabId,
361 createdAt: snapshot.updatedAt,
362 serverEpoch,
363 revision: 12,
364 snapshot,
365 });
366
367 reconcilePreviewServerSessions(ref, {
368 sessions: [],
369 serverEpoch: "server-b",
370 revision: 0,
371 });
372
373 const state = readThreadPreviewState(ref);
374 expect(state.sessions).toEqual({});
375 expect(state.serverEpoch).toBe("server-b");
376 expect(state.serverRevision).toBe(0);
377 });
378
379 it("does not carry raw-tab state across a server restart", () => {
380 const previous = makeSnapshot({
381 navStatus: { _tag: "Success", url: "https://old.example", title: "Old" },
382 updatedAt: "2026-01-01T00:00:02.000Z",
383 });
384 applyPreviewServerEventImpl(ref, {
385 type: "opened",
386 threadId: "thread-1",
387 tabId: previous.tabId,
388 createdAt: previous.updatedAt,
389 serverEpoch,
390 revision: 12,
391 snapshot: previous,
392 });
393 beginPreviewSessionClose(ref, previous.tabId);
394 applyPreviewDesktopState(ref, previous.tabId, {
395 hasWebContents: true,
396 navStatus: { _tag: "Idle" as const },
397 canGoBack: false,
398 canGoForward: false,
399 loading: false,
400 zoomFactor: 1,
401 pictureInPicture: false,
402 colorScheme: "system",
403 audioMuted: false,
404 audible: false,
405 controller: "none",
406 viewerSize: null,
407 favicon: null,
408 });
409 const restarted = makeSnapshot({
410 navStatus: { _tag: "Success", url: "https://new.example", title: "New" },
411 updatedAt: "2026-01-01T00:00:01.000Z",
412 });
413 reconcilePreviewServerSessions(ref, {
414 sessions: [restarted],
415 serverEpoch: "server-b",
416 revision: 0,
417 });
418
419 const state = readThreadPreviewState(ref);
420 expect(state.sessions[restarted.tabId]).toEqual(restarted);
421 expect(state.suppressedTabIds).toEqual(new Set());
422 expect(state.desktopByTabId).toEqual({});
423 expect(state.desktopOverlay).toBeNull();
424 });
425
426 it("does not replace a streamed snapshot with older SWR data", () => {
427 applyPreviewServerSnapshot(
428 ref,
429 makeSnapshot({
430 navStatus: { _tag: "Success", url: "http://localhost:5173/new", title: "New" },
431 updatedAt: "2026-01-01T00:00:02.000Z",
432 }),
433 );
434 applyPreviewServerSnapshot(
435 ref,
436 makeSnapshot({
437 navStatus: { _tag: "Success", url: "http://localhost:5173/old", title: "Old" },
438 updatedAt: "2026-01-01T00:00:01.000Z",
439 }),
440 );
441
442 const state = readThreadPreviewState(ref);
443 expect(state.snapshot?.navStatus).toEqual({
444 _tag: "Success",
445 url: "http://localhost:5173/new",
446 title: "New",
447 });
448 });
449});