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

rightPanelStore.test.ts

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