mirror of
https://github.com/morluto/rea.git
synced 2026-12-04 01:54:49 +00:00
test(ci): cover the counted-baseline comparison behind the architecture guard
This commit is contained in:
1 parent
b81a8c6968
commit
9a8aa2e9fd
4 files changed
+111
-27
No files matched your search
@@ -3,6 +3,7 @@ import { existsSync } from "node:fs";
|
||||
import { readFile, readdir, writeFile } from "node:fs/promises";
|
||||
import { join, relative, sep } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { compareCountedBaseline } from "./counted-baseline.mjs";
|
||||
|
||||
// Architecture guards for the strict-identity / best-effort-inference split.
|
||||
// - Composite dedup keys must not NUL-join variable-length or free-form
|
||||
@@ -174,43 +175,36 @@ const baselines = JSON.parse(await readFile(baselinePath, "utf8"));
|
||||
const shrinkBaselines = process.argv.includes("--shrink-baselines");
|
||||
let baselinesShrunk = false;
|
||||
const checkCounted = (label, key, selected, pattern, owners) => {
|
||||
const baseline = baselines[key] ?? {};
|
||||
const counts = new Map();
|
||||
for (const file of selected) {
|
||||
if (owners.includes(file.path)) continue;
|
||||
const count = file.content.match(pattern)?.length ?? 0;
|
||||
if (count > 0) counts.set(file.path, count);
|
||||
}
|
||||
const grown = [];
|
||||
const stale = [];
|
||||
for (const [path, count] of counts) {
|
||||
const allowed = baseline[path] ?? 0;
|
||||
if (count > allowed)
|
||||
grown.push(` ${path}: ${count} (baseline ${allowed})`);
|
||||
}
|
||||
for (const [path, allowed] of Object.entries(baseline)) {
|
||||
const count = counts.get(path) ?? 0;
|
||||
if (count < allowed) stale.push({ path, count, allowed });
|
||||
}
|
||||
if (grown.length > 0)
|
||||
failures.push(`${label}; new copies are not allowed:\n${grown.join("\n")}`);
|
||||
if (stale.length === 0) return;
|
||||
if (shrinkBaselines) {
|
||||
for (const { path, count } of stale)
|
||||
if (count === 0) delete baseline[path];
|
||||
else baseline[path] = count;
|
||||
baselines[key] = baseline;
|
||||
baselinesShrunk = true;
|
||||
return;
|
||||
}
|
||||
failures.push(
|
||||
`${label}; copies were removed, so lower the baseline with \`node scripts/verify/architecture-guards.mjs --shrink-baselines\`:\n${stale
|
||||
const compared = compareCountedBaseline(
|
||||
counts,
|
||||
baselines[key] ?? {},
|
||||
shrinkBaselines,
|
||||
);
|
||||
const lines = (differences) =>
|
||||
differences
|
||||
.map(
|
||||
({ path, count, allowed }) =>
|
||||
` ${path}: ${count} (baseline ${allowed})`,
|
||||
)
|
||||
.join("\n")}`,
|
||||
);
|
||||
.join("\n");
|
||||
if (compared.grown.length > 0)
|
||||
failures.push(
|
||||
`${label}; new copies are not allowed:\n${lines(compared.grown)}`,
|
||||
);
|
||||
if (compared.baseline !== (baselines[key] ?? {})) {
|
||||
baselines[key] = compared.baseline;
|
||||
baselinesShrunk = true;
|
||||
}
|
||||
if (compared.stale.length > 0)
|
||||
failures.push(
|
||||
`${label}; copies were removed, so lower the baseline with \`node scripts/verify/architecture-guards.mjs --shrink-baselines\`:\n${lines(compared.stale)}`,
|
||||
);
|
||||
};
|
||||
|
||||
// Cancellation reasons are required by `cancelledBy` in analysisErrorCore; a
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
/** One file whose observed count differs from its recorded baseline. */
|
||||
export interface CountedBaselineDifference {
|
||||
readonly path: string;
|
||||
readonly count: number;
|
||||
readonly allowed: number;
|
||||
}
|
||||
|
||||
/** Compare per-file counts with a baseline that may only shrink. */
|
||||
export function compareCountedBaseline(
|
||||
counts: ReadonlyMap<string, number>,
|
||||
baseline: Readonly<Record<string, number>>,
|
||||
shrink: boolean,
|
||||
): {
|
||||
readonly grown: CountedBaselineDifference[];
|
||||
readonly stale: CountedBaselineDifference[];
|
||||
readonly baseline: Record<string, number>;
|
||||
};
|
||||
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* Compare observed per-file counts with a counted baseline. A file may not
|
||||
* exceed its recorded count. A count below the baseline is stale; with
|
||||
* `shrink`, the returned baseline lowers it (and drops zero entries) but never
|
||||
* raises any count.
|
||||
*/
|
||||
export const compareCountedBaseline = (counts, baseline, shrink) => {
|
||||
const grown = [];
|
||||
const stale = [];
|
||||
for (const [path, count] of counts) {
|
||||
const allowed = baseline[path] ?? 0;
|
||||
if (count > allowed) grown.push({ path, count, allowed });
|
||||
}
|
||||
for (const [path, allowed] of Object.entries(baseline)) {
|
||||
const count = counts.get(path) ?? 0;
|
||||
if (count < allowed) stale.push({ path, count, allowed });
|
||||
}
|
||||
if (!shrink || stale.length === 0) return { grown, stale, baseline };
|
||||
const lowered = { ...baseline };
|
||||
for (const { path, count } of stale)
|
||||
if (count === 0) delete lowered[path];
|
||||
else lowered[path] = count;
|
||||
return { grown, stale: [], baseline: lowered };
|
||||
};
|
||||
@@ -0,0 +1,49 @@
|
||||
import { expect, it } from "vitest";
|
||||
|
||||
import { compareCountedBaseline } from "../../../scripts/verify/counted-baseline.mjs";
|
||||
|
||||
it("rejects a file that gains copies or is missing from the baseline", () => {
|
||||
const compared = compareCountedBaseline(
|
||||
new Map([
|
||||
["src/a.ts", 3],
|
||||
["src/new.ts", 1],
|
||||
]),
|
||||
{ "src/a.ts": 2 },
|
||||
false,
|
||||
);
|
||||
expect(compared.grown).toEqual([
|
||||
{ path: "src/a.ts", count: 3, allowed: 2 },
|
||||
{ path: "src/new.ts", count: 1, allowed: 0 },
|
||||
]);
|
||||
});
|
||||
|
||||
it("reports removed copies as stale until the baseline is lowered", () => {
|
||||
const baseline = { "src/a.ts": 2, "src/gone.ts": 1 };
|
||||
const compared = compareCountedBaseline(
|
||||
new Map([["src/a.ts", 1]]),
|
||||
baseline,
|
||||
false,
|
||||
);
|
||||
expect(compared.grown).toEqual([]);
|
||||
expect(compared.stale).toEqual([
|
||||
{ path: "src/a.ts", count: 1, allowed: 2 },
|
||||
{ path: "src/gone.ts", count: 0, allowed: 1 },
|
||||
]);
|
||||
expect(compared.baseline).toBe(baseline);
|
||||
});
|
||||
|
||||
it("only lowers counts when shrinking, never raising one that grew", () => {
|
||||
const baseline = { "src/a.ts": 2, "src/b.ts": 1, "src/gone.ts": 4 };
|
||||
const compared = compareCountedBaseline(
|
||||
new Map([
|
||||
["src/a.ts", 1],
|
||||
["src/b.ts", 5],
|
||||
]),
|
||||
baseline,
|
||||
true,
|
||||
);
|
||||
expect(compared.baseline).toEqual({ "src/a.ts": 1, "src/b.ts": 1 });
|
||||
expect(compared.grown).toEqual([{ path: "src/b.ts", count: 5, allowed: 1 }]);
|
||||
expect(compared.stale).toEqual([]);
|
||||
expect(baseline).toEqual({ "src/a.ts": 2, "src/b.ts": 1, "src/gone.ts": 4 });
|
||||
});
|
||||
Reference in new issue
Block a user