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

rightPanelStore.test.ts

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