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

rightPanelStore.test.ts

976 lines35.4 KB

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("opens workspace-root links as the singleton files explorer", () => {
451 const store = useRightPanelStore.getState();
452 store.openFile(refA, "README.md");
453 store.openFile(refA, ".");
454 store.openFile(refA, ".");
455 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
456 expect(state.activeSurfaceId).toBe("files");
457 expect(state.surfaces.map((surface) => surface.id)).toEqual(["file:README.md", "files"]);
458 store.closeSurface(refA, "files");
459 expect(
460 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).activeSurfaceId,
461 ).toBe("file:README.md");
462 store.openFile(refA, ".");
463 expect(
464 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).activeSurfaceId,
465 ).toBe("files");
466 });
467​
468 it("replaces the standalone explorer with peer file surfaces", () => {
469 useRightPanelStore.getState().open(refA, "files");
470 useRightPanelStore.getState().openFile(refA, "src/index.ts");
471 useRightPanelStore.getState().openFile(refA, "src/index.ts");
472 useRightPanelStore.getState().openFile(refA, "README.md");
473​
474 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
475 isOpen: true,
476 activeSurfaceId: "file:README.md",
477 surfaces: [
478 {
479 id: "file:src/index.ts",
480 kind: "file",
481 relativePath: "src/index.ts",
482 revealLine: null,
483 revealRequestId: 2,
484 },
485 {
486 id: "file:README.md",
487 kind: "file",
488 relativePath: "README.md",
489 revealLine: null,
490 revealRequestId: 1,
491 },
492 ],
493 });
494 });
495​
496 it.each([
497 ["generated\\", "generated"],
498 ["notes/meeting ", "notes/meeting"],
499 [" notes/meeting", "notes/meeting"],
500 ])("keeps %j and %j in separate file tabs", (firstPath, secondPath) => {
501 useRightPanelStore.getState().openFile(refA, firstPath);
502 useRightPanelStore.getState().openFile(refA, secondPath);
503​
504 expect(
505 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces,
506 ).toMatchObject([
507 { id: `file:${firstPath}`, relativePath: firstPath },
508 { id: `file:${secondPath}`, relativePath: secondPath },
509 ]);
510 });
511​
512 it.each([
513 ["docs/", "docs"],
514 ["docs///", "docs"],
515 ["/", "/"],
516 ["C:/", "C:/"],
517 ])("reuses the folder tab for %j and %j", (linkPath, treePath) => {
518 useRightPanelStore.getState().openFile(refA, linkPath);
519 useRightPanelStore.getState().openFile(refA, treePath);
520​
521 expect(
522 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces,
523 ).toMatchObject([{ id: `file:${treePath}`, relativePath: treePath, revealRequestId: 2 }]);
524 });
525​
526 it("opens an attachment as a file surface without the standalone explorer", () => {
527 const attachment = {
528 type: "file" as const,
529 id: "thread-A-attachment-pdf",
530 name: "report.pdf",
531 mimeType: "application/pdf",
532 sizeBytes: 42,
533 };
534 useRightPanelStore.getState().open(refA, "files");
535 useRightPanelStore.getState().openAttachment(refA, attachment);
536​
537 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
538 isOpen: true,
539 activeSurfaceId: "attachment:thread-A-attachment-pdf",
540 surfaces: [
541 {
542 id: "attachment:thread-A-attachment-pdf",
543 kind: "file",
544 relativePath: "report.pdf",
545 revealLine: null,
546 revealRequestId: 0,
547 attachment,
548 },
549 ],
550 });
551 });
552​
553 it("keeps attachment and workspace file ids disjoint", () => {
554 useRightPanelStore.getState().openFile(refA, "attachment:shared-id");
555 useRightPanelStore.getState().openAttachment(refA, {
556 type: "file",
557 id: "shared-id",
558 name: "report.pdf",
559 mimeType: "application/pdf",
560 sizeBytes: 42,
561 });
562​
563 expect(
564 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces.map(
565 (surface) => surface.id,
566 ),
567 ).toEqual(["file:attachment:shared-id", "attachment:shared-id"]);
568 });
569​
570 it("updates line reveal requests when reopening a file surface", () => {
571 useRightPanelStore.getState().openFile(refA, "src/index.ts", 42);
572 useRightPanelStore.getState().openFile(refA, "src/index.ts", 87);
573​
574 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
575 isOpen: true,
576 activeSurfaceId: "file:src/index.ts",
577 surfaces: [
578 {
579 id: "file:src/index.ts",
580 kind: "file",
581 relativePath: "src/index.ts",
582 revealLine: 87,
583 revealRequestId: 2,
584 },
585 ],
586 });
587​
588 useRightPanelStore.getState().openFile(refA, "src/index.ts");
589​
590 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
591 isOpen: true,
592 activeSurfaceId: "file:src/index.ts",
593 surfaces: [
594 {
595 id: "file:src/index.ts",
596 kind: "file",
597 relativePath: "src/index.ts",
598 revealLine: null,
599 revealRequestId: 3,
600 },
601 ],
602 });
603 });
604​
605 it("removes persisted file surfaces when their workspace no longer exists", () => {
606 useRightPanelStore.getState().openFile(refA, "src/index.ts");
607 useRightPanelStore.getState().open(refA, "agents");
608 useRightPanelStore.getState().openFile(refA, "README.md");
609​
610 useRightPanelStore.getState().reconcileFileSurfaces(refA, false);
611​
612 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
613 isOpen: true,
614 activeSurfaceId: "agents",
615 surfaces: [{ id: "agents", kind: "agents" }],
616 });
617​
618 useRightPanelStore.getState().openFile(refB, "conductor.json");
619 useRightPanelStore.getState().reconcileFileSurfaces(refB, false);
620 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refB)).toEqual({
621 isOpen: false,
622 activeSurfaceId: null,
623 surfaces: [],
624 });
625 });
626​
627 it("keeps attachment previews when their workspace is unavailable", () => {
628 const attachment = {
629 type: "file" as const,
630 id: "thread-A-attachment-pdf",
631 name: "report.pdf",
632 mimeType: "application/pdf",
633 sizeBytes: 42,
634 };
635 useRightPanelStore.getState().openFile(refA, "README.md");
636 useRightPanelStore.getState().openAttachment(refA, attachment);
637​
638 useRightPanelStore.getState().reconcileFileSurfaces(refA, false);
639​
640 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
641 isOpen: true,
642 activeSurfaceId: "attachment:thread-A-attachment-pdf",
643 surfaces: [
644 {
645 id: "attachment:thread-A-attachment-pdf",
646 kind: "file",
647 relativePath: "report.pdf",
648 revealLine: null,
649 revealRequestId: 0,
650 attachment,
651 },
652 ],
653 });
654 });
655​
656 it("close hides the panel without clearing its selected surface", () => {
657 useRightPanelStore.getState().open(refA, "agents");
658 useRightPanelStore.getState().close(refA);
659 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
660 expect(
661 selectSelectedRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA),
662 ).toEqual({ id: "agents", kind: "agents" });
663 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
664 isOpen: false,
665 activeSurfaceId: "agents",
666 surfaces: [{ id: "agents", kind: "agents" }],
667 });
668 });
669​
670 it("toggles empty panel visibility without creating a surface", () => {
671 useRightPanelStore.getState().toggleVisibility(refA);
672 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
673 isOpen: true,
674 activeSurfaceId: null,
675 surfaces: [],
676 });
677​
678 useRightPanelStore.getState().toggleVisibility(refA);
679 expect(useRightPanelStore.getState().byThreadKey).toEqual({});
680 });
681​
682 it("toggle hides the panel without discarding the active surface", () => {
683 useRightPanelStore.getState().toggle(refA, "diff");
684 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("diff");
685 useRightPanelStore.getState().toggle(refA, "diff");
686 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
687 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
688 isOpen: false,
689 activeSurfaceId: "diff",
690 surfaces: [{ id: "diff", kind: "diff" }],
691 });
692 });
693​
694 it("toggle to a different kind switches active", () => {
695 useRightPanelStore.getState().toggle(refA, "preview");
696 useRightPanelStore.getState().toggle(refA, "agents");
697 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("agents");
698 });
699​
700 it("removeThread clears persisted state", () => {
701 useRightPanelStore.getState().open(refA, "agents");
702 useRightPanelStore.getState().removeThread(refA);
703 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
704 });
705​
706 it("close on never-opened thread is a no-op", () => {
707 useRightPanelStore.getState().close(refA);
708 expect(useRightPanelStore.getState().byThreadKey).toEqual({});
709 });
710​
711 it("tracks one surface per browser session", () => {
712 useRightPanelStore.getState().openBrowser(refA, "tab-a");
713 useRightPanelStore.getState().openBrowser(refA, "tab-b");
714​
715 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
716 expect(state.surfaces.map((surface) => surface.id)).toEqual(["browser:tab-a", "browser:tab-b"]);
717 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
718 id: "browser:tab-b",
719 kind: "preview",
720 resourceId: "tab-b",
721 });
722 });
723​
724 it("tracks one surface per pull request", () => {
725 const first = { projectId: "project-a", repository: "pingdotgg/t3code", number: 4909 };
726 const second = { projectId: "project-a", repository: "pingdotgg/t3code", number: 4910 };
727 useRightPanelStore.getState().openPullRequest(refA, first);
728 useRightPanelStore.getState().openPullRequest(refA, second);
729 const url = "https://gitlab.example.com/pingdotgg/t3code/-/merge_requests/4909";
730 useRightPanelStore.getState().openPullRequest(refA, { ...first, url });
731 useRightPanelStore.getState().openPullRequest(refA, first);
732​
733 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
734 expect(state.surfaces.map((surface) => surface.id)).toEqual([
735 pullRequestSurfaceId(first),
736 pullRequestSurfaceId(second),
737 ]);
738 expect(state.activeSurfaceId).toBe(pullRequestSurfaceId(first));
739 expect(
740 selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA),
741 ).toMatchObject({ url });
742 expect(state.surfaces[1]).not.toHaveProperty("url");
743 });
744​
745 it("keeps matching repository and number on different hosts as separate tabs", () => {
746 const first = { projectId: "project-a", repository: "acme/api", number: 7, host: "github.com" };
747 const second = { ...first, host: "github.example.com" };
748 useRightPanelStore.getState().openPullRequest(refA, first);
749 useRightPanelStore.getState().openPullRequest(refA, second);
750 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
751 expect(state.surfaces).toEqual([pullRequestSurface(first), pullRequestSurface(second)]);
752 expect(pullRequestSurfaceId({ ...first, host: "GITHUB.COM" })).toBe(
753 pullRequestSurfaceId(first),
754 );
755 });
756​
757 it("keeps one pull request read from two servers as two tabs", () => {
758 const local = {
759 environmentId: "local",
760 projectId: "project-a",
761 repository: "pingdotgg/t3code",
762 number: 4909,
763 };
764 const remote = { ...local, environmentId: "remote" };
765​
766 useRightPanelStore.getState().openPullRequest(refA, local);
767 useRightPanelStore.getState().openPullRequest(refA, remote);
768​
769 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
770 expect(state.surfaces.map((surface) => surface.id)).toEqual([
771 pullRequestSurfaceId(local),
772 pullRequestSurfaceId(remote),
773 ]);
774 });
775​
776 it("keeps the page's panel tabs reachable when the set of connected servers changes", () => {
777 // The pull-requests page keys its one shared panel by a fixed sentinel environment, not by
778 // whichever capable server happens to sort first (see PULL_REQUESTS_PANEL_ENVIRONMENT_ID in
779 // _chat.pull-requests.tsx) — a server disconnecting must not move every open tab to a store
780 // key nobody wrote them under.
781 const panelId = ThreadId.make("pull-requests-panel");
782 const stableRef = scopeThreadRef("pull-requests-panel" as EnvironmentId, panelId);
783 const fromServerA = {
784 environmentId: "server-a",
785 projectId: "project-a",
786 repository: "pingdotgg/t3code",
787 number: 1,
788 };
789 const fromServerB = {
790 environmentId: "server-b",
791 projectId: "project-b",
792 repository: "pingdotgg/t3code",
793 number: 2,
794 };
795​
796 // Both servers connected: tabs from each open under the one stable ref.
797 useRightPanelStore.getState().openPullRequest(stableRef, fromServerA);
798 useRightPanelStore.getState().openPullRequest(stableRef, fromServerB);
799​
800 // Server A disconnects. The stable ref does not depend on which servers remain connected, so
801 // the same lookup still finds both tabs.
802 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, stableRef);
803 expect(state.surfaces.map((surface) => surface.id)).toEqual([
804 pullRequestSurfaceId(fromServerA),
805 pullRequestSurfaceId(fromServerB),
806 ]);
807​
808 // The bug this guards against: a ref keyed by the first capable environment instead of a
809 // fixed sentinel changes identity when that environment drops out, and a lookup under the new
810 // key finds nothing even though the tabs are still sitting under the old one.
811 const refWhileBothConnected = scopeThreadRef("server-a" as EnvironmentId, panelId);
812 const refAfterServerADisconnects = scopeThreadRef("server-b" as EnvironmentId, panelId);
813 expect(refWhileBothConnected).not.toEqual(refAfterServerADisconnects);
814 expect(
815 selectThreadRightPanelState(
816 useRightPanelStore.getState().byThreadKey,
817 refAfterServerADisconnects,
818 ).surfaces,
819 ).toEqual([]);
820 });
821​
822 it("tracks one surface per terminal session", () => {
823 useRightPanelStore.getState().openTerminal(refA, "term-1");
824 useRightPanelStore.getState().openTerminal(refA, "term-2");
825​
826 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
827 expect(state.surfaces).toEqual([
828 {
829 id: "terminal:term-1",
830 kind: "terminal",
831 resourceId: "term-1",
832 terminalIds: ["term-1"],
833 activeTerminalId: "term-1",
834 },
835 {
836 id: "terminal:term-2",
837 kind: "terminal",
838 resourceId: "term-2",
839 terminalIds: ["term-2"],
840 activeTerminalId: "term-2",
841 },
842 ]);
843 expect(state.activeSurfaceId).toBe("terminal:term-2");
844 });
845​
846 it("tracks split panes and the active pane within a terminal surface", () => {
847 useRightPanelStore.getState().openTerminal(refA, "term-1");
848 useRightPanelStore.getState().splitTerminal(refA, "terminal:term-1", "term-2");
849​
850 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
851 id: "terminal:term-1",
852 kind: "terminal",
853 resourceId: "term-1",
854 terminalIds: ["term-1", "term-2"],
855 activeTerminalId: "term-2",
856 });
857​
858 useRightPanelStore.getState().activateTerminal(refA, "terminal:term-1", "term-1");
859 useRightPanelStore.getState().closeTerminal(refA, "terminal:term-1", "term-1");
860 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
861 id: "terminal:term-1",
862 kind: "terminal",
863 resourceId: "term-1",
864 terminalIds: ["term-2"],
865 activeTerminalId: "term-2",
866 });
867 });
868​
869 it("tracks vertical layout for a terminal surface", () => {
870 useRightPanelStore.getState().openTerminal(refA, "term-1");
871 useRightPanelStore.getState().splitTerminal(refA, "terminal:term-1", "term-2", "vertical");
872​
873 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
874 id: "terminal:term-1",
875 kind: "terminal",
876 resourceId: "term-1",
877 terminalIds: ["term-1", "term-2"],
878 activeTerminalId: "term-2",
879 splitDirection: "vertical",
880 });
881 });
882​
883 it("closing the final terminal pane removes its surface and closes the panel", () => {
884 useRightPanelStore.getState().openTerminal(refA, "term-1");
885 useRightPanelStore.getState().closeTerminal(refA, "terminal:term-1", "term-1");
886​
887 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
888 isOpen: false,
889 activeSurfaceId: null,
890 surfaces: [],
891 });
892 });
893​
894 it("closing the active surface activates a neighboring surface", () => {
895 useRightPanelStore.getState().openBrowser(refA, "tab-a");
896 useRightPanelStore.getState().openTerminal(refA, "term-1");
897 useRightPanelStore.getState().closeSurface(refA, "terminal:term-1");
898​
899 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)?.id).toBe(
900 "browser:tab-a",
901 );
902 });
903​
904 it("closing the final surface closes the panel", () => {
905 useRightPanelStore.getState().openTerminal(refA, "term-1");
906 useRightPanelStore.getState().closeSurface(refA, "terminal:term-1");
907​
908 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
909 isOpen: false,
910 activeSurfaceId: null,
911 surfaces: [],
912 });
913 });
914​
915 it("closing other surfaces keeps the selected surface active", () => {
916 useRightPanelStore.getState().openBrowser(refA, "tab-a");
917 useRightPanelStore.getState().openFile(refA, "src/index.ts");
918 useRightPanelStore.getState().openTerminal(refA, "term-1");
919​
920 useRightPanelStore.getState().closeOtherSurfaces(refA, "file:src/index.ts");
921​
922 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
923 isOpen: true,
924 activeSurfaceId: "file:src/index.ts",
925 surfaces: [
926 {
927 id: "file:src/index.ts",
928 kind: "file",
929 relativePath: "src/index.ts",
930 revealLine: null,
931 revealRequestId: 1,
932 },
933 ],
934 });
935 });
936​
937 it("closing surfaces to the right activates the selected surface when active was removed", () => {
938 useRightPanelStore.getState().openBrowser(refA, "tab-a");
939 useRightPanelStore.getState().openFile(refA, "src/index.ts");
940 useRightPanelStore.getState().openTerminal(refA, "term-1");
941​
942 useRightPanelStore.getState().closeSurfacesToRight(refA, "browser:tab-a");
943​
944 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
945 isOpen: true,
946 activeSurfaceId: "browser:tab-a",
947 surfaces: [{ id: "browser:tab-a", kind: "preview", resourceId: "tab-a" }],
948 });
949 });
950​
951 it("closing all surfaces closes the panel", () => {
952 useRightPanelStore.getState().openBrowser(refA, "tab-a");
953 useRightPanelStore.getState().openFile(refA, "src/index.ts");
954​
955 useRightPanelStore.getState().closeAllSurfaces(refA);
956​
957 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
958 isOpen: false,
959 activeSurfaceId: null,
960 surfaces: [],
961 });
962 });
963​
964 it("reconciles browser surfaces without deleting other surface kinds", () => {
965 useRightPanelStore.getState().openTerminal(refA, "term-1");
966 useRightPanelStore.getState().openBrowser(refA, "tab-a");
967 useRightPanelStore.getState().openBrowser(refA, "tab-b");
968 useRightPanelStore.getState().reconcileBrowserSurfaces(refA, ["tab-b", "tab-c"]);
969​
970 expect(
971 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces.map(
972 (surface) => surface.id,
973 ),
974 ).toEqual(["terminal:term-1", "browser:tab-b", "browser:tab-c"]);
975 });
976});