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 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("open sets the active panel for a thread", () => {
106 useRightPanelStore.getState().open(refA, "preview");
107 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("preview");
108 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refB)).toBeNull();
109 });
110
111 it("opening a different kind keeps both surfaces and activates the new one", () => {
112 useRightPanelStore.getState().open(refA, "plan");
113 useRightPanelStore.getState().open(refA, "preview");
114 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("preview");
115 expect(
116 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces,
117 ).toHaveLength(2);
118 });
119
120 it("reopening an inactive singleton activates its existing surface", () => {
121 useRightPanelStore.getState().open(refA, "diff");
122 useRightPanelStore.getState().open(refA, "plan");
123 useRightPanelStore.getState().open(refA, "diff");
124
125 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
126 isOpen: true,
127 activeSurfaceId: "diff",
128 surfaces: [
129 { id: "diff", kind: "diff" },
130 { id: "plan", kind: "plan" },
131 ],
132 });
133 });
134
135 it("keeps files as a singleton surface", () => {
136 useRightPanelStore.getState().open(refA, "files");
137 useRightPanelStore.getState().open(refA, "files");
138 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
139 isOpen: true,
140 activeSurfaceId: "files",
141 surfaces: [{ id: "files", kind: "files" }],
142 });
143 });
144
145 it("replaces the standalone explorer with peer file surfaces", () => {
146 useRightPanelStore.getState().open(refA, "files");
147 useRightPanelStore.getState().openFile(refA, "src/index.ts");
148 useRightPanelStore.getState().openFile(refA, "src/index.ts");
149 useRightPanelStore.getState().openFile(refA, "README.md");
150
151 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
152 isOpen: true,
153 activeSurfaceId: "file:README.md",
154 surfaces: [
155 {
156 id: "file:src/index.ts",
157 kind: "file",
158 relativePath: "src/index.ts",
159 revealLine: null,
160 revealRequestId: 2,
161 },
162 {
163 id: "file:README.md",
164 kind: "file",
165 relativePath: "README.md",
166 revealLine: null,
167 revealRequestId: 1,
168 },
169 ],
170 });
171 });
172
173 it("updates line reveal requests when reopening a file surface", () => {
174 useRightPanelStore.getState().openFile(refA, "src/index.ts", 42);
175 useRightPanelStore.getState().openFile(refA, "src/index.ts", 87);
176
177 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
178 isOpen: true,
179 activeSurfaceId: "file:src/index.ts",
180 surfaces: [
181 {
182 id: "file:src/index.ts",
183 kind: "file",
184 relativePath: "src/index.ts",
185 revealLine: 87,
186 revealRequestId: 2,
187 },
188 ],
189 });
190
191 useRightPanelStore.getState().openFile(refA, "src/index.ts");
192
193 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
194 isOpen: true,
195 activeSurfaceId: "file:src/index.ts",
196 surfaces: [
197 {
198 id: "file:src/index.ts",
199 kind: "file",
200 relativePath: "src/index.ts",
201 revealLine: null,
202 revealRequestId: 3,
203 },
204 ],
205 });
206 });
207
208 it("removes persisted file surfaces when their workspace no longer exists", () => {
209 useRightPanelStore.getState().openFile(refA, "src/index.ts");
210 useRightPanelStore.getState().open(refA, "plan");
211 useRightPanelStore.getState().openFile(refA, "README.md");
212
213 useRightPanelStore.getState().reconcileFileSurfaces(refA, false);
214
215 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
216 isOpen: true,
217 activeSurfaceId: "plan",
218 surfaces: [{ id: "plan", kind: "plan" }],
219 });
220
221 useRightPanelStore.getState().openFile(refB, "conductor.json");
222 useRightPanelStore.getState().reconcileFileSurfaces(refB, false);
223 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refB)).toEqual({
224 isOpen: false,
225 activeSurfaceId: null,
226 surfaces: [],
227 });
228 });
229
230 it("close hides the panel without clearing its selected surface", () => {
231 useRightPanelStore.getState().open(refA, "plan");
232 useRightPanelStore.getState().close(refA);
233 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
234 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
235 isOpen: false,
236 activeSurfaceId: "plan",
237 surfaces: [{ id: "plan", kind: "plan" }],
238 });
239 });
240
241 it("toggles empty panel visibility without creating a surface", () => {
242 useRightPanelStore.getState().toggleVisibility(refA);
243 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
244 isOpen: true,
245 activeSurfaceId: null,
246 surfaces: [],
247 });
248
249 useRightPanelStore.getState().toggleVisibility(refA);
250 expect(useRightPanelStore.getState().byThreadKey).toEqual({});
251 });
252
253 it("toggle hides the panel without discarding the active surface", () => {
254 useRightPanelStore.getState().toggle(refA, "diff");
255 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("diff");
256 useRightPanelStore.getState().toggle(refA, "diff");
257 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
258 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
259 isOpen: false,
260 activeSurfaceId: "diff",
261 surfaces: [{ id: "diff", kind: "diff" }],
262 });
263 });
264
265 it("toggle to a different kind switches active", () => {
266 useRightPanelStore.getState().toggle(refA, "preview");
267 useRightPanelStore.getState().toggle(refA, "plan");
268 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("plan");
269 });
270
271 it("removeThread clears persisted state", () => {
272 useRightPanelStore.getState().open(refA, "plan");
273 useRightPanelStore.getState().removeThread(refA);
274 expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
275 });
276
277 it("close on never-opened thread is a no-op", () => {
278 useRightPanelStore.getState().close(refA);
279 expect(useRightPanelStore.getState().byThreadKey).toEqual({});
280 });
281
282 it("tracks one surface per browser session", () => {
283 useRightPanelStore.getState().openBrowser(refA, "tab-a");
284 useRightPanelStore.getState().openBrowser(refA, "tab-b");
285
286 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
287 expect(state.surfaces.map((surface) => surface.id)).toEqual(["browser:tab-a", "browser:tab-b"]);
288 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
289 id: "browser:tab-b",
290 kind: "preview",
291 resourceId: "tab-b",
292 });
293 });
294
295 it("tracks one surface per terminal session", () => {
296 useRightPanelStore.getState().openTerminal(refA, "term-1");
297 useRightPanelStore.getState().openTerminal(refA, "term-2");
298
299 const state = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
300 expect(state.surfaces).toEqual([
301 {
302 id: "terminal:term-1",
303 kind: "terminal",
304 resourceId: "term-1",
305 terminalIds: ["term-1"],
306 activeTerminalId: "term-1",
307 },
308 {
309 id: "terminal:term-2",
310 kind: "terminal",
311 resourceId: "term-2",
312 terminalIds: ["term-2"],
313 activeTerminalId: "term-2",
314 },
315 ]);
316 expect(state.activeSurfaceId).toBe("terminal:term-2");
317 });
318
319 it("tracks split panes and the active pane within a terminal surface", () => {
320 useRightPanelStore.getState().openTerminal(refA, "term-1");
321 useRightPanelStore.getState().splitTerminal(refA, "terminal:term-1", "term-2");
322
323 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
324 id: "terminal:term-1",
325 kind: "terminal",
326 resourceId: "term-1",
327 terminalIds: ["term-1", "term-2"],
328 activeTerminalId: "term-2",
329 });
330
331 useRightPanelStore.getState().activateTerminal(refA, "terminal:term-1", "term-1");
332 useRightPanelStore.getState().closeTerminal(refA, "terminal:term-1", "term-1");
333 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
334 id: "terminal:term-1",
335 kind: "terminal",
336 resourceId: "term-1",
337 terminalIds: ["term-2"],
338 activeTerminalId: "term-2",
339 });
340 });
341
342 it("tracks vertical layout for a terminal surface", () => {
343 useRightPanelStore.getState().openTerminal(refA, "term-1");
344 useRightPanelStore.getState().splitTerminal(refA, "terminal:term-1", "term-2", "vertical");
345
346 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
347 id: "terminal:term-1",
348 kind: "terminal",
349 resourceId: "term-1",
350 terminalIds: ["term-1", "term-2"],
351 activeTerminalId: "term-2",
352 splitDirection: "vertical",
353 });
354 });
355
356 it("closing the final terminal pane removes its surface and closes the panel", () => {
357 useRightPanelStore.getState().openTerminal(refA, "term-1");
358 useRightPanelStore.getState().closeTerminal(refA, "terminal:term-1", "term-1");
359
360 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
361 isOpen: false,
362 activeSurfaceId: null,
363 surfaces: [],
364 });
365 });
366
367 it("closing the active surface activates a neighboring surface", () => {
368 useRightPanelStore.getState().openBrowser(refA, "tab-a");
369 useRightPanelStore.getState().openTerminal(refA, "term-1");
370 useRightPanelStore.getState().closeSurface(refA, "terminal:term-1");
371
372 expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)?.id).toBe(
373 "browser:tab-a",
374 );
375 });
376
377 it("closing the final surface closes the panel", () => {
378 useRightPanelStore.getState().openTerminal(refA, "term-1");
379 useRightPanelStore.getState().closeSurface(refA, "terminal:term-1");
380
381 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
382 isOpen: false,
383 activeSurfaceId: null,
384 surfaces: [],
385 });
386 });
387
388 it("closing other surfaces keeps the selected surface active", () => {
389 useRightPanelStore.getState().openBrowser(refA, "tab-a");
390 useRightPanelStore.getState().openFile(refA, "src/index.ts");
391 useRightPanelStore.getState().openTerminal(refA, "term-1");
392
393 useRightPanelStore.getState().closeOtherSurfaces(refA, "file:src/index.ts");
394
395 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
396 isOpen: true,
397 activeSurfaceId: "file:src/index.ts",
398 surfaces: [
399 {
400 id: "file:src/index.ts",
401 kind: "file",
402 relativePath: "src/index.ts",
403 revealLine: null,
404 revealRequestId: 1,
405 },
406 ],
407 });
408 });
409
410 it("closing surfaces to the right activates the selected surface when active was removed", () => {
411 useRightPanelStore.getState().openBrowser(refA, "tab-a");
412 useRightPanelStore.getState().openFile(refA, "src/index.ts");
413 useRightPanelStore.getState().openTerminal(refA, "term-1");
414
415 useRightPanelStore.getState().closeSurfacesToRight(refA, "browser:tab-a");
416
417 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
418 isOpen: true,
419 activeSurfaceId: "browser:tab-a",
420 surfaces: [{ id: "browser:tab-a", kind: "preview", resourceId: "tab-a" }],
421 });
422 });
423
424 it("closing all surfaces closes the panel", () => {
425 useRightPanelStore.getState().openBrowser(refA, "tab-a");
426 useRightPanelStore.getState().openFile(refA, "src/index.ts");
427
428 useRightPanelStore.getState().closeAllSurfaces(refA);
429
430 expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
431 isOpen: false,
432 activeSurfaceId: null,
433 surfaces: [],
434 });
435 });
436
437 it("reconciles browser surfaces without deleting other surface kinds", () => {
438 useRightPanelStore.getState().openTerminal(refA, "term-1");
439 useRightPanelStore.getState().openBrowser(refA, "tab-a");
440 useRightPanelStore.getState().openBrowser(refA, "tab-b");
441 useRightPanelStore.getState().reconcileBrowserSurfaces(refA, ["tab-b", "tab-c"]);
442
443 expect(
444 selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA).surfaces.map(
445 (surface) => surface.id,
446 ),
447 ).toEqual(["terminal:term-1", "browser:tab-b", "browser:tab-c"]);
448 });
449});