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