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