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