The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { orderDiffFilesForReview } from "./diffFileOrder.ts"; |
| 4 | |
| 5 | interface TestFile { |
| 6 | readonly name: string; |
| 7 | readonly additionLines: ReadonlyArray<string>; |
| 8 | readonly deletionLines: ReadonlyArray<string>; |
| 9 | } |
| 10 | |
| 11 | function file(name: string, additionLines: ReadonlyArray<string> = []): TestFile { |
| 12 | return { name, additionLines, deletionLines: [] }; |
| 13 | } |
| 14 | |
| 15 | function order(files: ReadonlyArray<TestFile>): Array<string> { |
| 16 | return orderDiffFilesForReview(files, (entry) => entry.name).map((entry) => entry.name); |
| 17 | } |
| 18 | |
| 19 | describe("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("keeps both entries of a path that changed type, such as a file replaced by a symlink", () => { |
| 54 | const deleted = { ...file("AGENTS.md"), additionLines: [], deletionLines: ["# Agents"] }; |
| 55 | const added = file("AGENTS.md", ["CLAUDE.md"]); |
| 56 | expect( |
| 57 | orderDiffFilesForReview([file("CLAUDE.md"), deleted, added], (entry) => entry.name), |
| 58 | ).toEqual([deleted, added, file("CLAUDE.md")]); |
| 59 | }); |
| 60 | |
| 61 | it("pulls a test listed last into its directory, keeping the first-seen order", () => { |
| 62 | expect( |
| 63 | order([ |
| 64 | file("server/device/Host.ts"), |
| 65 | file("web/settings.ts"), |
| 66 | file("server/ws.ts"), |
| 67 | file("server/device/Host.test.ts"), |
| 68 | file("web/settings.test.ts"), |
| 69 | ]), |
| 70 | ).toEqual([ |
| 71 | "server/device/Host.ts", |
| 72 | "server/device/Host.test.ts", |
| 73 | "server/ws.ts", |
| 74 | "web/settings.ts", |
| 75 | "web/settings.test.ts", |
| 76 | ]); |
| 77 | }); |
| 78 | |
| 79 | it("resolves a specifier that climbs out of the importer's directory", () => { |
| 80 | expect( |
| 81 | order([file("src/ui/a.ts", ['import { b } from "../lib/b";']), file("src/lib/b.ts")]), |
| 82 | ).toEqual(["src/lib/b.ts", "src/ui/a.ts"]); |
| 83 | }); |
| 84 | |
| 85 | it("resolves a directory specifier to that directory's index file", () => { |
| 86 | expect( |
| 87 | order([file("src/a.ts", ['import { b } from "./lib";']), file("src/lib/index.ts")]), |
| 88 | ).toEqual(["src/lib/index.ts", "src/a.ts"]); |
| 89 | }); |
| 90 | |
| 91 | it("resolves .mts and .cts modules from NodeNext and extensionless specifiers", () => { |
| 92 | expect(order([file("a.mts", ['import { z } from "./z.mjs";']), file("z.mts")])).toEqual([ |
| 93 | "z.mts", |
| 94 | "a.mts", |
| 95 | ]); |
| 96 | expect(order([file("a.ts", ['import { z } from "./z";']), file("z.cts")])).toEqual([ |
| 97 | "z.cts", |
| 98 | "a.ts", |
| 99 | ]); |
| 100 | }); |
| 101 | |
| 102 | it("falls back to the specifier's last segment when the path does not line up", () => { |
| 103 | expect(order([file("src/a.ts", ['import { b } from "~/lib/b";']), file("lib/b.ts")])).toEqual([ |
| 104 | "lib/b.ts", |
| 105 | "src/a.ts", |
| 106 | ]); |
| 107 | }); |
| 108 | |
| 109 | it("falls back to the whole dotted module name, not its first segment", () => { |
| 110 | expect( |
| 111 | order([file("src/a.ts", ['import { b } from "~/z/b.logic";']), file("z/b.logic.ts")]), |
| 112 | ).toEqual(["z/b.logic.ts", "src/a.ts"]); |
| 113 | expect( |
| 114 | order([ |
| 115 | file("src/a.ts", ['import { b } from "~/z/b.logic";']), |
| 116 | file("z/b.logic.ts"), |
| 117 | file("other/b.ts"), |
| 118 | ]), |
| 119 | ).toEqual(["other/b.ts", "z/b.logic.ts", "src/a.ts"]); |
| 120 | }); |
| 121 | |
| 122 | it("ignores an ambiguous last segment rather than guessing", () => { |
| 123 | expect( |
| 124 | order([ |
| 125 | file("src/a.ts", ['import { b } from "~/somewhere/b";']), |
| 126 | file("one/b.ts"), |
| 127 | file("two/b.ts"), |
| 128 | ]), |
| 129 | ).toEqual(["one/b.ts", "src/a.ts", "two/b.ts"]); |
| 130 | }); |
| 131 | |
| 132 | it("resolves a specifier naming an extension, including the .js beside a .ts", () => { |
| 133 | expect(order([file("src/a.ts", ['import { b } from "./b.js";']), file("src/b.ts")])).toEqual([ |
| 134 | "src/b.ts", |
| 135 | "src/a.ts", |
| 136 | ]); |
| 137 | }); |
| 138 | |
| 139 | it("strips a bundler query such as ?worker but keeps a # subpath import", () => { |
| 140 | expect( |
| 141 | order([file("src/a.js", ['import Worker from "./z.js?worker";']), file("src/z.js")]), |
| 142 | ).toEqual(["src/z.js", "src/a.js"]); |
| 143 | expect( |
| 144 | order([file("a.ts", ['import url from "~/lib/b.logic?url";']), file("lib/b.logic.ts")]), |
| 145 | ).toEqual(["lib/b.logic.ts", "a.ts"]); |
| 146 | expect( |
| 147 | order([file("src/a.ts", ['import { z } from "#internal/z";']), file("src/z.ts")]), |
| 148 | ).toEqual(["src/z.ts", "src/a.ts"]); |
| 149 | }); |
| 150 | |
| 151 | it("reads require and bare imports too", () => { |
| 152 | expect( |
| 153 | order([ |
| 154 | file("src/a.ts", ['const b = require("./b");', 'import "./c";']), |
| 155 | file("src/b.ts"), |
| 156 | file("src/c.ts"), |
| 157 | ]), |
| 158 | ).toEqual(["src/b.ts", "src/c.ts", "src/a.ts"]); |
| 159 | }); |
| 160 | |
| 161 | it("falls back to path order inside a cycle", () => { |
| 162 | expect( |
| 163 | order([ |
| 164 | file("src/b.ts", ['import { a } from "./a";']), |
| 165 | file("src/a.ts", ['import { b } from "./b";']), |
| 166 | ]), |
| 167 | ).toEqual(["src/a.ts", "src/b.ts"]); |
| 168 | }); |
| 169 | |
| 170 | it("keeps the tiers apart and orders tests by the source they cover", () => { |
| 171 | expect( |
| 172 | order([ |
| 173 | file("pnpm-lock.yaml"), |
| 174 | file("src/a.test.ts"), |
| 175 | file("src/__tests__/b.ts"), |
| 176 | file("src/a.ts", ['import { b } from "./b";']), |
| 177 | file("src/b.ts"), |
| 178 | file("dist/a.js"), |
| 179 | ]), |
| 180 | ).toEqual([ |
| 181 | "src/b.ts", |
| 182 | "src/a.ts", |
| 183 | "src/__tests__/b.ts", |
| 184 | "src/a.test.ts", |
| 185 | "dist/a.js", |
| 186 | "pnpm-lock.yaml", |
| 187 | ]); |
| 188 | }); |
| 189 | |
| 190 | it("puts a test with no source in the diff after the ones that have theirs", () => { |
| 191 | expect(order([file("src/orphan.spec.ts"), file("src/a.test.ts"), file("src/a.ts")])).toEqual([ |
| 192 | "src/a.ts", |
| 193 | "src/a.test.ts", |
| 194 | "src/orphan.spec.ts", |
| 195 | ]); |
| 196 | }); |
| 197 | |
| 198 | it("pairs a test with the source whose name carries its own dotted suffix", () => { |
| 199 | expect( |
| 200 | order([ |
| 201 | file("src/c.browser.test.tsx"), |
| 202 | file("src/b.test.ts"), |
| 203 | file("src/a.logic.test.ts"), |
| 204 | file("src/c.tsx"), |
| 205 | file("src/b.ts"), |
| 206 | file("src/a.logic.ts"), |
| 207 | ]), |
| 208 | ).toEqual([ |
| 209 | "src/a.logic.ts", |
| 210 | "src/b.ts", |
| 211 | "src/c.tsx", |
| 212 | "src/a.logic.test.ts", |
| 213 | "src/b.test.ts", |
| 214 | "src/c.browser.test.tsx", |
| 215 | ]); |
| 216 | }); |
| 217 | }); |