1import { expect, it } from "vite-plus/test";
2
3import { buildFileTree, flattenFileTree } from "./fileTree";
4
5it("flattens a wide expanded directory without exceeding argument limits", () => {
6 const children = Array.from({ length: 200_000 }, (_, index) => ({
7 path: `root/file-${index}`,
8 name: `file-${index}`,
9 kind: "file" as const,
10 children: [],
11 }));
12 const nodes = [{ path: "root", name: "root", kind: "directory" as const, children }];
13 const rows = flattenFileTree({ nodes, expanded: new Set(["root"]) });
14 expect(rows).toHaveLength(children.length + 1);
15 expect(rows[0]).toEqual({ node: nodes[0], depth: 0 });
16 const middleChildIndex = Math.floor(children.length / 2);
17 expect(rows[middleChildIndex + 1]).toEqual({
18 node: children[middleChildIndex],
19 depth: 1,
20 });
21 expect(rows.at(-1)).toEqual({ node: children.at(-1), depth: 1 });
22});
23
24it("prunes unmatched subtrees without losing adjacent rows or their depths", () => {
25 const nodes = buildFileTree([
26 { kind: "file", path: "root/a/hidden.txt" },
27 { kind: "file", path: "root/b/match.txt" },
28 { kind: "file", path: "root/b/other.txt" },
29 { kind: "file", path: "root/c/hidden.txt" },
30 { kind: "file", path: "tail.txt" },
31 ]);
32 const expanded = new Set(["root", "root/a", "root/b", "root/c"]);
33 const rows = (searchResultPaths: ReadonlySet<string> | null) =>
34 flattenFileTree({ nodes, expanded, searchResultPaths }).map(
35 ({ node, depth }) => `${depth}:${node.path}`,
36 );
37
38 expect(rows(new Set(["root/b/match.txt", "tail.txt"]))).toEqual([
39 "0:root",
40 "1:root/b",
41 "2:root/b/match.txt",
42 "0:tail.txt",
43 ]);
44 expect(rows(new Set(["root/b"]))).toEqual(["0:root", "1:root/b"]);
45 expect(rows(new Set())).toEqual([]);
46 expect(rows(null)).toEqual([
47 "0:root",
48 "1:root/a",
49 "2:root/a/hidden.txt",
50 "1:root/b",
51 "2:root/b/match.txt",
52 "2:root/b/other.txt",
53 "1:root/c",
54 "2:root/c/hidden.txt",
55 "0:tail.txt",
56 ]);
57 expect(flattenFileTree({ nodes, expanded: new Set() }).map(({ node }) => node.path)).toEqual([
58 "root",
59 "tail.txt",
60 ]);
61});