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 one pull request read from two servers as two tabs", () => {
609 const local = {
610 environmentId: "local",
611 projectId: "project-a",
612 repository: "pingdotgg/t3code",
613 number: 4909,
614 };
615 const remote = { ...local, environmentId: "remote" };
616
617 useRightPanelStore.getState().openPullRequest(refA, local);
618 useRightPanelStore.getState().openPullRequest(refA, remote);
619
620 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
621 expect(state.surfaces.map((surface) => surface.id)).toEqual([
622 pullRequestSurfaceId(local),
623 pullRequestSurfaceId(remote),
624 ]);
625 });
626
627 it("keeps the page's panel tabs reachable when the set of connected servers changes", () => {
628 // The pull-requests page keys its one shared panel by a fixed sentinel environment, not by
629 // whichever capable server happens to sort first (see PULL_REQUESTS_PANEL_ENVIRONMENT_ID in
630 // _chat.pull-requests.tsx) — a server disconnecting must not move every open tab to a store
631 // key nobody wrote them under.
632 const panelId = ThreadId.make("pull-requests-panel");
633 const stableRef = scopeThreadRef("pull-requests-panel" as EnvironmentId, panelId);
634 const fromServerA = {
635 environmentId: "server-a",
636 projectId: "project-a",
637 repository: "pingdotgg/t3code",
638 number: 1,
639 };
640 const fromServerB = {
641 environmentId: "server-b",
642 projectId: "project-b",
643 repository: "pingdotgg/t3code",
644 number: 2,
645 };
646
647 // Both servers connected: tabs from each open under the one stable ref.
648 useRightPanelStore.getState().openPullRequest(stableRef, fromServerA);
649 useRightPanelStore.getState().openPullRequest(stableRef, fromServerB);
650
651 // Server A disconnects. The stable ref does not depend on which servers remain connected, so
652 // the same lookup still finds both tabs.
653 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, stableRef);
654 expect(state.surfaces.map((surface) => surface.id)).toEqual([
655 pullRequestSurfaceId(fromServerA),
656 pullRequestSurfaceId(fromServerB),
657 ]);
658
659 // The bug this guards against: a ref keyed by the first capable environment instead of a
660 // fixed sentinel changes identity when that environment drops out, and a lookup under the new
661 // key finds nothing even though the tabs are still sitting under the old one.
662 const refWhileBothConnected = scopeThreadRef("server-a" as EnvironmentId, panelId);
663 const refAfterServerADisconnects = scopeThreadRef("server-b" as EnvironmentId, panelId);
664 expect(refWhileBothConnected).not.toEqual(refAfterServerADisconnects);
665 expect(
666 selectThreadRightPanelState(
667 useRightPanelStore.getState().byThreadKey,
668 refAfterServerADisconnects,
669 ).surfaces,
670 ).toEqual([]);
671 });
672
673 it("tracks one surface per terminal session", () => {
674 useRightPanelStore.getState().openTerminal(refA, "term-1");
675 useRightPanelStore.getState().openTerminal(refA, "term-2");
676
677 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
678 expect(state.surfaces).toEqual([
679 {
680 id: "terminal:term-1",
681 kind: "terminal",
682 resourceId: "term-1",
683 terminalIds: ["term-1"],
684 activeTerminalId: "term-1",
685 },
686 {
687 id: "terminal:term-2",
688 kind: "terminal",
689 resourceId: "term-2",
690 terminalIds: ["term-2"],
691 activeTerminalId: "term-2",
692 },
693 ]);
694 expect(state.activeSurfaceId).toBe("terminal:term-2");
695 });
696
697 it("tracks split panes and the active pane within a terminal surface", () => {
698 useRightPanelStore.getState().openTerminal(refA, "term-1");
699 useRightPanelStore.getState().splitTerminal(refA, "terminal:term-1", "term-2");
700
701 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
702 id: "terminal:term-1",
703 kind: "terminal",
704 resourceId: "term-1",
705 terminalIds: ["term-1", "term-2"],
706 activeTerminalId: "term-2",
707 });
708
709 useRightPanelStore.getState().activateTerminal(refA, "terminal:term-1", "term-1");
710 useRightPanelStore.getState().closeTerminal(refA, "terminal:term-1", "term-1");
711 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
712 id: "terminal:term-1",
713 kind: "terminal",
714 resourceId: "term-1",
715 terminalIds: ["term-2"],
716 activeTerminalId: "term-2",
717 });
718 });
719
720 it("tracks vertical layout for a terminal surface", () => {
721 useRightPanelStore.getState().openTerminal(refA, "term-1");
722 useRightPanelStore.getState().splitTerminal(refA, "terminal:term-1", "term-2", "vertical");
723
724 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
725 id: "terminal:term-1",
726 kind: "terminal",
727 resourceId: "term-1",
728 terminalIds: ["term-1", "term-2"],
729 activeTerminalId: "term-2",
730 splitDirection: "vertical",
731 });
732 });
733
734 it("closing the final terminal pane removes its surface and closes the panel", () => {
735 useRightPanelStore.getState().openTerminal(refA, "term-1");
736 useRightPanelStore.getState().closeTerminal(refA, "terminal:term-1", "term-1");
737
738 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
739 isOpen: false,
740 activeSurfaceId: null,
741 surfaces: [],
742 });
743 });
744
745 it("closing the active surface activates a neighboring surface", () => {
746 useRightPanelStore.getState().openBrowser(refA, "tab-a");
747 useRightPanelStore.getState().openTerminal(refA, "term-1");
748 useRightPanelStore.getState().closeSurface(refA, "terminal:term-1");
749
750 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)?.id).toBe(
751 "browser:tab-a",
752 );
753 });
754
755 it("closing the final surface closes the panel", () => {
756 useRightPanelStore.getState().openTerminal(refA, "term-1");
757 useRightPanelStore.getState().closeSurface(refA, "terminal:term-1");
758
759 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
760 isOpen: false,
761 activeSurfaceId: null,
762 surfaces: [],
763 });
764 });
765
766 it("closing other surfaces keeps the selected surface active", () => {
767 useRightPanelStore.getState().openBrowser(refA, "tab-a");
768 useRightPanelStore.getState().openFile(refA, "src/index.ts");
769 useRightPanelStore.getState().openTerminal(refA, "term-1");
770
771 useRightPanelStore.getState().closeOtherSurfaces(refA, "file:src/index.ts");
772
773 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
774 isOpen: true,
775 activeSurfaceId: "file:src/index.ts",
776 surfaces: [
777 {
778 id: "file:src/index.ts",
779 kind: "file",
780 relativePath: "src/index.ts",
781 revealLine: null,
782 revealRequestId: 1,
783 },
784 ],
785 });
786 });
787
788 it("closing surfaces to the right activates the selected surface when active was removed", () => {
789 useRightPanelStore.getState().openBrowser(refA, "tab-a");
790 useRightPanelStore.getState().openFile(refA, "src/index.ts");
791 useRightPanelStore.getState().openTerminal(refA, "term-1");
792
793 useRightPanelStore.getState().closeSurfacesToRight(refA, "browser:tab-a");
794
795 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
796 isOpen: true,
797 activeSurfaceId: "browser:tab-a",
798 surfaces: [{ id: "browser:tab-a", kind: "preview", resourceId: "tab-a" }],
799 });
800 });
801
802 it("closing all surfaces closes the panel", () => {
803 useRightPanelStore.getState().openBrowser(refA, "tab-a");
804 useRightPanelStore.getState().openFile(refA, "src/index.ts");
805
806 useRightPanelStore.getState().closeAllSurfaces(refA);
807
808 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
809 isOpen: false,
810 activeSurfaceId: null,
811 surfaces: [],
812 });
813 });
814
815 it("reconciles browser surfaces without deleting other surface kinds", () => {
816 useRightPanelStore.getState().openTerminal(refA, "term-1");
817 useRightPanelStore.getState().openBrowser(refA, "tab-a");
818 useRightPanelStore.getState().openBrowser(refA, "tab-b");
819 useRightPanelStore.getState().reconcileBrowserSurfaces(refA, ["tab-b", "tab-c"]);
820
821 expect(
822 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces.map(
823 (surface) => surface.id,
824 ),
825 ).toEqual(["terminal:term-1", "browser:tab-b", "browser:tab-c"]);
826 });
827});