Open History to see the last commit
1import { describe, expect, it } from "vitest";
2
3import { buildTurnDiffTree, summarizeTurnDiffStats } from "./turnDiffTree";
4
5describe("summarizeTurnDiffStats", () => {
6 it("sums only files with numeric additions/deletions", () => {
7 const stat = summarizeTurnDiffStats([
8 { path: "README.md", additions: 3, deletions: 1 },
9 { path: "docs/notes.md" },
10 { path: "src/index.ts", additions: 5, deletions: 2 },
11 ]);
12
13 expect(stat).toEqual({ additions: 8, deletions: 3 });
14 });
15});
16
17describe("buildTurnDiffTree", () => {
18 it("builds nested directory nodes with aggregated stats", () => {
19 const tree = buildTurnDiffTree([
20 { path: "src/index.ts", additions: 2, deletions: 1 },
21 { path: "src/components/Button.tsx", additions: 4, deletions: 2 },
22 { path: "README.md", additions: 1, deletions: 0 },
23 ]);
24
25 expect(tree).toEqual([
26 {
27 kind: "directory",
28 name: "src",
29 path: "src",
30 stat: { additions: 6, deletions: 3 },
31 children: [
32 {
33 kind: "directory",
34 name: "components",
35 path: "src/components",
36 stat: { additions: 4, deletions: 2 },
37 children: [
38 {
39 kind: "file",
40 name: "Button.tsx",
41 path: "src/components/Button.tsx",
42 stat: { additions: 4, deletions: 2 },
43 },
44 ],
45 },
46 {
47 kind: "file",
48 name: "index.ts",
49 path: "src/index.ts",
50 stat: { additions: 2, deletions: 1 },
51 },
52 ],
53 },
54 {
55 kind: "file",
56 name: "README.md",
57 path: "README.md",
58 stat: { additions: 1, deletions: 0 },
59 },
60 ]);
61 });
62
63 it("keeps files without stat values and excludes them from directory totals", () => {
64 const tree = buildTurnDiffTree([
65 { path: "docs/notes.md" },
66 { path: "docs/todo.md", additions: 1, deletions: 1 },
67 ]);
68
69 expect(tree).toEqual([
70 {
71 kind: "directory",
72 name: "docs",
73 path: "docs",
74 stat: { additions: 1, deletions: 1 },
75 children: [
76 {
77 kind: "file",
78 name: "notes.md",
79 path: "docs/notes.md",
80 stat: null,
81 },
82 {
83 kind: "file",
84 name: "todo.md",
85 path: "docs/todo.md",
86 stat: { additions: 1, deletions: 1 },
87 },
88 ],
89 },
90 ]);
91 });
92
93 it("normalizes file paths with windows separators", () => {
94 const tree = buildTurnDiffTree([{ path: "apps\\web\\src\\index.ts", additions: 2, deletions: 1 }]);
95
96 expect(tree).toEqual([
97 {
98 kind: "directory",
99 name: "apps/web/src",
100 path: "apps/web/src",
101 stat: { additions: 2, deletions: 1 },
102 children: [
103 {
104 kind: "file",
105 name: "index.ts",
106 path: "apps/web/src/index.ts",
107 stat: { additions: 2, deletions: 1 },
108 },
109 ],
110 },
111 ]);
112 });
113
114 it("compacts only single-directory chains and stops at branch points", () => {
115 const tree = buildTurnDiffTree([
116 { path: "apps/server/src/index.ts", additions: 2, deletions: 1 },
117 { path: "apps/server/main.ts", additions: 4, deletions: 0 },
118 ]);
119
120 expect(tree).toEqual([
121 {
122 kind: "directory",
123 name: "apps/server",
124 path: "apps/server",
125 stat: { additions: 6, deletions: 1 },
126 children: [
127 {
128 kind: "directory",
129 name: "src",
130 path: "apps/server/src",
131 stat: { additions: 2, deletions: 1 },
132 children: [
133 {
134 kind: "file",
135 name: "index.ts",
136 path: "apps/server/src/index.ts",
137 stat: { additions: 2, deletions: 1 },
138 },
139 ],
140 },
141 {
142 kind: "file",
143 name: "main.ts",
144 path: "apps/server/main.ts",
145 stat: { additions: 4, deletions: 0 },
146 },
147 ],
148 },
149 ]);
150 });
151
152 it("preserves leading/trailing whitespace in path segments", () => {
153 const tree = buildTurnDiffTree([
154 { path: "a/file.ts", additions: 1, deletions: 0 },
155 { path: " a/file.ts", additions: 2, deletions: 0 },
156 ]);
157
158 expect(tree).toHaveLength(2);
159 const directoryNodes = tree.filter(
160 (node): node is Extract<(typeof tree)[number], { kind: "directory" }> => node.kind === "directory",
161 );
162 expect(directoryNodes.map((node) => node.name).toSorted()).toEqual([" a", "a"]);
163 expect(directoryNodes.map((node) => node.path).toSorted()).toEqual([" a", "a"]);
164 });
165});