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