Files
reverse-engineering-anything/scripts/verify/counted-baseline.mjs
T
morluto e652f4409e fix(verify): rewrite counted baselines only when a count was lowered (#10669)
* fix(verify): rewrite counted baselines only when a count was lowered

* test(verify): own the baseline write decision in a tested helper
2026-11-15 09:42:02 +08:00

39 lines
1.5 KiB
JavaScript

/**
* 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; `lowered` says whether it did, so callers rewrite a
* baseline only when it changed.
*/
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, lowered: false };
const shrunk = { ...baseline };
for (const { path, count } of stale)
if (count === 0) delete shrunk[path];
else shrunk[path] = count;
return { grown, stale: [], baseline: shrunk, lowered: true };
};
/**
* Record a comparison's lowered baseline under `key` in the full baseline
* map. Returns whether the map changed, so the caller rewrites the file only
* then. A key whose last count was removed is deleted rather than kept empty.
*/
export const recordLoweredBaseline = (baselines, key, compared) => {
if (!compared.lowered) return false;
if (Object.keys(compared.baseline).length === 0) delete baselines[key];
else baselines[key] = compared.baseline;
return true;
};