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