Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

rightPanelStore.test.ts

1,462 lines53.7 KB

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