Open History to see the last commit
1import type { FileDiffMetadata } from "@pierre/diffs";
2import { describe, expect, it } from "vite-plus/test";
3
4import { diffFileTier, orderDiffFiles } from "./pullRequestFileOrder.logic";
5
6/** Only the path and the patch's own lines matter here; the viewer fills the rest in. */
7function file(name: string, additionLines: ReadonlyArray<string> = []): FileDiffMetadata {
8 return { name, hunks: [], additionLines, deletionLines: [] } as unknown as FileDiffMetadata;
9}
10
11function order(files: ReadonlyArray<FileDiffMetadata>): Array<string> {
12 return orderDiffFiles(files).map((entry) => entry.name);
13}
14
15describe("diffFileTier", () => {
16 it("puts lockfiles, snapshots and build output last", () => {
17 expect(diffFileTier("pnpm-lock.yaml")).toBe("generated");
18 expect(diffFileTier("apps/web/package-lock.json")).toBe("generated");
19 expect(diffFileTier("src/__snapshots__/app.ts")).toBe("generated");
20 expect(diffFileTier("src/app.test.ts.snap")).toBe("generated");
21 expect(diffFileTier("src/api.generated.ts")).toBe("generated");
22 expect(diffFileTier("public/app.min.js")).toBe("generated");
23 expect(diffFileTier("dist/app.js")).toBe("generated");
24 expect(diffFileTier("packages/core/vendor/lib.js")).toBe("generated");
25 });
26
27 it("recognises a test by its name or by the directory holding it", () => {
28 expect(diffFileTier("src/app.test.ts")).toBe("test");
29 expect(diffFileTier("src/app.spec.tsx")).toBe("test");
30 expect(diffFileTier("src/__tests__/app.ts")).toBe("test");
31 expect(diffFileTier("test/app.ts")).toBe("test");
32 expect(diffFileTier("tests/helpers/app.ts")).toBe("test");
33 });
34
35 it("treats everything else as source, including files merely named like a directory", () => {
36 expect(diffFileTier("src/app.ts")).toBe("source");
37 expect(diffFileTier("src/testing.ts")).toBe("source");
38 expect(diffFileTier("src/dist.ts")).toBe("source");
39 });
40});
41
42describe("orderDiffFiles", () => {
43 it("answers an empty diff with an empty order", () => {
44 expect(order([])).toEqual([]);
45 });
46
47 it("puts a dependency before what imports it", () => {
48 expect(order([file("src/a.ts", ['import { b } from "./b";']), file("src/b.ts")])).toEqual([
49 "src/b.ts",
50 "src/a.ts",
51 ]);
52 });
53
54 it("follows a chain the whole way down", () => {
55 expect(
56 order([
57 file("src/a.ts", ['import { b } from "./b";']),
58 file("src/b.ts", ['import { c } from "./c";']),
59 file("src/c.ts"),
60 ]),
61 ).toEqual(["src/c.ts", "src/b.ts", "src/a.ts"]);
62 });
63
64 it("resolves a specifier that climbs out of the importer's directory", () => {
65 expect(
66 order([file("src/ui/a.ts", ['import { b } from "../lib/b";']), file("src/lib/b.ts")]),
67 ).toEqual(["src/lib/b.ts", "src/ui/a.ts"]);
68 });
69
70 it("resolves a directory specifier to that directory's index file", () => {
71 expect(
72 order([file("src/a.ts", ['import { b } from "./lib";']), file("src/lib/index.ts")]),
73 ).toEqual(["src/lib/index.ts", "src/a.ts"]);
74 });
75
76 it("falls back to the specifier's last segment when the path does not line up", () => {
77 // An aliased import (`~/lib/b`) resolves to nothing on disk here, but the diff has only one
78 // file that could be meant.
79 expect(order([file("src/a.ts", ['import { b } from "~/lib/b";']), file("lib/b.ts")])).toEqual([
80 "lib/b.ts",
81 "src/a.ts",
82 ]);
83 });
84
85 it("ignores an ambiguous last segment rather than guessing", () => {
86 expect(
87 order([
88 file("src/a.ts", ['import { b } from "~/somewhere/b";']),
89 file("one/b.ts"),
90 file("two/b.ts"),
91 ]),
92 ).toEqual(["one/b.ts", "src/a.ts", "two/b.ts"]);
93 });
94
95 it("resolves a specifier naming an extension, including the .js beside a .ts", () => {
96 expect(order([file("src/a.ts", ['import { b } from "./b.js";']), file("src/b.ts")])).toEqual([
97 "src/b.ts",
98 "src/a.ts",
99 ]);
100 });
101
102 it("reads require and bare imports too", () => {
103 expect(
104 order([
105 file("src/a.ts", ['const b = require("./b");', 'import "./c";']),
106 file("src/b.ts"),
107 file("src/c.ts"),
108 ]),
109 ).toEqual(["src/b.ts", "src/c.ts", "src/a.ts"]);
110 });
111
112 it("falls back to path order inside a cycle", () => {
113 expect(
114 order([
115 file("src/b.ts", ['import { a } from "./a";']),
116 file("src/a.ts", ['import { b } from "./b";']),
117 ]),
118 ).toEqual(["src/a.ts", "src/b.ts"]);
119 });
120
121 it("clusters unrelated files by path", () => {
122 expect(order([file("src/ui/b.ts"), file("src/lib/z.ts"), file("src/lib/a.ts")])).toEqual([
123 "src/lib/a.ts",
124 "src/lib/z.ts",
125 "src/ui/b.ts",
126 ]);
127 });
128
129 it("keeps the tiers apart and orders tests by the source they cover", () => {
130 expect(
131 order([
132 file("pnpm-lock.yaml"),
133 file("src/a.test.ts"),
134 file("src/__tests__/b.ts"),
135 file("src/a.ts", ['import { b } from "./b";']),
136 file("src/b.ts"),
137 file("dist/a.js"),
138 ]),
139 ).toEqual([
140 "src/b.ts",
141 "src/a.ts",
142 "src/__tests__/b.ts",
143 "src/a.test.ts",
144 "dist/a.js",
145 "pnpm-lock.yaml",
146 ]);
147 });
148
149 it("puts a test with no source in the diff after the ones that have theirs", () => {
150 expect(order([file("src/orphan.spec.ts"), file("src/a.test.ts"), file("src/a.ts")])).toEqual([
151 "src/a.ts",
152 "src/a.test.ts",
153 "src/orphan.spec.ts",
154 ]);
155 });
156
157 it("orders the same diff the same way whatever order it arrives in", () => {
158 const files = [
159 file("src/a.ts", ['import { b } from "./b";']),
160 file("src/b.ts"),
161 file("src/c.ts"),
162 file("src/a.test.ts"),
163 file("yarn.lock"),
164 ];
165 expect(order(files)).toEqual(order(files.toReversed()));
166 });
167});