mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 00:12:18 +00:00
test: give every scanning guard a floor on what it found
A guard that compares a sweep against an allowlist, or against "none of
these may exist", passes when the sweep comes back empty. walkSourceFiles
already refuses a short walk; this adds the same refusal for what a guard
does with the walk.
- requireFloor(label, found, floor) throws below the floor and refuses a
floor of zero.
- scanSourceMatches(root, pattern, { fileFloor, matchFloor }) walks, matches
and floors both the files read and the hits found.
Floors added, pinned at the current counts:
- coach_usage owner columns: the clauses it actually examined (6).
- measurement reference index: the foreign keys it extracted (2).
- job outcome fact inventory: job files (130, floor 120) and fact keys
(399, floor 350).
- tone harness: assessment prompt modules (11).
- coach locale default: turn pipeline modules (15, floor 12).
- locale integrity: message bundles (7).
- settings card header coverage and settings anatomy: the walked cards.
- coach metric scope inventory: insight route sources.
- side-effect drift guard: both enums populated, since two empty keysets
are equal.
The pill-mapper guard walked with globSync, which skips dot-prefixed
directories; it now walks through walkSourceFiles like the rest.
This commit is contained in:
1 parent
a3b5480d04
commit
b4577f5c31
12 files changed
+166
-37
No files matched your search
@@ -98,12 +98,14 @@ describe("coach_usage — the two counters move together", () => {
|
||||
|
||||
it("names operator_tokens in every clause that names total_tokens", () => {
|
||||
const offences: string[] = [];
|
||||
let examined = 0;
|
||||
for (const file of files) {
|
||||
const source = readFileSync(file, "utf8");
|
||||
if (!source.includes("coach_usage")) continue;
|
||||
for (const literal of sqlLiterals(source)) {
|
||||
for (const clause of columnClauses(literal)) {
|
||||
if (!clause.text.includes("total_tokens")) continue;
|
||||
examined += 1;
|
||||
if (clause.text.includes("operator_tokens")) continue;
|
||||
offences.push(
|
||||
`${relative(REPO_ROOT, file)} — ${clause.label}: ${clause.text
|
||||
@@ -118,6 +120,8 @@ describe("coach_usage — the two counters move together", () => {
|
||||
offences,
|
||||
`Every raw coach_usage clause that touches total_tokens must touch operator_tokens too:\n${offences.join("\n")}`,
|
||||
).toEqual([]);
|
||||
// No clauses examined would agree with the rule above on nothing.
|
||||
expect(examined).toBeGreaterThanOrEqual(6);
|
||||
});
|
||||
|
||||
it("keeps the raw-SQL writer set to the files that reason about the ledger", () => {
|
||||
|
||||
@@ -50,8 +50,8 @@
|
||||
* worktrees, each a full second copy of the tree that would double every
|
||||
* match. Every caller roots at `src/`, `e2e/` or a subtree of one.
|
||||
*/
|
||||
import { readdirSync } from "node:fs";
|
||||
import { sep } from "node:path";
|
||||
import { readdirSync, readFileSync } from "node:fs";
|
||||
import { join, sep } from "node:path";
|
||||
|
||||
export function walkSourceFiles(
|
||||
root: string,
|
||||
@@ -140,3 +140,92 @@ export function stripComments(source: string): string {
|
||||
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that a sweep found at least `floor` things, and returns them.
|
||||
*
|
||||
* `walkSourceFiles` puts a floor under the walk. This puts one under what a
|
||||
* guard does with it: the narrowed file set, the call sites a matcher picked
|
||||
* out, the routes an inventory extracted. A matcher that drifts out of step
|
||||
* with the code — a renamed helper, a call split across two lines, a regex
|
||||
* that stopped compiling the way it was meant to — returns an empty list, and
|
||||
* an empty list agrees with every allowlist and every "none of these may
|
||||
* exist" rule. That is how the Bearer-scope guard stayed green for weeks while
|
||||
* matching nothing.
|
||||
*
|
||||
* Pin `floor` at the real count when the set is a closed inventory, and below
|
||||
* it with headroom only when the count legitimately moves. A floor of zero is
|
||||
* refused: it is the bug this exists to prevent, spelled as a parameter.
|
||||
*/
|
||||
export function requireFloor<T>(
|
||||
label: string,
|
||||
found: readonly T[],
|
||||
floor: number,
|
||||
): readonly T[] {
|
||||
if (!Number.isInteger(floor) || floor < 1) {
|
||||
throw new Error(
|
||||
`requireFloor(${label}): floor must be a positive integer, got ${floor}.`,
|
||||
);
|
||||
}
|
||||
if (found.length < floor) {
|
||||
throw new Error(
|
||||
`${label}: found ${found.length}, below the stated floor of ${floor}. ` +
|
||||
`A matcher that finds this little has drifted from the code it ` +
|
||||
`reads; fix the matcher before lowering the floor.`,
|
||||
);
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
export interface SourceMatch {
|
||||
/** Path relative to the scanned root, posix-separated. */
|
||||
file: string;
|
||||
/** 1-based line of the match start. */
|
||||
line: number;
|
||||
/** The matched text. */
|
||||
text: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Walk `root`, run `pattern` over every kept file, and return each match —
|
||||
* with a floor on the files read AND a floor on the matches found.
|
||||
*
|
||||
* `pattern` must carry the `g` flag; it is the only way to report more than
|
||||
* one hit per file, and a non-global regex silently finding one hit per file
|
||||
* is the kind of quiet narrowing this helper is for.
|
||||
*/
|
||||
export function scanSourceMatches(
|
||||
root: string,
|
||||
pattern: RegExp,
|
||||
options: {
|
||||
fileFloor: number;
|
||||
matchFloor: number;
|
||||
extensions?: readonly string[];
|
||||
include?: (rel: string) => boolean;
|
||||
stripComments?: boolean;
|
||||
},
|
||||
): SourceMatch[] {
|
||||
if (!pattern.global) {
|
||||
throw new Error(`scanSourceMatches: pattern ${pattern} needs the g flag.`);
|
||||
}
|
||||
const files = walkSourceFiles(root, {
|
||||
floor: options.fileFloor,
|
||||
extensions: options.extensions,
|
||||
}).filter(options.include ?? (() => true));
|
||||
requireFloor(`scanSourceMatches(${root}) files`, files, options.fileFloor);
|
||||
|
||||
const matches: SourceMatch[] = [];
|
||||
for (const file of files) {
|
||||
const raw = readFileSync(join(root, file), "utf8");
|
||||
const source = options.stripComments ? stripComments(raw) : raw;
|
||||
for (const m of source.matchAll(pattern)) {
|
||||
const line = source.slice(0, m.index).split("\n").length;
|
||||
matches.push({ file, line, text: m[0] });
|
||||
}
|
||||
}
|
||||
return requireFloor(
|
||||
`scanSourceMatches(${root}, ${pattern})`,
|
||||
matches,
|
||||
options.matchFloor,
|
||||
) as SourceMatch[];
|
||||
}
|
||||
@@ -64,6 +64,7 @@ describe("columns that point into measurements are indexed", () => {
|
||||
const refs = measurementReferences();
|
||||
|
||||
it("finds the references (a matcher that finds none proves nothing)", () => {
|
||||
expect(refs.length).toBeGreaterThanOrEqual(2);
|
||||
expect(refs).toEqual(
|
||||
expect.arrayContaining([
|
||||
"ecg_recordings.measurement_id",
|
||||
|
||||
Reference in new issue
Block a user