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