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