Open History to see the last commit
1import {
2 FILL_PREVIEW_VIEWPORT,
3 type PreviewOpenInput,
4 type PreviewSessionSnapshot,
5 type ScopedThreadRef,
6} from "@t3tools/contracts";
7import { AsyncResult } from "effect/unstable/reactivity";
8import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
9
10import {
11 applyPreviewServerSnapshot,
12 readThreadPreviewState,
13 resetPreviewStateForTests,
14} from "~/previewStateStore";
15import { selectThreadRightPanelState, useRightPanelStore } from "~/rightPanelStore";
16
17import { addBrowserSurface } from "./addBrowserSurface";
18
19const threadRef = {
20 environmentId: "local" as ScopedThreadRef["environmentId"],
21 threadId: "thread-1" as ScopedThreadRef["threadId"],
22};
23
24const snapshot = (tabId: string): PreviewSessionSnapshot => ({
25 threadId: threadRef.threadId,
26 tabId,
27 navStatus: { _tag: "Idle" },
28 canGoBack: false,
29 canGoForward: false,
30 updatedAt: `2026-06-18T19:00:0${tabId.at(-1) ?? "0"}.000Z`,
31});
32
33beforeEach(() => {
34 resetPreviewStateForTests();
35 useRightPanelStore.setState({ byThreadKey: {} });
36});
37
38describe("addBrowserSurface", () => {
39 it("creates another preview session when a browser tab is already active", async () => {
40 const first = snapshot("tab-1");
41 const second = snapshot("tab-2");
42 applyPreviewServerSnapshot(threadRef, first);
43 useRightPanelStore.getState().openBrowser(threadRef, first.tabId);
44 const openPreview = vi.fn(async (_input: PreviewOpenInput) => AsyncResult.success(second));
45
46 await addBrowserSurface({ threadRef, openPreview: ({ input }) => openPreview(input) });
47
48 expect(openPreview).toHaveBeenCalledWith({
49 threadId: "thread-1",
50 viewport: FILL_PREVIEW_VIEWPORT,
51 });
52 expect(Object.keys(readThreadPreviewState(threadRef).sessions)).toEqual(["tab-1", "tab-2"]);
53 expect(
54 selectThreadRightPanelState(
55 useRightPanelStore.getState().byThreadKey,
56 threadRef,
57 ).surfaces.map((surface) => surface.id),
58 ).toEqual(["browser:tab-1", "browser:tab-2"]);
59 });
60});