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

pullRequestFileOrder.logic.test.ts

177 lines5.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("resolves a specifier that climbs out of the importer's directory", () => {
40 expect(
41 order([file("src/ui/a.ts", ['import { b } from "../lib/b";']), file("src/lib/b.ts")]),
42 ).toEqual(["src/lib/b.ts", "src/ui/a.ts"]);
43 });
44​
45 it("resolves a directory specifier to that directory's index file", () => {
46 expect(
47 order([file("src/a.ts", ['import { b } from "./lib";']), file("src/lib/index.ts")]),
48 ).toEqual(["src/lib/index.ts", "src/a.ts"]);
49 });
50​
51 it("resolves .mts and .cts modules from NodeNext and extensionless specifiers", () => {
52 expect(order([file("a.mts", ['import { z } from "./z.mjs";']), file("z.mts")])).toEqual([
53 "z.mts",
54 "a.mts",
55 ]);
56 expect(order([file("a.ts", ['import { z } from "./z";']), file("z.cts")])).toEqual([
57 "z.cts",
58 "a.ts",
59 ]);
60 });
61​
62 it("falls back to the specifier's last segment when the path does not line up", () => {
63 expect(order([file("src/a.ts", ['import { b } from "~/lib/b";']), file("lib/b.ts")])).toEqual([
64 "lib/b.ts",
65 "src/a.ts",
66 ]);
67 });
68​
69 it("falls back to the whole dotted module name, not its first segment", () => {
70 expect(
71 order([file("src/a.ts", ['import { b } from "~/z/b.logic";']), file("z/b.logic.ts")]),
72 ).toEqual(["z/b.logic.ts", "src/a.ts"]);
73 expect(
74 order([
75 file("src/a.ts", ['import { b } from "~/z/b.logic";']),
76 file("z/b.logic.ts"),
77 file("other/b.ts"),
78 ]),
79 ).toEqual(["other/b.ts", "z/b.logic.ts", "src/a.ts"]);
80 });
81​
82 it("ignores an ambiguous last segment rather than guessing", () => {
83 expect(
84 order([
85 file("src/a.ts", ['import { b } from "~/somewhere/b";']),
86 file("one/b.ts"),
87 file("two/b.ts"),
88 ]),
89 ).toEqual(["one/b.ts", "src/a.ts", "two/b.ts"]);
90 });
91​
92 it("resolves a specifier naming an extension, including the .js beside a .ts", () => {
93 expect(order([file("src/a.ts", ['import { b } from "./b.js";']), file("src/b.ts")])).toEqual([
94 "src/b.ts",
95 "src/a.ts",
96 ]);
97 });
98​
99 it("strips a bundler query such as ?worker but keeps a # subpath import", () => {
100 expect(
101 order([file("src/a.js", ['import Worker from "./z.js?worker";']), file("src/z.js")]),
102 ).toEqual(["src/z.js", "src/a.js"]);
103 expect(
104 order([file("a.ts", ['import url from "~/lib/b.logic?url";']), file("lib/b.logic.ts")]),
105 ).toEqual(["lib/b.logic.ts", "a.ts"]);
106 expect(
107 order([file("src/a.ts", ['import { z } from "#internal/z";']), file("src/z.ts")]),
108 ).toEqual(["src/z.ts", "src/a.ts"]);
109 });
110​
111 it("reads require and bare imports too", () => {
112 expect(
113 order([
114 file("src/a.ts", ['const b = require("./b");', 'import "./c";']),
115 file("src/b.ts"),
116 file("src/c.ts"),
117 ]),
118 ).toEqual(["src/b.ts", "src/c.ts", "src/a.ts"]);
119 });
120​
121 it("falls back to path order inside a cycle", () => {
122 expect(
123 order([
124 file("src/b.ts", ['import { a } from "./a";']),
125 file("src/a.ts", ['import { b } from "./b";']),
126 ]),
127 ).toEqual(["src/a.ts", "src/b.ts"]);
128 });
129​
130 it("keeps the tiers apart and orders tests by the source they cover", () => {
131 expect(
132 order([
133 file("pnpm-lock.yaml"),
134 file("src/a.test.ts"),
135 file("src/__tests__/b.ts"),
136 file("src/a.ts", ['import { b } from "./b";']),
137 file("src/b.ts"),
138 file("dist/a.js"),
139 ]),
140 ).toEqual([
141 "src/b.ts",
142 "src/a.ts",
143 "src/__tests__/b.ts",
144 "src/a.test.ts",
145 "dist/a.js",
146 "pnpm-lock.yaml",
147 ]);
148 });
149​
150 it("puts a test with no source in the diff after the ones that have theirs", () => {
151 expect(order([file("src/orphan.spec.ts"), file("src/a.test.ts"), file("src/a.ts")])).toEqual([
152 "src/a.ts",
153 "src/a.test.ts",
154 "src/orphan.spec.ts",
155 ]);
156 });
157​
158 it("pairs a test with the source whose name carries its own dotted suffix", () => {
159 expect(
160 order([
161 file("src/c.browser.test.tsx"),
162 file("src/b.test.ts"),
163 file("src/a.logic.test.ts"),
164 file("src/c.tsx"),
165 file("src/b.ts"),
166 file("src/a.logic.ts"),
167 ]),
168 ).toEqual([
169 "src/a.logic.ts",
170 "src/b.ts",
171 "src/c.tsx",
172 "src/a.logic.test.ts",
173 "src/b.test.ts",
174 "src/c.browser.test.tsx",
175 ]);
176 });
177});