Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

pullRequestFileOrder.logic.test.ts

149 lines4.5 KB

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);
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("puts a dependency before what imports it", () => {
40 expect(order([file("src/a.ts", ['import { b } from "./b";']), file("src/b.ts")])).toEqual([
41 "src/b.ts",
42 "src/a.ts",
43 ]);
44 });
45​
46 it("follows a chain the whole way down", () => {
47 expect(
48 order([
49 file("src/a.ts", ['import { b } from "./b";']),
50 file("src/b.ts", ['import { c } from "./c";']),
51 file("src/c.ts"),
52 ]),
53 ).toEqual(["src/c.ts", "src/b.ts", "src/a.ts"]);
54 });
55​
56 it("resolves a specifier that climbs out of the importer's directory", () => {
57 expect(
58 order([file("src/ui/a.ts", ['import { b } from "../lib/b";']), file("src/lib/b.ts")]),
59 ).toEqual(["src/lib/b.ts", "src/ui/a.ts"]);
60 });
61​
62 it("resolves a directory specifier to that directory's index file", () => {
63 expect(
64 order([file("src/a.ts", ['import { b } from "./lib";']), file("src/lib/index.ts")]),
65 ).toEqual(["src/lib/index.ts", "src/a.ts"]);
66 });
67​
68 it("falls back to the specifier's last segment when the path does not line up", () => {
69 expect(order([file("src/a.ts", ['import { b } from "~/lib/b";']), file("lib/b.ts")])).toEqual([
70 "lib/b.ts",
71 "src/a.ts",
72 ]);
73 });
74​
75 it("ignores an ambiguous last segment rather than guessing", () => {
76 expect(
77 order([
78 file("src/a.ts", ['import { b } from "~/somewhere/b";']),
79 file("one/b.ts"),
80 file("two/b.ts"),
81 ]),
82 ).toEqual(["one/b.ts", "src/a.ts", "two/b.ts"]);
83 });
84​
85 it("resolves a specifier naming an extension, including the .js beside a .ts", () => {
86 expect(order([file("src/a.ts", ['import { b } from "./b.js";']), file("src/b.ts")])).toEqual([
87 "src/b.ts",
88 "src/a.ts",
89 ]);
90 });
91​
92 it("reads require and bare imports too", () => {
93 expect(
94 order([
95 file("src/a.ts", ['const b = require("./b");', 'import "./c";']),
96 file("src/b.ts"),
97 file("src/c.ts"),
98 ]),
99 ).toEqual(["src/b.ts", "src/c.ts", "src/a.ts"]);
100 });
101​
102 it("falls back to path order inside a cycle", () => {
103 expect(
104 order([
105 file("src/b.ts", ['import { a } from "./a";']),
106 file("src/a.ts", ['import { b } from "./b";']),
107 ]),
108 ).toEqual(["src/a.ts", "src/b.ts"]);
109 });
110​
111 it("keeps the tiers apart and orders tests by the source they cover", () => {
112 expect(
113 order([
114 file("pnpm-lock.yaml"),
115 file("src/a.test.ts"),
116 file("src/__tests__/b.ts"),
117 file("src/a.ts", ['import { b } from "./b";']),
118 file("src/b.ts"),
119 file("dist/a.js"),
120 ]),
121 ).toEqual([
122 "src/b.ts",
123 "src/a.ts",
124 "src/__tests__/b.ts",
125 "src/a.test.ts",
126 "dist/a.js",
127 "pnpm-lock.yaml",
128 ]);
129 });
130​
131 it("puts a test with no source in the diff after the ones that have theirs", () => {
132 expect(order([file("src/orphan.spec.ts"), file("src/a.test.ts"), file("src/a.ts")])).toEqual([
133 "src/a.ts",
134 "src/a.test.ts",
135 "src/orphan.spec.ts",
136 ]);
137 });
138​
139 it("orders the same diff the same way whatever order it arrives in", () => {
140 const files = [
141 file("src/a.ts", ['import { b } from "./b";']),
142 file("src/b.ts"),
143 file("src/c.ts"),
144 file("src/a.test.ts"),
145 file("yarn.lock"),
146 ];
147 expect(order(files)).toEqual(order(files.toReversed()));
148 });
149});