Files
HealthLog/src/__tests__/report-selection-egress-guard.test.ts
T
Marc-André Bombeck 1ae137eb84 test(guards): sweep the directories the structural guards were skipping
`fs.globSync` never matches a leading dot, so every guard built on
`**/*.{ts,tsx}` walked past `src/app/.well-known` — the two OAuth
discovery documents and the apple-app-site-association handler sat
outside fourteen frozen-set sweeps. An unlisted `getSession()` planted
in the authorization-server route left the session-surface guard at nine
passed; it fails now.

Move those fourteen onto one shared walker built on `readdirSync` with
`recursive`, which has no dot rule, and give it a floor every caller has
to state, so a walk that comes back too small throws instead of
reporting a clean tree.

Give the same floor to the guards that compared a discovered set against
a frozen list with nothing asserting the set had been read: gutting the
walker in the inventory-write guard left its invariant test green,
because zero offenders reads exactly like no offenders. Raise the
migration-count floor off one, and assert the locale bundles are found
before the per-locale cases are generated from them.

Document, at each walker that prunes on purpose, what is pruned and that
dot-prefixed directories are not.
2026-08-02 16:21:19 +02:00

97 lines
4.0 KiB
TypeScript

/**
* Structural guard: every caller of the doctor-report aggregator is named here
* with the source of its selection.
*
* The aggregator's third parameter is required and brand-typed, so a new
* egress path cannot compile without naming a selection source. This is the
* second half of that: it cannot be ADDED without a human writing down which
* source, in this list, which is the moment a reviewer asks the consent
* question. Modelled on `bearer-scope-enforcement-guard.test.ts`.
*
* Adding a caller fails the build until the list is updated deliberately.
*
* Mutation check: add a `collectDoctorReportData(` call anywhere under `src/`
* that is not in the list → "the set of aggregator callers is frozen" goes red
* naming the file.
*/
import { describe, it, expect } from "vitest";
import { readFileSync, readdirSync, statSync } from "node:fs";
import { join } from "node:path";
const SRC = join(__dirname, "..");
/**
* Pruned on purpose: the generated Prisma client, build output, vendored
* dependencies, and the suites themselves are not the tree under audit. Note
* what is NOT pruned — this walk descends into dot-prefixed directories, so
* `src/app/.well-known` is inside the sweep. `fs.globSync` would drop it.
*/
const SKIP_DIRS = new Set(["__tests__", "node_modules", ".next", "generated"]);
/**
* Every production call site, with the source of its selection. A path here
* that no longer calls the aggregator is as much a failure as one that does
* and is missing — a stale entry is how a list stops being read.
*/
const CALLERS: Record<string, string> = {
"app/api/export/health-record/route.ts":
"the request body's `selection` field, minted through selectionFromRequest",
"lib/clinician-share/share-view-data.ts":
"the share link's frozen sectionsJson, minted through selectionFromStoredBlob",
"lib/fhir/rest.ts": "the owner's saved profile (resolveSavedSelection)",
"lib/mcp/resources.ts": "the owner's saved profile (resolveSavedSelection)",
"lib/mcp/prompts.ts": "the owner's saved profile (resolveSavedSelection)",
};
function walk(dir: string, out: string[] = []): string[] {
for (const name of readdirSync(dir)) {
if (SKIP_DIRS.has(name)) continue;
const p = join(dir, name);
if (statSync(p).isDirectory()) walk(p, out);
else if (name.endsWith(".ts") || name.endsWith(".tsx")) out.push(p);
}
return out;
}
/** Strip comments so a mention in prose is not read as a call. */
function stripComments(source: string): string {
return source.replace(/\/\*[\s\S]*?\*\//g, "").replace(/^\s*\/\/.*$/gm, "");
}
describe("doctor-report aggregator egress", () => {
// A sweep that finds nothing agrees with every allowlist, so the size of
// the tree being read is asserted before anything is concluded from it.
// Pinned below the real source-file count with headroom, not at one.
it("reads the tree it claims to sweep", () => {
expect(walk(SRC).length).toBeGreaterThan(1500);
});
it("keeps the set of aggregator callers frozen", () => {
const found: string[] = [];
for (const file of walk(SRC)) {
const rel = file.slice(SRC.length + 1).replace(/\\/g, "/");
// The aggregator's own module and its public surface are not callers.
if (rel === "lib/doctor-report/collect.ts") continue;
if (rel === "lib/doctor-report-data.ts") continue;
const body = stripComments(readFileSync(file, "utf8"));
if (body.includes("collectDoctorReportData(")) found.push(rel);
}
const expected = Object.keys(CALLERS).sort();
expect(
found.sort(),
"Every surface that assembles the whole record has to name where its " +
"selection came from. Add the file to CALLERS with its source, or " +
"remove the call.",
).toEqual(expected);
});
it("names a real selection source for each caller", () => {
for (const [path, source] of Object.entries(CALLERS)) {
expect(
source.length,
`${path} has no stated selection source`,
).toBeGreaterThan(10);
}
});
});