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.
This commit is contained in:
Marc-André Bombeck committed 2026-10-22 04:29:35 +02:00
1 parent c86c7d1a79
commit f3d7ab31e2
16 files changed
+204 -44

No files matched your search

@@ -16,8 +16,6 @@
* component tests render server-side and cannot click.
*/
import { describe, expect, it, vi } from "vitest";
import { readFileSync } from "node:fs";
import { join } from "node:path";
import {
defaultOnboardingSteps,
@@ -27,6 +25,7 @@ import {
import { runBaselineAttempt } from "../baseline-form";
import { runConfirmFinish } from "../confirm-screen";
import { readSource } from "@/__tests__/helpers/source-files";
type PassableStep = Parameters<
Parameters<typeof runConfirmFinish>[0]["passStep"]
@@ -92,10 +91,10 @@ describe("runBaselineAttempt", () => {
it("is the form's only way to move the pending flag", () => {
// A second, inline `setSaving(false)` is how the flag stopped being
// reset on one of the three exits in the first place.
const source = readFileSync(
join(process.cwd(), "src/components/onboarding/baseline-form.tsx"),
"utf8",
);
const source = readSource("src/components/onboarding/baseline-form.tsx", [
"export function BaselineForm",
"setSaving",
]);
expect(source).not.toContain("setSaving(false)");
expect(source).not.toContain("setSaving(true)");
});