Open History to see the last commit
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 { beforeEach, describe, expect, it, vi } from "vite-plus/test";
9
10import { type ClosedViewEntry, useClosedViewStore } from "./closedViewStore";
11import { __setClientSettingsForTests } from "./hooks/useSettings";
12import { readThreadPreviewState, resetPreviewStateForTests } from "./previewStateStore";
13import { planNextReopen, type ReopenOwnerState, reopenClosedView } from "./reopenClosedView";
14import {
15 selectThreadRightPanelState,
16 type ThreadRightPanelState,
17 useRightPanelStore,
18} from "./rightPanelStore";
19
20const threadRef = {
21 environmentId: "local" as ScopedThreadRef["environmentId"],
22 threadId: "thread-1" as ScopedThreadRef["threadId"],
23};
24const snapshot: PreviewSessionSnapshot = {
25 threadId: threadRef.threadId,
26 tabId: "old-tab",
27 navStatus: { _tag: "Success", url: "https://example.com", title: "Example" },
28 canGoBack: false,
29 canGoForward: false,
30 viewport: { _tag: "freeform", width: 1200, height: 800 },
31 profileId: "work",
32 updatedAt: "2026-09-22T12:00:00.000Z",
33};
34
35beforeEach(() => {
36 __setClientSettingsForTests(DEFAULT_CLIENT_SETTINGS);
37 resetPreviewStateForTests();
38 useClosedViewStore.setState({ entries: [] });
39 useRightPanelStore.setState({ byThreadKey: {}, userActionRevisionByThreadKey: {} });
40});
41
42describe("reopenClosedView", () => {
43 it("restores a file tab at its line without creating a browser session", async () => {
44 const openPreview = vi.fn();
45 const reopened = await reopenClosedView(
46 {
47 kind: "panel-tab",
48 threadRef,
49 surface: {
50 kind: "file",
51 id: "file:src/app.ts",
52 relativePath: "src/app.ts",
53 revealLine: 18,
54 revealRequestId: 1,
55 },
56 },
57 { openPreview, workspaceAvailable: true },
58 );
59 expect(reopened).toBe(true);
60 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, threadRef);
61 expect(state.isOpen).toBe(true);
62 expect(state.surfaces).toMatchObject([{ id: "file:src/app.ts", revealLine: 18 }]);
63 expect(openPreview).not.toHaveBeenCalled();
64 });
65
66 it("does not reopen workspace tabs without an available project", async () => {
67 const options = {
68 openPreview: vi.fn(),
69 workspaceAvailable: false,
70 };
71 for (const surface of [
72 { kind: "files", id: "files" },
73 {
74 kind: "file",
75 id: "file:src/app.ts",
76 relativePath: "src/app.ts",
77 revealLine: null,
78 revealRequestId: 0,
79 },
80 ] as const) {
81 expect(await reopenClosedView({ kind: "panel-tab", threadRef, surface }, options)).toBe(
82 false,
83 );
84 }
85 expect(
86 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, threadRef).surfaces,
87 ).toEqual([]);
88 });
89
90 it("recreates a browser tab with saved URL, viewport and profile, then selects its new ID", async () => {
91 const openPreview = vi.fn(async () => AsyncResult.success({ ...snapshot, tabId: "new-tab" }));
92 const result = await reopenClosedView(
93 { kind: "browser", threadRef, snapshot },
94 { openPreview, workspaceAvailable: false },
95 );
96 expect(result).toBe(true);
97 expect(openPreview).toHaveBeenCalledWith({
98 environmentId: threadRef.environmentId,
99 input: {
100 threadId: threadRef.threadId,
101 url: "https://example.com",
102 viewport: snapshot.viewport,
103 profileId: "work",
104 },
105 });
106 expect(readThreadPreviewState(threadRef).snapshot?.tabId).toBe("new-tab");
107 expect(
108 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, threadRef)
109 .activeSurfaceId,
110 ).toBe("browser:new-tab");
111 });
112
113 it("leaves the panel unchanged when a browser resource fails to reopen", async () => {
114 const result = await reopenClosedView(
115 { kind: "browser", threadRef, snapshot },
116 {
117 openPreview: async () => AsyncResult.failure(Cause.fail(new Error("offline"))),
118 workspaceAvailable: false,
119 },
120 );
121 expect(result).toBe(false);
122 expect(
123 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, threadRef).surfaces,
124 ).toEqual([]);
125 });
126});
127
128describe("planNextReopen", () => {
129 const entries: ClosedViewEntry[] = [
130 { id: "a", kind: "panel-tab", threadRef, surface: { kind: "diff", id: "diff" } },
131 { id: "b", kind: "panel-tab", threadRef, surface: { kind: "files", id: "files" } },
132 ];
133 const known: ReopenOwnerState = {
134 environmentKnown: true,
135 catalogReady: true,
136 ownerExists: true,
137 sideChatExists: true,
138 shellLive: true,
139 panel: { isOpen: false, activeSurfaceId: null, surfaces: [] },
140 };
141 const openDiff: ThreadRightPanelState = {
142 isOpen: true,
143 activeSurfaceId: "diff",
144 surfaces: [{ kind: "diff", id: "diff" }],
145 };
146
147 it.each([
148 ["restores the newest entry", [known, known], [], "a"],
149 [
150 "drops a removed environment once the catalog is ready",
151 [{ ...known, environmentKnown: false }, known],
152 ["a"],
153 "b",
154 ],
155 [
156 "waits for the catalog before dropping",
157 [{ ...known, environmentKnown: false, catalogReady: false }, known],
158 [],
159 null,
160 ],
161 [
162 "skips but keeps a thread deleted from a live shell",
163 [{ ...known, ownerExists: false }, known],
164 [],
165 "b",
166 ],
167 [
168 "skips but keeps a side chat deleted from a live shell",
169 [{ ...known, sideChatExists: false }, known],
170 [],
171 "b",
172 ],
173 [
174 "waits when a cached shell lacks the side chat",
175 [{ ...known, sideChatExists: false, shellLive: false }, known],
176 [],
177 null,
178 ],
179 [
180 "waits when a cached shell lacks the thread",
181 [{ ...known, ownerExists: false, shellLive: false }, known],
182 [],
183 null,
184 ],
185 ["drops a tab that is already open", [{ ...known, panel: openDiff }, known], ["a"], "b"],
186 [
187 "restores a tab whose panel is hidden",
188 [{ ...known, panel: { ...openDiff, isOpen: false } }],
189 [],
190 "a",
191 ],
192 ] as const)("%s", (_name, owners, dropped, restored) => {
193 const plan = planNextReopen(
194 entries.slice(0, owners.length),
195 (e) => owners[entries.indexOf(e)]!,
196 );
197 expect(plan.drop.map((e) => e.id)).toEqual(dropped);
198 expect(plan.restore?.id ?? null).toBe(restored);
199 });
200});