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