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("keeps a maximized panel per thread without counting it as a manual choice", () => {
326 const store = useRightPanelStore.getState();
327 store.open(refA, "preview");
328 store.open(refB, "diff");
329 const revision = store.getUserActionRevision(refA);
330 store.setMaximized(refA, true);
331 store.setMaximized(refB, true);
332 store.setMaximized(refB, false);
333
334 const { byThreadKey } = useRightPanelStore.getState();
335 expect(selectThreadRightPanelState(byThreadKey, refA).maximized).toBe(true);
336 expect(selectThreadRightPanelState(byThreadKey, refB).maximized).toBeUndefined();
337 expect(store.getUserActionRevision(refA)).toBe(revision);
338 });
339
340 it("restores a saved maximized panel during migration", () => {
341 const migrated = migratePersistedRightPanelState({
342 byThreadKey: {
343 "env-1:thread-A": {
344 isOpen: true,
345 activeSurfaceId: "browser:new",
346 surfaces: [{ id: "browser:new", kind: "preview", resourceId: null }],
347 maximized: true,
348 },
349 },
350 });
351 expect(selectThreadRightPanelState(migrated.byThreadKey, refA).maximized).toBe(true);
352 });
353
354 it("drops the legacy singleton terminal surface during migration", () => {
355 expect(
356 migratePersistedRightPanelState({
357 byThreadKey: {
358 "env-1:thread-A": {
359 activeSurfaceId: "terminal",
360 surfaces: [
361 { id: "browser:tab-a", kind: "preview", resourceId: "tab-a" },
362 { id: "terminal", kind: "terminal" },
363 ],
364 },
365 },
366 }),
367 ).toEqual({
368 byThreadKey: {
369 "env-1:thread-A": {
370 isOpen: false,
371 activeSurfaceId: null,
372 surfaces: [{ id: "browser:tab-a", kind: "preview", resourceId: "tab-a" }],
373 },
374 },
375 threadPanelVisibilityByThreadKey: {},
376 });
377 });
378
379 it("upgrades saved single-session terminal surfaces to split-capable surfaces", () => {
380 expect(
381 migratePersistedRightPanelState({
382 byThreadKey: {
383 "env-1:thread-A": {
384 isOpen: true,
385 activeSurfaceId: "terminal:term-1",
386 surfaces: [{ id: "terminal:term-1", kind: "terminal", resourceId: "term-1" }],
387 },
388 },
389 }),
390 ).toEqual({
391 byThreadKey: {
392 "env-1:thread-A": {
393 isOpen: true,
394 activeSurfaceId: "terminal:term-1",
395 surfaces: [
396 {
397 id: "terminal:term-1",
398 kind: "terminal",
399 resourceId: "term-1",
400 terminalIds: ["term-1"],
401 activeTerminalId: "term-1",
402 },
403 ],
404 },
405 },
406 threadPanelVisibilityByThreadKey: {},
407 });
408 });
409
410 it("upgrades saved file surfaces with neutral reveal state", () => {
411 expect(
412 migratePersistedRightPanelState({
413 byThreadKey: {
414 "env-1:thread-A": {
415 isOpen: true,
416 activeSurfaceId: "file:src/index.ts",
417 surfaces: [{ id: "file:src/index.ts", kind: "file", relativePath: "src/index.ts" }],
418 },
419 },
420 }),
421 ).toEqual({
422 byThreadKey: {
423 "env-1:thread-A": {
424 isOpen: true,
425 activeSurfaceId: "file:src/index.ts",
426 surfaces: [
427 {
428 id: "file:src/index.ts",
429 kind: "file",
430 relativePath: "src/index.ts",
431 revealLine: null,
432 revealRequestId: 0,
433 },
434 ],
435 },
436 },
437 threadPanelVisibilityByThreadKey: {},
438 });
439 });
440
441 it("upgrades the legacy singleton pull request surface to a reference-keyed tab", () => {
442 const id = pullRequestSurfaceId({
443 projectId: "project-a",
444 repository: "pingdotgg/t3code",
445 number: 4909,
446 });
447 expect(
448 migratePersistedRightPanelState({
449 byThreadKey: {
450 "env-1:thread-A": {
451 isOpen: true,
452 activeSurfaceId: "pull-request",
453 surfaces: [
454 {
455 id: "pull-request",
456 kind: "pull-request",
457 projectId: "project-a",
458 repository: "pingdotgg/t3code",
459 number: 4909,
460 },
461 ],
462 },
463 },
464 }),
465 ).toEqual({
466 byThreadKey: {
467 "env-1:thread-A": {
468 isOpen: true,
469 activeSurfaceId: id,
470 surfaces: [
471 {
472 id,
473 kind: "pull-request",
474 projectId: "project-a",
475 repository: "pingdotgg/t3code",
476 number: 4909,
477 },
478 ],
479 },
480 },
481 threadPanelVisibilityByThreadKey: {},
482 });
483 });
484
485 it("drops the pull-request list's shared panel so a restart opens the page fresh", () => {
486 const id = pullRequestSurfaceId({
487 projectId: "project-a",
488 repository: "pingdotgg/t3code",
489 number: 4909,
490 });
491 const panelState = {
492 isOpen: true,
493 activeSurfaceId: id,
494 surfaces: [
495 {
496 id,
497 kind: "pull-request" as const,
498 projectId: "project-a",
499 repository: "pingdotgg/t3code",
500 number: 4909,
501 },
502 ],
503 };
504 expect(
505 migratePersistedRightPanelState({
506 byThreadKey: {
507 "env-1:pull-requests-panel": panelState,
508 "env-1:thread-A": panelState,
509 },
510 }),
511 ).toEqual({
512 byThreadKey: { "env-1:thread-A": panelState },
513 threadPanelVisibilityByThreadKey: {},
514 });
515 });
516
517 it.each([
518 { kind: "plan", isOpen: true },
519 { kind: "agents", isOpen: true },
520 { kind: "agents", isOpen: false },
521 ])("drops $kind with isOpen=$isOpen and falls back", ({ kind, isOpen }) => {
522 expect(
523 migratePersistedRightPanelState({
524 byThreadKey: {
525 "env-1:thread-A": {
526 isOpen,
527 activeSurfaceId: kind,
528 surfaces: [{ id: kind, kind }],
529 },
530 "env-1:thread-B": {
531 isOpen,
532 activeSurfaceId: kind,
533 surfaces: [
534 { id: kind, kind },
535 { id: "diff", kind: "diff" },
536 ],
537 },
538 },
539 }),
540 ).toEqual({
541 byThreadKey: {
542 "env-1:thread-A": {
543 isOpen: false,
544 activeSurfaceId: null,
545 surfaces: [],
546 },
547 "env-1:thread-B": {
548 isOpen,
549 activeSurfaceId: "diff",
550 surfaces: [{ id: "diff", kind: "diff" }],
551 },
552 },
553 threadPanelVisibilityByThreadKey: {},
554 });
555 });
556
557 it("persists inline preference without restoring an open popover", () => {
558 expect(
559 migratePersistedRightPanelState({
560 threadPanelVisibilityByThreadKey: {
561 "env-1:thread-A": { inlineOpen: false, popoverOpen: true },
562 "env-1:thread-B": { inlineOpen: true, popoverOpen: true },
563 },
564 }),
565 ).toEqual({
566 byThreadKey: {},
567 threadPanelVisibilityByThreadKey: {
568 "env-1:thread-A": { inlineOpen: false, popoverOpen: false },
569 },
570 });
571 });
572
573 it("tracks inline and popover visibility independently", () => {
574 const store = useRightPanelStore.getState();
575
576 expect(selectThreadPanelOpen(store.threadPanelVisibilityByThreadKey, refA, "inline")).toBe(
577 true,
578 );
579 expect(selectThreadPanelOpen(store.threadPanelVisibilityByThreadKey, refA, "popover")).toBe(
580 false,
581 );
582
583 store.setThreadPanelOpen(refA, "inline", false);
584 store.toggleThreadPanel(refA, "popover");
585
586 expect(
587 selectThreadPanelVisibility(
588 useRightPanelStore.getState().threadPanelVisibilityByThreadKey,
589 refA,
590 ),
591 ).toEqual({ inlineOpen: false, popoverOpen: true });
592 expect(
593 selectThreadPanelVisibility(
594 useRightPanelStore.getState().threadPanelVisibilityByThreadKey,
595 refB,
596 ),
597 ).toEqual({ inlineOpen: true, popoverOpen: false });
598 });
599
600 it("closes the popover atomically when the real right panel opens", () => {
601 useRightPanelStore.getState().setThreadPanelOpen(refA, "popover", true);
602 useRightPanelStore.getState().open(refA, "diff");
603
604 expect(
605 selectThreadPanelVisibility(
606 useRightPanelStore.getState().threadPanelVisibilityByThreadKey,
607 refA,
608 ),
609 ).toEqual({ inlineOpen: true, popoverOpen: false });
610 });
611
612 it("keeps an open popover visible by promoting it to inline when the real panel closes", () => {
613 const store = useRightPanelStore.getState();
614 store.open(refA, "diff");
615 store.setThreadPanelOpen(refA, "inline", false);
616 store.setThreadPanelOpen(refA, "popover", true);
617 store.close(refA);
618
619 expect(
620 selectThreadPanelVisibility(
621 useRightPanelStore.getState().threadPanelVisibilityByThreadKey,
622 refA,
623 ),
624 ).toEqual({ inlineOpen: true, popoverOpen: true });
625 });
626
627 it("open sets the active panel for a thread", () => {
628 useRightPanelStore.getState().open(refA, "preview");
629 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("preview");
630 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refB)).toBeNull();
631 });
632
633 it("opening a different kind keeps both surfaces and activates the new one", () => {
634 useRightPanelStore.getState().open(refA, "device");
635 useRightPanelStore.getState().open(refA, "preview");
636 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("preview");
637 expect(
638 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces,
639 ).toHaveLength(2);
640 });
641
642 it("reopening an inactive singleton activates its existing surface", () => {
643 useRightPanelStore.getState().open(refA, "diff");
644 useRightPanelStore.getState().open(refA, "device");
645 useRightPanelStore.getState().open(refA, "diff");
646
647 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
648 isOpen: true,
649 activeSurfaceId: "diff",
650 surfaces: [
651 { id: "diff", kind: "diff" },
652 { id: "device", kind: "device" },
653 ],
654 });
655 });
656
657 it("keeps files as a singleton surface", () => {
658 useRightPanelStore.getState().open(refA, "files");
659 useRightPanelStore.getState().open(refA, "files");
660 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
661 isOpen: true,
662 activeSurfaceId: "files",
663 surfaces: [{ id: "files", kind: "files" }],
664 });
665 });
666
667 it("opens workspace-root links as the singleton files explorer", () => {
668 const store = useRightPanelStore.getState();
669 store.openFile(refA, "README.md");
670 store.openFile(refA, ".");
671 store.openFile(refA, ".");
672 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
673 expect(state.activeSurfaceId).toBe("files");
674 expect(state.surfaces.map((surface) => surface.id)).toEqual(["file:README.md", "files"]);
675 store.closeSurface(refA, "files");
676 expect(
677 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).activeSurfaceId,
678 ).toBe("file:README.md");
679 store.openFile(refA, ".");
680 expect(
681 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).activeSurfaceId,
682 ).toBe("files");
683 });
684
685 it("walks a folder browser up and back down in one tab", () => {
686 const store = useRightPanelStore.getState();
687 const state = () =>
688 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
689 store.openFile(refA, "README.md");
690 store.openFile(refA, "apps/web/src");
691
692 store.navigateFolder(refA, "file:apps/web/src", "apps", "apps/web/src");
693 expect(state().activeSurfaceId).toBe("file:apps");
694 expect(state().surfaces).toEqual([
695 expect.objectContaining({ id: "file:README.md" }),
696 expect.objectContaining({
697 id: "file:apps",
698 relativePath: "apps",
699 folderTrail: "apps/web/src",
700 }),
701 ]);
702
703 // The workspace root is the files explorer, still holding the way back down.
704 store.navigateFolder(refA, "file:apps", "", "apps/web/src");
705 expect(state().surfaces.map((surface) => surface.id)).toEqual(["file:README.md", "files"]);
706 expect(state().surfaces[1]).toEqual({
707 id: "files",
708 kind: "files",
709 folderTrail: "apps/web/src",
710 });
711
712 // Reaching the end of the trail leaves no trail behind.
713 store.navigateFolder(refA, "files", "apps/web/src", "apps/web/src");
714 expect(state().activeSurfaceId).toBe("file:apps/web/src");
715 expect(state().surfaces[1]).toEqual({
716 id: "file:apps/web/src",
717 kind: "file",
718 relativePath: "apps/web/src",
719 revealLine: null,
720 revealRequestId: 1,
721 });
722 });
723
724 it("switches to a tab that already shows the folder instead of replacing it", () => {
725 const store = useRightPanelStore.getState();
726 store.openFile(refA, "apps");
727 store.openFile(refA, "apps/web/src", 12);
728
729 store.navigateFolder(refA, "file:apps/web/src", "apps", "apps/web/src");
730 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
731 expect(state.activeSurfaceId).toBe("file:apps");
732 expect(state.surfaces).toEqual([
733 { id: "file:apps", kind: "file", relativePath: "apps", revealLine: null, revealRequestId: 1 },
734 ]);
735 });
736
737 it("keeps a file's tab when walking up from it, and comes back to it", () => {
738 const store = useRightPanelStore.getState();
739 const state = () =>
740 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
741 store.openFile(refA, "src/app.ts", 40);
742
743 store.navigateFolder(refA, "file:src/app.ts", "src", "src/app.ts", true);
744 expect(state().activeSurfaceId).toBe("file:src");
745 expect(state().surfaces).toEqual([
746 {
747 id: "file:src/app.ts",
748 kind: "file",
749 relativePath: "src/app.ts",
750 revealLine: 40,
751 revealRequestId: 1,
752 },
753 expect.objectContaining({ id: "file:src", folderTrail: "src/app.ts" }),
754 ]);
755
756 // The trail's end is the file: its tab comes back as it was, the folder tab goes.
757 store.navigateFolder(refA, "file:src", "src/app.ts", "src/app.ts");
758 expect(state().activeSurfaceId).toBe("file:src/app.ts");
759 expect(state().surfaces).toEqual([
760 {
761 id: "file:src/app.ts",
762 kind: "file",
763 relativePath: "src/app.ts",
764 revealLine: 40,
765 revealRequestId: 1,
766 },
767 ]);
768 });
769
770 it("ignores navigation from a tab that was closed", () => {
771 const store = useRightPanelStore.getState();
772 store.openFile(refA, "apps");
773 const before = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
774 store.navigateFolder(refA, "file:gone", "", "gone");
775 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toBe(
776 before,
777 );
778 });
779
780 it("replaces the standalone explorer with peer file surfaces", () => {
781 useRightPanelStore.getState().open(refA, "files");
782 useRightPanelStore.getState().openFile(refA, "src/index.ts");
783 useRightPanelStore.getState().openFile(refA, "src/index.ts");
784 useRightPanelStore.getState().openFile(refA, "README.md");
785
786 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
787 isOpen: true,
788 activeSurfaceId: "file:README.md",
789 surfaces: [
790 {
791 id: "file:src/index.ts",
792 kind: "file",
793 relativePath: "src/index.ts",
794 revealLine: null,
795 revealRequestId: 2,
796 },
797 {
798 id: "file:README.md",
799 kind: "file",
800 relativePath: "README.md",
801 revealLine: null,
802 revealRequestId: 1,
803 },
804 ],
805 });
806 });
807
808 it.each([
809 ["generated\\", "generated"],
810 ["notes/meeting ", "notes/meeting"],
811 [" notes/meeting", "notes/meeting"],
812 ])("keeps %j and %j in separate file tabs", (firstPath, secondPath) => {
813 useRightPanelStore.getState().openFile(refA, firstPath);
814 useRightPanelStore.getState().openFile(refA, secondPath);
815
816 expect(
817 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces,
818 ).toMatchObject([
819 { id: `file:${firstPath}`, relativePath: firstPath },
820 { id: `file:${secondPath}`, relativePath: secondPath },
821 ]);
822 });
823
824 it.each([
825 ["docs/", "docs"],
826 ["docs///", "docs"],
827 ["/", "/"],
828 ["C:/", "C:/"],
829 ])("reuses the folder tab for %j and %j", (linkPath, treePath) => {
830 useRightPanelStore.getState().openFile(refA, linkPath);
831 useRightPanelStore.getState().openFile(refA, treePath);
832
833 expect(
834 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces,
835 ).toMatchObject([{ id: `file:${treePath}`, relativePath: treePath, revealRequestId: 2 }]);
836 });
837
838 it("opens an attachment as a file surface without the standalone explorer", () => {
839 const attachment = {
840 type: "file" as const,
841 id: "thread-A-attachment-pdf",
842 name: "report.pdf",
843 mimeType: "application/pdf",
844 sizeBytes: 42,
845 };
846 useRightPanelStore.getState().open(refA, "files");
847 useRightPanelStore.getState().openAttachment(refA, attachment);
848
849 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
850 isOpen: true,
851 activeSurfaceId: "attachment:thread-A-attachment-pdf",
852 surfaces: [
853 {
854 id: "attachment:thread-A-attachment-pdf",
855 kind: "file",
856 relativePath: "report.pdf",
857 revealLine: null,
858 revealRequestId: 0,
859 attachment,
860 },
861 ],
862 });
863 });
864
865 it("keeps attachment and workspace file ids disjoint", () => {
866 useRightPanelStore.getState().openFile(refA, "attachment:shared-id");
867 useRightPanelStore.getState().openAttachment(refA, {
868 type: "file",
869 id: "shared-id",
870 name: "report.pdf",
871 mimeType: "application/pdf",
872 sizeBytes: 42,
873 });
874
875 expect(
876 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces.map(
877 (surface) => surface.id,
878 ),
879 ).toEqual(["file:attachment:shared-id", "attachment:shared-id"]);
880 });
881
882 it("updates line reveal requests when reopening a file surface", () => {
883 useRightPanelStore.getState().openFile(refA, "src/index.ts", 42);
884 useRightPanelStore.getState().openFile(refA, "src/index.ts", 87);
885
886 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
887 isOpen: true,
888 activeSurfaceId: "file:src/index.ts",
889 surfaces: [
890 {
891 id: "file:src/index.ts",
892 kind: "file",
893 relativePath: "src/index.ts",
894 revealLine: 87,
895 revealRequestId: 2,
896 },
897 ],
898 });
899
900 useRightPanelStore.getState().openFile(refA, "src/index.ts");
901
902 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
903 isOpen: true,
904 activeSurfaceId: "file:src/index.ts",
905 surfaces: [
906 {
907 id: "file:src/index.ts",
908 kind: "file",
909 relativePath: "src/index.ts",
910 revealLine: null,
911 revealRequestId: 3,
912 },
913 ],
914 });
915 });
916
917 it("removes persisted file surfaces when their workspace no longer exists", () => {
918 useRightPanelStore.getState().openFile(refA, "src/index.ts");
919 useRightPanelStore.getState().open(refA, "device");
920 useRightPanelStore.getState().openFile(refA, "README.md");
921
922 useRightPanelStore.getState().reconcileFileSurfaces(refA, false);
923
924 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
925 isOpen: true,
926 activeSurfaceId: "device",
927 surfaces: [{ id: "device", kind: "device" }],
928 });
929
930 useRightPanelStore.getState().openFile(refB, "conductor.json");
931 useRightPanelStore.getState().reconcileFileSurfaces(refB, false);
932 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refB)).toEqual({
933 isOpen: false,
934 activeSurfaceId: null,
935 surfaces: [],
936 });
937 });
938
939 it("keeps attachment previews when their workspace is unavailable", () => {
940 const attachment = {
941 type: "file" as const,
942 id: "thread-A-attachment-pdf",
943 name: "report.pdf",
944 mimeType: "application/pdf",
945 sizeBytes: 42,
946 };
947 useRightPanelStore.getState().openFile(refA, "README.md");
948 useRightPanelStore.getState().openAttachment(refA, attachment);
949
950 useRightPanelStore.getState().reconcileFileSurfaces(refA, false);
951
952 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
953 isOpen: true,
954 activeSurfaceId: "attachment:thread-A-attachment-pdf",
955 surfaces: [
956 {
957 id: "attachment:thread-A-attachment-pdf",
958 kind: "file",
959 relativePath: "report.pdf",
960 revealLine: null,
961 revealRequestId: 0,
962 attachment,
963 },
964 ],
965 });
966 });
967
968 it("close hides the panel without clearing its selected surface", () => {
969 useRightPanelStore.getState().open(refA, "device");
970 useRightPanelStore.getState().close(refA);
971 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
972 expect(
973 selectSelectedRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA),
974 ).toEqual({ id: "device", kind: "device" });
975 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
976 isOpen: false,
977 activeSurfaceId: "device",
978 surfaces: [{ id: "device", kind: "device" }],
979 });
980 });
981
982 it("toggles empty panel visibility without creating a surface", () => {
983 useRightPanelStore.getState().toggleVisibility(refA);
984 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
985 isOpen: true,
986 activeSurfaceId: null,
987 surfaces: [],
988 });
989
990 useRightPanelStore.getState().toggleVisibility(refA);
991 expect(useRightPanelStore.getState().byThreadKey).toEqual({});
992 });
993
994 it("toggle hides the panel without discarding the active surface", () => {
995 useRightPanelStore.getState().toggle(refA, "diff");
996 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("diff");
997 useRightPanelStore.getState().toggle(refA, "diff");
998 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
999 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
1000 isOpen: false,
1001 activeSurfaceId: "diff",
1002 surfaces: [{ id: "diff", kind: "diff" }],
1003 });
1004 });
1005
1006 it("toggle to a different kind switches active", () => {
1007 useRightPanelStore.getState().toggle(refA, "preview");
1008 useRightPanelStore.getState().toggle(refA, "device");
1009 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("device");
1010 });
1011
1012 it("removeThread clears persisted state", () => {
1013 useRightPanelStore.getState().open(refA, "device");
1014 useRightPanelStore.getState().removeThread(refA);
1015 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
1016 });
1017
1018 it("close on never-opened thread is a no-op", () => {
1019 useRightPanelStore.getState().close(refA);
1020 expect(useRightPanelStore.getState().byThreadKey).toEqual({});
1021 });
1022
1023 it("tracks one surface per browser session", () => {
1024 useRightPanelStore.getState().openBrowser(refA, "tab-a");
1025 useRightPanelStore.getState().openBrowser(refA, "tab-b");
1026
1027 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
1028 expect(state.surfaces.map((surface) => surface.id)).toEqual(["browser:tab-a", "browser:tab-b"]);
1029 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
1030 id: "browser:tab-b",
1031 kind: "preview",
1032 resourceId: "tab-b",
1033 });
1034 });
1035
1036 it("tracks one surface per pull request", () => {
1037 const first = { projectId: "project-a", repository: "pingdotgg/t3code", number: 4909 };
1038 const second = { projectId: "project-a", repository: "pingdotgg/t3code", number: 4910 };
1039 useRightPanelStore.getState().openPullRequest(refA, first);
1040 useRightPanelStore.getState().openPullRequest(refA, second);
1041 const url = "https://gitlab.example.com/pingdotgg/t3code/-/merge_requests/4909";
1042 useRightPanelStore.getState().openPullRequest(refA, { ...first, url });
1043 useRightPanelStore.getState().openPullRequest(refA, first);
1044
1045 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
1046 expect(state.surfaces.map((surface) => surface.id)).toEqual([
1047 pullRequestSurfaceId(first),
1048 pullRequestSurfaceId(second),
1049 ]);
1050 expect(state.activeSurfaceId).toBe(pullRequestSurfaceId(first));
1051 expect(
1052 selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA),
1053 ).toMatchObject({ url });
1054 expect(state.surfaces[1]).not.toHaveProperty("url");
1055 });
1056
1057 it("keeps matching repository and number on different hosts as separate tabs", () => {
1058 const first = { projectId: "project-a", repository: "acme/api", number: 7, host: "github.com" };
1059 const second = { ...first, host: "github.example.com" };
1060 useRightPanelStore.getState().openPullRequest(refA, first);
1061 useRightPanelStore.getState().openPullRequest(refA, second);
1062 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
1063 expect(state.surfaces).toEqual([pullRequestSurface(first), pullRequestSurface(second)]);
1064 expect(pullRequestSurfaceId({ ...first, host: "GITHUB.COM" })).toBe(
1065 pullRequestSurfaceId(first),
1066 );
1067 });
1068
1069 it("keeps one pull request read from two servers as two tabs", () => {
1070 const local = {
1071 environmentId: "local",
1072 projectId: "project-a",
1073 repository: "pingdotgg/t3code",
1074 number: 4909,
1075 };
1076 const remote = { ...local, environmentId: "remote" };
1077
1078 useRightPanelStore.getState().openPullRequest(refA, local);
1079 useRightPanelStore.getState().openPullRequest(refA, remote);
1080
1081 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
1082 expect(state.surfaces.map((surface) => surface.id)).toEqual([
1083 pullRequestSurfaceId(local),
1084 pullRequestSurfaceId(remote),
1085 ]);
1086 });
1087
1088 it("keeps the page's panel tabs reachable when the set of connected servers changes", () => {
1089 const panelId = ThreadId.make("pull-requests-panel");
1090 const stableRef = scopeThreadRef("pull-requests-panel" as EnvironmentId, panelId);
1091 const fromServerA = {
1092 environmentId: "server-a",
1093 projectId: "project-a",
1094 repository: "pingdotgg/t3code",
1095 number: 1,
1096 };
1097 const fromServerB = {
1098 environmentId: "server-b",
1099 projectId: "project-b",
1100 repository: "pingdotgg/t3code",
1101 number: 2,
1102 };
1103
1104 useRightPanelStore.getState().openPullRequest(stableRef, fromServerA);
1105 useRightPanelStore.getState().openPullRequest(stableRef, fromServerB);
1106
1107 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, stableRef);
1108 expect(state.surfaces.map((surface) => surface.id)).toEqual([
1109 pullRequestSurfaceId(fromServerA),
1110 pullRequestSurfaceId(fromServerB),
1111 ]);
1112
1113 const refWhileBothConnected = scopeThreadRef("server-a" as EnvironmentId, panelId);
1114 const refAfterServerADisconnects = scopeThreadRef("server-b" as EnvironmentId, panelId);
1115 expect(refWhileBothConnected).not.toEqual(refAfterServerADisconnects);
1116 expect(
1117 selectThreadRightPanelState(
1118 useRightPanelStore.getState().byThreadKey,
1119 refAfterServerADisconnects,
1120 ).surfaces,
1121 ).toEqual([]);
1122 });
1123
1124 it("tracks one surface per terminal session", () => {
1125 useRightPanelStore.getState().openTerminal(refA, "term-1");
1126 useRightPanelStore.getState().openTerminal(refA, "term-2");
1127
1128 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
1129 expect(state.surfaces).toEqual([
1130 {
1131 id: "terminal:term-1",
1132 kind: "terminal",
1133 resourceId: "term-1",
1134 terminalIds: ["term-1"],
1135 activeTerminalId: "term-1",
1136 },
1137 {
1138 id: "terminal:term-2",
1139 kind: "terminal",
1140 resourceId: "term-2",
1141 terminalIds: ["term-2"],
1142 activeTerminalId: "term-2",
1143 },
1144 ]);
1145 expect(state.activeSurfaceId).toBe("terminal:term-2");
1146 });
1147
1148 it("tracks split panes and the active pane within a terminal surface", () => {
1149 useRightPanelStore.getState().openTerminal(refA, "term-1");
1150 useRightPanelStore.getState().splitTerminal(refA, "terminal:term-1", "term-2");
1151
1152 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
1153 id: "terminal:term-1",
1154 kind: "terminal",
1155 resourceId: "term-1",
1156 terminalIds: ["term-1", "term-2"],
1157 activeTerminalId: "term-2",
1158 });
1159
1160 useRightPanelStore.getState().activateTerminal(refA, "terminal:term-1", "term-1");
1161 useRightPanelStore.getState().closeTerminal(refA, "terminal:term-1", "term-1");
1162 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
1163 id: "terminal:term-1",
1164 kind: "terminal",
1165 resourceId: "term-1",
1166 terminalIds: ["term-2"],
1167 activeTerminalId: "term-2",
1168 });
1169 });
1170
1171 it("tracks vertical layout for a terminal surface", () => {
1172 useRightPanelStore.getState().openTerminal(refA, "term-1");
1173 useRightPanelStore.getState().splitTerminal(refA, "terminal:term-1", "term-2", "vertical");
1174
1175 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
1176 id: "terminal:term-1",
1177 kind: "terminal",
1178 resourceId: "term-1",
1179 terminalIds: ["term-1", "term-2"],
1180 activeTerminalId: "term-2",
1181 splitDirection: "vertical",
1182 });
1183 });
1184
1185 it("closing the final terminal pane removes its surface and closes the panel", () => {
1186 useRightPanelStore.getState().openTerminal(refA, "term-1");
1187 useRightPanelStore.getState().closeTerminal(refA, "terminal:term-1", "term-1");
1188
1189 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
1190 isOpen: false,
1191 activeSurfaceId: null,
1192 surfaces: [],
1193 });
1194 });
1195
1196 it("closing the active surface activates a neighboring surface", () => {
1197 useRightPanelStore.getState().openBrowser(refA, "tab-a");
1198 useRightPanelStore.getState().openTerminal(refA, "term-1");
1199 useRightPanelStore.getState().closeSurface(refA, "terminal:term-1");
1200
1201 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)?.id).toBe(
1202 "browser:tab-a",
1203 );
1204 });
1205
1206 it("closing the final surface closes the panel", () => {
1207 useRightPanelStore.getState().openTerminal(refA, "term-1");
1208 useRightPanelStore.getState().closeSurface(refA, "terminal:term-1");
1209
1210 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
1211 isOpen: false,
1212 activeSurfaceId: null,
1213 surfaces: [],
1214 });
1215 });
1216
1217 it("closing other surfaces keeps the selected surface active", () => {
1218 useRightPanelStore.getState().openBrowser(refA, "tab-a");
1219 useRightPanelStore.getState().openFile(refA, "src/index.ts");
1220 useRightPanelStore.getState().openTerminal(refA, "term-1");
1221
1222 useRightPanelStore.getState().closeOtherSurfaces(refA, "file:src/index.ts");
1223
1224 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
1225 isOpen: true,
1226 activeSurfaceId: "file:src/index.ts",
1227 surfaces: [
1228 {
1229 id: "file:src/index.ts",
1230 kind: "file",
1231 relativePath: "src/index.ts",
1232 revealLine: null,
1233 revealRequestId: 1,
1234 },
1235 ],
1236 });
1237 });
1238
1239 it("closing surfaces to the right activates the selected surface when active was removed", () => {
1240 useRightPanelStore.getState().openBrowser(refA, "tab-a");
1241 useRightPanelStore.getState().openFile(refA, "src/index.ts");
1242 useRightPanelStore.getState().openTerminal(refA, "term-1");
1243
1244 useRightPanelStore.getState().closeSurfacesToRight(refA, "browser:tab-a");
1245
1246 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
1247 isOpen: true,
1248 activeSurfaceId: "browser:tab-a",
1249 surfaces: [{ id: "browser:tab-a", kind: "preview", resourceId: "tab-a" }],
1250 });
1251 });
1252
1253 it("closing all surfaces closes the panel", () => {
1254 useRightPanelStore.getState().openBrowser(refA, "tab-a");
1255 useRightPanelStore.getState().openFile(refA, "src/index.ts");
1256
1257 useRightPanelStore.getState().closeAllSurfaces(refA);
1258
1259 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
1260 isOpen: false,
1261 activeSurfaceId: null,
1262 surfaces: [],
1263 });
1264 });
1265
1266 it("does not replace a stale surface with a hidden tab, but preserves explicit opens", () => {
1267 const store = useRightPanelStore.getState();
1268 store.openBrowser(refA, "stale-tab");
1269 store.reconcileBrowserSurfaces(refA, ["hidden-tab"], new Set(["hidden-tab"]));
1270 expect(
1271 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA),
1272 ).toMatchObject({
1273 activeSurfaceId: null,
1274 surfaces: [],
1275 });
1276
1277 store.openBrowser(refA, "hidden-tab");
1278 store.reconcileBrowserSurfaces(
1279 refA,
1280 ["hidden-tab", "other-tab"],
1281 new Set(["hidden-tab", "other-tab"]),
1282 );
1283 expect(
1284 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA),
1285 ).toMatchObject({
1286 isOpen: true,
1287 activeSurfaceId: "browser:hidden-tab",
1288 surfaces: [{ id: "browser:hidden-tab", kind: "preview", resourceId: "hidden-tab" }],
1289 });
1290 });
1291
1292 it("reconciles browser surfaces without deleting other surface kinds", () => {
1293 useRightPanelStore.getState().openTerminal(refA, "term-1");
1294 useRightPanelStore.getState().openBrowser(refA, "tab-a");
1295 useRightPanelStore.getState().openBrowser(refA, "tab-b");
1296 useRightPanelStore.getState().reconcileBrowserSurfaces(refA, ["tab-b", "tab-c"]);
1297
1298 expect(
1299 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces.map(
1300 (surface) => surface.id,
1301 ),
1302 ).toEqual(["terminal:term-1", "browser:tab-b", "browser:tab-c"]);
1303 });
1304
1305 it("moves a surface to a new index and keeps it there through browser reconciliation", () => {
1306 const store = useRightPanelStore.getState();
1307 store.openTerminal(refA, "term-1");
1308 store.openBrowser(refA, "tab-a");
1309 store.open(refA, "diff");
1310 const revision = store.getUserActionRevision(refA);
1311 const surfaceIds = () =>
1312 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces.map(
1313 (surface) => surface.id,
1314 );
1315
1316 store.moveSurface(refA, "browser:tab-a", 0);
1317 expect(surfaceIds()).toEqual(["browser:tab-a", "terminal:term-1", "diff"]);
1318 store.moveSurface(refA, "browser:tab-a", 2);
1319 expect(surfaceIds()).toEqual(["terminal:term-1", "diff", "browser:tab-a"]);
1320 store.moveSurface(refA, "browser:tab-a", 0);
1321
1322 store.reconcileBrowserSurfaces(refA, ["tab-a", "tab-b"]);
1323 expect(surfaceIds()).toEqual(["browser:tab-a", "terminal:term-1", "diff", "browser:tab-b"]);
1324 expect(store.getUserActionRevision(refA)).toBe(revision);
1325 });
1326});