Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { buildTurnDiffTree } from "./turnDiffTree";
4
5describe("buildTurnDiffTree", () => {
6 it("sorts directory and file names numerically with directories first", () => {
7 const tree = buildTurnDiffTree([
8 { path: "file10.ts", kind: "modified", additions: 1, deletions: 0 },
9 { path: "dir10/file10.ts", kind: "modified", additions: 1, deletions: 0 },
10 { path: "file2.ts", kind: "modified", additions: 1, deletions: 0 },
11 { path: "dir2/file10.ts", kind: "modified", additions: 1, deletions: 0 },
12 { path: "dir2/file2.ts", kind: "modified", additions: 1, deletions: 0 },
13 ]);
14
15 expect(tree.map((node) => node.name)).toEqual(["dir2", "dir10", "file2.ts", "file10.ts"]);
16 const directory = tree[0];
17 expect(directory?.kind).toBe("directory");
18 if (directory?.kind !== "directory") throw new Error("Expected a directory");
19 expect(directory.children.map((node) => node.name)).toEqual(["file2.ts", "file10.ts"]);
20 });
21
22 it("preserves input order for case- and accent-insensitive name ties", () => {
23 const tree = buildTurnDiffTree([
24 { path: "a.ts", kind: "modified", additions: 1, deletions: 0 },
25 { path: "A.ts", kind: "modified", additions: 2, deletions: 0 },
26 { path: "FOLDER/a.ts", kind: "modified", additions: 3, deletions: 0 },
27 { path: "folder/a.ts", kind: "modified", additions: 4, deletions: 0 },
28 { path: "café.ts", kind: "modified", additions: 5, deletions: 0 },
29 { path: "Cafe.ts", kind: "modified", additions: 6, deletions: 0 },
30 { path: "cafe.ts", kind: "modified", additions: 7, deletions: 0 },
31 ]);
32
33 expect(tree.map((node) => node.name)).toEqual([
34 "FOLDER",
35 "folder",
36 "a.ts",
37 "A.ts",
38 "café.ts",
39 "Cafe.ts",
40 "cafe.ts",
41 ]);
42 });
43
44 it("builds nested directory nodes with aggregated stats", () => {
45 const tree = buildTurnDiffTree([
46 { path: "src/index.ts", kind: "modified", additions: 2, deletions: 1 },
47 { path: "src/components/Button.tsx", kind: "modified", additions: 4, deletions: 2 },
48 { path: "README.md", kind: "modified", additions: 1, deletions: 0 },
49 ]);
50
51 expect(tree).toEqual([
52 {
53 kind: "directory",
54 name: "src",
55 path: "src",
56 stat: { additions: 6, deletions: 3 },
57 children: [
58 {
59 kind: "directory",
60 name: "components",
61 path: "src/components",
62 stat: { additions: 4, deletions: 2 },
63 children: [
64 {
65 kind: "file",
66 name: "Button.tsx",
67 path: "src/components/Button.tsx",
68 stat: { additions: 4, deletions: 2 },
69 },
70 ],
71 },
72 {
73 kind: "file",
74 name: "index.ts",
75 path: "src/index.ts",
76 stat: { additions: 2, deletions: 1 },
77 },
78 ],
79 },
80 {
81 kind: "file",
82 name: "README.md",
83 path: "README.md",
84 stat: { additions: 1, deletions: 0 },
85 },
86 ]);
87 });
88
89 it("compacts only single-directory chains and stops at branch points", () => {
90 const tree = buildTurnDiffTree([
91 { path: "apps/server/src/index.ts", kind: "modified", additions: 2, deletions: 1 },
92 { path: "apps/server/main.ts", kind: "modified", additions: 4, deletions: 0 },
93 ]);
94
95 expect(tree).toEqual([
96 {
97 kind: "directory",
98 name: "apps/server",
99 path: "apps/server",
100 stat: { additions: 6, deletions: 1 },
101 children: [
102 {
103 kind: "directory",
104 name: "src",
105 path: "apps/server/src",
106 stat: { additions: 2, deletions: 1 },
107 children: [
108 {
109 kind: "file",
110 name: "index.ts",
111 path: "apps/server/src/index.ts",
112 stat: { additions: 2, deletions: 1 },
113 },
114 ],
115 },
116 {
117 kind: "file",
118 name: "main.ts",
119 path: "apps/server/main.ts",
120 stat: { additions: 4, deletions: 0 },
121 },
122 ],
123 },
124 ]);
125 });
126});