Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

rightPanelStore.test.ts

656 lines21.8 KB

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 pullRequestSurfaceId,
8 selectActiveRightPanel,
9 selectActiveRightPanelSurface,
10 selectSelectedRightPanelSurface,
11 selectThreadPanelOpen,
12 selectThreadPanelVisibility,
13 selectThreadRightPanelState,
14 useRightPanelStore,
15} from "./rightPanelStore";
16​
17const refA = scopeThreadRef("env-1" as EnvironmentId, ThreadId.make("thread-A"));
18const refB = scopeThreadRef("env-1" as EnvironmentId, ThreadId.make("thread-B"));
19​
20beforeEach(() => {
21 useRightPanelStore.setState({ byThreadKey: {}, threadPanelVisibilityByThreadKey: {} });
22});
23​
24describe("rightPanelStore", () => {
25 it("drops the legacy singleton terminal surface during migration", () => {
26 expect(
27 migratePersistedRightPanelState({
28 byThreadKey: {
29 "env-1:thread-A": {
30 activeSurfaceId: "terminal",
31 surfaces: [
32 { id: "browser:tab-a", kind: "preview", resourceId: "tab-a" },
33 { id: "terminal", kind: "terminal" },
34 ],
35 },
36 },
37 }),
38 ).toEqual({
39 byThreadKey: {
40 "env-1:thread-A": {
41 isOpen: false,
42 activeSurfaceId: null,
43 surfaces: [{ id: "browser:tab-a", kind: "preview", resourceId: "tab-a" }],
44 },
45 },
46 threadPanelVisibilityByThreadKey: {},
47 });
48 });
49​
50 it("upgrades saved single-session terminal surfaces to split-capable surfaces", () => {
51 expect(
52 migratePersistedRightPanelState({
53 byThreadKey: {
54 "env-1:thread-A": {
55 isOpen: true,
56 activeSurfaceId: "terminal:term-1",
57 surfaces: [{ id: "terminal:term-1", kind: "terminal", resourceId: "term-1" }],
58 },
59 },
60 }),
61 ).toEqual({
62 byThreadKey: {
63 "env-1:thread-A": {
64 isOpen: true,
65 activeSurfaceId: "terminal:term-1",
66 surfaces: [
67 {
68 id: "terminal:term-1",
69 kind: "terminal",
70 resourceId: "term-1",
71 terminalIds: ["term-1"],
72 activeTerminalId: "term-1",
73 },
74 ],
75 },
76 },
77 threadPanelVisibilityByThreadKey: {},
78 });
79 });
80​
81 it("upgrades saved file surfaces with neutral reveal state", () => {
82 expect(
83 migratePersistedRightPanelState({
84 byThreadKey: {
85 "env-1:thread-A": {
86 isOpen: true,
87 activeSurfaceId: "file:src/index.ts",
88 surfaces: [{ id: "file:src/index.ts", kind: "file", relativePath: "src/index.ts" }],
89 },
90 },
91 }),
92 ).toEqual({
93 byThreadKey: {
94 "env-1:thread-A": {
95 isOpen: true,
96 activeSurfaceId: "file:src/index.ts",
97 surfaces: [
98 {
99 id: "file:src/index.ts",
100 kind: "file",
101 relativePath: "src/index.ts",
102 revealLine: null,
103 revealRequestId: 0,
104 },
105 ],
106 },
107 },
108 threadPanelVisibilityByThreadKey: {},
109 });
110 });
111​
112 it("upgrades the legacy singleton pull request surface to a reference-keyed tab", () => {
113 const id = pullRequestSurfaceId({
114 projectId: "project-a",
115 repository: "pingdotgg/t3code",
116 number: 4909,
117 });
118 expect(
119 migratePersistedRightPanelState({
120 byThreadKey: {
121 "env-1:thread-A": {
122 isOpen: true,
123 activeSurfaceId: "pull-request",
124 surfaces: [
125 {
126 id: "pull-request",
127 kind: "pull-request",
128 projectId: "project-a",
129 repository: "pingdotgg/t3code",
130 number: 4909,
131 },
132 ],
133 },
134 },
135 }),
136 ).toEqual({
137 byThreadKey: {
138 "env-1:thread-A": {
139 isOpen: true,
140 activeSurfaceId: id,
141 surfaces: [
142 {
143 id,
144 kind: "pull-request",
145 projectId: "project-a",
146 repository: "pingdotgg/t3code",
147 number: 4909,
148 },
149 ],
150 },
151 },
152 threadPanelVisibilityByThreadKey: {},
153 });
154 });
155​
156 it("drops the pull-request list's shared panel so a restart opens the page fresh", () => {
157 const id = pullRequestSurfaceId({
158 projectId: "project-a",
159 repository: "pingdotgg/t3code",
160 number: 4909,
161 });
162 const panelState = {
163 isOpen: true,
164 activeSurfaceId: id,
165 surfaces: [
166 {
167 id,
168 kind: "pull-request" as const,
169 projectId: "project-a",
170 repository: "pingdotgg/t3code",
171 number: 4909,
172 },
173 ],
174 };
175 expect(
176 migratePersistedRightPanelState({
177 byThreadKey: {
178 "env-1:pull-requests-panel": panelState,
179 "env-1:thread-A": panelState,
180 },
181 }),
182 ).toEqual({
183 byThreadKey: { "env-1:thread-A": panelState },
184 threadPanelVisibilityByThreadKey: {},
185 });
186 });
187​
188 it("drops persisted plan surfaces and does not reopen an empty panel", () => {
189 expect(
190 migratePersistedRightPanelState({
191 byThreadKey: {
192 "env-1:thread-A": {
193 isOpen: true,
194 activeSurfaceId: "plan",
195 surfaces: [{ id: "plan", kind: "plan" }],
196 },
197 "env-1:thread-B": {
198 isOpen: true,
199 activeSurfaceId: "plan",
200 surfaces: [
201 { id: "plan", kind: "plan" },
202 { id: "diff", kind: "diff" },
203 ],
204 },
205 },
206 }),
207 ).toEqual({
208 byThreadKey: {
209 "env-1:thread-A": {
210 isOpen: false,
211 activeSurfaceId: null,
212 surfaces: [],
213 },
214 "env-1:thread-B": {
215 isOpen: true,
216 activeSurfaceId: "diff",
217 surfaces: [{ id: "diff", kind: "diff" }],
218 },
219 },
220 threadPanelVisibilityByThreadKey: {},
221 });
222 });
223​
224 it("persists inline preference without restoring an open popover", () => {
225 expect(
226 migratePersistedRightPanelState({
227 threadPanelVisibilityByThreadKey: {
228 "env-1:thread-A": { inlineOpen: false, popoverOpen: true },
229 "env-1:thread-B": { inlineOpen: true, popoverOpen: true },
230 },
231 }),
232 ).toEqual({
233 byThreadKey: {},
234 threadPanelVisibilityByThreadKey: {
235 "env-1:thread-A": { inlineOpen: false, popoverOpen: false },
236 },
237 });
238 });
239​
240 it("tracks inline and popover visibility independently", () => {
241 const store = useRightPanelStore.getState();
242​
243 expect(selectThreadPanelOpen(store.threadPanelVisibilityByThreadKey, refA, "inline")).toBe(
244 true,
245 );
246 expect(selectThreadPanelOpen(store.threadPanelVisibilityByThreadKey, refA, "popover")).toBe(
247 false,
248 );
249​
250 store.setThreadPanelOpen(refA, "inline", false);
251 store.toggleThreadPanel(refA, "popover");
252​
253 expect(
254 selectThreadPanelVisibility(
255 useRightPanelStore.getState().threadPanelVisibilityByThreadKey,
256 refA,
257 ),
258 ).toEqual({ inlineOpen: false, popoverOpen: true });
259 expect(
260 selectThreadPanelVisibility(
261 useRightPanelStore.getState().threadPanelVisibilityByThreadKey,
262 refB,
263 ),
264 ).toEqual({ inlineOpen: true, popoverOpen: false });
265 });
266​
267 it("closes the popover atomically when the real right panel opens", () => {
268 useRightPanelStore.getState().setThreadPanelOpen(refA, "popover", true);
269 useRightPanelStore.getState().open(refA, "diff");
270​
271 expect(
272 selectThreadPanelVisibility(
273 useRightPanelStore.getState().threadPanelVisibilityByThreadKey,
274 refA,
275 ),
276 ).toEqual({ inlineOpen: true, popoverOpen: false });
277 });
278​
279 it("keeps an open popover visible by promoting it to inline when the real panel closes", () => {
280 const store = useRightPanelStore.getState();
281 store.open(refA, "diff");
282 store.setThreadPanelOpen(refA, "inline", false);
283 store.setThreadPanelOpen(refA, "popover", true);
284 store.close(refA);
285​
286 expect(
287 selectThreadPanelVisibility(
288 useRightPanelStore.getState().threadPanelVisibilityByThreadKey,
289 refA,
290 ),
291 ).toEqual({ inlineOpen: true, popoverOpen: true });
292 });
293​
294 it("open sets the active panel for a thread", () => {
295 useRightPanelStore.getState().open(refA, "preview");
296 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("preview");
297 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refB)).toBeNull();
298 });
299​
300 it("opening a different kind keeps both surfaces and activates the new one", () => {
301 useRightPanelStore.getState().open(refA, "agents");
302 useRightPanelStore.getState().open(refA, "preview");
303 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("preview");
304 expect(
305 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces,
306 ).toHaveLength(2);
307 });
308​
309 it("reopening an inactive singleton activates its existing surface", () => {
310 useRightPanelStore.getState().open(refA, "diff");
311 useRightPanelStore.getState().open(refA, "agents");
312 useRightPanelStore.getState().open(refA, "diff");
313​
314 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
315 isOpen: true,
316 activeSurfaceId: "diff",
317 surfaces: [
318 { id: "diff", kind: "diff" },
319 { id: "agents", kind: "agents" },
320 ],
321 });
322 });
323​
324 it("keeps files as a singleton surface", () => {
325 useRightPanelStore.getState().open(refA, "files");
326 useRightPanelStore.getState().open(refA, "files");
327 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
328 isOpen: true,
329 activeSurfaceId: "files",
330 surfaces: [{ id: "files", kind: "files" }],
331 });
332 });
333​
334 it("replaces the standalone explorer with peer file surfaces", () => {
335 useRightPanelStore.getState().open(refA, "files");
336 useRightPanelStore.getState().openFile(refA, "src/index.ts");
337 useRightPanelStore.getState().openFile(refA, "src/index.ts");
338 useRightPanelStore.getState().openFile(refA, "README.md");
339​
340 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
341 isOpen: true,
342 activeSurfaceId: "file:README.md",
343 surfaces: [
344 {
345 id: "file:src/index.ts",
346 kind: "file",
347 relativePath: "src/index.ts",
348 revealLine: null,
349 revealRequestId: 2,
350 },
351 {
352 id: "file:README.md",
353 kind: "file",
354 relativePath: "README.md",
355 revealLine: null,
356 revealRequestId: 1,
357 },
358 ],
359 });
360 });
361​
362 it("updates line reveal requests when reopening a file surface", () => {
363 useRightPanelStore.getState().openFile(refA, "src/index.ts", 42);
364 useRightPanelStore.getState().openFile(refA, "src/index.ts", 87);
365​
366 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
367 isOpen: true,
368 activeSurfaceId: "file:src/index.ts",
369 surfaces: [
370 {
371 id: "file:src/index.ts",
372 kind: "file",
373 relativePath: "src/index.ts",
374 revealLine: 87,
375 revealRequestId: 2,
376 },
377 ],
378 });
379​
380 useRightPanelStore.getState().openFile(refA, "src/index.ts");
381​
382 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
383 isOpen: true,
384 activeSurfaceId: "file:src/index.ts",
385 surfaces: [
386 {
387 id: "file:src/index.ts",
388 kind: "file",
389 relativePath: "src/index.ts",
390 revealLine: null,
391 revealRequestId: 3,
392 },
393 ],
394 });
395 });
396​
397 it("removes persisted file surfaces when their workspace no longer exists", () => {
398 useRightPanelStore.getState().openFile(refA, "src/index.ts");
399 useRightPanelStore.getState().open(refA, "agents");
400 useRightPanelStore.getState().openFile(refA, "README.md");
401​
402 useRightPanelStore.getState().reconcileFileSurfaces(refA, false);
403​
404 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
405 isOpen: true,
406 activeSurfaceId: "agents",
407 surfaces: [{ id: "agents", kind: "agents" }],
408 });
409​
410 useRightPanelStore.getState().openFile(refB, "conductor.json");
411 useRightPanelStore.getState().reconcileFileSurfaces(refB, false);
412 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refB)).toEqual({
413 isOpen: false,
414 activeSurfaceId: null,
415 surfaces: [],
416 });
417 });
418​
419 it("close hides the panel without clearing its selected surface", () => {
420 useRightPanelStore.getState().open(refA, "agents");
421 useRightPanelStore.getState().close(refA);
422 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
423 expect(
424 selectSelectedRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA),
425 ).toEqual({ id: "agents", kind: "agents" });
426 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
427 isOpen: false,
428 activeSurfaceId: "agents",
429 surfaces: [{ id: "agents", kind: "agents" }],
430 });
431 });
432​
433 it("toggles empty panel visibility without creating a surface", () => {
434 useRightPanelStore.getState().toggleVisibility(refA);
435 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
436 isOpen: true,
437 activeSurfaceId: null,
438 surfaces: [],
439 });
440​
441 useRightPanelStore.getState().toggleVisibility(refA);
442 expect(useRightPanelStore.getState().byThreadKey).toEqual({});
443 });
444​
445 it("toggle hides the panel without discarding the active surface", () => {
446 useRightPanelStore.getState().toggle(refA, "diff");
447 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("diff");
448 useRightPanelStore.getState().toggle(refA, "diff");
449 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
450 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
451 isOpen: false,
452 activeSurfaceId: "diff",
453 surfaces: [{ id: "diff", kind: "diff" }],
454 });
455 });
456​
457 it("toggle to a different kind switches active", () => {
458 useRightPanelStore.getState().toggle(refA, "preview");
459 useRightPanelStore.getState().toggle(refA, "agents");
460 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("agents");
461 });
462​
463 it("removeThread clears persisted state", () => {
464 useRightPanelStore.getState().open(refA, "agents");
465 useRightPanelStore.getState().removeThread(refA);
466 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
467 });
468​
469 it("close on never-opened thread is a no-op", () => {
470 useRightPanelStore.getState().close(refA);
471 expect(useRightPanelStore.getState().byThreadKey).toEqual({});
472 });
473​
474 it("tracks one surface per browser session", () => {
475 useRightPanelStore.getState().openBrowser(refA, "tab-a");
476 useRightPanelStore.getState().openBrowser(refA, "tab-b");
477​
478 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
479 expect(state.surfaces.map((surface) => surface.id)).toEqual(["browser:tab-a", "browser:tab-b"]);
480 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
481 id: "browser:tab-b",
482 kind: "preview",
483 resourceId: "tab-b",
484 });
485 });
486​
487 it("tracks one surface per pull request", () => {
488 const first = { projectId: "project-a", repository: "pingdotgg/t3code", number: 4909 };
489 const second = { projectId: "project-a", repository: "pingdotgg/t3code", number: 4910 };
490 useRightPanelStore.getState().openPullRequest(refA, first);
491 useRightPanelStore.getState().openPullRequest(refA, second);
492 useRightPanelStore.getState().openPullRequest(refA, first);
493​
494 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
495 expect(state.surfaces.map((surface) => surface.id)).toEqual([
496 pullRequestSurfaceId(first),
497 pullRequestSurfaceId(second),
498 ]);
499 expect(state.activeSurfaceId).toBe(pullRequestSurfaceId(first));
500 });
501​
502 it("tracks one surface per terminal session", () => {
503 useRightPanelStore.getState().openTerminal(refA, "term-1");
504 useRightPanelStore.getState().openTerminal(refA, "term-2");
505​
506 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
507 expect(state.surfaces).toEqual([
508 {
509 id: "terminal:term-1",
510 kind: "terminal",
511 resourceId: "term-1",
512 terminalIds: ["term-1"],
513 activeTerminalId: "term-1",
514 },
515 {
516 id: "terminal:term-2",
517 kind: "terminal",
518 resourceId: "term-2",
519 terminalIds: ["term-2"],
520 activeTerminalId: "term-2",
521 },
522 ]);
523 expect(state.activeSurfaceId).toBe("terminal:term-2");
524 });
525​
526 it("tracks split panes and the active pane within a terminal surface", () => {
527 useRightPanelStore.getState().openTerminal(refA, "term-1");
528 useRightPanelStore.getState().splitTerminal(refA, "terminal:term-1", "term-2");
529​
530 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
531 id: "terminal:term-1",
532 kind: "terminal",
533 resourceId: "term-1",
534 terminalIds: ["term-1", "term-2"],
535 activeTerminalId: "term-2",
536 });
537​
538 useRightPanelStore.getState().activateTerminal(refA, "terminal:term-1", "term-1");
539 useRightPanelStore.getState().closeTerminal(refA, "terminal:term-1", "term-1");
540 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
541 id: "terminal:term-1",
542 kind: "terminal",
543 resourceId: "term-1",
544 terminalIds: ["term-2"],
545 activeTerminalId: "term-2",
546 });
547 });
548​
549 it("tracks vertical layout for a terminal surface", () => {
550 useRightPanelStore.getState().openTerminal(refA, "term-1");
551 useRightPanelStore.getState().splitTerminal(refA, "terminal:term-1", "term-2", "vertical");
552​
553 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
554 id: "terminal:term-1",
555 kind: "terminal",
556 resourceId: "term-1",
557 terminalIds: ["term-1", "term-2"],
558 activeTerminalId: "term-2",
559 splitDirection: "vertical",
560 });
561 });
562​
563 it("closing the final terminal pane removes its surface and closes the panel", () => {
564 useRightPanelStore.getState().openTerminal(refA, "term-1");
565 useRightPanelStore.getState().closeTerminal(refA, "terminal:term-1", "term-1");
566​
567 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
568 isOpen: false,
569 activeSurfaceId: null,
570 surfaces: [],
571 });
572 });
573​
574 it("closing the active surface activates a neighboring surface", () => {
575 useRightPanelStore.getState().openBrowser(refA, "tab-a");
576 useRightPanelStore.getState().openTerminal(refA, "term-1");
577 useRightPanelStore.getState().closeSurface(refA, "terminal:term-1");
578​
579 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)?.id).toBe(
580 "browser:tab-a",
581 );
582 });
583​
584 it("closing the final surface closes the panel", () => {
585 useRightPanelStore.getState().openTerminal(refA, "term-1");
586 useRightPanelStore.getState().closeSurface(refA, "terminal:term-1");
587​
588 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
589 isOpen: false,
590 activeSurfaceId: null,
591 surfaces: [],
592 });
593 });
594​
595 it("closing other surfaces keeps the selected surface active", () => {
596 useRightPanelStore.getState().openBrowser(refA, "tab-a");
597 useRightPanelStore.getState().openFile(refA, "src/index.ts");
598 useRightPanelStore.getState().openTerminal(refA, "term-1");
599​
600 useRightPanelStore.getState().closeOtherSurfaces(refA, "file:src/index.ts");
601​
602 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
603 isOpen: true,
604 activeSurfaceId: "file:src/index.ts",
605 surfaces: [
606 {
607 id: "file:src/index.ts",
608 kind: "file",
609 relativePath: "src/index.ts",
610 revealLine: null,
611 revealRequestId: 1,
612 },
613 ],
614 });
615 });
616​
617 it("closing surfaces to the right activates the selected surface when active was removed", () => {
618 useRightPanelStore.getState().openBrowser(refA, "tab-a");
619 useRightPanelStore.getState().openFile(refA, "src/index.ts");
620 useRightPanelStore.getState().openTerminal(refA, "term-1");
621​
622 useRightPanelStore.getState().closeSurfacesToRight(refA, "browser:tab-a");
623​
624 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
625 isOpen: true,
626 activeSurfaceId: "browser:tab-a",
627 surfaces: [{ id: "browser:tab-a", kind: "preview", resourceId: "tab-a" }],
628 });
629 });
630​
631 it("closing all surfaces closes the panel", () => {
632 useRightPanelStore.getState().openBrowser(refA, "tab-a");
633 useRightPanelStore.getState().openFile(refA, "src/index.ts");
634​
635 useRightPanelStore.getState().closeAllSurfaces(refA);
636​
637 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
638 isOpen: false,
639 activeSurfaceId: null,
640 surfaces: [],
641 });
642 });
643​
644 it("reconciles browser surfaces without deleting other surface kinds", () => {
645 useRightPanelStore.getState().openTerminal(refA, "term-1");
646 useRightPanelStore.getState().openBrowser(refA, "tab-a");
647 useRightPanelStore.getState().openBrowser(refA, "tab-b");
648 useRightPanelStore.getState().reconcileBrowserSurfaces(refA, ["tab-b", "tab-c"]);
649​
650 expect(
651 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces.map(
652 (surface) => surface.id,
653 ),
654 ).toEqual(["terminal:term-1", "browser:tab-b", "browser:tab-c"]);
655 });
656});