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

addBrowserSurface.test.ts

80 lines2.5 KB

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