mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-15 15:02:04 +00:00
Five layers, because the defect had five places to hide. The catalogue guard checks what the compiler cannot state: every leaf resolves a label a human can read, every group resolves one, the fenced tier stays last, and the shipped template intersects the fence at nothing. The selection guard pins the three minting functions against each other. A stored blob this build cannot read resolves to the empty selection, never to a default set — that is what makes the revoked legacy links revocable rather than silently re-scoped. A request naming an id the server does not know is refused by name rather than dropped, because dropping it would narrow a scope the caller did express. The egress guard freezes the set of aggregator callers with the source of each one's selection written down. The required brand-typed parameter already stops a new path compiling without an answer; this stops one being added without a human writing the answer down, which is the moment a reviewer asks the question. The behavioural sweep aggregates every leaf in and out against a fixture that carries a row for each, and asserts the payload differs. It is parametrised over the catalogue, so a leaf added later is covered the day it is added. Its positive control asserts the all-on payload is genuinely populated first, so a "no difference" result means the gate is inert and not that the account is empty. Beside it: an unmapped measurement type is excluded rather than passed through, no table behind an unchosen leaf is queried at all, and an empty selection produces an empty record. The artefact layer runs the real route and reads what comes out — the FHIR bundle, the PDF text and the zip's contents — with a positive control beside every exclusion. Mutation-checked: making the gate admit everything reddens the sweep for every leaf; passing an unmapped type through reddens the fail-closed case; dropping the mood query's condition reddens the zero-read case; returning the BMI unconditionally reddens the sweep naming BODY_MASS_INDEX; adding a fenced leaf to the template reddens the purity guard; removing the panel's seeding block reddens the first-run and repeat-run cases; dropping the insurance refusal reddens the share-link case; adding a caller outside the frozen list reddens the egress guard naming the file. The panel suite grew past the practice name it was written for and is renamed for what it now covers: the three states, the fenced tier having no group control, and the scope line naming its sensitive inclusions. Integration coverage moves to the leaf model, including the case this release exists for: an account with data in every domain and a caller that named nothing gets an empty record. The OpenAPI contract catches up with the routes — the required selection, the refused keys, the share summary's new fields, the moved `Patient/$everything`, the Coverage search and the five typed reads.
106 lines
3.6 KiB
TypeScript
106 lines
3.6 KiB
TypeScript
/**
|
|
* v1.30.22 — the fail-closed backstop inside `collectDoctorReportData`.
|
|
*
|
|
* The MCP surfaces now gate before calling, but gating only at call sites is
|
|
* exactly the arrangement that failed: `loadFhirContext` has an internal
|
|
* backstop for the same aggregate and the routes above it still gate, while
|
|
* this function had none and three MCP callers plus a public share link
|
|
* forgot. The backstop makes the door itself refuse, so the next caller
|
|
* cannot reintroduce the leak by omission.
|
|
*
|
|
* It throws rather than returning an envelope because reaching it means a
|
|
* caller is missing its gate — a bug to surface, not a flow to serve. Callers
|
|
* that can degrade gracefully (the MCP resources, the clinician share) gate
|
|
* themselves first and never reach it.
|
|
*/
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
const { prismaMock } = vi.hoisted(() => {
|
|
// Every model access throws, so any DB touch past the backstop is loud.
|
|
const boom = () => {
|
|
throw new Error("DB touched");
|
|
};
|
|
return {
|
|
prismaMock: new Proxy(
|
|
{},
|
|
{
|
|
get: () => new Proxy({}, { get: () => boom }),
|
|
},
|
|
),
|
|
};
|
|
});
|
|
vi.mock("@/lib/db", () => ({ prisma: prismaMock }));
|
|
vi.mock("@/lib/modules/gate", async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import("@/lib/modules/gate")>();
|
|
return { ...actual, resolveModuleMap: vi.fn() };
|
|
});
|
|
|
|
import { collectDoctorReportData } from "../doctor-report-data";
|
|
import { resolveModuleMap, MODULE_KEYS } from "@/lib/modules/gate";
|
|
import {
|
|
EMPTY_REPORT_SELECTION,
|
|
selectionFromLeaves,
|
|
} from "@/lib/report-selection/selection";
|
|
|
|
const RANGE = {
|
|
start: new Date("2026-01-01T00:00:00.000Z"),
|
|
end: new Date("2026-04-01T00:00:00.000Z"),
|
|
days: 90,
|
|
};
|
|
|
|
/** Every module on, then apply the overrides. */
|
|
function moduleMap(overrides: Record<string, boolean> = {}) {
|
|
const out: Record<string, boolean> = {};
|
|
for (const key of MODULE_KEYS) out[key] = true;
|
|
return { ...out, ...overrides } as Record<string, boolean>;
|
|
}
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
describe("collectDoctorReportData — doctorReport backstop", () => {
|
|
it("throws before any DB read when the module is off", async () => {
|
|
await expect(
|
|
collectDoctorReportData(
|
|
"u1",
|
|
RANGE,
|
|
EMPTY_REPORT_SELECTION,
|
|
// Injected rather than read, so the refusal is attributable to the
|
|
// module state and not to a DB stub.
|
|
{
|
|
moduleMap: moduleMap({
|
|
doctorReport: false,
|
|
}) as never,
|
|
},
|
|
),
|
|
).rejects.toThrow(/doctorReport/);
|
|
});
|
|
|
|
it("resolves the map itself when none is injected, and still refuses", async () => {
|
|
// The real callers do not inject; this is the path that actually protects
|
|
// the aggregate in production.
|
|
vi.mocked(resolveModuleMap).mockResolvedValue(
|
|
moduleMap({ doctorReport: false }) as never,
|
|
);
|
|
|
|
await expect(
|
|
collectDoctorReportData("u1", RANGE, EMPTY_REPORT_SELECTION),
|
|
).rejects.toThrow(/doctorReport/);
|
|
expect(resolveModuleMap).toHaveBeenCalledWith("u1");
|
|
});
|
|
|
|
it("does not refuse when the module is on", async () => {
|
|
// Positive control. The aggregate needs a full DB behind it, which this
|
|
// suite deliberately does not provide — so it will fail on the first
|
|
// query. What matters is that it gets THERE: the failure must be the DB
|
|
// stub, never the backstop, or the "off" assertions above would pass
|
|
// trivially for a function that always throws.
|
|
await expect(
|
|
collectDoctorReportData("u1", RANGE, selectionFromLeaves(["WEIGHT"]), {
|
|
moduleMap: moduleMap() as never,
|
|
}),
|
|
).rejects.toThrow("DB touched");
|
|
});
|
|
});
|