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 pullRequestSurface,
8 pullRequestSurfaceId,
9 selectActiveRightPanel,
10 selectActiveRightPanelSurface,
11 selectSelectedRightPanelSurface,
12 selectThreadPanelOpen,
13 selectThreadPanelVisibility,
14 selectThreadRightPanelState,
15 useRightPanelStore,
16} from "./rightPanelStore";
17
18const refA = scopeThreadRef("env-1" as EnvironmentId, ThreadId.make("thread-A"));
19const refB = scopeThreadRef("env-1" as EnvironmentId, ThreadId.make("thread-B"));
20
21beforeEach(() => {
22 useRightPanelStore.setState({
23 byThreadKey: {},
24 threadPanelVisibilityByThreadKey: {},
25 userActionRevisionByThreadKey: {},
26 });
27});
28
29describe("rightPanelStore", () => {
30 it("gives each host/device its own tab and preserves renamed tabs", () => {
31 const store = useRightPanelStore.getState();
32 const android = {
33 hostId: "nucbox",
34 deviceId: "emulator-5580",
35 name: "Pixel",
36 platform: "android",
37 } as const;
38 const ios = { hostId: "macmini", deviceId: "ios-1", name: "iPhone", platform: "ios" } as const;
39 store.open(refA, "device");
40 store.openDevice(refA, android);
41 store.open(refA, "device");
42 expect(
43 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces,
44 ).toHaveLength(2);
45 store.openDevice(refA, ios);
46 let state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
47 expect(state.surfaces.map((surface) => surface.id)).toEqual([
48 "device:nucbox:emulator-5580",
49 "device:macmini:ios-1",
50 ]);
51 store.renameDevice(refA, "device:nucbox:emulator-5580", "Android test");
52 store.openDevice(refA, android);
53 state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
54 expect(state.surfaces).toHaveLength(2);
55 expect(state.surfaces[0]).toMatchObject({ title: "Android test", target: android });
56 expect(state.activeSurfaceId).toBe("device:nucbox:emulator-5580");
57 store.closeSurface(refA, state.activeSurfaceId!);
58 expect(
59 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces,
60 ).toEqual([expect.objectContaining({ target: ios })]);
61 });
62
63 it("does not collide when two hosts expose the same device id", () => {
64 const store = useRightPanelStore.getState();
65 const device = { deviceId: "emulator-5554", name: "Pixel", platform: "android" } as const;
66 store.openDevice(refA, { ...device, hostId: "a:b" });
67 store.openDevice(refA, { ...device, hostId: "a" });
68 expect(
69 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces,
70 ).toHaveLength(2);
71 expect(
72 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refB).surfaces,
73 ).toHaveLength(0);
74 });
75
76 it.each(["one", "all", "others", "right"])(
77 "keeps device tabs dismissed across reload after closing %s",
78 (mode) => {
79 const store = useRightPanelStore.getState();
80 const target = {
81 hostId: "nucbox",
82 deviceId: "emulator-5580",
83 name: "Pixel",
84 platform: "android",
85 } as const;
86 store.open(refA, "files");
87 store.openDevice(refA, target);
88 if (mode === "one") store.closeSurface(refA, "device:nucbox:emulator-5580");
89 if (mode === "all") store.closeAllSurfaces(refA);
90 if (mode === "others") store.closeOtherSurfaces(refA, "files");
91 if (mode === "right") store.closeSurfacesToRight(refA, "files");
92 const persisted = JSON.parse(
93 JSON.stringify({ byThreadKey: useRightPanelStore.getState().byThreadKey }),
94 );
95 useRightPanelStore.setState(migratePersistedRightPanelState(persisted));
96 store.openDevice(refA, target, true);
97 expect(
98 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces.some(
99 (surface) => surface.kind === "device",
100 ),
101 ).toBe(false);
102 store.openDevice(refA, target);
103 expect(
104 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces.some(
105 (surface) => surface.kind === "device",
106 ),
107 ).toBe(true);
108 },
109 );
110
111 const completedDiff = { id: "diff", kind: "diff" } as const;
112 const linkedPullRequest = pullRequestSurface({
113 projectId: "project-a",
114 repository: "pingdotgg/t3code",
115 number: 42,
116 });
117
118 it.each([
119 { order: "diff-first", surface: linkedPullRequest },
120 { order: "pull-request-first", surface: linkedPullRequest },
121 { order: "diff-first", surface: { id: "pull-requests", kind: "pull-requests" } as const },
122 {
123 order: "pull-request-first",
124 surface: { id: "pull-requests", kind: "pull-requests" } as const,
125 },
126 ])(
127 "prioritizes $surface.kind over browser and diff with $order delivery",
128 ({ order, surface }) => {
129 const store = useRightPanelStore.getState();
130 store.openBrowser(refA, "existing-browser");
131 const revision = store.getUserActionRevision(refA);
132 const requests = order === "diff-first" ? [completedDiff, surface] : [surface, completedDiff];
133 for (const surface of requests) store.openProactive(refA, surface, revision);
134 store.reconcileBrowserSurfaces(refA, ["existing-browser", "agent-browser"]);
135
136 expect(
137 selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA),
138 ).toEqual(surface);
139
140 store.open(refA, "diff");
141 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("diff");
142 },
143 );
144
145 it.each([
146 { choice: "file", choose: () => useRightPanelStore.getState().openFile(refA, "src/app.ts") },
147 {
148 choice: "pull request",
149 choose: () =>
150 useRightPanelStore.getState().openPullRequest(refA, { ...linkedPullRequest, number: 41 }),
151 },
152 { choice: "browser", choose: () => useRightPanelStore.getState().openBrowser(refA, "tab-a") },
153 {
154 choice: "terminal",
155 choose: () => useRightPanelStore.getState().openTerminal(refA, "term-1"),
156 },
157 {
158 choice: "same tab",
159 choose: () => useRightPanelStore.getState().activateSurface(refA, "diff"),
160 },
161 { choice: "hide", choose: () => useRightPanelStore.getState().close(refA) },
162 { choice: "toggle", choose: () => useRightPanelStore.getState().toggle(refA, "diff") },
163 { choice: "close all", choose: () => useRightPanelStore.getState().closeAllSurfaces(refA) },
164 {
165 choice: "terminal close",
166 choose: () => {
167 const store = useRightPanelStore.getState();
168 store.openTerminal(refA, "term-1");
169 store.closeTerminal(refA, "terminal:term-1", "term-1");
170 },
171 },
172 ])("keeps a later $choice choice when automatic requests arrive", ({ choose }) => {
173 const store = useRightPanelStore.getState();
174 store.open(refA, "diff");
175 const revision = store.getUserActionRevision(refA);
176 choose();
177 const chosen = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
178
179 expect(store.openProactive(refA, completedDiff, revision)).toBe(false);
180 expect(store.openProactive(refA, linkedPullRequest, revision)).toBe(false);
181 expect(
182 store.openProactive(refA, { id: "pull-requests", kind: "pull-requests" }, revision),
183 ).toBe(false);
184 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toBe(
185 chosen,
186 );
187 });
188
189 it("allows automatic panels for a later turn after a manual choice", () => {
190 const store = useRightPanelStore.getState();
191 const firstTurnRevision = store.getUserActionRevision(refA);
192 store.openFile(refA, "src/app.ts");
193 expect(store.openProactive(refA, completedDiff, firstTurnRevision)).toBe(false);
194
195 const nextTurnRevision = store.getUserActionRevision(refA);
196 expect(store.openProactive(refA, completedDiff, nextTurnRevision)).toBe(true);
197 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("diff");
198 });
199
200 it("keeps manual choices scoped to their thread and environment", () => {
201 const otherEnvironment = scopeThreadRef("env-2" as EnvironmentId, refA.threadId);
202 const store = useRightPanelStore.getState();
203 const revision = store.getUserActionRevision(refA);
204 store.openFile(refB, "src/app.ts");
205 store.openFile(otherEnvironment, "src/app.ts");
206
207 expect(store.openProactive(refA, completedDiff, revision)).toBe(true);
208 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refB)).toBe("file");
209 expect(
210 selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, otherEnvironment),
211 ).toBe("file");
212 });
213
214 it("does not treat resource reconciliation as a manual choice", () => {
215 const store = useRightPanelStore.getState();
216 store.openFile(refA, "src/app.ts");
217 const revision = store.getUserActionRevision(refA);
218 store.reconcileBrowserSurfaces(refA, ["agent-browser"]);
219 store.reconcileFileSurfaces(refA, false);
220
221 expect(store.openProactive(refA, completedDiff, revision)).toBe(true);
222 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("diff");
223 });
224
225 it("drops the legacy singleton terminal surface during migration", () => {
226 expect(
227 migratePersistedRightPanelState({
228 byThreadKey: {
229 "env-1:thread-A": {
230 activeSurfaceId: "terminal",
231 surfaces: [
232 { id: "browser:tab-a", kind: "preview", resourceId: "tab-a" },
233 { id: "terminal", kind: "terminal" },
234 ],
235 },
236 },
237 }),
238 ).toEqual({
239 byThreadKey: {
240 "env-1:thread-A": {
241 isOpen: false,
242 activeSurfaceId: null,
243 surfaces: [{ id: "browser:tab-a", kind: "preview", resourceId: "tab-a" }],
244 },
245 },
246 threadPanelVisibilityByThreadKey: {},
247 });
248 });
249
250 it("upgrades saved single-session terminal surfaces to split-capable surfaces", () => {
251 expect(
252 migratePersistedRightPanelState({
253 byThreadKey: {
254 "env-1:thread-A": {
255 isOpen: true,
256 activeSurfaceId: "terminal:term-1",
257 surfaces: [{ id: "terminal:term-1", kind: "terminal", resourceId: "term-1" }],
258 },
259 },
260 }),
261 ).toEqual({
262 byThreadKey: {
263 "env-1:thread-A": {
264 isOpen: true,
265 activeSurfaceId: "terminal:term-1",
266 surfaces: [
267 {
268 id: "terminal:term-1",
269 kind: "terminal",
270 resourceId: "term-1",
271 terminalIds: ["term-1"],
272 activeTerminalId: "term-1",
273 },
274 ],
275 },
276 },
277 threadPanelVisibilityByThreadKey: {},
278 });
279 });
280
281 it("upgrades saved file surfaces with neutral reveal state", () => {
282 expect(
283 migratePersistedRightPanelState({
284 byThreadKey: {
285 "env-1:thread-A": {
286 isOpen: true,
287 activeSurfaceId: "file:src/index.ts",
288 surfaces: [{ id: "file:src/index.ts", kind: "file", relativePath: "src/index.ts" }],
289 },
290 },
291 }),
292 ).toEqual({
293 byThreadKey: {
294 "env-1:thread-A": {
295 isOpen: true,
296 activeSurfaceId: "file:src/index.ts",
297 surfaces: [
298 {
299 id: "file:src/index.ts",
300 kind: "file",
301 relativePath: "src/index.ts",
302 revealLine: null,
303 revealRequestId: 0,
304 },
305 ],
306 },
307 },
308 threadPanelVisibilityByThreadKey: {},
309 });
310 });
311
312 it("upgrades the legacy singleton pull request surface to a reference-keyed tab", () => {
313 const id = pullRequestSurfaceId({
314 projectId: "project-a",
315 repository: "pingdotgg/t3code",
316 number: 4909,
317 });
318 expect(
319 migratePersistedRightPanelState({
320 byThreadKey: {
321 "env-1:thread-A": {
322 isOpen: true,
323 activeSurfaceId: "pull-request",
324 surfaces: [
325 {
326 id: "pull-request",
327 kind: "pull-request",
328 projectId: "project-a",
329 repository: "pingdotgg/t3code",
330 number: 4909,
331 },
332 ],
333 },
334 },
335 }),
336 ).toEqual({
337 byThreadKey: {
338 "env-1:thread-A": {
339 isOpen: true,
340 activeSurfaceId: id,
341 surfaces: [
342 {
343 id,
344 kind: "pull-request",
345 projectId: "project-a",
346 repository: "pingdotgg/t3code",
347 number: 4909,
348 },
349 ],
350 },
351 },
352 threadPanelVisibilityByThreadKey: {},
353 });
354 });
355
356 it("drops the pull-request list's shared panel so a restart opens the page fresh", () => {
357 const id = pullRequestSurfaceId({
358 projectId: "project-a",
359 repository: "pingdotgg/t3code",
360 number: 4909,
361 });
362 const panelState = {
363 isOpen: true,
364 activeSurfaceId: id,
365 surfaces: [
366 {
367 id,
368 kind: "pull-request" as const,
369 projectId: "project-a",
370 repository: "pingdotgg/t3code",
371 number: 4909,
372 },
373 ],
374 };
375 expect(
376 migratePersistedRightPanelState({
377 byThreadKey: {
378 "env-1:pull-requests-panel": panelState,
379 "env-1:thread-A": panelState,
380 },
381 }),
382 ).toEqual({
383 byThreadKey: { "env-1:thread-A": panelState },
384 threadPanelVisibilityByThreadKey: {},
385 });
386 });
387
388 it("drops persisted plan surfaces and does not reopen an empty panel", () => {
389 expect(
390 migratePersistedRightPanelState({
391 byThreadKey: {
392 "env-1:thread-A": {
393 isOpen: true,
394 activeSurfaceId: "plan",
395 surfaces: [{ id: "plan", kind: "plan" }],
396 },
397 "env-1:thread-B": {
398 isOpen: true,
399 activeSurfaceId: "plan",
400 surfaces: [
401 { id: "plan", kind: "plan" },
402 { id: "diff", kind: "diff" },
403 ],
404 },
405 },
406 }),
407 ).toEqual({
408 byThreadKey: {
409 "env-1:thread-A": {
410 isOpen: false,
411 activeSurfaceId: null,
412 surfaces: [],
413 },
414 "env-1:thread-B": {
415 isOpen: true,
416 activeSurfaceId: "diff",
417 surfaces: [{ id: "diff", kind: "diff" }],
418 },
419 },
420 threadPanelVisibilityByThreadKey: {},
421 });
422 });
423
424 it("persists inline preference without restoring an open popover", () => {
425 expect(
426 migratePersistedRightPanelState({
427 threadPanelVisibilityByThreadKey: {
428 "env-1:thread-A": { inlineOpen: false, popoverOpen: true },
429 "env-1:thread-B": { inlineOpen: true, popoverOpen: true },
430 },
431 }),
432 ).toEqual({
433 byThreadKey: {},
434 threadPanelVisibilityByThreadKey: {
435 "env-1:thread-A": { inlineOpen: false, popoverOpen: false },
436 },
437 });
438 });
439
440 it("tracks inline and popover visibility independently", () => {
441 const store = useRightPanelStore.getState();
442
443 expect(selectThreadPanelOpen(store.threadPanelVisibilityByThreadKey, refA, "inline")).toBe(
444 true,
445 );
446 expect(selectThreadPanelOpen(store.threadPanelVisibilityByThreadKey, refA, "popover")).toBe(
447 false,
448 );
449
450 store.setThreadPanelOpen(refA, "inline", false);
451 store.toggleThreadPanel(refA, "popover");
452
453 expect(
454 selectThreadPanelVisibility(
455 useRightPanelStore.getState().threadPanelVisibilityByThreadKey,
456 refA,
457 ),
458 ).toEqual({ inlineOpen: false, popoverOpen: true });
459 expect(
460 selectThreadPanelVisibility(
461 useRightPanelStore.getState().threadPanelVisibilityByThreadKey,
462 refB,
463 ),
464 ).toEqual({ inlineOpen: true, popoverOpen: false });
465 });
466
467 it("closes the popover atomically when the real right panel opens", () => {
468 useRightPanelStore.getState().setThreadPanelOpen(refA, "popover", true);
469 useRightPanelStore.getState().open(refA, "diff");
470
471 expect(
472 selectThreadPanelVisibility(
473 useRightPanelStore.getState().threadPanelVisibilityByThreadKey,
474 refA,
475 ),
476 ).toEqual({ inlineOpen: true, popoverOpen: false });
477 });
478
479 it("keeps an open popover visible by promoting it to inline when the real panel closes", () => {
480 const store = useRightPanelStore.getState();
481 store.open(refA, "diff");
482 store.setThreadPanelOpen(refA, "inline", false);
483 store.setThreadPanelOpen(refA, "popover", true);
484 store.close(refA);
485
486 expect(
487 selectThreadPanelVisibility(
488 useRightPanelStore.getState().threadPanelVisibilityByThreadKey,
489 refA,
490 ),
491 ).toEqual({ inlineOpen: true, popoverOpen: true });
492 });
493
494 it("open sets the active panel for a thread", () => {
495 useRightPanelStore.getState().open(refA, "preview");
496 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("preview");
497 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refB)).toBeNull();
498 });
499
500 it("opening a different kind keeps both surfaces and activates the new one", () => {
501 useRightPanelStore.getState().open(refA, "agents");
502 useRightPanelStore.getState().open(refA, "preview");
503 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("preview");
504 expect(
505 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces,
506 ).toHaveLength(2);
507 });
508
509 it("reopening an inactive singleton activates its existing surface", () => {
510 useRightPanelStore.getState().open(refA, "diff");
511 useRightPanelStore.getState().open(refA, "agents");
512 useRightPanelStore.getState().open(refA, "diff");
513
514 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
515 isOpen: true,
516 activeSurfaceId: "diff",
517 surfaces: [
518 { id: "diff", kind: "diff" },
519 { id: "agents", kind: "agents" },
520 ],
521 });
522 });
523
524 it("keeps files as a singleton surface", () => {
525 useRightPanelStore.getState().open(refA, "files");
526 useRightPanelStore.getState().open(refA, "files");
527 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
528 isOpen: true,
529 activeSurfaceId: "files",
530 surfaces: [{ id: "files", kind: "files" }],
531 });
532 });
533
534 it("replaces the standalone explorer with peer file surfaces", () => {
535 useRightPanelStore.getState().open(refA, "files");
536 useRightPanelStore.getState().openFile(refA, "src/index.ts");
537 useRightPanelStore.getState().openFile(refA, "src/index.ts");
538 useRightPanelStore.getState().openFile(refA, "README.md");
539
540 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
541 isOpen: true,
542 activeSurfaceId: "file:README.md",
543 surfaces: [
544 {
545 id: "file:src/index.ts",
546 kind: "file",
547 relativePath: "src/index.ts",
548 revealLine: null,
549 revealRequestId: 2,
550 },
551 {
552 id: "file:README.md",
553 kind: "file",
554 relativePath: "README.md",
555 revealLine: null,
556 revealRequestId: 1,
557 },
558 ],
559 });
560 });
561
562 it.each([
563 ["generated\\", "generated"],
564 ["notes/meeting ", "notes/meeting"],
565 [" notes/meeting", "notes/meeting"],
566 ])("keeps %j and %j in separate file tabs", (firstPath, secondPath) => {
567 useRightPanelStore.getState().openFile(refA, firstPath);
568 useRightPanelStore.getState().openFile(refA, secondPath);
569
570 expect(
571 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces,
572 ).toMatchObject([
573 { id: `file:${firstPath}`, relativePath: firstPath },
574 { id: `file:${secondPath}`, relativePath: secondPath },
575 ]);
576 });
577
578 it.each([
579 ["docs/", "docs"],
580 ["docs///", "docs"],
581 ["/", "/"],
582 ["C:/", "C:/"],
583 ])("reuses the folder tab for %j and %j", (linkPath, treePath) => {
584 useRightPanelStore.getState().openFile(refA, linkPath);
585 useRightPanelStore.getState().openFile(refA, treePath);
586
587 expect(
588 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces,
589 ).toMatchObject([{ id: `file:${treePath}`, relativePath: treePath, revealRequestId: 2 }]);
590 });
591
592 it("opens an attachment as a file surface without the standalone explorer", () => {
593 const attachment = {
594 type: "file" as const,
595 id: "thread-A-attachment-pdf",
596 name: "report.pdf",
597 mimeType: "application/pdf",
598 sizeBytes: 42,
599 };
600 useRightPanelStore.getState().open(refA, "files");
601 useRightPanelStore.getState().openAttachment(refA, attachment);
602
603 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
604 isOpen: true,
605 activeSurfaceId: "attachment:thread-A-attachment-pdf",
606 surfaces: [
607 {
608 id: "attachment:thread-A-attachment-pdf",
609 kind: "file",
610 relativePath: "report.pdf",
611 revealLine: null,
612 revealRequestId: 0,
613 attachment,
614 },
615 ],
616 });
617 });
618
619 it("keeps attachment and workspace file ids disjoint", () => {
620 useRightPanelStore.getState().openFile(refA, "attachment:shared-id");
621 useRightPanelStore.getState().openAttachment(refA, {
622 type: "file",
623 id: "shared-id",
624 name: "report.pdf",
625 mimeType: "application/pdf",
626 sizeBytes: 42,
627 });
628
629 expect(
630 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces.map(
631 (surface) => surface.id,
632 ),
633 ).toEqual(["file:attachment:shared-id", "attachment:shared-id"]);
634 });
635
636 it("updates line reveal requests when reopening a file surface", () => {
637 useRightPanelStore.getState().openFile(refA, "src/index.ts", 42);
638 useRightPanelStore.getState().openFile(refA, "src/index.ts", 87);
639
640 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
641 isOpen: true,
642 activeSurfaceId: "file:src/index.ts",
643 surfaces: [
644 {
645 id: "file:src/index.ts",
646 kind: "file",
647 relativePath: "src/index.ts",
648 revealLine: 87,
649 revealRequestId: 2,
650 },
651 ],
652 });
653
654 useRightPanelStore.getState().openFile(refA, "src/index.ts");
655
656 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
657 isOpen: true,
658 activeSurfaceId: "file:src/index.ts",
659 surfaces: [
660 {
661 id: "file:src/index.ts",
662 kind: "file",
663 relativePath: "src/index.ts",
664 revealLine: null,
665 revealRequestId: 3,
666 },
667 ],
668 });
669 });
670
671 it("removes persisted file surfaces when their workspace no longer exists", () => {
672 useRightPanelStore.getState().openFile(refA, "src/index.ts");
673 useRightPanelStore.getState().open(refA, "agents");
674 useRightPanelStore.getState().openFile(refA, "README.md");
675
676 useRightPanelStore.getState().reconcileFileSurfaces(refA, false);
677
678 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
679 isOpen: true,
680 activeSurfaceId: "agents",
681 surfaces: [{ id: "agents", kind: "agents" }],
682 });
683
684 useRightPanelStore.getState().openFile(refB, "conductor.json");
685 useRightPanelStore.getState().reconcileFileSurfaces(refB, false);
686 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refB)).toEqual({
687 isOpen: false,
688 activeSurfaceId: null,
689 surfaces: [],
690 });
691 });
692
693 it("keeps attachment previews when their workspace is unavailable", () => {
694 const attachment = {
695 type: "file" as const,
696 id: "thread-A-attachment-pdf",
697 name: "report.pdf",
698 mimeType: "application/pdf",
699 sizeBytes: 42,
700 };
701 useRightPanelStore.getState().openFile(refA, "README.md");
702 useRightPanelStore.getState().openAttachment(refA, attachment);
703
704 useRightPanelStore.getState().reconcileFileSurfaces(refA, false);
705
706 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
707 isOpen: true,
708 activeSurfaceId: "attachment:thread-A-attachment-pdf",
709 surfaces: [
710 {
711 id: "attachment:thread-A-attachment-pdf",
712 kind: "file",
713 relativePath: "report.pdf",
714 revealLine: null,
715 revealRequestId: 0,
716 attachment,
717 },
718 ],
719 });
720 });
721
722 it("close hides the panel without clearing its selected surface", () => {
723 useRightPanelStore.getState().open(refA, "agents");
724 useRightPanelStore.getState().close(refA);
725 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
726 expect(
727 selectSelectedRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA),
728 ).toEqual({ id: "agents", kind: "agents" });
729 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
730 isOpen: false,
731 activeSurfaceId: "agents",
732 surfaces: [{ id: "agents", kind: "agents" }],
733 });
734 });
735
736 it("toggles empty panel visibility without creating a surface", () => {
737 useRightPanelStore.getState().toggleVisibility(refA);
738 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
739 isOpen: true,
740 activeSurfaceId: null,
741 surfaces: [],
742 });
743
744 useRightPanelStore.getState().toggleVisibility(refA);
745 expect(useRightPanelStore.getState().byThreadKey).toEqual({});
746 });
747
748 it("toggle hides the panel without discarding the active surface", () => {
749 useRightPanelStore.getState().toggle(refA, "diff");
750 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("diff");
751 useRightPanelStore.getState().toggle(refA, "diff");
752 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
753 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
754 isOpen: false,
755 activeSurfaceId: "diff",
756 surfaces: [{ id: "diff", kind: "diff" }],
757 });
758 });
759
760 it("toggle to a different kind switches active", () => {
761 useRightPanelStore.getState().toggle(refA, "preview");
762 useRightPanelStore.getState().toggle(refA, "agents");
763 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("agents");
764 });
765
766 it("removeThread clears persisted state", () => {
767 useRightPanelStore.getState().open(refA, "agents");
768 useRightPanelStore.getState().removeThread(refA);
769 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
770 });
771
772 it("close on never-opened thread is a no-op", () => {
773 useRightPanelStore.getState().close(refA);
774 expect(useRightPanelStore.getState().byThreadKey).toEqual({});
775 });
776
777 it("tracks one surface per browser session", () => {
778 useRightPanelStore.getState().openBrowser(refA, "tab-a");
779 useRightPanelStore.getState().openBrowser(refA, "tab-b");
780
781 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
782 expect(state.surfaces.map((surface) => surface.id)).toEqual(["browser:tab-a", "browser:tab-b"]);
783 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
784 id: "browser:tab-b",
785 kind: "preview",
786 resourceId: "tab-b",
787 });
788 });
789
790 it("tracks one surface per pull request", () => {
791 const first = { projectId: "project-a", repository: "pingdotgg/t3code", number: 4909 };
792 const second = { projectId: "project-a", repository: "pingdotgg/t3code", number: 4910 };
793 useRightPanelStore.getState().openPullRequest(refA, first);
794 useRightPanelStore.getState().openPullRequest(refA, second);
795 const url = "https://gitlab.example.com/pingdotgg/t3code/-/merge_requests/4909";
796 useRightPanelStore.getState().openPullRequest(refA, { ...first, url });
797 useRightPanelStore.getState().openPullRequest(refA, first);
798
799 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
800 expect(state.surfaces.map((surface) => surface.id)).toEqual([
801 pullRequestSurfaceId(first),
802 pullRequestSurfaceId(second),
803 ]);
804 expect(state.activeSurfaceId).toBe(pullRequestSurfaceId(first));
805 expect(
806 selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA),
807 ).toMatchObject({ url });
808 expect(state.surfaces[1]).not.toHaveProperty("url");
809 });
810
811 it("keeps matching repository and number on different hosts as separate tabs", () => {
812 const first = { projectId: "project-a", repository: "acme/api", number: 7, host: "github.com" };
813 const second = { ...first, host: "github.example.com" };
814 useRightPanelStore.getState().openPullRequest(refA, first);
815 useRightPanelStore.getState().openPullRequest(refA, second);
816 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
817 expect(state.surfaces).toEqual([pullRequestSurface(first), pullRequestSurface(second)]);
818 expect(pullRequestSurfaceId({ ...first, host: "GITHUB.COM" })).toBe(
819 pullRequestSurfaceId(first),
820 );
821 });
822
823 it("keeps one pull request read from two servers as two tabs", () => {
824 const local = {
825 environmentId: "local",
826 projectId: "project-a",
827 repository: "pingdotgg/t3code",
828 number: 4909,
829 };
830 const remote = { ...local, environmentId: "remote" };
831
832 useRightPanelStore.getState().openPullRequest(refA, local);
833 useRightPanelStore.getState().openPullRequest(refA, remote);
834
835 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
836 expect(state.surfaces.map((surface) => surface.id)).toEqual([
837 pullRequestSurfaceId(local),
838 pullRequestSurfaceId(remote),
839 ]);
840 });
841
842 it("keeps the page's panel tabs reachable when the set of connected servers changes", () => {
843 // The pull-requests page keys its one shared panel by a fixed sentinel environment, not by
844 // whichever capable server happens to sort first (see PULL_REQUESTS_PANEL_ENVIRONMENT_ID in
845 // _chat.pull-requests.tsx) — a server disconnecting must not move every open tab to a store
846 // key nobody wrote them under.
847 const panelId = ThreadId.make("pull-requests-panel");
848 const stableRef = scopeThreadRef("pull-requests-panel" as EnvironmentId, panelId);
849 const fromServerA = {
850 environmentId: "server-a",
851 projectId: "project-a",
852 repository: "pingdotgg/t3code",
853 number: 1,
854 };
855 const fromServerB = {
856 environmentId: "server-b",
857 projectId: "project-b",
858 repository: "pingdotgg/t3code",
859 number: 2,
860 };
861
862 // Both servers connected: tabs from each open under the one stable ref.
863 useRightPanelStore.getState().openPullRequest(stableRef, fromServerA);
864 useRightPanelStore.getState().openPullRequest(stableRef, fromServerB);
865
866 // Server A disconnects. The stable ref does not depend on which servers remain connected, so
867 // the same lookup still finds both tabs.
868 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, stableRef);
869 expect(state.surfaces.map((surface) => surface.id)).toEqual([
870 pullRequestSurfaceId(fromServerA),
871 pullRequestSurfaceId(fromServerB),
872 ]);
873
874 // The bug this guards against: a ref keyed by the first capable environment instead of a
875 // fixed sentinel changes identity when that environment drops out, and a lookup under the new
876 // key finds nothing even though the tabs are still sitting under the old one.
877 const refWhileBothConnected = scopeThreadRef("server-a" as EnvironmentId, panelId);
878 const refAfterServerADisconnects = scopeThreadRef("server-b" as EnvironmentId, panelId);
879 expect(refWhileBothConnected).not.toEqual(refAfterServerADisconnects);
880 expect(
881 selectThreadRightPanelState(
882 useRightPanelStore.getState().byThreadKey,
883 refAfterServerADisconnects,
884 ).surfaces,
885 ).toEqual([]);
886 });
887
888 it("tracks one surface per terminal session", () => {
889 useRightPanelStore.getState().openTerminal(refA, "term-1");
890 useRightPanelStore.getState().openTerminal(refA, "term-2");
891
892 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
893 expect(state.surfaces).toEqual([
894 {
895 id: "terminal:term-1",
896 kind: "terminal",
897 resourceId: "term-1",
898 terminalIds: ["term-1"],
899 activeTerminalId: "term-1",
900 },
901 {
902 id: "terminal:term-2",
903 kind: "terminal",
904 resourceId: "term-2",
905 terminalIds: ["term-2"],
906 activeTerminalId: "term-2",
907 },
908 ]);
909 expect(state.activeSurfaceId).toBe("terminal:term-2");
910 });
911
912 it("tracks split panes and the active pane within a terminal surface", () => {
913 useRightPanelStore.getState().openTerminal(refA, "term-1");
914 useRightPanelStore.getState().splitTerminal(refA, "terminal:term-1", "term-2");
915
916 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
917 id: "terminal:term-1",
918 kind: "terminal",
919 resourceId: "term-1",
920 terminalIds: ["term-1", "term-2"],
921 activeTerminalId: "term-2",
922 });
923
924 useRightPanelStore.getState().activateTerminal(refA, "terminal:term-1", "term-1");
925 useRightPanelStore.getState().closeTerminal(refA, "terminal:term-1", "term-1");
926 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
927 id: "terminal:term-1",
928 kind: "terminal",
929 resourceId: "term-1",
930 terminalIds: ["term-2"],
931 activeTerminalId: "term-2",
932 });
933 });
934
935 it("tracks vertical layout for a terminal surface", () => {
936 useRightPanelStore.getState().openTerminal(refA, "term-1");
937 useRightPanelStore.getState().splitTerminal(refA, "terminal:term-1", "term-2", "vertical");
938
939 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
940 id: "terminal:term-1",
941 kind: "terminal",
942 resourceId: "term-1",
943 terminalIds: ["term-1", "term-2"],
944 activeTerminalId: "term-2",
945 splitDirection: "vertical",
946 });
947 });
948
949 it("closing the final terminal pane removes its surface and closes the panel", () => {
950 useRightPanelStore.getState().openTerminal(refA, "term-1");
951 useRightPanelStore.getState().closeTerminal(refA, "terminal:term-1", "term-1");
952
953 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
954 isOpen: false,
955 activeSurfaceId: null,
956 surfaces: [],
957 });
958 });
959
960 it("closing the active surface activates a neighboring surface", () => {
961 useRightPanelStore.getState().openBrowser(refA, "tab-a");
962 useRightPanelStore.getState().openTerminal(refA, "term-1");
963 useRightPanelStore.getState().closeSurface(refA, "terminal:term-1");
964
965 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)?.id).toBe(
966 "browser:tab-a",
967 );
968 });
969
970 it("closing the final surface closes the panel", () => {
971 useRightPanelStore.getState().openTerminal(refA, "term-1");
972 useRightPanelStore.getState().closeSurface(refA, "terminal:term-1");
973
974 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
975 isOpen: false,
976 activeSurfaceId: null,
977 surfaces: [],
978 });
979 });
980
981 it("closing other surfaces keeps the selected surface active", () => {
982 useRightPanelStore.getState().openBrowser(refA, "tab-a");
983 useRightPanelStore.getState().openFile(refA, "src/index.ts");
984 useRightPanelStore.getState().openTerminal(refA, "term-1");
985
986 useRightPanelStore.getState().closeOtherSurfaces(refA, "file:src/index.ts");
987
988 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
989 isOpen: true,
990 activeSurfaceId: "file:src/index.ts",
991 surfaces: [
992 {
993 id: "file:src/index.ts",
994 kind: "file",
995 relativePath: "src/index.ts",
996 revealLine: null,
997 revealRequestId: 1,
998 },
999 ],
1000 });
1001 });
1002
1003 it("closing surfaces to the right activates the selected surface when active was removed", () => {
1004 useRightPanelStore.getState().openBrowser(refA, "tab-a");
1005 useRightPanelStore.getState().openFile(refA, "src/index.ts");
1006 useRightPanelStore.getState().openTerminal(refA, "term-1");
1007
1008 useRightPanelStore.getState().closeSurfacesToRight(refA, "browser:tab-a");
1009
1010 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
1011 isOpen: true,
1012 activeSurfaceId: "browser:tab-a",
1013 surfaces: [{ id: "browser:tab-a", kind: "preview", resourceId: "tab-a" }],
1014 });
1015 });
1016
1017 it("closing all surfaces closes the panel", () => {
1018 useRightPanelStore.getState().openBrowser(refA, "tab-a");
1019 useRightPanelStore.getState().openFile(refA, "src/index.ts");
1020
1021 useRightPanelStore.getState().closeAllSurfaces(refA);
1022
1023 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
1024 isOpen: false,
1025 activeSurfaceId: null,
1026 surfaces: [],
1027 });
1028 });
1029
1030 it("reconciles browser surfaces without deleting other surface kinds", () => {
1031 useRightPanelStore.getState().openTerminal(refA, "term-1");
1032 useRightPanelStore.getState().openBrowser(refA, "tab-a");
1033 useRightPanelStore.getState().openBrowser(refA, "tab-b");
1034 useRightPanelStore.getState().reconcileBrowserSurfaces(refA, ["tab-b", "tab-c"]);
1035
1036 expect(
1037 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces.map(
1038 (surface) => surface.id,
1039 ),
1040 ).toEqual(["terminal:term-1", "browser:tab-b", "browser:tab-c"]);
1041 });
1042});