1import { describe, expect, it, vi } from "@effect/vitest";
2
3import { areAllDirectoriesExpanded, setAllDirectoriesExpanded } from "./fileTreeExpansion";
4
5type FakeDirectoryItem = {
6 isDirectory: () => true;
7 isExpanded: () => boolean;
8 expand: () => void;
9 collapse: () => void;
10};
11
12function makeModel(expanded: Record<string, boolean>) {
13 const items = new Map<string, FakeDirectoryItem>();
14 return {
15 getItem: (path: string) => {
16 const existing = items.get(path);
17 if (existing !== undefined) return existing;
18 const item: FakeDirectoryItem = {
19 isDirectory: () => true,
20 isExpanded: () => expanded[path] ?? false,
21 expand: () => {
22 expanded[path] = true;
23 },
24 collapse: () => {
25 expanded[path] = false;
26 },
27 };
28 items.set(path, item);
29 return item;
30 },
31 };
32}
33
34describe("file tree expansion", () => {
35 it("requires at least one directory and detects whether all are expanded", () => {
36 const model = makeModel({ "src/": true, "test/": true });
37 expect(areAllDirectoriesExpanded(model, [])).toBe(false);
38 expect(areAllDirectoriesExpanded(model, ["src/", "test/"])).toBe(true);
39 expect(
40 areAllDirectoriesExpanded(makeModel({ "src/": true, "test/": false }), ["src/", "test/"]),
41 ).toBe(false);
42 });
43
44 it("expands and collapses every directory", () => {
45 const expanded = { "src/": true, "test/": false };
46 const model = makeModel(expanded);
47 setAllDirectoriesExpanded(model, ["src/", "test/"], true);
48 expect(expanded).toEqual({ "src/": true, "test/": true });
49 setAllDirectoriesExpanded(model, ["src/", "test/"], false);
50 expect(expanded).toEqual({ "src/": false, "test/": false });
51 });
52
53 it("skips directories already at the requested state", () => {
54 const model = makeModel({ "src/": true });
55 const item = model.getItem("src/");
56 const collapse = vi.spyOn(item, "collapse");
57 setAllDirectoriesExpanded(model, ["src/"], true);
58 expect(collapse).not.toHaveBeenCalled();
59 });
60});