Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { orderDiffFilesForReview } from "./diffFileOrder.ts";
4
5interface TestFile {
6 readonly name: string;
7 readonly additionLines: ReadonlyArray<string>;
8 readonly deletionLines: ReadonlyArray<string>;
9}
10
11function file(name: string, additionLines: ReadonlyArray<string> = []): TestFile {
12 return { name, additionLines, deletionLines: [] };
13}
14
15function order(files: ReadonlyArray<TestFile>): Array<string> {
16 return orderDiffFilesForReview(files, (entry) => entry.name).map((entry) => entry.name);
17}
18
19describe("orderDiffFilesForReview", () => {
20 it("places source before tests and generated files across path conventions", () => {
21 const source = ["src/app.ts", "src/dist.ts", "src/testing.ts"];
22 const tests = [
23 "src/__tests__/app.ts",
24 "src/app.spec.tsx",
25 "src/app.test.ts",
26 "test/app.ts",
27 "tests/helpers/app.ts",
28 ];
29 const generated = [
30 "apps/web/package-lock.json",
31 "dist/app.js",
32 "packages/core/vendor/lib.js",
33 "pnpm-lock.yaml",
34 "public/app.min.js",
35 "src/__snapshots__/app.ts",
36 "src/api.generated.ts",
37 "src/app.test.ts.snap",
38 ];
39 expect(
40 order([...generated, ...tests, ...source].toReversed().map((path) => file(path))),
41 ).toEqual([
42 ...source,
43 ...tests.slice(0, 3),
44 // Generated files under src/ stay in src/ so the list matches the tree.
45 "src/__snapshots__/app.ts",
46 "src/api.generated.ts",
47 "src/app.test.ts.snap",
48 ...tests.slice(3),
49 ...generated.slice(0, 5),
50 ]);
51 });
52
53 it("keeps both entries of a path that changed type, such as a file replaced by a symlink", () => {
54 const deleted = { ...file("AGENTS.md"), additionLines: [], deletionLines: ["# Agents"] };
55 const added = file("AGENTS.md", ["CLAUDE.md"]);
56 expect(
57 orderDiffFilesForReview([file("CLAUDE.md"), deleted, added], (entry) => entry.name),
58 ).toEqual([deleted, added, file("CLAUDE.md")]);
59 });
60
61 it("pulls a test listed last into its directory, keeping the first-seen order", () => {
62 expect(
63 order([
64 file("server/device/Host.ts"),
65 file("web/settings.ts"),
66 file("server/ws.ts"),
67 file("server/device/Host.test.ts"),
68 file("web/settings.test.ts"),
69 ]),
70 ).toEqual([
71 "server/device/Host.ts",
72 "server/device/Host.test.ts",
73 "server/ws.ts",
74 "web/settings.ts",
75 "web/settings.test.ts",
76 ]);
77 });
78
79 it("resolves a specifier that climbs out of the importer's directory", () => {
80 expect(
81 order([file("src/ui/a.ts", ['import { b } from "../lib/b";']), file("src/lib/b.ts")]),
82 ).toEqual(["src/lib/b.ts", "src/ui/a.ts"]);
83 });
84
85 it("resolves a directory specifier to that directory's index file", () => {
86 expect(
87 order([file("src/a.ts", ['import { b } from "./lib";']), file("src/lib/index.ts")]),
88 ).toEqual(["src/lib/index.ts", "src/a.ts"]);
89 });
90
91 it("resolves .mts and .cts modules from NodeNext and extensionless specifiers", () => {
92 expect(order([file("a.mts", ['import { z } from "./z.mjs";']), file("z.mts")])).toEqual([
93 "z.mts",
94 "a.mts",
95 ]);
96 expect(order([file("a.ts", ['import { z } from "./z";']), file("z.cts")])).toEqual([
97 "z.cts",
98 "a.ts",
99 ]);
100 });
101
102 it("falls back to the specifier's last segment when the path does not line up", () => {
103 expect(order([file("src/a.ts", ['import { b } from "~/lib/b";']), file("lib/b.ts")])).toEqual([
104 "lib/b.ts",
105 "src/a.ts",
106 ]);
107 });
108
109 it("falls back to the whole dotted module name, not its first segment", () => {
110 expect(
111 order([file("src/a.ts", ['import { b } from "~/z/b.logic";']), file("z/b.logic.ts")]),
112 ).toEqual(["z/b.logic.ts", "src/a.ts"]);
113 expect(
114 order([
115 file("src/a.ts", ['import { b } from "~/z/b.logic";']),
116 file("z/b.logic.ts"),
117 file("other/b.ts"),
118 ]),
119 ).toEqual(["other/b.ts", "z/b.logic.ts", "src/a.ts"]);
120 });
121
122 it("ignores an ambiguous last segment rather than guessing", () => {
123 expect(
124 order([
125 file("src/a.ts", ['import { b } from "~/somewhere/b";']),
126 file("one/b.ts"),
127 file("two/b.ts"),
128 ]),
129 ).toEqual(["one/b.ts", "src/a.ts", "two/b.ts"]);
130 });
131
132 it("resolves a specifier naming an extension, including the .js beside a .ts", () => {
133 expect(order([file("src/a.ts", ['import { b } from "./b.js";']), file("src/b.ts")])).toEqual([
134 "src/b.ts",
135 "src/a.ts",
136 ]);
137 });
138
139 it("strips a bundler query such as ?worker but keeps a # subpath import", () => {
140 expect(
141 order([file("src/a.js", ['import Worker from "./z.js?worker";']), file("src/z.js")]),
142 ).toEqual(["src/z.js", "src/a.js"]);
143 expect(
144 order([file("a.ts", ['import url from "~/lib/b.logic?url";']), file("lib/b.logic.ts")]),
145 ).toEqual(["lib/b.logic.ts", "a.ts"]);
146 expect(
147 order([file("src/a.ts", ['import { z } from "#internal/z";']), file("src/z.ts")]),
148 ).toEqual(["src/z.ts", "src/a.ts"]);
149 });
150
151 it("reads require and bare imports too", () => {
152 expect(
153 order([
154 file("src/a.ts", ['const b = require("./b");', 'import "./c";']),
155 file("src/b.ts"),
156 file("src/c.ts"),
157 ]),
158 ).toEqual(["src/b.ts", "src/c.ts", "src/a.ts"]);
159 });
160
161 it("falls back to path order inside a cycle", () => {
162 expect(
163 order([
164 file("src/b.ts", ['import { a } from "./a";']),
165 file("src/a.ts", ['import { b } from "./b";']),
166 ]),
167 ).toEqual(["src/a.ts", "src/b.ts"]);
168 });
169
170 it("matches naive lexicographical topological ordering across random cyclic and acyclic graphs", () => {
171 function naiveOrderByImports(
172 paths: ReadonlyArray<string>,
173 imports: ReadonlyMap<string, ReadonlySet<string>>,
174 ): Array<string> {
175 const remaining = new Set(paths);
176 const ordered: Array<string> = [];
177 const sorted = [...paths].sort((left, right) => left.localeCompare(right));
178 while (remaining.size > 0) {
179 const candidates = sorted.filter((path) => remaining.has(path));
180 const next =
181 candidates.find((path) =>
182 [...(imports.get(path) ?? [])].every((dependency) => !remaining.has(dependency)),
183 ) ?? candidates[0]!;
184 remaining.delete(next);
185 ordered.push(next);
186 }
187 return ordered;
188 }
189
190 let seed = 0x5eed1234;
191 const nextRand = () => {
192 seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0;
193 return seed / 0x100000000;
194 };
195
196 for (let trial = 0; trial < 30; trial++) {
197 const count = 4 + (trial % 15);
198 const paths = Array.from({ length: count }, (_, index) => {
199 return `src/f${String(index).padStart(2, "0")}.ts`;
200 });
201 const imports = new Map<string, Set<string>>();
202 const files: Array<TestFile> = [];
203 const edgeProbability = trial % 3 === 0 ? 0.12 : trial % 3 === 1 ? 0.3 : 0.55;
204
205 for (let index = 0; index < count; index++) {
206 const deps = new Set<string>();
207 const lines: Array<string> = [];
208 for (let target = 0; target < count; target++) {
209 if (target === index) continue;
210 if (nextRand() < edgeProbability) {
211 deps.add(paths[target]!);
212 lines.push(`import "./f${String(target).padStart(2, "0")}";`);
213 }
214 }
215 imports.set(paths[index]!, deps);
216 files.push(file(paths[index]!, lines));
217 }
218
219 expect(order(files)).toEqual(naiveOrderByImports(paths, imports));
220 }
221 });
222
223 it("keeps the tiers apart and orders tests by the source they cover", () => {
224 expect(
225 order([
226 file("pnpm-lock.yaml"),
227 file("src/a.test.ts"),
228 file("src/__tests__/b.ts"),
229 file("src/a.ts", ['import { b } from "./b";']),
230 file("src/b.ts"),
231 file("dist/a.js"),
232 ]),
233 ).toEqual([
234 "src/b.ts",
235 "src/a.ts",
236 "src/__tests__/b.ts",
237 "src/a.test.ts",
238 "dist/a.js",
239 "pnpm-lock.yaml",
240 ]);
241 });
242
243 it("puts a test with no source in the diff after the ones that have theirs", () => {
244 expect(order([file("src/orphan.spec.ts"), file("src/a.test.ts"), file("src/a.ts")])).toEqual([
245 "src/a.ts",
246 "src/a.test.ts",
247 "src/orphan.spec.ts",
248 ]);
249 });
250
251 it("pairs a test with the source whose name carries its own dotted suffix", () => {
252 expect(
253 order([
254 file("src/c.browser.test.tsx"),
255 file("src/b.test.ts"),
256 file("src/a.logic.test.ts"),
257 file("src/c.tsx"),
258 file("src/b.ts"),
259 file("src/a.logic.ts"),
260 ]),
261 ).toEqual([
262 "src/a.logic.ts",
263 "src/b.ts",
264 "src/c.tsx",
265 "src/a.logic.test.ts",
266 "src/b.test.ts",
267 "src/c.browser.test.tsx",
268 ]);
269 });
270});