Files
HealthLog/src/__tests__/source-read-helpers.test.ts
T
Marc-André Bombeck f3d7ab31e2 test(guards): make path-pinned source reads fail when their subject moves
A test that reads a source file by path and asserts an absence
(`expect(source).not.toMatch(…)`) stays green after a split: the code it
watched moved next door and the old path no longer holds it. A missing file
throws; a file that kept its name but lost its contents does not.

- `readSource(path, anchors)` in helpers/source-files.ts reads one file and
  requires every anchor (a string or pattern the watched code carries) to
  match at least once; it throws on a missing file, a lost anchor, or a read
  without an anchor.
- `readSourceFolder(dir, { floor })` is the folder-scan form for a subject
  that was split into several files.
- The twelve reads that were asserted only negatively now go through
  `readSource` with an anchor: the batch route, the integration-sync
  registrar, the dashboard client, the mood insights client, the sleep
  overview and sleep page, the sidebar, the baseline form, the account
  section (two guards), api-handler's requireAuth and the Google Health sync.
- The two schema parsers the wipe and enum guards share now refuse a parse
  of the real schema that finds fewer than 50 models (and 10 enums), so a
  moved file or a drifted block regex cannot leave those guards comparing
  an allowlist against nothing. Synthetic schemas a test writes stay exempt.

Proof: renaming `setSaving` in the baseline form made the converted
confirm-failure guard throw "the file no longer contains setSaving"; the
helper's own test plants a missing file, a lost string anchor, a lost regex
anchor, an empty anchor list and an under-floor folder, each of which throws.
2026-10-22 04:29:35 +02:00

56 lines
1.8 KiB
TypeScript

/**
* `readSource` and `readSourceFolder`: the reads a path-pinned guard goes
* through, so a split file fails the guard instead of leaving it green.
*/
import { describe, expect, it } from "vitest";
import { readSource, readSourceFolder } from "./helpers/source-files";
const HERE = "src/__tests__/helpers/source-files.ts";
describe("readSource", () => {
it("returns the text when every anchor is present", () => {
const text = readSource(HERE, [
"export function readSource(",
/export function walkSourceFiles\(/,
]);
expect(text.length).toBeGreaterThan(1000);
});
it("throws on a file that does not exist", () => {
expect(() => readSource("src/lib/does-not-exist.ts", "anything")).toThrow(
/does not exist/,
);
});
it("throws when an anchor no longer matches", () => {
expect(() =>
readSource(HERE, ["export function readSource(", "noSuchAnchorHere"]),
).toThrow(/no longer contains noSuchAnchorHere/);
expect(() => readSource(HERE, /export function neverDeclared\(/)).toThrow(
/no longer contains/,
);
});
it("refuses a read without an anchor", () => {
expect(() => readSource(HERE, [])).toThrow(/at least one anchor/);
});
});
describe("readSourceFolder", () => {
it("returns the files with their text, above the floor", () => {
const files = readSourceFolder("src/__tests__/helpers", { floor: 5 });
expect(files.length).toBeGreaterThanOrEqual(5);
expect(files.map(([rel]) => rel)).toContain("source-files.ts");
});
it("throws when the folder holds fewer files than the floor", () => {
expect(() =>
readSourceFolder("src/__tests__/helpers", {
floor: 1,
include: (rel) => rel === "nothing-matches.ts",
}),
).toThrow(/below the stated floor/);
});
});