Skip to content
Source
Sign inSign up

turnDiffTree.test.ts

171 lines5.4 KB

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 expect(
91 buildTurnDiffTree([
92 { path: "src/a/b/first.ts", kind: "modified", additions: 1, deletions: 0 },
93 { path: "src/c/d/second.ts", kind: "modified", additions: 2, deletions: 0 },
94 ]),
95 ).toMatchObject([
96 {
97 name: "src",
98 children: [
99 { name: "a/b", path: "src/a/b", children: [{ name: "first.ts" }] },
100 { name: "c/d", path: "src/c/d", children: [{ name: "second.ts" }] },
101 ],
102 },
103 ]);
104​
105 const tree = buildTurnDiffTree([
106 { path: "apps/server/src/index.ts", kind: "modified", additions: 2, deletions: 1 },
107 { path: "apps/server/main.ts", kind: "modified", additions: 4, deletions: 0 },
108 ]);
109​
110 expect(tree).toEqual([
111 {
112 kind: "directory",
113 name: "apps/server",
114 path: "apps/server",
115 stat: { additions: 6, deletions: 1 },
116 children: [
117 {
118 kind: "directory",
119 name: "src",
120 path: "apps/server/src",
121 stat: { additions: 2, deletions: 1 },
122 children: [
123 {
124 kind: "file",
125 name: "index.ts",
126 path: "apps/server/src/index.ts",
127 stat: { additions: 2, deletions: 1 },
128 },
129 ],
130 },
131 {
132 kind: "file",
133 name: "main.ts",
134 path: "apps/server/main.ts",
135 stat: { additions: 4, deletions: 0 },
136 },
137 ],
138 },
139 ]);
140 });
141​
142 it("keeps a literal backslash in a POSIX file name instead of splitting it into a folder", () => {
143 expect(
144 buildTurnDiffTree([
145 { path: "back\\slash.txt", kind: "modified", additions: 1, deletions: 0 },
146 { path: "back/slash.txt", kind: "modified", additions: 2, deletions: 0 },
147 ]),
148 ).toEqual([
149 {
150 kind: "directory",
151 name: "back",
152 path: "back",
153 stat: { additions: 2, deletions: 0 },
154 children: [
155 {
156 kind: "file",
157 name: "slash.txt",
158 path: "back/slash.txt",
159 stat: { additions: 2, deletions: 0 },
160 },
161 ],
162 },
163 {
164 kind: "file",
165 name: "back\\slash.txt",
166 path: "back\\slash.txt",
167 stat: { additions: 1, deletions: 0 },
168 },
169 ]);
170 });
171});