The best way to control Claude Code, Codex and more from any computer or phone

main
ts 202 lines 7.08 KB
Raw
1 import {
2 DEFAULT_CLIENT_SETTINGS,
3 type PreviewSessionSnapshot,
4 type ScopedThreadRef,
5 } from "@t3tools/contracts";
6 import * as Cause from "effect/Cause";
7 import { AsyncResult } from "effect/reactivity";
8 import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
9
10 import * as browserDefaults from "~/browser/browserDefaults";
11 import {
12 BrowserSettingsReadError,
13 openFileInPreview,
14 openUrlInPreview,
15 } from "~/browser/openFileInPreview";
16 import * as previewRuntime from "~/browser/previewRuntime";
17 import { __setClientSettingsForTests } from "~/hooks/useSettings";
18 import {
19 hostRightPanel,
20 selectActiveRightPanelSurface,
21 useRightPanelStore,
22 } from "~/rightPanelStore";
23 import {
24 applyPreviewServerSnapshot,
25 readThreadPreviewState,
26 resetPreviewStateForTests,
27 setActivePreviewTab,
28 } from "~/previewStateStore";
29
30 import { openPreviewSession } from "./openPreviewSession";
31
32 const threadRef = {
33 environmentId: "local" as ScopedThreadRef["environmentId"],
34 threadId: "thread-1" as ScopedThreadRef["threadId"],
35 };
36
37 const 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
50 beforeEach(() => {
51 resetPreviewStateForTests();
52 useRightPanelStore.setState({ byThreadKey: {}, threadPanelVisibilityByThreadKey: {} });
53 __setClientSettingsForTests(DEFAULT_CLIENT_SETTINGS);
54 });
55
56 afterEach(() => {
57 vi.restoreAllMocks();
58 });
59
60 describe("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 it.each(["new tab", "link"] as const)(
102 "opens a server tab from a %s with the default zoom and appearance",
103 async (entryPoint) => {
104 __setClientSettingsForTests({
105 ...DEFAULT_CLIENT_SETTINGS,
106 browserDefaultZoomFactor: 0.8,
107 browserDefaultAppearance: "dark",
108 });
109 const openPreview = vi.fn(async () => AsyncResult.success(snapshot));
110
111 if (entryPoint === "link") {
112 vi.spyOn(previewRuntime, "previewRuntimeFor").mockReturnValue("server");
113 await openUrlInPreview({ openPreview, threadRef, url: "https://t3.chat/" });
114 } else {
115 await openPreviewSession({ openPreview, threadRef, runtime: "server" });
116 }
117
118 expect(openPreview).toHaveBeenCalledExactlyOnceWith({
119 environmentId: threadRef.environmentId,
120 input: expect.objectContaining({ runtime: "server", zoomFactor: 0.8, colorScheme: "dark" }),
121 });
122 },
123 );
124 });
125
126 describe("openUrlInPreview from a link", () => {
127 it.each(["link", "file"] as const)(
128 "opens a side chat's %s in the parent's browser panel after the pane unmounts",
129 async (entryPoint) => {
130 const sideChatRef = { ...threadRef, threadId: "side-chat" as ScopedThreadRef["threadId"] };
131 const unhost = hostRightPanel(sideChatRef, threadRef);
132 const openPreview = vi.fn(async () => AsyncResult.success(snapshot));
133 vi.spyOn(previewRuntime, "isPreviewAvailableFor").mockReturnValue(true);
134 vi.spyOn(browserDefaults, "resolveBrowserDefaults").mockImplementation(async () => {
135 unhost();
136 return browserDefaults.getBrowserDefaults();
137 });
138
139 if (entryPoint === "link") {
140 await openUrlInPreview({ openPreview, threadRef: sideChatRef, url: "https://t3.chat/" });
141 } else {
142 await openFileInPreview({
143 threadRef: sideChatRef,
144 filePath: "/repo/index.html",
145 workspaceRoot: "/repo",
146 httpBaseUrl: "http://localhost:3000",
147 createAssetUrl: async () => {
148 unhost();
149 return AsyncResult.success({ relativeUrl: "/assets/index.html", expiresAt: 0 });
150 },
151 openPreview,
152 });
153 }
154
155 expect(openPreview).toHaveBeenCalledWith(
156 expect.objectContaining({
157 input: expect.objectContaining({ threadId: threadRef.threadId }),
158 }),
159 );
160 expect(readThreadPreviewState(threadRef).activeTabId).toBe(snapshot.tabId);
161 expect(readThreadPreviewState(sideChatRef).sessions).toEqual({});
162 expect(
163 selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, threadRef)?.kind,
164 ).toBe("preview");
165 },
166 );
167
168 // regression: #16961 a background link open switched the active preview tab
169 it("keeps the current tab active for a background open", async () => {
170 applyPreviewServerSnapshot(threadRef, { ...snapshot, tabId: "tab-current" });
171
172 await openUrlInPreview({
173 openPreview: async () => AsyncResult.success(snapshot),
174 threadRef,
175 url: "https://t3.chat/",
176 background: true,
177 });
178
179 const state = readThreadPreviewState(threadRef);
180 expect(state.activeTabId).toBe("tab-current");
181 expect(Object.keys(state.sessions).toSorted()).toEqual(["tab-1", "tab-current"]);
182 });
183
184 it("keeps a tab the user picked while a background open was in flight", async () => {
185 applyPreviewServerSnapshot(threadRef, { ...snapshot, tabId: "tab-current" });
186 applyPreviewServerSnapshot(threadRef, { ...snapshot, tabId: "tab-other" });
187 setActivePreviewTab(threadRef, "tab-current");
188
189 await openUrlInPreview({
190 openPreview: async () => {
191 applyPreviewServerSnapshot(threadRef, snapshot);
192 setActivePreviewTab(threadRef, "tab-other");
193 return AsyncResult.success(snapshot);
194 },
195 threadRef,
196 url: "https://t3.chat/",
197 background: true,
198 });
199
200 expect(readThreadPreviewState(threadRef).activeTabId).toBe("tab-other");
201 });
202 });