@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 305 lines 10.4 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 { beforeEach, describe, expect, it, vi } from "vite-plus/test";
9
10 import * as previewRuntime from "./browser/previewRuntime";
11 import { type ClosedViewEntry, useClosedViewStore } from "./closedViewStore";
12 import { __setClientSettingsForTests } from "./hooks/useSettings";
13 import { readThreadPreviewState, resetPreviewStateForTests } from "./previewStateStore";
14 import { planNextReopen, type ReopenOwnerState, reopenClosedView } from "./reopenClosedView";
15 import {
16 hostRightPanel,
17 selectThreadRightPanelState,
18 type ThreadRightPanelState,
19 useRightPanelStore,
20 } from "./rightPanelStore";
21
22 const threadRef = {
23 environmentId: "local" as ScopedThreadRef["environmentId"],
24 threadId: "thread-1" as ScopedThreadRef["threadId"],
25 };
26 const snapshot: PreviewSessionSnapshot = {
27 threadId: threadRef.threadId,
28 tabId: "old-tab",
29 navStatus: { _tag: "Success", url: "https://example.com", title: "Example" },
30 canGoBack: false,
31 canGoForward: false,
32 viewport: { _tag: "freeform", width: 1200, height: 800 },
33 profileId: "work",
34 zoomFactor: 1.5,
35 colorScheme: "dark",
36 updatedAt: "2026-09-22T12:00:00.000Z",
37 };
38
39 beforeEach(() => {
40 __setClientSettingsForTests(DEFAULT_CLIENT_SETTINGS);
41 resetPreviewStateForTests();
42 useClosedViewStore.setState({ entries: [] });
43 useRightPanelStore.setState({ byThreadKey: {}, userActionRevisionByThreadKey: {} });
44 });
45
46 describe("reopenClosedView", () => {
47 it.each([
48 { relativePath: "src/app.ts", line: 18, folderTrail: undefined },
49 { relativePath: "apps", line: null, folderTrail: "apps/web/src" },
50 { relativePath: "", line: null, folderTrail: "apps/web/src" },
51 ])(
52 "restores the captured file or folder tab at '$relativePath'",
53 async ({ relativePath, line, folderTrail }) => {
54 const panels = useRightPanelStore.getState();
55 if (folderTrail === undefined) panels.openFile(threadRef, relativePath, line ?? undefined);
56 else {
57 panels.openFile(threadRef, folderTrail);
58 panels.navigateFolder(threadRef, `file:${folderTrail}`, relativePath, folderTrail);
59 }
60 const expected =
61 relativePath === ""
62 ? { kind: "files", id: "files", folderTrail }
63 : {
64 kind: "file",
65 id: `file:${relativePath}`,
66 relativePath,
67 revealLine: line,
68 revealRequestId: 1,
69 ...(folderTrail === undefined ? {} : { folderTrail }),
70 };
71 const before = selectThreadRightPanelState(
72 useRightPanelStore.getState().byThreadKey,
73 threadRef,
74 );
75 expect(before.surfaces).toEqual([expected]);
76 panels.closeSurface(threadRef, expected.id);
77 expect(
78 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, threadRef).surfaces,
79 ).toEqual([]);
80 const entries = useClosedViewStore.getState().entries;
81 expect(entries).toEqual([
82 { id: expect.any(String), kind: "panel-tab", threadRef, surface: expected },
83 ]);
84 const openPreview = vi.fn();
85 const reopened = await reopenClosedView(entries[0]!, {
86 openPreview,
87 workspaceAvailable: true,
88 });
89 expect(reopened).toBe(true);
90 const state = selectThreadRightPanelState(
91 useRightPanelStore.getState().byThreadKey,
92 threadRef,
93 );
94 expect(state.isOpen).toBe(true);
95 expect(state.activeSurfaceId).toBe(expected.id);
96 expect(state.surfaces).toEqual([expected]);
97 expect(openPreview).not.toHaveBeenCalled();
98 },
99 );
100
101 it.each([
102 { hosted: false, folderTrail: "packages/contracts/src" },
103 { hosted: false, folderTrail: undefined },
104 { hosted: true, folderTrail: "packages/contracts/src" },
105 { hosted: true, folderTrail: undefined },
106 ])(
107 "preserves a hidden root tab with trail $folderTrail (hosted: $hosted)",
108 async ({ hosted, folderTrail }) => {
109 const panels = useRightPanelStore.getState();
110 panels.openFile(threadRef, "apps/web/src");
111 panels.navigateFolder(threadRef, "file:apps/web/src", "", "apps/web/src");
112 panels.closeSurface(threadRef, "files");
113 const entry = useClosedViewStore.getState().entries[0]!;
114 const hostRef = hosted
115 ? { ...threadRef, threadId: "host" as ScopedThreadRef["threadId"] }
116 : threadRef;
117 const unhost = hosted ? hostRightPanel(threadRef, hostRef) : undefined;
118 try {
119 if (folderTrail === undefined) panels.open(threadRef, "files");
120 else {
121 panels.openFile(threadRef, folderTrail);
122 panels.navigateFolder(threadRef, `file:${folderTrail}`, "", folderTrail);
123 }
124 panels.close(threadRef);
125 const before = selectThreadRightPanelState(
126 useRightPanelStore.getState().byThreadKey,
127 hostRef,
128 );
129 expect(before.isOpen).toBe(false);
130 const plan = planNextReopen([entry], () => ({
131 environmentKnown: true,
132 catalogReady: true,
133 ownerExists: true,
134 sideChatExists: true,
135 shellLive: true,
136 panel: before,
137 }));
138 expect(plan.restore).toBe(entry);
139 const openPreview = vi.fn();
140 expect(
141 await reopenClosedView(plan.restore!, { openPreview, workspaceAvailable: true }),
142 ).toBe(true);
143 const after = selectThreadRightPanelState(
144 useRightPanelStore.getState().byThreadKey,
145 hostRef,
146 );
147 expect(after.isOpen).toBe(true);
148 expect(after.activeSurfaceId).toBe("files");
149 expect(after.surfaces).toEqual([
150 { kind: "files", id: "files", ...(folderTrail === undefined ? {} : { folderTrail }) },
151 ]);
152 expect(openPreview).not.toHaveBeenCalled();
153 } finally {
154 unhost?.();
155 }
156 },
157 );
158
159 it("does not reopen workspace tabs without an available project", async () => {
160 const options = {
161 openPreview: vi.fn(),
162 workspaceAvailable: false,
163 };
164 for (const surface of [
165 { kind: "files", id: "files", folderTrail: "apps/web/src" },
166 {
167 kind: "file",
168 id: "file:src/app.ts",
169 relativePath: "src/app.ts",
170 revealLine: null,
171 revealRequestId: 0,
172 },
173 {
174 kind: "file",
175 id: "file:apps",
176 relativePath: "apps",
177 revealLine: null,
178 revealRequestId: 1,
179 folderTrail: "apps/web/src",
180 },
181 ] as const) {
182 expect(await reopenClosedView({ kind: "panel-tab", threadRef, surface }, options)).toBe(
183 false,
184 );
185 }
186 expect(
187 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, threadRef).surfaces,
188 ).toEqual([]);
189 });
190
191 it("recreates a server browser tab with its saved URL, viewport, profile, zoom and appearance, then selects its new ID", async () => {
192 using _runtime = vi.spyOn(previewRuntime, "previewRuntimeFor").mockReturnValue("server");
193 const openPreview = vi.fn(async () => AsyncResult.success({ ...snapshot, tabId: "new-tab" }));
194 const result = await reopenClosedView(
195 { kind: "browser", threadRef, snapshot },
196 { openPreview, workspaceAvailable: false },
197 );
198 expect(result).toBe(true);
199 expect(openPreview).toHaveBeenCalledWith({
200 environmentId: threadRef.environmentId,
201 input: {
202 threadId: threadRef.threadId,
203 url: "https://example.com",
204 viewport: snapshot.viewport,
205 profileId: "work",
206 runtime: "server",
207 zoomFactor: 1.5,
208 colorScheme: "dark",
209 },
210 });
211 expect(readThreadPreviewState(threadRef).snapshot?.tabId).toBe("new-tab");
212 expect(
213 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, threadRef)
214 .activeSurfaceId,
215 ).toBe("browser:new-tab");
216 });
217
218 it("leaves the panel unchanged when a browser resource fails to reopen", async () => {
219 const result = await reopenClosedView(
220 { kind: "browser", threadRef, snapshot },
221 {
222 openPreview: async () => AsyncResult.failure(Cause.fail(new Error("offline"))),
223 workspaceAvailable: false,
224 },
225 );
226 expect(result).toBe(false);
227 expect(
228 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, threadRef).surfaces,
229 ).toEqual([]);
230 });
231 });
232
233 describe("planNextReopen", () => {
234 const entries: ClosedViewEntry[] = [
235 { id: "a", kind: "panel-tab", threadRef, surface: { kind: "diff", id: "diff" } },
236 { id: "b", kind: "panel-tab", threadRef, surface: { kind: "files", id: "files" } },
237 ];
238 const known: ReopenOwnerState = {
239 environmentKnown: true,
240 catalogReady: true,
241 ownerExists: true,
242 sideChatExists: true,
243 shellLive: true,
244 panel: { isOpen: false, activeSurfaceId: null, surfaces: [] },
245 };
246 const openDiff: ThreadRightPanelState = {
247 isOpen: true,
248 activeSurfaceId: "diff",
249 surfaces: [{ kind: "diff", id: "diff" }],
250 };
251
252 it.each([
253 ["restores the newest entry", [known, known], [], "a"],
254 [
255 "drops a removed environment once the catalog is ready",
256 [{ ...known, environmentKnown: false }, known],
257 ["a"],
258 "b",
259 ],
260 [
261 "waits for the catalog before dropping",
262 [{ ...known, environmentKnown: false, catalogReady: false }, known],
263 [],
264 null,
265 ],
266 [
267 "skips but keeps a thread deleted from a live shell",
268 [{ ...known, ownerExists: false }, known],
269 [],
270 "b",
271 ],
272 [
273 "skips but keeps a side chat deleted from a live shell",
274 [{ ...known, sideChatExists: false }, known],
275 [],
276 "b",
277 ],
278 [
279 "waits when a cached shell lacks the side chat",
280 [{ ...known, sideChatExists: false, shellLive: false }, known],
281 [],
282 null,
283 ],
284 [
285 "waits when a cached shell lacks the thread",
286 [{ ...known, ownerExists: false, shellLive: false }, known],
287 [],
288 null,
289 ],
290 ["drops a tab that is already open", [{ ...known, panel: openDiff }, known], ["a"], "b"],
291 [
292 "restores a tab whose panel is hidden",
293 [{ ...known, panel: { ...openDiff, isOpen: false } }],
294 [],
295 "a",
296 ],
297 ] as const)("%s", (_name, owners, dropped, restored) => {
298 const plan = planNextReopen(
299 entries.slice(0, owners.length),
300 (e) => owners[entries.indexOf(e)]!,
301 );
302 expect(plan.drop.map((e) => e.id)).toEqual(dropped);
303 expect(plan.restore?.id ?? null).toBe(restored);
304 });
305 });