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