-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathpanelLayoutStore.test.ts
More file actions
631 lines (525 loc) · 20.3 KB
/
Copy pathpanelLayoutStore.test.ts
File metadata and controls
631 lines (525 loc) · 20.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
import {
assertActiveTab,
assertGroupStructure,
assertPanelLayout,
assertTabCount,
findPanelById,
type GroupNode,
getLayout,
getNestedPanel,
getPanelTree,
openMultipleFiles,
withRootGroup,
} from "@test/panelTestHelpers";
import { beforeEach, describe, expect, it } from "vitest";
import { usePanelLayoutStore } from "./panelLayoutStore";
describe("panelLayoutStore", () => {
beforeEach(() => {
usePanelLayoutStore.getState().clearAllLayouts();
localStorage.clear();
});
describe("initial state", () => {
it("returns null for non-existent task", () => {
const layout = usePanelLayoutStore.getState().getLayout("task-1");
expect(layout).toBeNull();
});
it("creates default layout when task is initialized", () => {
usePanelLayoutStore.getState().initializeTask("task-1");
const layout = usePanelLayoutStore.getState().getLayout("task-1");
expect(layout).not.toBeNull();
expect(layout?.panelTree.type).toBe("group");
});
it("creates default layout with correct structure", () => {
usePanelLayoutStore.getState().initializeTask("task-1");
withRootGroup("task-1", (root: GroupNode) => {
assertGroupStructure(root, {
direction: "vertical",
childCount: 2,
sizes: [70, 30],
});
assertPanelLayout(root, [
{ panelId: "main-panel", expectedTabs: ["logs"], activeTab: "logs" },
{
panelId: "terminal-panel",
expectedTabs: ["shell"],
activeTab: "shell",
},
]);
});
});
});
describe("openFile", () => {
beforeEach(() => {
usePanelLayoutStore.getState().initializeTask("task-1");
});
it("adds file tab to main panel", () => {
usePanelLayoutStore.getState().openFile("task-1", "src/App.tsx");
assertTabCount(getPanelTree("task-1"), "main-panel", 2);
assertPanelLayout(getPanelTree("task-1"), [
{
panelId: "main-panel",
expectedTabs: ["logs", "file-src/App.tsx"],
},
]);
});
it("sets newly opened file as active", () => {
usePanelLayoutStore.getState().openFile("task-1", "src/App.tsx");
assertActiveTab(getPanelTree("task-1"), "main-panel", "file-src/App.tsx");
});
it("does not duplicate file if already open", () => {
usePanelLayoutStore.getState().openFile("task-1", "src/App.tsx");
usePanelLayoutStore.getState().openFile("task-1", "src/App.tsx");
const panel = findPanelById(getPanelTree("task-1"), "main-panel");
const fileTabs = panel?.content.tabs.filter((t: { id: string }) =>
t.id.startsWith("file-"),
);
expect(fileTabs).toHaveLength(1);
});
it("sets existing file as active when opened again", () => {
openMultipleFiles("task-1", ["src/App.tsx", "src/Other.tsx"]);
usePanelLayoutStore.getState().openFile("task-1", "src/App.tsx");
assertActiveTab(getPanelTree("task-1"), "main-panel", "file-src/App.tsx");
});
it("tracks open files in metadata", () => {
openMultipleFiles("task-1", ["src/App.tsx", "src/Other.tsx"]);
const layout = getLayout("task-1");
expect(layout.openFiles).toContain("src/App.tsx");
expect(layout.openFiles).toContain("src/Other.tsx");
expect(layout.openFiles).toHaveLength(2);
});
});
describe("openArtifact", () => {
beforeEach(() => {
usePanelLayoutStore.getState().initializeTask("task-1");
});
it("adds artifact tab to main panel", () => {
usePanelLayoutStore.getState().openArtifact("task-1", "plan.md");
const panel = findPanelById(getPanelTree("task-1"), "main-panel");
const artifactTab = panel?.content.tabs.find((t: { id: string }) =>
t.id.startsWith("artifact-"),
);
expect(artifactTab?.id).toBe("artifact-plan.md");
});
it("tracks open artifacts in metadata", () => {
usePanelLayoutStore.getState().openArtifact("task-1", "plan.md");
usePanelLayoutStore.getState().openArtifact("task-1", "notes.md");
const layout = getLayout("task-1");
expect(layout.openArtifacts).toContain("plan.md");
expect(layout.openArtifacts).toContain("notes.md");
expect(layout.openArtifacts).toHaveLength(2);
});
});
describe("closeTab", () => {
beforeEach(() => {
usePanelLayoutStore.getState().initializeTask("task-1");
openMultipleFiles("task-1", ["src/App.tsx", "src/Other.tsx"]);
});
it("removes tab from panel", () => {
usePanelLayoutStore
.getState()
.closeTab("task-1", "main-panel", "file-src/App.tsx");
const panel = findPanelById(getPanelTree("task-1"), "main-panel");
const fileTab = panel?.content.tabs.find(
(t: { id: string }) => t.id === "file-src/App.tsx",
);
expect(fileTab).toBeUndefined();
});
it("removes file from metadata", () => {
usePanelLayoutStore
.getState()
.closeTab("task-1", "main-panel", "file-src/App.tsx");
const layout = getLayout("task-1");
expect(layout.openFiles).not.toContain("src/App.tsx");
expect(layout.openFiles).toContain("src/Other.tsx");
});
it("auto-selects next tab when closing active tab", () => {
usePanelLayoutStore
.getState()
.closeTab("task-1", "main-panel", "file-src/Other.tsx");
assertActiveTab(getPanelTree("task-1"), "main-panel", "file-src/App.tsx");
});
it("falls back to logs when last file tab closed", () => {
usePanelLayoutStore
.getState()
.closeTab("task-1", "main-panel", "file-src/App.tsx");
usePanelLayoutStore
.getState()
.closeTab("task-1", "main-panel", "file-src/Other.tsx");
assertActiveTab(getPanelTree("task-1"), "main-panel", "logs");
});
});
describe("setActiveTab", () => {
beforeEach(() => {
usePanelLayoutStore.getState().initializeTask("task-1");
openMultipleFiles("task-1", ["src/App.tsx", "src/Other.tsx"]);
});
it("changes active tab in panel", () => {
usePanelLayoutStore
.getState()
.setActiveTab("task-1", "main-panel", "file-src/App.tsx");
assertActiveTab(getPanelTree("task-1"), "main-panel", "file-src/App.tsx");
});
});
describe("task isolation", () => {
it("keeps tasks isolated from each other", () => {
usePanelLayoutStore.getState().initializeTask("task-1");
usePanelLayoutStore.getState().initializeTask("task-2");
openMultipleFiles("task-1", ["src/App.tsx"]);
openMultipleFiles("task-2", ["src/Other.tsx"]);
const layout1 = getLayout("task-1");
const layout2 = getLayout("task-2");
expect(layout1.openFiles).toContain("src/App.tsx");
expect(layout1.openFiles).not.toContain("src/Other.tsx");
expect(layout2.openFiles).toContain("src/Other.tsx");
expect(layout2.openFiles).not.toContain("src/App.tsx");
});
});
describe("panel size persistence", () => {
beforeEach(() => {
usePanelLayoutStore.getState().initializeTask("task-1");
});
it("preserves custom panel sizes when opening a file", () => {
usePanelLayoutStore
.getState()
.updateSizes("task-1", "left-group", [60, 40]);
openMultipleFiles("task-1", ["src/App.tsx"]);
withRootGroup("task-1", (root) => {
expect(root.sizes).toEqual([60, 40]);
});
});
it("preserves custom panel sizes when switching tabs", () => {
usePanelLayoutStore
.getState()
.updateSizes("task-1", "left-group", [55, 45]);
openMultipleFiles("task-1", ["src/App.tsx", "src/Other.tsx"]);
usePanelLayoutStore
.getState()
.setActiveTab("task-1", "main-panel", "file-src/App.tsx");
withRootGroup("task-1", (root) => {
expect(root.sizes).toEqual([55, 45]);
});
});
it("preserves custom panel sizes when closing tabs", () => {
usePanelLayoutStore
.getState()
.updateSizes("task-1", "left-group", [80, 20]);
openMultipleFiles("task-1", ["src/App.tsx", "src/Other.tsx"]);
usePanelLayoutStore
.getState()
.closeTab("task-1", "main-panel", "file-src/Other.tsx");
withRootGroup("task-1", (root) => {
expect(root.sizes).toEqual([80, 20]);
});
});
});
describe("persistence", () => {
it("persists state to localStorage", () => {
usePanelLayoutStore.getState().initializeTask("task-1");
usePanelLayoutStore.getState().openFile("task-1", "src/App.tsx");
const storedData = localStorage.getItem("panel-layout-store");
expect(storedData).not.toBeNull();
const parsed = JSON.parse(storedData ?? "");
expect(parsed.state.taskLayouts["task-1"]).toBeDefined();
expect(parsed.state.taskLayouts["task-1"].openFiles).toContain(
"src/App.tsx",
);
});
it("restores state from localStorage", () => {
usePanelLayoutStore.getState().initializeTask("task-1");
usePanelLayoutStore.getState().openFile("task-1", "src/App.tsx");
const storedData = localStorage.getItem("panel-layout-store");
usePanelLayoutStore.getState().clearAllLayouts();
expect(usePanelLayoutStore.getState().getLayout("task-1")).toBeNull();
if (storedData) {
localStorage.setItem("panel-layout-store", storedData);
usePanelLayoutStore.persist.rehydrate();
}
const restoredLayout = getLayout("task-1");
expect(restoredLayout.openFiles).toContain("src/App.tsx");
});
});
describe("drag state", () => {
beforeEach(() => {
usePanelLayoutStore.getState().initializeTask("task-1");
usePanelLayoutStore.getState().openFile("task-1", "src/App.tsx");
});
it("tracks dragging tab state", () => {
usePanelLayoutStore
.getState()
.setDraggingTab("task-1", "file-src/App.tsx", "main-panel");
const layout = getLayout("task-1");
expect(layout.draggingTabId).toBe("file-src/App.tsx");
expect(layout.draggingTabPanelId).toBe("main-panel");
});
it("clears dragging tab state", () => {
usePanelLayoutStore
.getState()
.setDraggingTab("task-1", "file-src/App.tsx", "main-panel");
usePanelLayoutStore.getState().clearDraggingTab("task-1");
const layout = getLayout("task-1");
expect(layout.draggingTabId).toBeNull();
expect(layout.draggingTabPanelId).toBeNull();
});
it("isolates drag state between tasks", () => {
usePanelLayoutStore.getState().initializeTask("task-2");
usePanelLayoutStore
.getState()
.setDraggingTab("task-1", "file-src/App.tsx", "main-panel");
const layout1 = getLayout("task-1");
const layout2 = getLayout("task-2");
expect(layout1.draggingTabId).toBe("file-src/App.tsx");
expect(layout2.draggingTabId).toBeNull();
});
});
describe("reorderTabs", () => {
beforeEach(() => {
usePanelLayoutStore.getState().initializeTask("task-1");
openMultipleFiles("task-1", [
"src/App.tsx",
"src/Other.tsx",
"src/Third.tsx",
]);
});
it("reorders tabs within a panel", () => {
// tabs: [logs, file-src/App.tsx, file-src/Other.tsx, file-src/Third.tsx]
// move index 1 to index 3
usePanelLayoutStore.getState().reorderTabs("task-1", "main-panel", 1, 3);
const panel = findPanelById(getPanelTree("task-1"), "main-panel");
const tabIds = panel?.content.tabs.map((t: { id: string }) => t.id);
expect(tabIds?.[1]).toBe("file-src/Other.tsx");
expect(tabIds?.[3]).toBe("file-src/App.tsx");
});
it("preserves active tab after reorder", () => {
usePanelLayoutStore
.getState()
.setActiveTab("task-1", "main-panel", "file-src/App.tsx");
usePanelLayoutStore.getState().reorderTabs("task-1", "main-panel", 1, 3);
assertActiveTab(getPanelTree("task-1"), "main-panel", "file-src/App.tsx");
});
});
describe("moveTab", () => {
beforeEach(() => {
usePanelLayoutStore.getState().initializeTask("task-1");
usePanelLayoutStore.getState().openFile("task-1", "src/App.tsx");
});
it("moves tab between panels", () => {
usePanelLayoutStore
.getState()
.moveTab("task-1", "file-src/App.tsx", "main-panel", "terminal-panel");
const mainPanel = findPanelById(getPanelTree("task-1"), "main-panel");
const terminalPanel = findPanelById(
getPanelTree("task-1"),
"terminal-panel",
);
expect(
mainPanel?.content.tabs.find((t) => t.id === "file-src/App.tsx"),
).toBeUndefined();
expect(
terminalPanel?.content.tabs.find((t) => t.id === "file-src/App.tsx"),
).toBeDefined();
});
it("sets moved tab as active in target panel", () => {
usePanelLayoutStore
.getState()
.moveTab("task-1", "file-src/App.tsx", "main-panel", "terminal-panel");
assertActiveTab(
getPanelTree("task-1"),
"terminal-panel",
"file-src/App.tsx",
);
});
});
describe("splitPanel", () => {
beforeEach(() => {
usePanelLayoutStore.getState().initializeTask("task-1");
openMultipleFiles("task-1", ["src/App.tsx", "src/Other.tsx"]);
});
it.each([
["right", "horizontal"],
["left", "horizontal"],
["top", "vertical"],
["bottom", "vertical"],
] as const)(
"splits panel %s creates %s layout",
(direction, expectedDirection) => {
usePanelLayoutStore
.getState()
.splitPanel(
"task-1",
"file-src/App.tsx",
"main-panel",
"main-panel",
direction,
);
// After split, main-panel becomes a group
const mainPanelNode = getNestedPanel("task-1", 0);
expect(mainPanelNode.type).toBe("group");
if (mainPanelNode.type === "group") {
expect(mainPanelNode.direction).toBe(expectedDirection);
expect(mainPanelNode.children).toHaveLength(2);
}
},
);
it("moves tab to new split panel", () => {
usePanelLayoutStore
.getState()
.splitPanel(
"task-1",
"file-src/App.tsx",
"main-panel",
"main-panel",
"right",
);
// After right split: main-panel becomes a group with [original, new]
const mainPanelNode = getNestedPanel("task-1", 0);
expect(mainPanelNode.type).toBe("group");
if (mainPanelNode.type === "group") {
const newPanel = mainPanelNode.children[1];
expect(newPanel.type).toBe("leaf");
if (newPanel.type === "leaf") {
expect(
newPanel.content.tabs.some((t) => t.id === "file-src/App.tsx"),
).toBe(true);
expect(newPanel.content.activeTabId).toBe("file-src/App.tsx");
}
}
});
});
describe("updateSizes", () => {
beforeEach(() => {
usePanelLayoutStore.getState().initializeTask("task-1");
});
it("updates panel group sizes", () => {
usePanelLayoutStore
.getState()
.updateSizes("task-1", "left-group", [60, 40]);
withRootGroup("task-1", (root: GroupNode) => {
expect(root.sizes).toEqual([60, 40]);
});
});
});
describe("tree cleanup", () => {
beforeEach(() => {
usePanelLayoutStore.getState().initializeTask("task-1");
openMultipleFiles("task-1", ["src/App.tsx", "src/Other.tsx"]);
});
it("removes empty panels after closing all tabs", () => {
usePanelLayoutStore
.getState()
.splitPanel(
"task-1",
"file-src/App.tsx",
"main-panel",
"main-panel",
"right",
);
// Find the new panel and close its tab
const mainPanelNode = getNestedPanel("task-1", 0);
if (mainPanelNode.type === "group") {
const newPanel = mainPanelNode.children[1];
usePanelLayoutStore
.getState()
.closeTab("task-1", newPanel.id, "file-src/App.tsx");
}
// After closing, the group should simplify back to a leaf
const updatedMainPanel = getNestedPanel("task-1", 0);
expect(updatedMainPanel.type).toBe("leaf");
});
});
describe("preview tabs", () => {
beforeEach(() => {
usePanelLayoutStore.getState().initializeTask("task-1");
});
it("creates preview tab by default when opening a file", () => {
usePanelLayoutStore.getState().openFile("task-1", "src/App.tsx");
const panel = findPanelById(getPanelTree("task-1"), "main-panel");
const fileTab = panel?.content.tabs.find(
(t: { id: string }) => t.id === "file-src/App.tsx",
);
expect(fileTab?.isPreview).toBe(true);
});
it("replaces existing preview tab when opening another file", () => {
usePanelLayoutStore.getState().openFile("task-1", "src/App.tsx");
usePanelLayoutStore.getState().openFile("task-1", "src/Other.tsx");
const panel = findPanelById(getPanelTree("task-1"), "main-panel");
const fileTabs = panel?.content.tabs.filter((t: { id: string }) =>
t.id.startsWith("file-"),
);
expect(fileTabs).toHaveLength(1);
expect(fileTabs?.[0].id).toBe("file-src/Other.tsx");
expect(fileTabs?.[0].isPreview).toBe(true);
});
it("creates permanent tab when asPreview is false", () => {
usePanelLayoutStore.getState().openFile("task-1", "src/App.tsx", false);
const panel = findPanelById(getPanelTree("task-1"), "main-panel");
const fileTab = panel?.content.tabs.find(
(t: { id: string }) => t.id === "file-src/App.tsx",
);
expect(fileTab?.isPreview).toBe(false);
});
it("keeps preview tab as preview when re-clicking same file", () => {
usePanelLayoutStore.getState().openFile("task-1", "src/App.tsx");
usePanelLayoutStore.getState().openFile("task-1", "src/App.tsx");
const panel = findPanelById(getPanelTree("task-1"), "main-panel");
const fileTab = panel?.content.tabs.find(
(t: { id: string }) => t.id === "file-src/App.tsx",
);
expect(fileTab?.isPreview).toBe(true);
});
it("pins preview tab when double-clicking (asPreview=false)", () => {
usePanelLayoutStore.getState().openFile("task-1", "src/App.tsx");
usePanelLayoutStore.getState().openFile("task-1", "src/App.tsx", false);
const panel = findPanelById(getPanelTree("task-1"), "main-panel");
const fileTab = panel?.content.tabs.find(
(t: { id: string }) => t.id === "file-src/App.tsx",
);
expect(fileTab?.isPreview).toBe(false);
});
it("keepTab sets isPreview to false", () => {
usePanelLayoutStore.getState().openFile("task-1", "src/App.tsx");
usePanelLayoutStore
.getState()
.keepTab("task-1", "main-panel", "file-src/App.tsx");
const panel = findPanelById(getPanelTree("task-1"), "main-panel");
const fileTab = panel?.content.tabs.find(
(t: { id: string }) => t.id === "file-src/App.tsx",
);
expect(fileTab?.isPreview).toBe(false);
});
it("does not replace non-preview tabs when opening preview", () => {
usePanelLayoutStore.getState().openFile("task-1", "src/App.tsx", false);
usePanelLayoutStore.getState().openFile("task-1", "src/Other.tsx");
const panel = findPanelById(getPanelTree("task-1"), "main-panel");
const fileTabs = panel?.content.tabs.filter((t: { id: string }) =>
t.id.startsWith("file-"),
);
expect(fileTabs).toHaveLength(2);
expect(
fileTabs?.find((t) => t.id === "file-src/App.tsx")?.isPreview,
).toBe(false);
expect(
fileTabs?.find((t) => t.id === "file-src/Other.tsx")?.isPreview,
).toBe(true);
});
it("openDiff creates preview tab by default", () => {
usePanelLayoutStore
.getState()
.openDiff("task-1", "src/App.tsx", "modified");
const panel = findPanelById(getPanelTree("task-1"), "main-panel");
const diffTab = panel?.content.tabs.find((t: { id: string }) =>
t.id.startsWith("diff-"),
);
expect(diffTab?.isPreview).toBe(true);
});
it("openDiff creates permanent tab when asPreview is false", () => {
usePanelLayoutStore
.getState()
.openDiff("task-1", "src/App.tsx", "modified", false);
const panel = findPanelById(getPanelTree("task-1"), "main-panel");
const diffTab = panel?.content.tabs.find((t: { id: string }) =>
t.id.startsWith("diff-"),
);
expect(diffTab?.isPreview).toBe(false);
});
});
});