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.each([
54 "bun.lock",
55 "deno.lock",
56 "npm-shrinkwrap.json",
57 "poetry.lock",
58 "Pipfile.lock",
59 "flake.lock",
60 ])("places the %s lockfile after source like other lockfiles", (lockfile) => {
61 expect(order([file(lockfile), file("package.json"), file("src/index.ts")])).toEqual([
62 "package.json",
63 "src/index.ts",
64 lockfile,
65 ]);
66 });
67
68 it("keeps both entries of a path that changed type, such as a file replaced by a symlink", () => {
69 const deleted = { ...file("AGENTS.md"), additionLines: [], deletionLines: ["# Agents"] };
70 const added = file("AGENTS.md", ["CLAUDE.md"]);
71 expect(
72 orderDiffFilesForReview([file("CLAUDE.md"), deleted, added], (entry) => entry.name),
73 ).toEqual([deleted, added, file("CLAUDE.md")]);
74 });
75
76 it("pulls a test listed last into its directory, keeping the first-seen order", () => {
77 expect(
78 order([
79 file("server/device/Host.ts"),
80 file("web/settings.ts"),
81 file("server/ws.ts"),
82 file("server/device/Host.test.ts"),
83 file("web/settings.test.ts"),
84 ]),
85 ).toEqual([
86 "server/device/Host.ts",
87 "server/device/Host.test.ts",
88 "server/ws.ts",
89 "web/settings.ts",
90 "web/settings.test.ts",
91 ]);
92 });
93
94 it("resolves a specifier that climbs out of the importer's directory", () => {
95 expect(
96 order([file("src/ui/a.ts", ['import { b } from "../lib/b";']), file("src/lib/b.ts")]),
97 ).toEqual(["src/lib/b.ts", "src/ui/a.ts"]);
98 expect(order([file("a.ts", ['import { z } from "../z";']), file("z.ts")])).toEqual([
99 "a.ts",
100 "z.ts",
101 ]);
102 });
103
104 it("resolves a directory specifier to that directory's index file", () => {
105 expect(
106 order([file("src/a.ts", ['import { b } from "./lib";']), file("src/lib/index.ts")]),
107 ).toEqual(["src/lib/index.ts", "src/a.ts"]);
108 expect(order([file("a/app.ts", ['import { x } from "..";']), file("index.ts")])).toEqual([
109 "index.ts",
110 "a/app.ts",
111 ]);
112 expect(order([file("app.ts", ['import { x } from ".";']), file("index.ts")])).toEqual([
113 "index.ts",
114 "app.ts",
115 ]);
116 });
117
118 it("resolves .mts and .cts modules from NodeNext and extensionless specifiers", () => {
119 expect(order([file("a.mts", ['import { z } from "./z.mjs";']), file("z.mts")])).toEqual([
120 "z.mts",
121 "a.mts",
122 ]);
123 expect(order([file("a.ts", ['import { z } from "./z";']), file("z.cts")])).toEqual([
124 "z.cts",
125 "a.ts",
126 ]);
127 });
128
129 it("falls back to the specifier's last segment when the path does not line up", () => {
130 expect(order([file("src/a.ts", ['import { b } from "~/lib/b";']), file("lib/b.ts")])).toEqual([
131 "lib/b.ts",
132 "src/a.ts",
133 ]);
134 });
135
136 it("does not fall back to the basename for a relative specifier outside the diff", () => {
137 expect(order([file("src/a.ts", ['import "./z";']), file("zzz/z.ts")])).toEqual([
138 "src/a.ts",
139 "zzz/z.ts",
140 ]);
141 expect(order([file("src/sub/a.ts", ['import "../z";']), file("zzz/z.ts")])).toEqual([
142 "src/sub/a.ts",
143 "zzz/z.ts",
144 ]);
145 });
146
147 it("falls back to the whole dotted module name, not its first segment", () => {
148 expect(
149 order([file("src/a.ts", ['import { b } from "~/z/b.logic";']), file("z/b.logic.ts")]),
150 ).toEqual(["z/b.logic.ts", "src/a.ts"]);
151 expect(
152 order([
153 file("src/a.ts", ['import { b } from "~/z/b.logic";']),
154 file("z/b.logic.ts"),
155 file("other/b.ts"),
156 ]),
157 ).toEqual(["other/b.ts", "z/b.logic.ts", "src/a.ts"]);
158 });
159
160 it("ignores an ambiguous last segment rather than guessing", () => {
161 expect(
162 order([
163 file("src/a.ts", ['import { b } from "~/somewhere/b";']),
164 file("one/b.ts"),
165 file("two/b.ts"),
166 ]),
167 ).toEqual(["one/b.ts", "src/a.ts", "two/b.ts"]);
168 });
169
170 it("resolves a specifier naming an extension, including the .js beside a .ts", () => {
171 expect(order([file("src/a.ts", ['import { b } from "./b.js";']), file("src/b.ts")])).toEqual([
172 "src/b.ts",
173 "src/a.ts",
174 ]);
175 expect(order([file("a.js", ['import "./z.mjs";']), file("z.mjs"), file("z.js")])).toEqual([
176 "z.js",
177 "z.mjs",
178 "a.js",
179 ]);
180 });
181
182 it("strips a bundler query such as ?worker but keeps a # subpath import", () => {
183 expect(
184 order([file("src/a.js", ['import Worker from "./z.js?worker";']), file("src/z.js")]),
185 ).toEqual(["src/z.js", "src/a.js"]);
186 expect(
187 order([file("a.ts", ['import url from "~/lib/b.logic?url";']), file("lib/b.logic.ts")]),
188 ).toEqual(["lib/b.logic.ts", "a.ts"]);
189 expect(
190 order([file("src/a.ts", ['import { z } from "#internal/z";']), file("src/z.ts")]),
191 ).toEqual(["src/z.ts", "src/a.ts"]);
192 });
193
194 it("reads require and bare imports too", () => {
195 expect(
196 order([
197 file("src/a.ts", ['const b = require("./b");', 'import "./c";']),
198 file("src/b.ts"),
199 file("src/c.ts"),
200 ]),
201 ).toEqual(["src/b.ts", "src/c.ts", "src/a.ts"]);
202 });
203
204 it("falls back to path order inside a cycle", () => {
205 expect(
206 order([
207 file("src/b.ts", ['import { a } from "./a";']),
208 file("src/a.ts", ['import { b } from "./b";']),
209 ]),
210 ).toEqual(["src/a.ts", "src/b.ts"]);
211 });
212
213 it("matches naive lexicographical topological ordering across random cyclic and acyclic graphs", () => {
214 function naiveOrderByImports(
215 paths: ReadonlyArray<string>,
216 imports: ReadonlyMap<string, ReadonlySet<string>>,
217 ): Array<string> {
218 const remaining = new Set(paths);
219 const ordered: Array<string> = [];
220 const sorted = [...paths].sort((left, right) => left.localeCompare(right));
221 while (remaining.size > 0) {
222 const candidates = sorted.filter((path) => remaining.has(path));
223 const next =
224 candidates.find((path) =>
225 [...(imports.get(path) ?? [])].every((dependency) => !remaining.has(dependency)),
226 ) ?? candidates[0]!;
227 remaining.delete(next);
228 ordered.push(next);
229 }
230 return ordered;
231 }
232
233 let seed = 0x5eed1234;
234 const nextRand = () => {
235 seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0;
236 return seed / 0x100000000;
237 };
238
239 for (let trial = 0; trial < 30; trial++) {
240 const count = 4 + (trial % 15);
241 const paths = Array.from({ length: count }, (_, index) => {
242 return `src/f${String(index).padStart(2, "0")}.ts`;
243 });
244 const imports = new Map<string, Set<string>>();
245 const files: Array<TestFile> = [];
246 const edgeProbability = trial % 3 === 0 ? 0.12 : trial % 3 === 1 ? 0.3 : 0.55;
247
248 for (let index = 0; index < count; index++) {
249 const deps = new Set<string>();
250 const lines: Array<string> = [];
251 for (let target = 0; target < count; target++) {
252 if (target === index) continue;
253 if (nextRand() < edgeProbability) {
254 deps.add(paths[target]!);
255 lines.push(`import "./f${String(target).padStart(2, "0")}";`);
256 }
257 }
258 imports.set(paths[index]!, deps);
259 files.push(file(paths[index]!, lines));
260 }
261
262 expect(order(files)).toEqual(naiveOrderByImports(paths, imports));
263 }
264 });
265
266 it("keeps the tiers apart and orders tests by the source they cover", () => {
267 expect(
268 order([
269 file("pnpm-lock.yaml"),
270 file("src/a.test.ts"),
271 file("src/__tests__/b.ts"),
272 file("src/a.ts", ['import { b } from "./b";']),
273 file("src/b.ts"),
274 file("dist/a.js"),
275 ]),
276 ).toEqual([
277 "src/b.ts",
278 "src/a.ts",
279 "src/__tests__/b.ts",
280 "src/a.test.ts",
281 "dist/a.js",
282 "pnpm-lock.yaml",
283 ]);
284 });
285
286 it("orders tests by the source beside them when another folder has the same name", () => {
287 expect(
288 order([
289 file("pkg-a/src/index.ts"),
290 file("pkg-a/src/utils.ts"),
291 file("pkg-b/src/index.ts", ['import { u } from "./utils";']),
292 file("pkg-b/src/utils.ts"),
293 file("pkg-b/src/index.test.ts"),
294 file("pkg-b/src/utils.test.ts"),
295 ]),
296 ).toEqual([
297 "pkg-a/src/index.ts",
298 "pkg-a/src/utils.ts",
299 "pkg-b/src/utils.ts",
300 "pkg-b/src/index.ts",
301 "pkg-b/src/utils.test.ts",
302 "pkg-b/src/index.test.ts",
303 ]);
304 });
305
306 it("prefers the source beside a dotted test over a longer same-named one elsewhere", () => {
307 expect(
308 order([
309 file("y/a.logic.ts"),
310 file("z/a.ts", ['import { b } from "./b";']),
311 file("z/b.ts"),
312 file("z/a.logic.test.ts"),
313 file("z/b.test.ts"),
314 ]),
315 ).toEqual(["y/a.logic.ts", "z/b.ts", "z/a.ts", "z/b.test.ts", "z/a.logic.test.ts"]);
316 });
317
318 it("puts a test with no source in the diff after the ones that have theirs", () => {
319 expect(order([file("src/orphan.spec.ts"), file("src/a.test.ts"), file("src/a.ts")])).toEqual([
320 "src/a.ts",
321 "src/a.test.ts",
322 "src/orphan.spec.ts",
323 ]);
324 });
325
326 it("pairs a test with the source whose name carries its own dotted suffix", () => {
327 expect(
328 order([
329 file("src/c.browser.test.tsx"),
330 file("src/b.test.ts"),
331 file("src/a.logic.test.ts"),
332 file("src/c.tsx"),
333 file("src/b.ts"),
334 file("src/a.logic.ts"),
335 ]),
336 ).toEqual([
337 "src/a.logic.ts",
338 "src/b.ts",
339 "src/c.tsx",
340 "src/a.logic.test.ts",
341 "src/b.test.ts",
342 "src/c.browser.test.tsx",
343 ]);
344 });
345});