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

rightPanelStore.test.ts

484 lines16.5 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 selectActiveRightPanel,
8 selectActiveRightPanelSurface,
9 selectThreadRightPanelState,
10 useRightPanelStore,
11} from "./rightPanelStore";
12​
13const refA = scopeThreadRef("env-1" as EnvironmentId, ThreadId.make("thread-A"));
14const refB = scopeThreadRef("env-1" as EnvironmentId, ThreadId.make("thread-B"));
15​
16beforeEach(() => {
17 useRightPanelStore.setState({ byThreadKey: {} });
18});
19​
20describe("rightPanelStore", () => {
21 it("drops the legacy singleton terminal surface during migration", () => {
22 expect(
23 migratePersistedRightPanelState({
24 byThreadKey: {
25 "env-1:thread-A": {
26 activeSurfaceId: "terminal",
27 surfaces: [
28 { id: "browser:tab-a", kind: "preview", resourceId: "tab-a" },
29 { id: "terminal", kind: "terminal" },
30 ],
31 },
32 },
33 }),
34 ).toEqual({
35 byThreadKey: {
36 "env-1:thread-A": {
37 isOpen: false,
38 activeSurfaceId: null,
39 surfaces: [{ id: "browser:tab-a", kind: "preview", resourceId: "tab-a" }],
40 },
41 },
42 });
43 });
44​
45 it("upgrades saved single-session terminal surfaces to split-capable surfaces", () => {
46 expect(
47 migratePersistedRightPanelState({
48 byThreadKey: {
49 "env-1:thread-A": {
50 isOpen: true,
51 activeSurfaceId: "terminal:term-1",
52 surfaces: [{ id: "terminal:term-1", kind: "terminal", resourceId: "term-1" }],
53 },
54 },
55 }),
56 ).toEqual({
57 byThreadKey: {
58 "env-1:thread-A": {
59 isOpen: true,
60 activeSurfaceId: "terminal:term-1",
61 surfaces: [
62 {
63 id: "terminal:term-1",
64 kind: "terminal",
65 resourceId: "term-1",
66 terminalIds: ["term-1"],
67 activeTerminalId: "term-1",
68 },
69 ],
70 },
71 },
72 });
73 });
74​
75 it("upgrades saved file surfaces with neutral reveal state", () => {
76 expect(
77 migratePersistedRightPanelState({
78 byThreadKey: {
79 "env-1:thread-A": {
80 isOpen: true,
81 activeSurfaceId: "file:src/index.ts",
82 surfaces: [{ id: "file:src/index.ts", kind: "file", relativePath: "src/index.ts" }],
83 },
84 },
85 }),
86 ).toEqual({
87 byThreadKey: {
88 "env-1:thread-A": {
89 isOpen: true,
90 activeSurfaceId: "file:src/index.ts",
91 surfaces: [
92 {
93 id: "file:src/index.ts",
94 kind: "file",
95 relativePath: "src/index.ts",
96 revealLine: null,
97 revealRequestId: 0,
98 },
99 ],
100 },
101 },
102 });
103 });
104​
105 it("drops persisted plan surfaces and does not reopen an empty panel", () => {
106 expect(
107 migratePersistedRightPanelState({
108 byThreadKey: {
109 "env-1:thread-A": {
110 isOpen: true,
111 activeSurfaceId: "plan",
112 surfaces: [{ id: "plan", kind: "plan" }],
113 },
114 "env-1:thread-B": {
115 isOpen: true,
116 activeSurfaceId: "plan",
117 surfaces: [
118 { id: "plan", kind: "plan" },
119 { id: "diff", kind: "diff" },
120 ],
121 },
122 },
123 }),
124 ).toEqual({
125 byThreadKey: {
126 "env-1:thread-A": {
127 isOpen: false,
128 activeSurfaceId: null,
129 surfaces: [],
130 },
131 "env-1:thread-B": {
132 isOpen: true,
133 activeSurfaceId: "diff",
134 surfaces: [{ id: "diff", kind: "diff" }],
135 },
136 },
137 });
138 });
139​
140 it("open sets the active panel for a thread", () => {
141 useRightPanelStore.getState().open(refA, "preview");
142 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("preview");
143 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refB)).toBeNull();
144 });
145​
146 it("opening a different kind keeps both surfaces and activates the new one", () => {
147 useRightPanelStore.getState().open(refA, "agents");
148 useRightPanelStore.getState().open(refA, "preview");
149 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("preview");
150 expect(
151 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces,
152 ).toHaveLength(2);
153 });
154​
155 it("reopening an inactive singleton activates its existing surface", () => {
156 useRightPanelStore.getState().open(refA, "diff");
157 useRightPanelStore.getState().open(refA, "agents");
158 useRightPanelStore.getState().open(refA, "diff");
159​
160 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
161 isOpen: true,
162 activeSurfaceId: "diff",
163 surfaces: [
164 { id: "diff", kind: "diff" },
165 { id: "agents", kind: "agents" },
166 ],
167 });
168 });
169​
170 it("keeps files as a singleton surface", () => {
171 useRightPanelStore.getState().open(refA, "files");
172 useRightPanelStore.getState().open(refA, "files");
173 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
174 isOpen: true,
175 activeSurfaceId: "files",
176 surfaces: [{ id: "files", kind: "files" }],
177 });
178 });
179​
180 it("replaces the standalone explorer with peer file surfaces", () => {
181 useRightPanelStore.getState().open(refA, "files");
182 useRightPanelStore.getState().openFile(refA, "src/index.ts");
183 useRightPanelStore.getState().openFile(refA, "src/index.ts");
184 useRightPanelStore.getState().openFile(refA, "README.md");
185​
186 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
187 isOpen: true,
188 activeSurfaceId: "file:README.md",
189 surfaces: [
190 {
191 id: "file:src/index.ts",
192 kind: "file",
193 relativePath: "src/index.ts",
194 revealLine: null,
195 revealRequestId: 2,
196 },
197 {
198 id: "file:README.md",
199 kind: "file",
200 relativePath: "README.md",
201 revealLine: null,
202 revealRequestId: 1,
203 },
204 ],
205 });
206 });
207​
208 it("updates line reveal requests when reopening a file surface", () => {
209 useRightPanelStore.getState().openFile(refA, "src/index.ts", 42);
210 useRightPanelStore.getState().openFile(refA, "src/index.ts", 87);
211​
212 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
213 isOpen: true,
214 activeSurfaceId: "file:src/index.ts",
215 surfaces: [
216 {
217 id: "file:src/index.ts",
218 kind: "file",
219 relativePath: "src/index.ts",
220 revealLine: 87,
221 revealRequestId: 2,
222 },
223 ],
224 });
225​
226 useRightPanelStore.getState().openFile(refA, "src/index.ts");
227​
228 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
229 isOpen: true,
230 activeSurfaceId: "file:src/index.ts",
231 surfaces: [
232 {
233 id: "file:src/index.ts",
234 kind: "file",
235 relativePath: "src/index.ts",
236 revealLine: null,
237 revealRequestId: 3,
238 },
239 ],
240 });
241 });
242​
243 it("removes persisted file surfaces when their workspace no longer exists", () => {
244 useRightPanelStore.getState().openFile(refA, "src/index.ts");
245 useRightPanelStore.getState().open(refA, "agents");
246 useRightPanelStore.getState().openFile(refA, "README.md");
247​
248 useRightPanelStore.getState().reconcileFileSurfaces(refA, false);
249​
250 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
251 isOpen: true,
252 activeSurfaceId: "agents",
253 surfaces: [{ id: "agents", kind: "agents" }],
254 });
255​
256 useRightPanelStore.getState().openFile(refB, "conductor.json");
257 useRightPanelStore.getState().reconcileFileSurfaces(refB, false);
258 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refB)).toEqual({
259 isOpen: false,
260 activeSurfaceId: null,
261 surfaces: [],
262 });
263 });
264​
265 it("close hides the panel without clearing its selected surface", () => {
266 useRightPanelStore.getState().open(refA, "agents");
267 useRightPanelStore.getState().close(refA);
268 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
269 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
270 isOpen: false,
271 activeSurfaceId: "agents",
272 surfaces: [{ id: "agents", kind: "agents" }],
273 });
274 });
275​
276 it("toggles empty panel visibility without creating a surface", () => {
277 useRightPanelStore.getState().toggleVisibility(refA);
278 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
279 isOpen: true,
280 activeSurfaceId: null,
281 surfaces: [],
282 });
283​
284 useRightPanelStore.getState().toggleVisibility(refA);
285 expect(useRightPanelStore.getState().byThreadKey).toEqual({});
286 });
287​
288 it("toggle hides the panel without discarding the active surface", () => {
289 useRightPanelStore.getState().toggle(refA, "diff");
290 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("diff");
291 useRightPanelStore.getState().toggle(refA, "diff");
292 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
293 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
294 isOpen: false,
295 activeSurfaceId: "diff",
296 surfaces: [{ id: "diff", kind: "diff" }],
297 });
298 });
299​
300 it("toggle to a different kind switches active", () => {
301 useRightPanelStore.getState().toggle(refA, "preview");
302 useRightPanelStore.getState().toggle(refA, "agents");
303 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("agents");
304 });
305​
306 it("removeThread clears persisted state", () => {
307 useRightPanelStore.getState().open(refA, "agents");
308 useRightPanelStore.getState().removeThread(refA);
309 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
310 });
311​
312 it("close on never-opened thread is a no-op", () => {
313 useRightPanelStore.getState().close(refA);
314 expect(useRightPanelStore.getState().byThreadKey).toEqual({});
315 });
316​
317 it("tracks one surface per browser session", () => {
318 useRightPanelStore.getState().openBrowser(refA, "tab-a");
319 useRightPanelStore.getState().openBrowser(refA, "tab-b");
320​
321 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
322 expect(state.surfaces.map((surface) => surface.id)).toEqual(["browser:tab-a", "browser:tab-b"]);
323 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
324 id: "browser:tab-b",
325 kind: "preview",
326 resourceId: "tab-b",
327 });
328 });
329​
330 it("tracks one surface per terminal session", () => {
331 useRightPanelStore.getState().openTerminal(refA, "term-1");
332 useRightPanelStore.getState().openTerminal(refA, "term-2");
333​
334 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
335 expect(state.surfaces).toEqual([
336 {
337 id: "terminal:term-1",
338 kind: "terminal",
339 resourceId: "term-1",
340 terminalIds: ["term-1"],
341 activeTerminalId: "term-1",
342 },
343 {
344 id: "terminal:term-2",
345 kind: "terminal",
346 resourceId: "term-2",
347 terminalIds: ["term-2"],
348 activeTerminalId: "term-2",
349 },
350 ]);
351 expect(state.activeSurfaceId).toBe("terminal:term-2");
352 });
353​
354 it("tracks split panes and the active pane within a terminal surface", () => {
355 useRightPanelStore.getState().openTerminal(refA, "term-1");
356 useRightPanelStore.getState().splitTerminal(refA, "terminal:term-1", "term-2");
357​
358 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
359 id: "terminal:term-1",
360 kind: "terminal",
361 resourceId: "term-1",
362 terminalIds: ["term-1", "term-2"],
363 activeTerminalId: "term-2",
364 });
365​
366 useRightPanelStore.getState().activateTerminal(refA, "terminal:term-1", "term-1");
367 useRightPanelStore.getState().closeTerminal(refA, "terminal:term-1", "term-1");
368 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
369 id: "terminal:term-1",
370 kind: "terminal",
371 resourceId: "term-1",
372 terminalIds: ["term-2"],
373 activeTerminalId: "term-2",
374 });
375 });
376​
377 it("tracks vertical layout for a terminal surface", () => {
378 useRightPanelStore.getState().openTerminal(refA, "term-1");
379 useRightPanelStore.getState().splitTerminal(refA, "terminal:term-1", "term-2", "vertical");
380​
381 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
382 id: "terminal:term-1",
383 kind: "terminal",
384 resourceId: "term-1",
385 terminalIds: ["term-1", "term-2"],
386 activeTerminalId: "term-2",
387 splitDirection: "vertical",
388 });
389 });
390​
391 it("closing the final terminal pane removes its surface and closes the panel", () => {
392 useRightPanelStore.getState().openTerminal(refA, "term-1");
393 useRightPanelStore.getState().closeTerminal(refA, "terminal:term-1", "term-1");
394​
395 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
396 isOpen: false,
397 activeSurfaceId: null,
398 surfaces: [],
399 });
400 });
401​
402 it("closing the active surface activates a neighboring surface", () => {
403 useRightPanelStore.getState().openBrowser(refA, "tab-a");
404 useRightPanelStore.getState().openTerminal(refA, "term-1");
405 useRightPanelStore.getState().closeSurface(refA, "terminal:term-1");
406​
407 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)?.id).toBe(
408 "browser:tab-a",
409 );
410 });
411​
412 it("closing the final surface closes the panel", () => {
413 useRightPanelStore.getState().openTerminal(refA, "term-1");
414 useRightPanelStore.getState().closeSurface(refA, "terminal:term-1");
415​
416 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
417 isOpen: false,
418 activeSurfaceId: null,
419 surfaces: [],
420 });
421 });
422​
423 it("closing other surfaces keeps the selected surface active", () => {
424 useRightPanelStore.getState().openBrowser(refA, "tab-a");
425 useRightPanelStore.getState().openFile(refA, "src/index.ts");
426 useRightPanelStore.getState().openTerminal(refA, "term-1");
427​
428 useRightPanelStore.getState().closeOtherSurfaces(refA, "file:src/index.ts");
429​
430 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
431 isOpen: true,
432 activeSurfaceId: "file:src/index.ts",
433 surfaces: [
434 {
435 id: "file:src/index.ts",
436 kind: "file",
437 relativePath: "src/index.ts",
438 revealLine: null,
439 revealRequestId: 1,
440 },
441 ],
442 });
443 });
444​
445 it("closing surfaces to the right activates the selected surface when active was removed", () => {
446 useRightPanelStore.getState().openBrowser(refA, "tab-a");
447 useRightPanelStore.getState().openFile(refA, "src/index.ts");
448 useRightPanelStore.getState().openTerminal(refA, "term-1");
449​
450 useRightPanelStore.getState().closeSurfacesToRight(refA, "browser:tab-a");
451​
452 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
453 isOpen: true,
454 activeSurfaceId: "browser:tab-a",
455 surfaces: [{ id: "browser:tab-a", kind: "preview", resourceId: "tab-a" }],
456 });
457 });
458​
459 it("closing all surfaces closes the panel", () => {
460 useRightPanelStore.getState().openBrowser(refA, "tab-a");
461 useRightPanelStore.getState().openFile(refA, "src/index.ts");
462​
463 useRightPanelStore.getState().closeAllSurfaces(refA);
464​
465 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
466 isOpen: false,
467 activeSurfaceId: null,
468 surfaces: [],
469 });
470 });
471​
472 it("reconciles browser surfaces without deleting other surface kinds", () => {
473 useRightPanelStore.getState().openTerminal(refA, "term-1");
474 useRightPanelStore.getState().openBrowser(refA, "tab-a");
475 useRightPanelStore.getState().openBrowser(refA, "tab-b");
476 useRightPanelStore.getState().reconcileBrowserSurfaces(refA, ["tab-b", "tab-c"]);
477​
478 expect(
479 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces.map(
480 (surface) => surface.id,
481 ),
482 ).toEqual(["terminal:term-1", "browser:tab-b", "browser:tab-c"]);
483 });
484});