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

fileTree.test.ts

86 lines2.8 KB

1import { describe, expect, it } from "vite-plus/test";
2import type { ProjectEntry } from "@t3tools/contracts";
3​
4import { buildFileTree, defaultExpandedTreePaths, flattenFileTree } from "./fileTree";
5​
6const entries = [
7 { kind: "file", path: "README.md" },
8 { kind: "directory", path: "src" },
9 { kind: "file", path: "src/index.ts" },
10 { kind: "file", path: "src/components/App.tsx" },
11 { kind: "file", path: "package.json" },
12] satisfies ReadonlyArray<ProjectEntry>;
13​
14describe("mobile file tree helpers", () => {
15 it("builds a deterministic hierarchy with directories before files", () => {
16 const tree = buildFileTree(entries);
17​
18 expect(tree.map((node) => `${node.kind}:${node.path}`)).toEqual([
19 "directory:src",
20 "file:package.json",
21 "file:README.md",
22 ]);
23 expect(tree[0]?.children.map((node) => `${node.kind}:${node.path}`)).toEqual([
24 "directory:src/components",
25 "file:src/index.ts",
26 ]);
27 });
28​
29 it("flattens expanded directories and hides collapsed descendants", () => {
30 const tree = buildFileTree(entries);
31​
32 expect(
33 flattenFileTree({
34 nodes: tree,
35 expanded: new Set(["src"]),
36 }).map((item) => `${item.depth}:${item.node.path}`),
37 ).toEqual(["0:src", "1:src/components", "1:src/index.ts", "0:package.json", "0:README.md"]);
38​
39 expect(
40 flattenFileTree({
41 nodes: tree,
42 expanded: new Set(),
43 }).map((item) => item.node.path),
44 ).toEqual(["src", "package.json", "README.md"]);
45 });
46​
47 it("includes matching descendants and their ancestors during search", () => {
48 const tree = buildFileTree(entries);
49​
50 expect(
51 flattenFileTree({
52 nodes: tree,
53 expanded: new Set(),
54 searchResultPaths: new Set(["src/components/App.tsx"]),
55 }).map((item) => item.node.path),
56 ).toEqual(["src", "src/components", "src/components/App.tsx"]);
57 });
58​
59 it("excludes cached children of matched directories", () => {
60 expect(
61 flattenFileTree({
62 nodes: buildFileTree(entries),
63 expanded: new Set(["src", "src/components"]),
64 searchResultPaths: new Set(["src/components"]),
65 }).map((item) => item.node.path),
66 ).toEqual(["src", "src/components"]);
67 });
68​
69 it("keeps an empty search empty and restores expansion when search clears", () => {
70 const tree = buildFileTree(entries);
71 expect(
72 flattenFileTree({ nodes: tree, expanded: new Set(["src"]), searchResultPaths: new Set() }),
73 ).toEqual([]);
74 expect(
75 flattenFileTree({ nodes: tree, expanded: new Set(), searchResultPaths: null }).map(
76 (item) => item.node.path,
77 ),
78 ).toEqual(["src", "package.json", "README.md"]);
79 });
80​
81 it("expands top-level directories by default", () => {
82 const tree = buildFileTree(entries);
83​
84 expect([...defaultExpandedTreePaths(tree)]).toEqual(["src"]);
85 });
86});