Open History to see the last commit
1import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2import { type EnvironmentId, ThreadId } from "@t3tools/contracts";
3import { beforeEach, describe, expect, it } from "vite-plus/test";
4
5import {
6 migratePersistedRightPanelState,
7 pullRequestSurface,
8 pullRequestSurfaceId,
9 selectActiveRightPanel,
10 selectActiveRightPanelSurface,
11 selectSelectedRightPanelSurface,
12 selectThreadRightPanelState,
13 useRightPanelStore,
14} from "./rightPanelStore";
15
16const refA = scopeThreadRef("env-1" as EnvironmentId, ThreadId.make("thread-A"));
17const refB = scopeThreadRef("env-1" as EnvironmentId, ThreadId.make("thread-B"));
18
19beforeEach(() => {
20 useRightPanelStore.setState({ byThreadKey: {}, userActionRevisionByThreadKey: {} });
21});
22
23describe("rightPanelStore", () => {
24 it("gives each host/device its own tab and preserves renamed tabs", () => {
25 const store = useRightPanelStore.getState();
26 const android = {
27 hostId: "nucbox",
28 deviceId: "emulator-5580",
29 name: "Pixel",
30 platform: "android",
31 } as const;
32 const ios = { hostId: "macmini", deviceId: "ios-1", name: "iPhone", platform: "ios" } as const;
33 store.open(refA, "device");
34 store.openDevice(refA, android);
35 store.open(refA, "device");
36 expect(
37 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces,
38 ).toHaveLength(2);
39 store.openDevice(refA, ios);
40 let state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
41 expect(state.surfaces.map((surface) => surface.id)).toEqual([
42 "device:nucbox:emulator-5580",
43 "device:macmini:ios-1",
44 ]);
45 store.renameDevice(refA, "device:nucbox:emulator-5580", "Android test");
46 store.openDevice(refA, android);
47 state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
48 expect(state.surfaces).toHaveLength(2);
49 expect(state.surfaces[0]).toMatchObject({ title: "Android test", target: android });
50 expect(state.activeSurfaceId).toBe("device:nucbox:emulator-5580");
51 store.closeSurface(refA, state.activeSurfaceId!);
52 expect(
53 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces,
54 ).toEqual([expect.objectContaining({ target: ios })]);
55 });
56
57 it("does not collide when two hosts expose the same device id", () => {
58 const store = useRightPanelStore.getState();
59 const device = { deviceId: "emulator-5554", name: "Pixel", platform: "android" } as const;
60 store.openDevice(refA, { ...device, hostId: "a:b" });
61 store.openDevice(refA, { ...device, hostId: "a" });
62 expect(
63 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces,
64 ).toHaveLength(2);
65 expect(
66 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refB).surfaces,
67 ).toHaveLength(0);
68 });
69
70 it.each(["one", "all", "others", "right"])(
71 "keeps device tabs dismissed across reload after closing %s",
72 (mode) => {
73 const store = useRightPanelStore.getState();
74 const target = {
75 hostId: "nucbox",
76 deviceId: "emulator-5580",
77 name: "Pixel",
78 platform: "android",
79 } as const;
80 store.open(refA, "files");
81 store.openDevice(refA, target);
82 if (mode === "one") store.closeSurface(refA, "device:nucbox:emulator-5580");
83 if (mode === "all") store.closeAllSurfaces(refA);
84 if (mode === "others") store.closeOtherSurfaces(refA, "files");
85 if (mode === "right") store.closeSurfacesToRight(refA, "files");
86 const persisted = JSON.parse(
87 JSON.stringify({ byThreadKey: useRightPanelStore.getState().byThreadKey }),
88 );
89 useRightPanelStore.setState(migratePersistedRightPanelState(persisted));
90 store.openDevice(refA, target, true);
91 expect(
92 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces.some(
93 (surface) => surface.kind === "device",
94 ),
95 ).toBe(false);
96 store.openDevice(refA, target);
97 expect(
98 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces.some(
99 (surface) => surface.kind === "device",
100 ),
101 ).toBe(true);
102 },
103 );
104
105 const completedDiff = { id: "diff", kind: "diff" } as const;
106 const linkedPullRequest = pullRequestSurface({
107 projectId: "project-a",
108 repository: "pingdotgg/t3code",
109 number: 42,
110 });
111
112 it.each(["diff-first", "pull-request-first"])(
113 "prioritizes the linked pull request over browser and diff with %s delivery",
114 (order) => {
115 const store = useRightPanelStore.getState();
116 store.openBrowser(refA, "existing-browser");
117 const revision = store.getUserActionRevision(refA);
118 const requests =
119 order === "diff-first"
120 ? [completedDiff, linkedPullRequest]
121 : [linkedPullRequest, completedDiff];
122 for (const surface of requests) store.openProactive(refA, surface, revision);
123 store.reconcileBrowserSurfaces(refA, ["existing-browser", "agent-browser"]);
124
125 expect(
126 selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA),
127 ).toEqual(linkedPullRequest);
128
129 store.open(refA, "diff");
130 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("diff");
131 },
132 );
133
134 it.each([
135 { choice: "file", choose: () => useRightPanelStore.getState().openFile(refA, "src/app.ts") },
136 {
137 choice: "pull request",
138 choose: () =>
139 useRightPanelStore.getState().openPullRequest(refA, { ...linkedPullRequest, number: 41 }),
140 },
141 { choice: "browser", choose: () => useRightPanelStore.getState().openBrowser(refA, "tab-a") },
142 {
143 choice: "terminal",
144 choose: () => useRightPanelStore.getState().openTerminal(refA, "term-1"),
145 },
146 {
147 choice: "same tab",
148 choose: () => useRightPanelStore.getState().activateSurface(refA, "diff"),
149 },
150 { choice: "hide", choose: () => useRightPanelStore.getState().close(refA) },
151 { choice: "toggle", choose: () => useRightPanelStore.getState().toggle(refA, "diff") },
152 { choice: "close all", choose: () => useRightPanelStore.getState().closeAllSurfaces(refA) },
153 {
154 choice: "terminal close",
155 choose: () => {
156 const store = useRightPanelStore.getState();
157 store.openTerminal(refA, "term-1");
158 store.closeTerminal(refA, "terminal:term-1", "term-1");
159 },
160 },
161 ])("keeps a later $choice choice when automatic requests arrive", ({ choose }) => {
162 const store = useRightPanelStore.getState();
163 store.open(refA, "diff");
164 const revision = store.getUserActionRevision(refA);
165 choose();
166 const chosen = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
167
168 expect(store.openProactive(refA, completedDiff, revision)).toBe(false);
169 expect(store.openProactive(refA, linkedPullRequest, revision)).toBe(false);
170 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toBe(
171 chosen,
172 );
173 });
174
175 it("allows automatic panels for a later turn after a manual choice", () => {
176 const store = useRightPanelStore.getState();
177 const firstTurnRevision = store.getUserActionRevision(refA);
178 store.openFile(refA, "src/app.ts");
179 expect(store.openProactive(refA, completedDiff, firstTurnRevision)).toBe(false);
180
181 const nextTurnRevision = store.getUserActionRevision(refA);
182 expect(store.openProactive(refA, completedDiff, nextTurnRevision)).toBe(true);
183 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("diff");
184 });
185
186 it("keeps manual choices scoped to their thread and environment", () => {
187 const otherEnvironment = scopeThreadRef("env-2" as EnvironmentId, refA.threadId);
188 const store = useRightPanelStore.getState();
189 const revision = store.getUserActionRevision(refA);
190 store.openFile(refB, "src/app.ts");
191 store.openFile(otherEnvironment, "src/app.ts");
192
193 expect(store.openProactive(refA, completedDiff, revision)).toBe(true);
194 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refB)).toBe("file");
195 expect(
196 selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, otherEnvironment),
197 ).toBe("file");
198 });
199
200 it("does not treat resource reconciliation as a manual choice", () => {
201 const store = useRightPanelStore.getState();
202 store.openFile(refA, "src/app.ts");
203 const revision = store.getUserActionRevision(refA);
204 store.reconcileBrowserSurfaces(refA, ["agent-browser"]);
205 store.reconcileFileSurfaces(refA, false);
206
207 expect(store.openProactive(refA, completedDiff, revision)).toBe(true);
208 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("diff");
209 });
210
211 it("drops the legacy singleton terminal surface during migration", () => {
212 expect(
213 migratePersistedRightPanelState({
214 byThreadKey: {
215 "env-1:thread-A": {
216 activeSurfaceId: "terminal",
217 surfaces: [
218 { id: "browser:tab-a", kind: "preview", resourceId: "tab-a" },
219 { id: "terminal", kind: "terminal" },
220 ],
221 },
222 },
223 }),
224 ).toEqual({
225 byThreadKey: {
226 "env-1:thread-A": {
227 isOpen: false,
228 activeSurfaceId: null,
229 surfaces: [{ id: "browser:tab-a", kind: "preview", resourceId: "tab-a" }],
230 },
231 },
232 });
233 });
234
235 it("upgrades saved single-session terminal surfaces to split-capable surfaces", () => {
236 expect(
237 migratePersistedRightPanelState({
238 byThreadKey: {
239 "env-1:thread-A": {
240 isOpen: true,
241 activeSurfaceId: "terminal:term-1",
242 surfaces: [{ id: "terminal:term-1", kind: "terminal", resourceId: "term-1" }],
243 },
244 },
245 }),
246 ).toEqual({
247 byThreadKey: {
248 "env-1:thread-A": {
249 isOpen: true,
250 activeSurfaceId: "terminal:term-1",
251 surfaces: [
252 {
253 id: "terminal:term-1",
254 kind: "terminal",
255 resourceId: "term-1",
256 terminalIds: ["term-1"],
257 activeTerminalId: "term-1",
258 },
259 ],
260 },
261 },
262 });
263 });
264
265 it("upgrades saved file surfaces with neutral reveal state", () => {
266 expect(
267 migratePersistedRightPanelState({
268 byThreadKey: {
269 "env-1:thread-A": {
270 isOpen: true,
271 activeSurfaceId: "file:src/index.ts",
272 surfaces: [{ id: "file:src/index.ts", kind: "file", relativePath: "src/index.ts" }],
273 },
274 },
275 }),
276 ).toEqual({
277 byThreadKey: {
278 "env-1:thread-A": {
279 isOpen: true,
280 activeSurfaceId: "file:src/index.ts",
281 surfaces: [
282 {
283 id: "file:src/index.ts",
284 kind: "file",
285 relativePath: "src/index.ts",
286 revealLine: null,
287 revealRequestId: 0,
288 },
289 ],
290 },
291 },
292 });
293 });
294
295 it("upgrades the legacy singleton pull request surface to a reference-keyed tab", () => {
296 const id = pullRequestSurfaceId({
297 projectId: "project-a",
298 repository: "pingdotgg/t3code",
299 number: 4909,
300 });
301 expect(
302 migratePersistedRightPanelState({
303 byThreadKey: {
304 "env-1:thread-A": {
305 isOpen: true,
306 activeSurfaceId: "pull-request",
307 surfaces: [
308 {
309 id: "pull-request",
310 kind: "pull-request",
311 projectId: "project-a",
312 repository: "pingdotgg/t3code",
313 number: 4909,
314 },
315 ],
316 },
317 },
318 }),
319 ).toEqual({
320 byThreadKey: {
321 "env-1:thread-A": {
322 isOpen: true,
323 activeSurfaceId: id,
324 surfaces: [
325 {
326 id,
327 kind: "pull-request",
328 projectId: "project-a",
329 repository: "pingdotgg/t3code",
330 number: 4909,
331 },
332 ],
333 },
334 },
335 });
336 });
337
338 it("drops the pull-request list's shared panel so a restart opens the page fresh", () => {
339 const id = pullRequestSurfaceId({
340 projectId: "project-a",
341 repository: "pingdotgg/t3code",
342 number: 4909,
343 });
344 const panelState = {
345 isOpen: true,
346 activeSurfaceId: id,
347 surfaces: [
348 {
349 id,
350 kind: "pull-request" as const,
351 projectId: "project-a",
352 repository: "pingdotgg/t3code",
353 number: 4909,
354 },
355 ],
356 };
357 expect(
358 migratePersistedRightPanelState({
359 byThreadKey: {
360 "env-1:pull-requests-panel": panelState,
361 "env-1:thread-A": panelState,
362 },
363 }),
364 ).toEqual({ byThreadKey: { "env-1:thread-A": panelState } });
365 });
366
367 it("drops persisted plan surfaces and does not reopen an empty panel", () => {
368 expect(
369 migratePersistedRightPanelState({
370 byThreadKey: {
371 "env-1:thread-A": {
372 isOpen: true,
373 activeSurfaceId: "plan",
374 surfaces: [{ id: "plan", kind: "plan" }],
375 },
376 "env-1:thread-B": {
377 isOpen: true,
378 activeSurfaceId: "plan",
379 surfaces: [
380 { id: "plan", kind: "plan" },
381 { id: "diff", kind: "diff" },
382 ],
383 },
384 },
385 }),
386 ).toEqual({
387 byThreadKey: {
388 "env-1:thread-A": {
389 isOpen: false,
390 activeSurfaceId: null,
391 surfaces: [],
392 },
393 "env-1:thread-B": {
394 isOpen: true,
395 activeSurfaceId: "diff",
396 surfaces: [{ id: "diff", kind: "diff" }],
397 },
398 },
399 });
400 });
401
402 it("open sets the active panel for a thread", () => {
403 useRightPanelStore.getState().open(refA, "preview");
404 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("preview");
405 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refB)).toBeNull();
406 });
407
408 it("opening a different kind keeps both surfaces and activates the new one", () => {
409 useRightPanelStore.getState().open(refA, "agents");
410 useRightPanelStore.getState().open(refA, "preview");
411 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("preview");
412 expect(
413 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces,
414 ).toHaveLength(2);
415 });
416
417 it("reopening an inactive singleton activates its existing surface", () => {
418 useRightPanelStore.getState().open(refA, "diff");
419 useRightPanelStore.getState().open(refA, "agents");
420 useRightPanelStore.getState().open(refA, "diff");
421
422 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
423 isOpen: true,
424 activeSurfaceId: "diff",
425 surfaces: [
426 { id: "diff", kind: "diff" },
427 { id: "agents", kind: "agents" },
428 ],
429 });
430 });
431
432 it("keeps files as a singleton surface", () => {
433 useRightPanelStore.getState().open(refA, "files");
434 useRightPanelStore.getState().open(refA, "files");
435 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
436 isOpen: true,
437 activeSurfaceId: "files",
438 surfaces: [{ id: "files", kind: "files" }],
439 });
440 });
441
442 it("replaces the standalone explorer with peer file surfaces", () => {
443 useRightPanelStore.getState().open(refA, "files");
444 useRightPanelStore.getState().openFile(refA, "src/index.ts");
445 useRightPanelStore.getState().openFile(refA, "src/index.ts");
446 useRightPanelStore.getState().openFile(refA, "README.md");
447
448 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
449 isOpen: true,
450 activeSurfaceId: "file:README.md",
451 surfaces: [
452 {
453 id: "file:src/index.ts",
454 kind: "file",
455 relativePath: "src/index.ts",
456 revealLine: null,
457 revealRequestId: 2,
458 },
459 {
460 id: "file:README.md",
461 kind: "file",
462 relativePath: "README.md",
463 revealLine: null,
464 revealRequestId: 1,
465 },
466 ],
467 });
468 });
469
470 it("opens an attachment as a file surface without the standalone explorer", () => {
471 const attachment = {
472 type: "file" as const,
473 id: "thread-A-attachment-pdf",
474 name: "report.pdf",
475 mimeType: "application/pdf",
476 sizeBytes: 42,
477 };
478 useRightPanelStore.getState().open(refA, "files");
479 useRightPanelStore.getState().openAttachment(refA, attachment);
480
481 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
482 isOpen: true,
483 activeSurfaceId: "attachment:thread-A-attachment-pdf",
484 surfaces: [
485 {
486 id: "attachment:thread-A-attachment-pdf",
487 kind: "file",
488 relativePath: "report.pdf",
489 revealLine: null,
490 revealRequestId: 0,
491 attachment,
492 },
493 ],
494 });
495 });
496
497 it("keeps attachment and workspace file ids disjoint", () => {
498 useRightPanelStore.getState().openFile(refA, "attachment:shared-id");
499 useRightPanelStore.getState().openAttachment(refA, {
500 type: "file",
501 id: "shared-id",
502 name: "report.pdf",
503 mimeType: "application/pdf",
504 sizeBytes: 42,
505 });
506
507 expect(
508 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces.map(
509 (surface) => surface.id,
510 ),
511 ).toEqual(["file:attachment:shared-id", "attachment:shared-id"]);
512 });
513
514 it("updates line reveal requests when reopening a file surface", () => {
515 useRightPanelStore.getState().openFile(refA, "src/index.ts", 42);
516 useRightPanelStore.getState().openFile(refA, "src/index.ts", 87);
517
518 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
519 isOpen: true,
520 activeSurfaceId: "file:src/index.ts",
521 surfaces: [
522 {
523 id: "file:src/index.ts",
524 kind: "file",
525 relativePath: "src/index.ts",
526 revealLine: 87,
527 revealRequestId: 2,
528 },
529 ],
530 });
531
532 useRightPanelStore.getState().openFile(refA, "src/index.ts");
533
534 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
535 isOpen: true,
536 activeSurfaceId: "file:src/index.ts",
537 surfaces: [
538 {
539 id: "file:src/index.ts",
540 kind: "file",
541 relativePath: "src/index.ts",
542 revealLine: null,
543 revealRequestId: 3,
544 },
545 ],
546 });
547 });
548
549 it("removes persisted file surfaces when their workspace no longer exists", () => {
550 useRightPanelStore.getState().openFile(refA, "src/index.ts");
551 useRightPanelStore.getState().open(refA, "agents");
552 useRightPanelStore.getState().openFile(refA, "README.md");
553
554 useRightPanelStore.getState().reconcileFileSurfaces(refA, false);
555
556 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
557 isOpen: true,
558 activeSurfaceId: "agents",
559 surfaces: [{ id: "agents", kind: "agents" }],
560 });
561
562 useRightPanelStore.getState().openFile(refB, "conductor.json");
563 useRightPanelStore.getState().reconcileFileSurfaces(refB, false);
564 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refB)).toEqual({
565 isOpen: false,
566 activeSurfaceId: null,
567 surfaces: [],
568 });
569 });
570
571 it("keeps attachment previews when their workspace is unavailable", () => {
572 const attachment = {
573 type: "file" as const,
574 id: "thread-A-attachment-pdf",
575 name: "report.pdf",
576 mimeType: "application/pdf",
577 sizeBytes: 42,
578 };
579 useRightPanelStore.getState().openFile(refA, "README.md");
580 useRightPanelStore.getState().openAttachment(refA, attachment);
581
582 useRightPanelStore.getState().reconcileFileSurfaces(refA, false);
583
584 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
585 isOpen: true,
586 activeSurfaceId: "attachment:thread-A-attachment-pdf",
587 surfaces: [
588 {
589 id: "attachment:thread-A-attachment-pdf",
590 kind: "file",
591 relativePath: "report.pdf",
592 revealLine: null,
593 revealRequestId: 0,
594 attachment,
595 },
596 ],
597 });
598 });
599
600 it("close hides the panel without clearing its selected surface", () => {
601 useRightPanelStore.getState().open(refA, "agents");
602 useRightPanelStore.getState().close(refA);
603 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
604 expect(
605 selectSelectedRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA),
606 ).toEqual({ id: "agents", kind: "agents" });
607 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
608 isOpen: false,
609 activeSurfaceId: "agents",
610 surfaces: [{ id: "agents", kind: "agents" }],
611 });
612 });
613
614 it("toggles empty panel visibility without creating a surface", () => {
615 useRightPanelStore.getState().toggleVisibility(refA);
616 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
617 isOpen: true,
618 activeSurfaceId: null,
619 surfaces: [],
620 });
621
622 useRightPanelStore.getState().toggleVisibility(refA);
623 expect(useRightPanelStore.getState().byThreadKey).toEqual({});
624 });
625
626 it("toggle hides the panel without discarding the active surface", () => {
627 useRightPanelStore.getState().toggle(refA, "diff");
628 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("diff");
629 useRightPanelStore.getState().toggle(refA, "diff");
630 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
631 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
632 isOpen: false,
633 activeSurfaceId: "diff",
634 surfaces: [{ id: "diff", kind: "diff" }],
635 });
636 });
637
638 it("toggle to a different kind switches active", () => {
639 useRightPanelStore.getState().toggle(refA, "preview");
640 useRightPanelStore.getState().toggle(refA, "agents");
641 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("agents");
642 });
643
644 it("removeThread clears persisted state", () => {
645 useRightPanelStore.getState().open(refA, "agents");
646 useRightPanelStore.getState().removeThread(refA);
647 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
648 });
649
650 it("close on never-opened thread is a no-op", () => {
651 useRightPanelStore.getState().close(refA);
652 expect(useRightPanelStore.getState().byThreadKey).toEqual({});
653 });
654
655 it("tracks one surface per browser session", () => {
656 useRightPanelStore.getState().openBrowser(refA, "tab-a");
657 useRightPanelStore.getState().openBrowser(refA, "tab-b");
658
659 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
660 expect(state.surfaces.map((surface) => surface.id)).toEqual(["browser:tab-a", "browser:tab-b"]);
661 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
662 id: "browser:tab-b",
663 kind: "preview",
664 resourceId: "tab-b",
665 });
666 });
667
668 it("tracks one surface per pull request", () => {
669 const first = { projectId: "project-a", repository: "pingdotgg/t3code", number: 4909 };
670 const second = { projectId: "project-a", repository: "pingdotgg/t3code", number: 4910 };
671 useRightPanelStore.getState().openPullRequest(refA, first);
672 useRightPanelStore.getState().openPullRequest(refA, second);
673 const url = "https://gitlab.example.com/pingdotgg/t3code/-/merge_requests/4909";
674 useRightPanelStore.getState().openPullRequest(refA, { ...first, url });
675 useRightPanelStore.getState().openPullRequest(refA, first);
676
677 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
678 expect(state.surfaces.map((surface) => surface.id)).toEqual([
679 pullRequestSurfaceId(first),
680 pullRequestSurfaceId(second),
681 ]);
682 expect(state.activeSurfaceId).toBe(pullRequestSurfaceId(first));
683 expect(
684 selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA),
685 ).toMatchObject({ url });
686 expect(state.surfaces[1]).not.toHaveProperty("url");
687 });
688
689 it("keeps matching repository and number on different hosts as separate tabs", () => {
690 const first = { projectId: "project-a", repository: "acme/api", number: 7, host: "github.com" };
691 const second = { ...first, host: "github.example.com" };
692 useRightPanelStore.getState().openPullRequest(refA, first);
693 useRightPanelStore.getState().openPullRequest(refA, second);
694 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
695 expect(state.surfaces).toEqual([pullRequestSurface(first), pullRequestSurface(second)]);
696 expect(pullRequestSurfaceId({ ...first, host: "GITHUB.COM" })).toBe(
697 pullRequestSurfaceId(first),
698 );
699 });
700
701 it("keeps one pull request read from two servers as two tabs", () => {
702 const local = {
703 environmentId: "local",
704 projectId: "project-a",
705 repository: "pingdotgg/t3code",
706 number: 4909,
707 };
708 const remote = { ...local, environmentId: "remote" };
709
710 useRightPanelStore.getState().openPullRequest(refA, local);
711 useRightPanelStore.getState().openPullRequest(refA, remote);
712
713 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
714 expect(state.surfaces.map((surface) => surface.id)).toEqual([
715 pullRequestSurfaceId(local),
716 pullRequestSurfaceId(remote),
717 ]);
718 });
719
720 it("keeps the page's panel tabs reachable when the set of connected servers changes", () => {
721 // The pull-requests page keys its one shared panel by a fixed sentinel environment, not by
722 // whichever capable server happens to sort first (see PULL_REQUESTS_PANEL_ENVIRONMENT_ID in
723 // _chat.pull-requests.tsx) — a server disconnecting must not move every open tab to a store
724 // key nobody wrote them under.
725 const panelId = ThreadId.make("pull-requests-panel");
726 const stableRef = scopeThreadRef("pull-requests-panel" as EnvironmentId, panelId);
727 const fromServerA = {
728 environmentId: "server-a",
729 projectId: "project-a",
730 repository: "pingdotgg/t3code",
731 number: 1,
732 };
733 const fromServerB = {
734 environmentId: "server-b",
735 projectId: "project-b",
736 repository: "pingdotgg/t3code",
737 number: 2,
738 };
739
740 // Both servers connected: tabs from each open under the one stable ref.
741 useRightPanelStore.getState().openPullRequest(stableRef, fromServerA);
742 useRightPanelStore.getState().openPullRequest(stableRef, fromServerB);
743
744 // Server A disconnects. The stable ref does not depend on which servers remain connected, so
745 // the same lookup still finds both tabs.
746 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, stableRef);
747 expect(state.surfaces.map((surface) => surface.id)).toEqual([
748 pullRequestSurfaceId(fromServerA),
749 pullRequestSurfaceId(fromServerB),
750 ]);
751
752 // The bug this guards against: a ref keyed by the first capable environment instead of a
753 // fixed sentinel changes identity when that environment drops out, and a lookup under the new
754 // key finds nothing even though the tabs are still sitting under the old one.
755 const refWhileBothConnected = scopeThreadRef("server-a" as EnvironmentId, panelId);
756 const refAfterServerADisconnects = scopeThreadRef("server-b" as EnvironmentId, panelId);
757 expect(refWhileBothConnected).not.toEqual(refAfterServerADisconnects);
758 expect(
759 selectThreadRightPanelState(
760 useRightPanelStore.getState().byThreadKey,
761 refAfterServerADisconnects,
762 ).surfaces,
763 ).toEqual([]);
764 });
765
766 it("tracks one surface per terminal session", () => {
767 useRightPanelStore.getState().openTerminal(refA, "term-1");
768 useRightPanelStore.getState().openTerminal(refA, "term-2");
769
770 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
771 expect(state.surfaces).toEqual([
772 {
773 id: "terminal:term-1",
774 kind: "terminal",
775 resourceId: "term-1",
776 terminalIds: ["term-1"],
777 activeTerminalId: "term-1",
778 },
779 {
780 id: "terminal:term-2",
781 kind: "terminal",
782 resourceId: "term-2",
783 terminalIds: ["term-2"],
784 activeTerminalId: "term-2",
785 },
786 ]);
787 expect(state.activeSurfaceId).toBe("terminal:term-2");
788 });
789
790 it("tracks split panes and the active pane within a terminal surface", () => {
791 useRightPanelStore.getState().openTerminal(refA, "term-1");
792 useRightPanelStore.getState().splitTerminal(refA, "terminal:term-1", "term-2");
793
794 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
795 id: "terminal:term-1",
796 kind: "terminal",
797 resourceId: "term-1",
798 terminalIds: ["term-1", "term-2"],
799 activeTerminalId: "term-2",
800 });
801
802 useRightPanelStore.getState().activateTerminal(refA, "terminal:term-1", "term-1");
803 useRightPanelStore.getState().closeTerminal(refA, "terminal:term-1", "term-1");
804 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
805 id: "terminal:term-1",
806 kind: "terminal",
807 resourceId: "term-1",
808 terminalIds: ["term-2"],
809 activeTerminalId: "term-2",
810 });
811 });
812
813 it("tracks vertical layout for a terminal surface", () => {
814 useRightPanelStore.getState().openTerminal(refA, "term-1");
815 useRightPanelStore.getState().splitTerminal(refA, "terminal:term-1", "term-2", "vertical");
816
817 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
818 id: "terminal:term-1",
819 kind: "terminal",
820 resourceId: "term-1",
821 terminalIds: ["term-1", "term-2"],
822 activeTerminalId: "term-2",
823 splitDirection: "vertical",
824 });
825 });
826
827 it("closing the final terminal pane removes its surface and closes the panel", () => {
828 useRightPanelStore.getState().openTerminal(refA, "term-1");
829 useRightPanelStore.getState().closeTerminal(refA, "terminal:term-1", "term-1");
830
831 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
832 isOpen: false,
833 activeSurfaceId: null,
834 surfaces: [],
835 });
836 });
837
838 it("closing the active surface activates a neighboring surface", () => {
839 useRightPanelStore.getState().openBrowser(refA, "tab-a");
840 useRightPanelStore.getState().openTerminal(refA, "term-1");
841 useRightPanelStore.getState().closeSurface(refA, "terminal:term-1");
842
843 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)?.id).toBe(
844 "browser:tab-a",
845 );
846 });
847
848 it("closing the final surface closes the panel", () => {
849 useRightPanelStore.getState().openTerminal(refA, "term-1");
850 useRightPanelStore.getState().closeSurface(refA, "terminal:term-1");
851
852 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
853 isOpen: false,
854 activeSurfaceId: null,
855 surfaces: [],
856 });
857 });
858
859 it("closing other surfaces keeps the selected surface active", () => {
860 useRightPanelStore.getState().openBrowser(refA, "tab-a");
861 useRightPanelStore.getState().openFile(refA, "src/index.ts");
862 useRightPanelStore.getState().openTerminal(refA, "term-1");
863
864 useRightPanelStore.getState().closeOtherSurfaces(refA, "file:src/index.ts");
865
866 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
867 isOpen: true,
868 activeSurfaceId: "file:src/index.ts",
869 surfaces: [
870 {
871 id: "file:src/index.ts",
872 kind: "file",
873 relativePath: "src/index.ts",
874 revealLine: null,
875 revealRequestId: 1,
876 },
877 ],
878 });
879 });
880
881 it("closing surfaces to the right activates the selected surface when active was removed", () => {
882 useRightPanelStore.getState().openBrowser(refA, "tab-a");
883 useRightPanelStore.getState().openFile(refA, "src/index.ts");
884 useRightPanelStore.getState().openTerminal(refA, "term-1");
885
886 useRightPanelStore.getState().closeSurfacesToRight(refA, "browser:tab-a");
887
888 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
889 isOpen: true,
890 activeSurfaceId: "browser:tab-a",
891 surfaces: [{ id: "browser:tab-a", kind: "preview", resourceId: "tab-a" }],
892 });
893 });
894
895 it("closing all surfaces closes the panel", () => {
896 useRightPanelStore.getState().openBrowser(refA, "tab-a");
897 useRightPanelStore.getState().openFile(refA, "src/index.ts");
898
899 useRightPanelStore.getState().closeAllSurfaces(refA);
900
901 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
902 isOpen: false,
903 activeSurfaceId: null,
904 surfaces: [],
905 });
906 });
907
908 it("reconciles browser surfaces without deleting other surface kinds", () => {
909 useRightPanelStore.getState().openTerminal(refA, "term-1");
910 useRightPanelStore.getState().openBrowser(refA, "tab-a");
911 useRightPanelStore.getState().openBrowser(refA, "tab-b");
912 useRightPanelStore.getState().reconcileBrowserSurfaces(refA, ["tab-b", "tab-c"]);
913
914 expect(
915 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces.map(
916 (surface) => surface.id,
917 ),
918 ).toEqual(["terminal:term-1", "browser:tab-b", "browser:tab-c"]);
919 });
920});