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.each([
389 { kind: "plan", isOpen: true },
390 { kind: "agents", isOpen: true },
391 { kind: "agents", isOpen: false },
392 ])("drops $kind with isOpen=$isOpen and falls back", ({ kind, isOpen }) => {
393 expect(
394 migratePersistedRightPanelState({
395 byThreadKey: {
396 "env-1:thread-A": {
397 isOpen,
398 activeSurfaceId: kind,
399 surfaces: [{ id: kind, kind }],
400 },
401 "env-1:thread-B": {
402 isOpen,
403 activeSurfaceId: kind,
404 surfaces: [
405 { id: kind, kind },
406 { id: "diff", kind: "diff" },
407 ],
408 },
409 },
410 }),
411 ).toEqual({
412 byThreadKey: {
413 "env-1:thread-A": {
414 isOpen: false,
415 activeSurfaceId: null,
416 surfaces: [],
417 },
418 "env-1:thread-B": {
419 isOpen,
420 activeSurfaceId: "diff",
421 surfaces: [{ id: "diff", kind: "diff" }],
422 },
423 },
424 threadPanelVisibilityByThreadKey: {},
425 });
426 });
427
428 it("persists inline preference without restoring an open popover", () => {
429 expect(
430 migratePersistedRightPanelState({
431 threadPanelVisibilityByThreadKey: {
432 "env-1:thread-A": { inlineOpen: false, popoverOpen: true },
433 "env-1:thread-B": { inlineOpen: true, popoverOpen: true },
434 },
435 }),
436 ).toEqual({
437 byThreadKey: {},
438 threadPanelVisibilityByThreadKey: {
439 "env-1:thread-A": { inlineOpen: false, popoverOpen: false },
440 },
441 });
442 });
443
444 it("tracks inline and popover visibility independently", () => {
445 const store = useRightPanelStore.getState();
446
447 expect(selectThreadPanelOpen(store.threadPanelVisibilityByThreadKey, refA, "inline")).toBe(
448 true,
449 );
450 expect(selectThreadPanelOpen(store.threadPanelVisibilityByThreadKey, refA, "popover")).toBe(
451 false,
452 );
453
454 store.setThreadPanelOpen(refA, "inline", false);
455 store.toggleThreadPanel(refA, "popover");
456
457 expect(
458 selectThreadPanelVisibility(
459 useRightPanelStore.getState().threadPanelVisibilityByThreadKey,
460 refA,
461 ),
462 ).toEqual({ inlineOpen: false, popoverOpen: true });
463 expect(
464 selectThreadPanelVisibility(
465 useRightPanelStore.getState().threadPanelVisibilityByThreadKey,
466 refB,
467 ),
468 ).toEqual({ inlineOpen: true, popoverOpen: false });
469 });
470
471 it("closes the popover atomically when the real right panel opens", () => {
472 useRightPanelStore.getState().setThreadPanelOpen(refA, "popover", true);
473 useRightPanelStore.getState().open(refA, "diff");
474
475 expect(
476 selectThreadPanelVisibility(
477 useRightPanelStore.getState().threadPanelVisibilityByThreadKey,
478 refA,
479 ),
480 ).toEqual({ inlineOpen: true, popoverOpen: false });
481 });
482
483 it("keeps an open popover visible by promoting it to inline when the real panel closes", () => {
484 const store = useRightPanelStore.getState();
485 store.open(refA, "diff");
486 store.setThreadPanelOpen(refA, "inline", false);
487 store.setThreadPanelOpen(refA, "popover", true);
488 store.close(refA);
489
490 expect(
491 selectThreadPanelVisibility(
492 useRightPanelStore.getState().threadPanelVisibilityByThreadKey,
493 refA,
494 ),
495 ).toEqual({ inlineOpen: true, popoverOpen: true });
496 });
497
498 it("open sets the active panel for a thread", () => {
499 useRightPanelStore.getState().open(refA, "preview");
500 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("preview");
501 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refB)).toBeNull();
502 });
503
504 it("opening a different kind keeps both surfaces and activates the new one", () => {
505 useRightPanelStore.getState().open(refA, "device");
506 useRightPanelStore.getState().open(refA, "preview");
507 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("preview");
508 expect(
509 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces,
510 ).toHaveLength(2);
511 });
512
513 it("reopening an inactive singleton activates its existing surface", () => {
514 useRightPanelStore.getState().open(refA, "diff");
515 useRightPanelStore.getState().open(refA, "device");
516 useRightPanelStore.getState().open(refA, "diff");
517
518 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
519 isOpen: true,
520 activeSurfaceId: "diff",
521 surfaces: [
522 { id: "diff", kind: "diff" },
523 { id: "device", kind: "device" },
524 ],
525 });
526 });
527
528 it("keeps files as a singleton surface", () => {
529 useRightPanelStore.getState().open(refA, "files");
530 useRightPanelStore.getState().open(refA, "files");
531 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
532 isOpen: true,
533 activeSurfaceId: "files",
534 surfaces: [{ id: "files", kind: "files" }],
535 });
536 });
537
538 it("opens workspace-root links as the singleton files explorer", () => {
539 const store = useRightPanelStore.getState();
540 store.openFile(refA, "README.md");
541 store.openFile(refA, ".");
542 store.openFile(refA, ".");
543 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
544 expect(state.activeSurfaceId).toBe("files");
545 expect(state.surfaces.map((surface) => surface.id)).toEqual(["file:README.md", "files"]);
546 store.closeSurface(refA, "files");
547 expect(
548 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).activeSurfaceId,
549 ).toBe("file:README.md");
550 store.openFile(refA, ".");
551 expect(
552 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).activeSurfaceId,
553 ).toBe("files");
554 });
555
556 it("replaces the standalone explorer with peer file surfaces", () => {
557 useRightPanelStore.getState().open(refA, "files");
558 useRightPanelStore.getState().openFile(refA, "src/index.ts");
559 useRightPanelStore.getState().openFile(refA, "src/index.ts");
560 useRightPanelStore.getState().openFile(refA, "README.md");
561
562 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
563 isOpen: true,
564 activeSurfaceId: "file:README.md",
565 surfaces: [
566 {
567 id: "file:src/index.ts",
568 kind: "file",
569 relativePath: "src/index.ts",
570 revealLine: null,
571 revealRequestId: 2,
572 },
573 {
574 id: "file:README.md",
575 kind: "file",
576 relativePath: "README.md",
577 revealLine: null,
578 revealRequestId: 1,
579 },
580 ],
581 });
582 });
583
584 it.each([
585 ["generated\\", "generated"],
586 ["notes/meeting ", "notes/meeting"],
587 [" notes/meeting", "notes/meeting"],
588 ])("keeps %j and %j in separate file tabs", (firstPath, secondPath) => {
589 useRightPanelStore.getState().openFile(refA, firstPath);
590 useRightPanelStore.getState().openFile(refA, secondPath);
591
592 expect(
593 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces,
594 ).toMatchObject([
595 { id: `file:${firstPath}`, relativePath: firstPath },
596 { id: `file:${secondPath}`, relativePath: secondPath },
597 ]);
598 });
599
600 it.each([
601 ["docs/", "docs"],
602 ["docs///", "docs"],
603 ["/", "/"],
604 ["C:/", "C:/"],
605 ])("reuses the folder tab for %j and %j", (linkPath, treePath) => {
606 useRightPanelStore.getState().openFile(refA, linkPath);
607 useRightPanelStore.getState().openFile(refA, treePath);
608
609 expect(
610 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces,
611 ).toMatchObject([{ id: `file:${treePath}`, relativePath: treePath, revealRequestId: 2 }]);
612 });
613
614 it("opens an attachment as a file surface without the standalone explorer", () => {
615 const attachment = {
616 type: "file" as const,
617 id: "thread-A-attachment-pdf",
618 name: "report.pdf",
619 mimeType: "application/pdf",
620 sizeBytes: 42,
621 };
622 useRightPanelStore.getState().open(refA, "files");
623 useRightPanelStore.getState().openAttachment(refA, attachment);
624
625 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
626 isOpen: true,
627 activeSurfaceId: "attachment:thread-A-attachment-pdf",
628 surfaces: [
629 {
630 id: "attachment:thread-A-attachment-pdf",
631 kind: "file",
632 relativePath: "report.pdf",
633 revealLine: null,
634 revealRequestId: 0,
635 attachment,
636 },
637 ],
638 });
639 });
640
641 it("keeps attachment and workspace file ids disjoint", () => {
642 useRightPanelStore.getState().openFile(refA, "attachment:shared-id");
643 useRightPanelStore.getState().openAttachment(refA, {
644 type: "file",
645 id: "shared-id",
646 name: "report.pdf",
647 mimeType: "application/pdf",
648 sizeBytes: 42,
649 });
650
651 expect(
652 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces.map(
653 (surface) => surface.id,
654 ),
655 ).toEqual(["file:attachment:shared-id", "attachment:shared-id"]);
656 });
657
658 it("updates line reveal requests when reopening a file surface", () => {
659 useRightPanelStore.getState().openFile(refA, "src/index.ts", 42);
660 useRightPanelStore.getState().openFile(refA, "src/index.ts", 87);
661
662 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
663 isOpen: true,
664 activeSurfaceId: "file:src/index.ts",
665 surfaces: [
666 {
667 id: "file:src/index.ts",
668 kind: "file",
669 relativePath: "src/index.ts",
670 revealLine: 87,
671 revealRequestId: 2,
672 },
673 ],
674 });
675
676 useRightPanelStore.getState().openFile(refA, "src/index.ts");
677
678 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
679 isOpen: true,
680 activeSurfaceId: "file:src/index.ts",
681 surfaces: [
682 {
683 id: "file:src/index.ts",
684 kind: "file",
685 relativePath: "src/index.ts",
686 revealLine: null,
687 revealRequestId: 3,
688 },
689 ],
690 });
691 });
692
693 it("removes persisted file surfaces when their workspace no longer exists", () => {
694 useRightPanelStore.getState().openFile(refA, "src/index.ts");
695 useRightPanelStore.getState().open(refA, "device");
696 useRightPanelStore.getState().openFile(refA, "README.md");
697
698 useRightPanelStore.getState().reconcileFileSurfaces(refA, false);
699
700 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
701 isOpen: true,
702 activeSurfaceId: "device",
703 surfaces: [{ id: "device", kind: "device" }],
704 });
705
706 useRightPanelStore.getState().openFile(refB, "conductor.json");
707 useRightPanelStore.getState().reconcileFileSurfaces(refB, false);
708 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refB)).toEqual({
709 isOpen: false,
710 activeSurfaceId: null,
711 surfaces: [],
712 });
713 });
714
715 it("keeps attachment previews when their workspace is unavailable", () => {
716 const attachment = {
717 type: "file" as const,
718 id: "thread-A-attachment-pdf",
719 name: "report.pdf",
720 mimeType: "application/pdf",
721 sizeBytes: 42,
722 };
723 useRightPanelStore.getState().openFile(refA, "README.md");
724 useRightPanelStore.getState().openAttachment(refA, attachment);
725
726 useRightPanelStore.getState().reconcileFileSurfaces(refA, false);
727
728 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
729 isOpen: true,
730 activeSurfaceId: "attachment:thread-A-attachment-pdf",
731 surfaces: [
732 {
733 id: "attachment:thread-A-attachment-pdf",
734 kind: "file",
735 relativePath: "report.pdf",
736 revealLine: null,
737 revealRequestId: 0,
738 attachment,
739 },
740 ],
741 });
742 });
743
744 it("close hides the panel without clearing its selected surface", () => {
745 useRightPanelStore.getState().open(refA, "device");
746 useRightPanelStore.getState().close(refA);
747 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
748 expect(
749 selectSelectedRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA),
750 ).toEqual({ id: "device", kind: "device" });
751 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
752 isOpen: false,
753 activeSurfaceId: "device",
754 surfaces: [{ id: "device", kind: "device" }],
755 });
756 });
757
758 it("toggles empty panel visibility without creating a surface", () => {
759 useRightPanelStore.getState().toggleVisibility(refA);
760 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
761 isOpen: true,
762 activeSurfaceId: null,
763 surfaces: [],
764 });
765
766 useRightPanelStore.getState().toggleVisibility(refA);
767 expect(useRightPanelStore.getState().byThreadKey).toEqual({});
768 });
769
770 it("toggle hides the panel without discarding the active surface", () => {
771 useRightPanelStore.getState().toggle(refA, "diff");
772 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("diff");
773 useRightPanelStore.getState().toggle(refA, "diff");
774 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
775 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
776 isOpen: false,
777 activeSurfaceId: "diff",
778 surfaces: [{ id: "diff", kind: "diff" }],
779 });
780 });
781
782 it("toggle to a different kind switches active", () => {
783 useRightPanelStore.getState().toggle(refA, "preview");
784 useRightPanelStore.getState().toggle(refA, "device");
785 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("device");
786 });
787
788 it("removeThread clears persisted state", () => {
789 useRightPanelStore.getState().open(refA, "device");
790 useRightPanelStore.getState().removeThread(refA);
791 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
792 });
793
794 it("close on never-opened thread is a no-op", () => {
795 useRightPanelStore.getState().close(refA);
796 expect(useRightPanelStore.getState().byThreadKey).toEqual({});
797 });
798
799 it("tracks one surface per browser session", () => {
800 useRightPanelStore.getState().openBrowser(refA, "tab-a");
801 useRightPanelStore.getState().openBrowser(refA, "tab-b");
802
803 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
804 expect(state.surfaces.map((surface) => surface.id)).toEqual(["browser:tab-a", "browser:tab-b"]);
805 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
806 id: "browser:tab-b",
807 kind: "preview",
808 resourceId: "tab-b",
809 });
810 });
811
812 it("tracks one surface per pull request", () => {
813 const first = { projectId: "project-a", repository: "pingdotgg/t3code", number: 4909 };
814 const second = { projectId: "project-a", repository: "pingdotgg/t3code", number: 4910 };
815 useRightPanelStore.getState().openPullRequest(refA, first);
816 useRightPanelStore.getState().openPullRequest(refA, second);
817 const url = "https://gitlab.example.com/pingdotgg/t3code/-/merge_requests/4909";
818 useRightPanelStore.getState().openPullRequest(refA, { ...first, url });
819 useRightPanelStore.getState().openPullRequest(refA, first);
820
821 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
822 expect(state.surfaces.map((surface) => surface.id)).toEqual([
823 pullRequestSurfaceId(first),
824 pullRequestSurfaceId(second),
825 ]);
826 expect(state.activeSurfaceId).toBe(pullRequestSurfaceId(first));
827 expect(
828 selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA),
829 ).toMatchObject({ url });
830 expect(state.surfaces[1]).not.toHaveProperty("url");
831 });
832
833 it("keeps matching repository and number on different hosts as separate tabs", () => {
834 const first = { projectId: "project-a", repository: "acme/api", number: 7, host: "github.com" };
835 const second = { ...first, host: "github.example.com" };
836 useRightPanelStore.getState().openPullRequest(refA, first);
837 useRightPanelStore.getState().openPullRequest(refA, second);
838 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
839 expect(state.surfaces).toEqual([pullRequestSurface(first), pullRequestSurface(second)]);
840 expect(pullRequestSurfaceId({ ...first, host: "GITHUB.COM" })).toBe(
841 pullRequestSurfaceId(first),
842 );
843 });
844
845 it("keeps one pull request read from two servers as two tabs", () => {
846 const local = {
847 environmentId: "local",
848 projectId: "project-a",
849 repository: "pingdotgg/t3code",
850 number: 4909,
851 };
852 const remote = { ...local, environmentId: "remote" };
853
854 useRightPanelStore.getState().openPullRequest(refA, local);
855 useRightPanelStore.getState().openPullRequest(refA, remote);
856
857 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
858 expect(state.surfaces.map((surface) => surface.id)).toEqual([
859 pullRequestSurfaceId(local),
860 pullRequestSurfaceId(remote),
861 ]);
862 });
863
864 it("keeps the page's panel tabs reachable when the set of connected servers changes", () => {
865 // The pull-requests page keys its one shared panel by a fixed sentinel environment, not by
866 // whichever capable server happens to sort first (see PULL_REQUESTS_PANEL_ENVIRONMENT_ID in
867 // _chat.pull-requests.tsx) — a server disconnecting must not move every open tab to a store
868 // key nobody wrote them under.
869 const panelId = ThreadId.make("pull-requests-panel");
870 const stableRef = scopeThreadRef("pull-requests-panel" as EnvironmentId, panelId);
871 const fromServerA = {
872 environmentId: "server-a",
873 projectId: "project-a",
874 repository: "pingdotgg/t3code",
875 number: 1,
876 };
877 const fromServerB = {
878 environmentId: "server-b",
879 projectId: "project-b",
880 repository: "pingdotgg/t3code",
881 number: 2,
882 };
883
884 // Both servers connected: tabs from each open under the one stable ref.
885 useRightPanelStore.getState().openPullRequest(stableRef, fromServerA);
886 useRightPanelStore.getState().openPullRequest(stableRef, fromServerB);
887
888 // Server A disconnects. The stable ref does not depend on which servers remain connected, so
889 // the same lookup still finds both tabs.
890 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, stableRef);
891 expect(state.surfaces.map((surface) => surface.id)).toEqual([
892 pullRequestSurfaceId(fromServerA),
893 pullRequestSurfaceId(fromServerB),
894 ]);
895
896 // The bug this guards against: a ref keyed by the first capable environment instead of a
897 // fixed sentinel changes identity when that environment drops out, and a lookup under the new
898 // key finds nothing even though the tabs are still sitting under the old one.
899 const refWhileBothConnected = scopeThreadRef("server-a" as EnvironmentId, panelId);
900 const refAfterServerADisconnects = scopeThreadRef("server-b" as EnvironmentId, panelId);
901 expect(refWhileBothConnected).not.toEqual(refAfterServerADisconnects);
902 expect(
903 selectThreadRightPanelState(
904 useRightPanelStore.getState().byThreadKey,
905 refAfterServerADisconnects,
906 ).surfaces,
907 ).toEqual([]);
908 });
909
910 it("tracks one surface per terminal session", () => {
911 useRightPanelStore.getState().openTerminal(refA, "term-1");
912 useRightPanelStore.getState().openTerminal(refA, "term-2");
913
914 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
915 expect(state.surfaces).toEqual([
916 {
917 id: "terminal:term-1",
918 kind: "terminal",
919 resourceId: "term-1",
920 terminalIds: ["term-1"],
921 activeTerminalId: "term-1",
922 },
923 {
924 id: "terminal:term-2",
925 kind: "terminal",
926 resourceId: "term-2",
927 terminalIds: ["term-2"],
928 activeTerminalId: "term-2",
929 },
930 ]);
931 expect(state.activeSurfaceId).toBe("terminal:term-2");
932 });
933
934 it("tracks split panes and the active pane within a terminal surface", () => {
935 useRightPanelStore.getState().openTerminal(refA, "term-1");
936 useRightPanelStore.getState().splitTerminal(refA, "terminal:term-1", "term-2");
937
938 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
939 id: "terminal:term-1",
940 kind: "terminal",
941 resourceId: "term-1",
942 terminalIds: ["term-1", "term-2"],
943 activeTerminalId: "term-2",
944 });
945
946 useRightPanelStore.getState().activateTerminal(refA, "terminal:term-1", "term-1");
947 useRightPanelStore.getState().closeTerminal(refA, "terminal:term-1", "term-1");
948 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
949 id: "terminal:term-1",
950 kind: "terminal",
951 resourceId: "term-1",
952 terminalIds: ["term-2"],
953 activeTerminalId: "term-2",
954 });
955 });
956
957 it("tracks vertical layout for a terminal surface", () => {
958 useRightPanelStore.getState().openTerminal(refA, "term-1");
959 useRightPanelStore.getState().splitTerminal(refA, "terminal:term-1", "term-2", "vertical");
960
961 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
962 id: "terminal:term-1",
963 kind: "terminal",
964 resourceId: "term-1",
965 terminalIds: ["term-1", "term-2"],
966 activeTerminalId: "term-2",
967 splitDirection: "vertical",
968 });
969 });
970
971 it("closing the final terminal pane removes its surface and closes the panel", () => {
972 useRightPanelStore.getState().openTerminal(refA, "term-1");
973 useRightPanelStore.getState().closeTerminal(refA, "terminal:term-1", "term-1");
974
975 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
976 isOpen: false,
977 activeSurfaceId: null,
978 surfaces: [],
979 });
980 });
981
982 it("closing the active surface activates a neighboring surface", () => {
983 useRightPanelStore.getState().openBrowser(refA, "tab-a");
984 useRightPanelStore.getState().openTerminal(refA, "term-1");
985 useRightPanelStore.getState().closeSurface(refA, "terminal:term-1");
986
987 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)?.id).toBe(
988 "browser:tab-a",
989 );
990 });
991
992 it("closing the final surface closes the panel", () => {
993 useRightPanelStore.getState().openTerminal(refA, "term-1");
994 useRightPanelStore.getState().closeSurface(refA, "terminal:term-1");
995
996 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
997 isOpen: false,
998 activeSurfaceId: null,
999 surfaces: [],
1000 });
1001 });
1002
1003 it("closing other surfaces keeps the selected surface active", () => {
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().closeOtherSurfaces(refA, "file:src/index.ts");
1009
1010 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
1011 isOpen: true,
1012 activeSurfaceId: "file:src/index.ts",
1013 surfaces: [
1014 {
1015 id: "file:src/index.ts",
1016 kind: "file",
1017 relativePath: "src/index.ts",
1018 revealLine: null,
1019 revealRequestId: 1,
1020 },
1021 ],
1022 });
1023 });
1024
1025 it("closing surfaces to the right activates the selected surface when active was removed", () => {
1026 useRightPanelStore.getState().openBrowser(refA, "tab-a");
1027 useRightPanelStore.getState().openFile(refA, "src/index.ts");
1028 useRightPanelStore.getState().openTerminal(refA, "term-1");
1029
1030 useRightPanelStore.getState().closeSurfacesToRight(refA, "browser:tab-a");
1031
1032 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
1033 isOpen: true,
1034 activeSurfaceId: "browser:tab-a",
1035 surfaces: [{ id: "browser:tab-a", kind: "preview", resourceId: "tab-a" }],
1036 });
1037 });
1038
1039 it("closing all surfaces closes the panel", () => {
1040 useRightPanelStore.getState().openBrowser(refA, "tab-a");
1041 useRightPanelStore.getState().openFile(refA, "src/index.ts");
1042
1043 useRightPanelStore.getState().closeAllSurfaces(refA);
1044
1045 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
1046 isOpen: false,
1047 activeSurfaceId: null,
1048 surfaces: [],
1049 });
1050 });
1051
1052 it("does not replace a stale surface with a hidden tab, but preserves explicit opens", () => {
1053 const store = useRightPanelStore.getState();
1054 store.openBrowser(refA, "stale-tab");
1055 store.reconcileBrowserSurfaces(refA, ["hidden-tab"], new Set(["hidden-tab"]));
1056 expect(
1057 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA),
1058 ).toMatchObject({
1059 activeSurfaceId: null,
1060 surfaces: [],
1061 });
1062
1063 store.openBrowser(refA, "hidden-tab");
1064 store.reconcileBrowserSurfaces(
1065 refA,
1066 ["hidden-tab", "other-tab"],
1067 new Set(["hidden-tab", "other-tab"]),
1068 );
1069 expect(
1070 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA),
1071 ).toMatchObject({
1072 isOpen: true,
1073 activeSurfaceId: "browser:hidden-tab",
1074 surfaces: [{ id: "browser:hidden-tab", kind: "preview", resourceId: "hidden-tab" }],
1075 });
1076 });
1077
1078 it("reconciles browser surfaces without deleting other surface kinds", () => {
1079 useRightPanelStore.getState().openTerminal(refA, "term-1");
1080 useRightPanelStore.getState().openBrowser(refA, "tab-a");
1081 useRightPanelStore.getState().openBrowser(refA, "tab-b");
1082 useRightPanelStore.getState().reconcileBrowserSurfaces(refA, ["tab-b", "tab-c"]);
1083
1084 expect(
1085 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces.map(
1086 (surface) => surface.id,
1087 ),
1088 ).toEqual(["terminal:term-1", "browser:tab-b", "browser:tab-c"]);
1089 });
1090});