The best way to control Claude Code, Codex and more from any computer or phone

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