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