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

fileTree.test.ts

109 lines3.3 KB

1import { describe, expect, it } from "vite-plus/test";
2import type { ProjectEntry } from "@t3tools/contracts";
3​
4import {
5 buildFileTree,
6 countFileNodes,
7 defaultExpandedTreePaths,
8 firstFilePath,
9 flattenFileTree,
10} from "./fileTree";
11​
12const entries = [
13 { kind: "file", path: "README.md" },
14 { kind: "directory", path: "src" },
15 { kind: "file", path: "src/index.ts" },
16 { kind: "file", path: "src/components/App.tsx" },
17 { kind: "file", path: "package.json" },
18] satisfies ReadonlyArray<ProjectEntry>;
19​
20describe("mobile file tree helpers", () => {
21 it("builds a deterministic hierarchy with directories before files", () => {
22 const tree = buildFileTree(entries);
23​
24 expect(tree.map((node) => `${node.kind}:${node.path}`)).toEqual([
25 "directory:src",
26 "file:package.json",
27 "file:README.md",
28 ]);
29 expect(tree[0]?.children.map((node) => `${node.kind}:${node.path}`)).toEqual([
30 "directory:src/components",
31 "file:src/index.ts",
32 ]);
33 expect(countFileNodes(tree)).toBe(4);
34 expect(firstFilePath(tree)).toBe("src/components/App.tsx");
35 });
36​
37 it("flattens expanded directories and hides collapsed descendants", () => {
38 const tree = buildFileTree(entries);
39​
40 expect(
41 flattenFileTree({
42 nodes: tree,
43 expanded: new Set(["src"]),
44 }).map((item) => `${item.depth}:${item.node.path}`),
45 ).toEqual(["0:src", "1:src/components", "1:src/index.ts", "0:package.json", "0:README.md"]);
46​
47 expect(
48 flattenFileTree({
49 nodes: tree,
50 expanded: new Set(),
51 }).map((item) => item.node.path),
52 ).toEqual(["src", "package.json", "README.md"]);
53 });
54​
55 it("includes matching descendants and their ancestors during search", () => {
56 const tree = buildFileTree(entries);
57​
58 expect(
59 flattenFileTree({
60 nodes: tree,
61 expanded: new Set(),
62 searchQuery: "app",
63 }).map((item) => item.node.path),
64 ).toEqual(["src", "src/components", "src/components/App.tsx"]);
65 });
66​
67 it("supports fuzzy, whitespace-separated path queries", () => {
68 const tree = buildFileTree([
69 {
70 kind: "file",
71 path: "docs/internals/workspace-layout.md",
72 },
73 {
74 kind: "file",
75 path: ".repos/alchemy-effect/examples/aws-lambda/src/JobNotifications.ts",
76 },
77 { kind: "directory", path: "apps/web/src/components/chat" },
78 { kind: "file", path: "apps/web/src/components/chat/ChatHeader.test.ts" },
79 { kind: "file", path: "apps/web/src/components/chat/ChatHeader.tsx" },
80 { kind: "file", path: "apps/web/src/components/chat/Composer.tsx" },
81 ]);
82​
83 const expectedPaths = [
84 "apps",
85 "apps/web",
86 "apps/web/src",
87 "apps/web/src/components",
88 "apps/web/src/components/chat",
89 "apps/web/src/components/chat/ChatHeader.test.ts",
90 "apps/web/src/components/chat/ChatHeader.tsx",
91 ];
92​
93 for (const searchQuery of ["chat hea", "cht hdr"]) {
94 expect(
95 flattenFileTree({
96 nodes: tree,
97 expanded: new Set(),
98 searchQuery,
99 }).map((item) => item.node.path),
100 ).toEqual(expectedPaths);
101 }
102 });
103​
104 it("expands top-level directories by default", () => {
105 const tree = buildFileTree(entries);
106​
107 expect([...defaultExpandedTreePaths(tree)]).toEqual(["src"]);
108 });
109});