Open History to see the last commit
1import { scopeThreadRef } from "@v12/client-runtime/environment";
2import { type EnvironmentId, ThreadId } from "@v12/contracts";
3import { beforeEach, describe, expect, it } from "vite-plus/test";
4
5import {
6 migratePersistedRightPanelState,
7 selectActiveRightPanel,
8 selectActiveRightPanelSurface,
9 selectThreadRightPanelState,
10 useRightPanelStore,
11} from "./rightPanelStore";
12
13const refA = scopeThreadRef("env-1" as EnvironmentId, ThreadId.make("thread-A"));
14const refB = scopeThreadRef("env-1" as EnvironmentId, ThreadId.make("thread-B"));
15
16beforeEach(() => {
17 useRightPanelStore.setState({ byThreadKey: {} });
18});
19
20describe("rightPanelStore", () => {
21 it("drops the legacy singleton terminal surface during migration", () => {
22 expect(
23 migratePersistedRightPanelState({
24 byThreadKey: {
25 "env-1:thread-A": {
26 activeSurfaceId: "terminal",
27 surfaces: [
28 { id: "browser:tab-a", kind: "preview", resourceId: "tab-a" },
29 { id: "terminal", kind: "terminal" },
30 ],
31 },
32 },
33 }),
34 ).toEqual({
35 byThreadKey: {
36 "env-1:thread-A": {
37 isOpen: false,
38 activeSurfaceId: null,
39 surfaces: [{ id: "browser:tab-a", kind: "preview", resourceId: "tab-a" }],
40 },
41 },
42 });
43 });
44
45 it("upgrades saved single-session terminal surfaces to split-capable surfaces", () => {
46 expect(
47 migratePersistedRightPanelState({
48 byThreadKey: {
49 "env-1:thread-A": {
50 isOpen: true,
51 activeSurfaceId: "terminal:term-1",
52 surfaces: [{ id: "terminal:term-1", kind: "terminal", resourceId: "term-1" }],
53 },
54 },
55 }),
56 ).toEqual({
57 byThreadKey: {
58 "env-1:thread-A": {
59 isOpen: true,
60 activeSurfaceId: "terminal:term-1",
61 surfaces: [
62 {
63 id: "terminal:term-1",
64 kind: "terminal",
65 resourceId: "term-1",
66 terminalIds: ["term-1"],
67 activeTerminalId: "term-1",
68 },
69 ],
70 },
71 },
72 });
73 });
74
75 it("upgrades saved file surfaces with neutral reveal state", () => {
76 expect(
77 migratePersistedRightPanelState({
78 byThreadKey: {
79 "env-1:thread-A": {
80 isOpen: true,
81 activeSurfaceId: "file:src/index.ts",
82 surfaces: [{ id: "file:src/index.ts", kind: "file", relativePath: "src/index.ts" }],
83 },
84 },
85 }),
86 ).toEqual({
87 byThreadKey: {
88 "env-1:thread-A": {
89 isOpen: true,
90 activeSurfaceId: "file:src/index.ts",
91 surfaces: [
92 {
93 id: "file:src/index.ts",
94 kind: "file",
95 relativePath: "src/index.ts",
96 revealLine: null,
97 revealRequestId: 0,
98 },
99 ],
100 },
101 },
102 });
103 });
104
105 it("open sets the active panel for a thread", () => {
106 useRightPanelStore.getState().open(refA, "preview");
107 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("preview");
108 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refB)).toBeNull();
109 });
110
111 it("opening a different kind keeps both surfaces and activates the new one", () => {
112 useRightPanelStore.getState().open(refA, "plan");
113 useRightPanelStore.getState().open(refA, "preview");
114 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("preview");
115 expect(
116 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces,
117 ).toHaveLength(2);
118 });
119
120 it("keeps files as a singleton surface", () => {
121 useRightPanelStore.getState().open(refA, "files");
122 useRightPanelStore.getState().open(refA, "files");
123 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
124 isOpen: true,
125 activeSurfaceId: "files",
126 surfaces: [{ id: "files", kind: "files" }],
127 });
128 });
129
130 it("replaces the standalone explorer with peer file surfaces", () => {
131 useRightPanelStore.getState().open(refA, "files");
132 useRightPanelStore.getState().openFile(refA, "src/index.ts");
133 useRightPanelStore.getState().openFile(refA, "src/index.ts");
134 useRightPanelStore.getState().openFile(refA, "README.md");
135
136 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
137 isOpen: true,
138 activeSurfaceId: "file:README.md",
139 surfaces: [
140 {
141 id: "file:src/index.ts",
142 kind: "file",
143 relativePath: "src/index.ts",
144 revealLine: null,
145 revealRequestId: 2,
146 },
147 {
148 id: "file:README.md",
149 kind: "file",
150 relativePath: "README.md",
151 revealLine: null,
152 revealRequestId: 1,
153 },
154 ],
155 });
156 });
157
158 it("updates line reveal requests when reopening a file surface", () => {
159 useRightPanelStore.getState().openFile(refA, "src/index.ts", 42);
160 useRightPanelStore.getState().openFile(refA, "src/index.ts", 87);
161
162 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
163 isOpen: true,
164 activeSurfaceId: "file:src/index.ts",
165 surfaces: [
166 {
167 id: "file:src/index.ts",
168 kind: "file",
169 relativePath: "src/index.ts",
170 revealLine: 87,
171 revealRequestId: 2,
172 },
173 ],
174 });
175
176 useRightPanelStore.getState().openFile(refA, "src/index.ts");
177
178 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
179 isOpen: true,
180 activeSurfaceId: "file:src/index.ts",
181 surfaces: [
182 {
183 id: "file:src/index.ts",
184 kind: "file",
185 relativePath: "src/index.ts",
186 revealLine: null,
187 revealRequestId: 3,
188 },
189 ],
190 });
191 });
192
193 it("removes persisted file surfaces when their workspace no longer exists", () => {
194 useRightPanelStore.getState().openFile(refA, "src/index.ts");
195 useRightPanelStore.getState().open(refA, "plan");
196 useRightPanelStore.getState().openFile(refA, "README.md");
197
198 useRightPanelStore.getState().reconcileFileSurfaces(refA, false);
199
200 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
201 isOpen: true,
202 activeSurfaceId: "plan",
203 surfaces: [{ id: "plan", kind: "plan" }],
204 });
205
206 useRightPanelStore.getState().openFile(refB, "conductor.json");
207 useRightPanelStore.getState().reconcileFileSurfaces(refB, false);
208 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refB)).toEqual({
209 isOpen: false,
210 activeSurfaceId: null,
211 surfaces: [],
212 });
213 });
214
215 it("close hides the panel without clearing its selected surface", () => {
216 useRightPanelStore.getState().open(refA, "plan");
217 useRightPanelStore.getState().close(refA);
218 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
219 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
220 isOpen: false,
221 activeSurfaceId: "plan",
222 surfaces: [{ id: "plan", kind: "plan" }],
223 });
224 });
225
226 it("toggles empty panel visibility without creating a surface", () => {
227 useRightPanelStore.getState().toggleVisibility(refA);
228 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
229 isOpen: true,
230 activeSurfaceId: null,
231 surfaces: [],
232 });
233
234 useRightPanelStore.getState().toggleVisibility(refA);
235 expect(useRightPanelStore.getState().byThreadKey).toEqual({});
236 });
237
238 it("toggle hides the panel without discarding the active surface", () => {
239 useRightPanelStore.getState().toggle(refA, "diff");
240 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("diff");
241 useRightPanelStore.getState().toggle(refA, "diff");
242 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
243 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
244 isOpen: false,
245 activeSurfaceId: "diff",
246 surfaces: [{ id: "diff", kind: "diff" }],
247 });
248 });
249
250 it("toggle to a different kind switches active", () => {
251 useRightPanelStore.getState().toggle(refA, "preview");
252 useRightPanelStore.getState().toggle(refA, "plan");
253 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("plan");
254 });
255
256 it("removeThread clears persisted state", () => {
257 useRightPanelStore.getState().open(refA, "plan");
258 useRightPanelStore.getState().removeThread(refA);
259 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
260 });
261
262 it("close on never-opened thread is a no-op", () => {
263 useRightPanelStore.getState().close(refA);
264 expect(useRightPanelStore.getState().byThreadKey).toEqual({});
265 });
266
267 it("tracks one surface per browser session", () => {
268 useRightPanelStore.getState().openBrowser(refA, "tab-a");
269 useRightPanelStore.getState().openBrowser(refA, "tab-b");
270
271 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
272 expect(state.surfaces.map((surface) => surface.id)).toEqual(["browser:tab-a", "browser:tab-b"]);
273 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
274 id: "browser:tab-b",
275 kind: "preview",
276 resourceId: "tab-b",
277 });
278 });
279
280 it("tracks one surface per terminal session", () => {
281 useRightPanelStore.getState().openTerminal(refA, "term-1");
282 useRightPanelStore.getState().openTerminal(refA, "term-2");
283
284 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
285 expect(state.surfaces).toEqual([
286 {
287 id: "terminal:term-1",
288 kind: "terminal",
289 resourceId: "term-1",
290 terminalIds: ["term-1"],
291 activeTerminalId: "term-1",
292 },
293 {
294 id: "terminal:term-2",
295 kind: "terminal",
296 resourceId: "term-2",
297 terminalIds: ["term-2"],
298 activeTerminalId: "term-2",
299 },
300 ]);
301 expect(state.activeSurfaceId).toBe("terminal:term-2");
302 });
303
304 it("tracks split panes and the active pane within a terminal surface", () => {
305 useRightPanelStore.getState().openTerminal(refA, "term-1");
306 useRightPanelStore.getState().splitTerminal(refA, "terminal:term-1", "term-2");
307
308 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
309 id: "terminal:term-1",
310 kind: "terminal",
311 resourceId: "term-1",
312 terminalIds: ["term-1", "term-2"],
313 activeTerminalId: "term-2",
314 });
315
316 useRightPanelStore.getState().activateTerminal(refA, "terminal:term-1", "term-1");
317 useRightPanelStore.getState().closeTerminal(refA, "terminal:term-1", "term-1");
318 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
319 id: "terminal:term-1",
320 kind: "terminal",
321 resourceId: "term-1",
322 terminalIds: ["term-2"],
323 activeTerminalId: "term-2",
324 });
325 });
326
327 it("tracks vertical layout for a terminal surface", () => {
328 useRightPanelStore.getState().openTerminal(refA, "term-1");
329 useRightPanelStore.getState().splitTerminal(refA, "terminal:term-1", "term-2", "vertical");
330
331 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
332 id: "terminal:term-1",
333 kind: "terminal",
334 resourceId: "term-1",
335 terminalIds: ["term-1", "term-2"],
336 activeTerminalId: "term-2",
337 splitDirection: "vertical",
338 });
339 });
340
341 it("closing the final terminal pane removes its surface and closes the panel", () => {
342 useRightPanelStore.getState().openTerminal(refA, "term-1");
343 useRightPanelStore.getState().closeTerminal(refA, "terminal:term-1", "term-1");
344
345 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
346 isOpen: false,
347 activeSurfaceId: null,
348 surfaces: [],
349 });
350 });
351
352 it("closing the active surface activates a neighboring surface", () => {
353 useRightPanelStore.getState().openBrowser(refA, "tab-a");
354 useRightPanelStore.getState().openTerminal(refA, "term-1");
355 useRightPanelStore.getState().closeSurface(refA, "terminal:term-1");
356
357 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)?.id).toBe(
358 "browser:tab-a",
359 );
360 });
361
362 it("closing the final surface closes the panel", () => {
363 useRightPanelStore.getState().openTerminal(refA, "term-1");
364 useRightPanelStore.getState().closeSurface(refA, "terminal:term-1");
365
366 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
367 isOpen: false,
368 activeSurfaceId: null,
369 surfaces: [],
370 });
371 });
372
373 it("closing other surfaces keeps the selected surface active", () => {
374 useRightPanelStore.getState().openBrowser(refA, "tab-a");
375 useRightPanelStore.getState().openFile(refA, "src/index.ts");
376 useRightPanelStore.getState().openTerminal(refA, "term-1");
377
378 useRightPanelStore.getState().closeOtherSurfaces(refA, "file:src/index.ts");
379
380 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
381 isOpen: true,
382 activeSurfaceId: "file:src/index.ts",
383 surfaces: [
384 {
385 id: "file:src/index.ts",
386 kind: "file",
387 relativePath: "src/index.ts",
388 revealLine: null,
389 revealRequestId: 1,
390 },
391 ],
392 });
393 });
394
395 it("closing surfaces to the right activates the selected surface when active was removed", () => {
396 useRightPanelStore.getState().openBrowser(refA, "tab-a");
397 useRightPanelStore.getState().openFile(refA, "src/index.ts");
398 useRightPanelStore.getState().openTerminal(refA, "term-1");
399
400 useRightPanelStore.getState().closeSurfacesToRight(refA, "browser:tab-a");
401
402 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
403 isOpen: true,
404 activeSurfaceId: "browser:tab-a",
405 surfaces: [{ id: "browser:tab-a", kind: "preview", resourceId: "tab-a" }],
406 });
407 });
408
409 it("closing all surfaces closes the panel", () => {
410 useRightPanelStore.getState().openBrowser(refA, "tab-a");
411 useRightPanelStore.getState().openFile(refA, "src/index.ts");
412
413 useRightPanelStore.getState().closeAllSurfaces(refA);
414
415 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
416 isOpen: false,
417 activeSurfaceId: null,
418 surfaces: [],
419 });
420 });
421
422 it("reconciles browser surfaces without deleting other surface kinds", () => {
423 useRightPanelStore.getState().openTerminal(refA, "term-1");
424 useRightPanelStore.getState().openBrowser(refA, "tab-a");
425 useRightPanelStore.getState().openBrowser(refA, "tab-b");
426 useRightPanelStore.getState().reconcileBrowserSurfaces(refA, ["tab-b", "tab-c"]);
427
428 expect(
429 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces.map(
430 (surface) => surface.id,
431 ),
432 ).toEqual(["terminal:term-1", "browser:tab-b", "browser:tab-c"]);
433 });
434});