Open History to see the last commit
1import { describe, expect, it } from "vitest";
2
3import { buildWorkspaceFileTree } from "./workspaceFileTree";
4
5describe("buildWorkspaceFileTree", () => {
6 it("builds nested directories and sorts folders before files", () => {
7 const tree = buildWorkspaceFileTree({
8 entries: [
9 { path: "notes/today.md", kind: "file", parentPath: "notes" },
10 { path: "README.md", kind: "file" },
11 { path: "notes", kind: "directory" },
12 { path: "notes/archive", kind: "directory", parentPath: "notes" },
13 { path: "notes/archive/old.md", kind: "file", parentPath: "notes/archive" },
14 ],
15 });
16
17 expect(tree.map((node) => `${node.kind}:${node.name}`)).toEqual([
18 "directory:notes",
19 "file:README.md",
20 ]);
21 expect(tree[0]).toMatchObject({
22 kind: "directory",
23 name: "notes",
24 children: [
25 { kind: "directory", name: "archive" },
26 { kind: "file", name: "today.md" },
27 ],
28 });
29 });
30
31 it("renders trailing-slash entries (submodules / nested git repos) as directories", () => {
32 const tree = buildWorkspaceFileTree({
33 entries: [
34 { path: "README.md", kind: "file" },
35 { path: "workbench/", kind: "file" },
36 { path: "workbench-pi-fix/", kind: "file" },
37 ],
38 });
39
40 expect(tree.map((node) => `${node.kind}:${node.name}`)).toEqual([
41 "directory:workbench",
42 "directory:workbench-pi-fix",
43 "file:README.md",
44 ]);
45 });
46
47 it("marks changed files and bubbles that state up to parent directories", () => {
48 const tree = buildWorkspaceFileTree({
49 entries: [
50 { path: "docs", kind: "directory" },
51 { path: "docs/brief.md", kind: "file", parentPath: "docs" },
52 { path: "docs/archive", kind: "directory", parentPath: "docs" },
53 { path: "docs/archive/old.md", kind: "file", parentPath: "docs/archive" },
54 ],
55 changedPaths: new Set(["docs/archive/old.md"]),
56 });
57
58 expect(tree[0]).toMatchObject({
59 kind: "directory",
60 name: "docs",
61 changed: true,
62 children: [
63 { kind: "directory", name: "archive", changed: true },
64 { kind: "file", name: "brief.md", changed: false },
65 ],
66 });
67 });
68});