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

openPreviewSession.test.ts

178 lines6.2 KB

1import {
2 DEFAULT_CLIENT_SETTINGS,
3 type PreviewSessionSnapshot,
4 type ScopedThreadRef,
5} from "@t3tools/contracts";
6import * as Cause from "effect/Cause";
7import { AsyncResult } from "effect/reactivity";
8import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
9​
10import * as browserDefaults from "~/browser/browserDefaults";
11import {
12 BrowserSettingsReadError,
13 openFileInPreview,
14 openUrlInPreview,
15} from "~/browser/openFileInPreview";
16import * as previewRuntime from "~/browser/previewRuntime";
17import { __setClientSettingsForTests } from "~/hooks/useSettings";
18import {
19 hostRightPanel,
20 selectActiveRightPanelSurface,
21 useRightPanelStore,
22} from "~/rightPanelStore";
23import {
24 applyPreviewServerSnapshot,
25 readThreadPreviewState,
26 resetPreviewStateForTests,
27 setActivePreviewTab,
28} from "~/previewStateStore";
29​
30import { openPreviewSession } from "./openPreviewSession";
31​
32const threadRef = {
33 environmentId: "local" as ScopedThreadRef["environmentId"],
34 threadId: "thread-1" as ScopedThreadRef["threadId"],
35};
36​
37const snapshot: PreviewSessionSnapshot = {
38 threadId: threadRef.threadId,
39 tabId: "tab-1",
40 navStatus: {
41 _tag: "Loading",
42 url: "https://t3.chat/",
43 title: "",
44 },
45 canGoBack: false,
46 canGoForward: false,
47 updatedAt: "2026-06-11T23:00:00.000Z",
48};
49​
50beforeEach(() => {
51 resetPreviewStateForTests();
52 useRightPanelStore.setState({ byThreadKey: {}, threadPanelVisibilityByThreadKey: {} });
53 __setClientSettingsForTests(DEFAULT_CLIENT_SETTINGS);
54});
55​
56afterEach(() => {
57 vi.restoreAllMocks();
58});
59​
60describe("openPreviewSession", () => {
61 it.each(["session", "link"] as const)(
62 "does not open a %s with unread settings and uses the saved profile on retry",
63 async (entryPoint) => {
64 const failure = new Error("Settings read failed");
65 vi.spyOn(browserDefaults, "resolveBrowserDefaults").mockRejectedValueOnce(failure);
66 const viewport = { _tag: "freeform", width: 1280, height: 720 } as const;
67 __setClientSettingsForTests({
68 ...DEFAULT_CLIENT_SETTINGS,
69 browserDefaultViewport: viewport,
70 browserDefaultProfileId: "work",
71 browserProfiles: [{ id: "work", name: "Work", kind: "persistent" }],
72 });
73 const openPreview = vi.fn(async () => AsyncResult.success(snapshot));
74 const input = { openPreview, threadRef, url: "https://t3.chat/" };
75 const open = entryPoint === "session" ? openPreviewSession : openUrlInPreview;
76​
77 const result = await open(input);
78​
79 expect(result._tag).toBe("Failure");
80 if (result._tag === "Failure") {
81 expect(Cause.squash(result.cause)).toBeInstanceOf(BrowserSettingsReadError);
82 expect(Cause.squash(result.cause)).toMatchObject({ cause: failure });
83 }
84 expect(openPreview).not.toHaveBeenCalled();
85 expect(readThreadPreviewState(threadRef).snapshot).toBeNull();
86 expect(readThreadPreviewState(threadRef).recentlySeenUrls).toEqual([]);
87​
88 await expect(open(input)).resolves.toMatchObject({ _tag: "Success" });
89 expect(openPreview).toHaveBeenCalledExactlyOnceWith({
90 environmentId: threadRef.environmentId,
91 input: {
92 threadId: threadRef.threadId,
93 url: input.url,
94 viewport,
95 profileId: "work",
96 },
97 });
98 },
99 );
100});
101​
102describe("openUrlInPreview from a link", () => {
103 it.each(["link", "file"] as const)(
104 "opens a side chat's %s in the parent's browser panel after the pane unmounts",
105 async (entryPoint) => {
106 const sideChatRef = { ...threadRef, threadId: "side-chat" as ScopedThreadRef["threadId"] };
107 const unhost = hostRightPanel(sideChatRef, threadRef);
108 const openPreview = vi.fn(async () => AsyncResult.success(snapshot));
109 vi.spyOn(previewRuntime, "isPreviewAvailableFor").mockReturnValue(true);
110 vi.spyOn(browserDefaults, "resolveBrowserDefaults").mockImplementation(async () => {
111 unhost();
112 return browserDefaults.getBrowserDefaults();
113 });
114​
115 if (entryPoint === "link") {
116 await openUrlInPreview({ openPreview, threadRef: sideChatRef, url: "https://t3.chat/" });
117 } else {
118 await openFileInPreview({
119 threadRef: sideChatRef,
120 filePath: "/repo/index.html",
121 workspaceRoot: "/repo",
122 httpBaseUrl: "http://localhost:3000",
123 createAssetUrl: async () => {
124 unhost();
125 return AsyncResult.success({ relativeUrl: "/assets/index.html", expiresAt: 0 });
126 },
127 openPreview,
128 });
129 }
130​
131 expect(openPreview).toHaveBeenCalledWith(
132 expect.objectContaining({
133 input: expect.objectContaining({ threadId: threadRef.threadId }),
134 }),
135 );
136 expect(readThreadPreviewState(threadRef).activeTabId).toBe(snapshot.tabId);
137 expect(readThreadPreviewState(sideChatRef).sessions).toEqual({});
138 expect(
139 selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, threadRef)?.kind,
140 ).toBe("preview");
141 },
142 );
143​
144 // regression: #16961 a background link open switched the active preview tab
145 it("keeps the current tab active for a background open", async () => {
146 applyPreviewServerSnapshot(threadRef, { ...snapshot, tabId: "tab-current" });
147​
148 await openUrlInPreview({
149 openPreview: async () => AsyncResult.success(snapshot),
150 threadRef,
151 url: "https://t3.chat/",
152 background: true,
153 });
154​
155 const state = readThreadPreviewState(threadRef);
156 expect(state.activeTabId).toBe("tab-current");
157 expect(Object.keys(state.sessions).toSorted()).toEqual(["tab-1", "tab-current"]);
158 });
159​
160 it("keeps a tab the user picked while a background open was in flight", async () => {
161 applyPreviewServerSnapshot(threadRef, { ...snapshot, tabId: "tab-current" });
162 applyPreviewServerSnapshot(threadRef, { ...snapshot, tabId: "tab-other" });
163 setActivePreviewTab(threadRef, "tab-current");
164​
165 await openUrlInPreview({
166 openPreview: async () => {
167 applyPreviewServerSnapshot(threadRef, snapshot);
168 setActivePreviewTab(threadRef, "tab-other");
169 return AsyncResult.success(snapshot);
170 },
171 threadRef,
172 url: "https://t3.chat/",
173 background: true,
174 });
175​
176 expect(readThreadPreviewState(threadRef).activeTabId).toBe("tab-other");
177 });
178});