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