t3-code-android-nightly/packages/client-runtime/src/diffFileOrder.test.ts
AymanKhan9 6d4375aaf8
perf(client-runtime): avoid quadratic scans when ordering diff files by imports (#42233)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
2026-11-24 08:12:32 -08:00

270 lines
8.6 KiB
TypeScript

import { describe, expect, it } from "vite-plus/test";
import { orderDiffFilesForReview } from "./diffFileOrder.ts";
interface TestFile {
readonly name: string;
readonly additionLines: ReadonlyArray<string>;
readonly deletionLines: ReadonlyArray<string>;
}
function file(name: string, additionLines: ReadonlyArray<string> = []): TestFile {
return { name, additionLines, deletionLines: [] };
}
function order(files: ReadonlyArray<TestFile>): Array<string> {
return orderDiffFilesForReview(files, (entry) => entry.name).map((entry) => entry.name);
}
describe("orderDiffFilesForReview", () => {
it("places source before tests and generated files across path conventions", () => {
const source = ["src/app.ts", "src/dist.ts", "src/testing.ts"];
const tests = [
"src/__tests__/app.ts",
"src/app.spec.tsx",
"src/app.test.ts",
"test/app.ts",
"tests/helpers/app.ts",
];
const generated = [
"apps/web/package-lock.json",
"dist/app.js",
"packages/core/vendor/lib.js",
"pnpm-lock.yaml",
"public/app.min.js",
"src/__snapshots__/app.ts",
"src/api.generated.ts",
"src/app.test.ts.snap",
];
expect(
order([...generated, ...tests, ...source].toReversed().map((path) => file(path))),
).toEqual([
...source,
...tests.slice(0, 3),
// Generated files under src/ stay in src/ so the list matches the tree.
"src/__snapshots__/app.ts",
"src/api.generated.ts",
"src/app.test.ts.snap",
...tests.slice(3),
...generated.slice(0, 5),
]);
});
it("keeps both entries of a path that changed type, such as a file replaced by a symlink", () => {
const deleted = { ...file("AGENTS.md"), additionLines: [], deletionLines: ["# Agents"] };
const added = file("AGENTS.md", ["CLAUDE.md"]);
expect(
orderDiffFilesForReview([file("CLAUDE.md"), deleted, added], (entry) => entry.name),
).toEqual([deleted, added, file("CLAUDE.md")]);
});
it("pulls a test listed last into its directory, keeping the first-seen order", () => {
expect(
order([
file("server/device/Host.ts"),
file("web/settings.ts"),
file("server/ws.ts"),
file("server/device/Host.test.ts"),
file("web/settings.test.ts"),
]),
).toEqual([
"server/device/Host.ts",
"server/device/Host.test.ts",
"server/ws.ts",
"web/settings.ts",
"web/settings.test.ts",
]);
});
it("resolves a specifier that climbs out of the importer's directory", () => {
expect(
order([file("src/ui/a.ts", ['import { b } from "../lib/b";']), file("src/lib/b.ts")]),
).toEqual(["src/lib/b.ts", "src/ui/a.ts"]);
});
it("resolves a directory specifier to that directory's index file", () => {
expect(
order([file("src/a.ts", ['import { b } from "./lib";']), file("src/lib/index.ts")]),
).toEqual(["src/lib/index.ts", "src/a.ts"]);
});
it("resolves .mts and .cts modules from NodeNext and extensionless specifiers", () => {
expect(order([file("a.mts", ['import { z } from "./z.mjs";']), file("z.mts")])).toEqual([
"z.mts",
"a.mts",
]);
expect(order([file("a.ts", ['import { z } from "./z";']), file("z.cts")])).toEqual([
"z.cts",
"a.ts",
]);
});
it("falls back to the specifier's last segment when the path does not line up", () => {
expect(order([file("src/a.ts", ['import { b } from "~/lib/b";']), file("lib/b.ts")])).toEqual([
"lib/b.ts",
"src/a.ts",
]);
});
it("falls back to the whole dotted module name, not its first segment", () => {
expect(
order([file("src/a.ts", ['import { b } from "~/z/b.logic";']), file("z/b.logic.ts")]),
).toEqual(["z/b.logic.ts", "src/a.ts"]);
expect(
order([
file("src/a.ts", ['import { b } from "~/z/b.logic";']),
file("z/b.logic.ts"),
file("other/b.ts"),
]),
).toEqual(["other/b.ts", "z/b.logic.ts", "src/a.ts"]);
});
it("ignores an ambiguous last segment rather than guessing", () => {
expect(
order([
file("src/a.ts", ['import { b } from "~/somewhere/b";']),
file("one/b.ts"),
file("two/b.ts"),
]),
).toEqual(["one/b.ts", "src/a.ts", "two/b.ts"]);
});
it("resolves a specifier naming an extension, including the .js beside a .ts", () => {
expect(order([file("src/a.ts", ['import { b } from "./b.js";']), file("src/b.ts")])).toEqual([
"src/b.ts",
"src/a.ts",
]);
});
it("strips a bundler query such as ?worker but keeps a # subpath import", () => {
expect(
order([file("src/a.js", ['import Worker from "./z.js?worker";']), file("src/z.js")]),
).toEqual(["src/z.js", "src/a.js"]);
expect(
order([file("a.ts", ['import url from "~/lib/b.logic?url";']), file("lib/b.logic.ts")]),
).toEqual(["lib/b.logic.ts", "a.ts"]);
expect(
order([file("src/a.ts", ['import { z } from "#internal/z";']), file("src/z.ts")]),
).toEqual(["src/z.ts", "src/a.ts"]);
});
it("reads require and bare imports too", () => {
expect(
order([
file("src/a.ts", ['const b = require("./b");', 'import "./c";']),
file("src/b.ts"),
file("src/c.ts"),
]),
).toEqual(["src/b.ts", "src/c.ts", "src/a.ts"]);
});
it("falls back to path order inside a cycle", () => {
expect(
order([
file("src/b.ts", ['import { a } from "./a";']),
file("src/a.ts", ['import { b } from "./b";']),
]),
).toEqual(["src/a.ts", "src/b.ts"]);
});
it("matches naive lexicographical topological ordering across random cyclic and acyclic graphs", () => {
function naiveOrderByImports(
paths: ReadonlyArray<string>,
imports: ReadonlyMap<string, ReadonlySet<string>>,
): Array<string> {
const remaining = new Set(paths);
const ordered: Array<string> = [];
const sorted = [...paths].sort((left, right) => left.localeCompare(right));
while (remaining.size > 0) {
const candidates = sorted.filter((path) => remaining.has(path));
const next =
candidates.find((path) =>
[...(imports.get(path) ?? [])].every((dependency) => !remaining.has(dependency)),
) ?? candidates[0]!;
remaining.delete(next);
ordered.push(next);
}
return ordered;
}
let seed = 0x5eed1234;
const nextRand = () => {
seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0;
return seed / 0x100000000;
};
for (let trial = 0; trial < 30; trial++) {
const count = 4 + (trial % 15);
const paths = Array.from({ length: count }, (_, index) => {
return `src/f${String(index).padStart(2, "0")}.ts`;
});
const imports = new Map<string, Set<string>>();
const files: Array<TestFile> = [];
const edgeProbability = trial % 3 === 0 ? 0.12 : trial % 3 === 1 ? 0.3 : 0.55;
for (let index = 0; index < count; index++) {
const deps = new Set<string>();
const lines: Array<string> = [];
for (let target = 0; target < count; target++) {
if (target === index) continue;
if (nextRand() < edgeProbability) {
deps.add(paths[target]!);
lines.push(`import "./f${String(target).padStart(2, "0")}";`);
}
}
imports.set(paths[index]!, deps);
files.push(file(paths[index]!, lines));
}
expect(order(files)).toEqual(naiveOrderByImports(paths, imports));
}
});
it("keeps the tiers apart and orders tests by the source they cover", () => {
expect(
order([
file("pnpm-lock.yaml"),
file("src/a.test.ts"),
file("src/__tests__/b.ts"),
file("src/a.ts", ['import { b } from "./b";']),
file("src/b.ts"),
file("dist/a.js"),
]),
).toEqual([
"src/b.ts",
"src/a.ts",
"src/__tests__/b.ts",
"src/a.test.ts",
"dist/a.js",
"pnpm-lock.yaml",
]);
});
it("puts a test with no source in the diff after the ones that have theirs", () => {
expect(order([file("src/orphan.spec.ts"), file("src/a.test.ts"), file("src/a.ts")])).toEqual([
"src/a.ts",
"src/a.test.ts",
"src/orphan.spec.ts",
]);
});
it("pairs a test with the source whose name carries its own dotted suffix", () => {
expect(
order([
file("src/c.browser.test.tsx"),
file("src/b.test.ts"),
file("src/a.logic.test.ts"),
file("src/c.tsx"),
file("src/b.ts"),
file("src/a.logic.ts"),
]),
).toEqual([
"src/a.logic.ts",
"src/b.ts",
"src/c.tsx",
"src/a.logic.test.ts",
"src/b.test.ts",
"src/c.browser.test.tsx",
]);
});
});