From 300092f4e03afef155ff2f80ac0778cd463a8932 Mon Sep 17 00:00:00 2001 From: Andrew Cole Date: Sat, 28 Nov 2026 06:34:47 -0800 Subject: [PATCH] Commit hook: a form's whole-return tests run only when the commit changes the form Co-Authored-By: Claude --- .husky/pre-commit | 11 ++++--- CLAUDE.md | 2 +- engine/tests/README.md | 2 +- package.json | 1 + scripts/hook-unit-tests.mjs | 62 +++++++++++++++++++++++++++++++++++++ 5 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 scripts/hook-unit-tests.mjs diff --git a/.husky/pre-commit b/.husky/pre-commit index 9bde7f444..93ef3988c 100644 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -2,13 +2,16 @@ # 1. format:check — Prettier, verify only (run `npm run format` to fix) # 2. lint — eslint, one rule: multi-line comments are starred blocks # 3. test — golden suite, values only: engine/tests/golden/runAll.ts -# 4. test:unit — vitest, engine/tests/unit + canon, cade and web unit tests +# 4. test:unit:hook — the unit tests of canon, cade and web, and the engine's +# without the whole-return tests (each form's form.test.ts), which run only +# for the forms this commit changes (scripts/hook-unit-tests.mjs) # -# The slow tests do not run here. The integration tests, the full +# The slow tests do not run here. Every unit test, the integration tests, the full # required-inputs check and the PDF fill run on GitHub for each pull request # (.github/workflows/gates.yml), side by side, and a pull request merges only # when its "all gates" check is green. Run one locally when you need it: -# `npm run test:integration`, `npm run test:inputs`, `npm run test:pdf`. +# `npm run test:unit`, `npm run test:integration`, `npm run test:inputs`, +# `npm run test:pdf`. # # Skip with --no-verify if you're committing a deliberate scenario update # in two stages (expected.ts before fact-fix, or vice versa). Don't make @@ -17,4 +20,4 @@ npm run format:check npm run lint npm test -npm run test:unit +npm run test:unit:hook diff --git a/CLAUDE.md b/CLAUDE.md index f791ee785..090e2a60e 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -493,7 +493,7 @@ Most readers open a PR on a phone and do not open the diff. The description must - `Risk: routine` or `Risk: high — `; add `Complex: ` when the reader needs more than a glance, and `Pattern change: ` when the PR changes how we do something. A pattern change also updates this file. 3. **What changed**: one bullet per idea, in the order a reader would ask. Name a file only when the reader has to go there. 4. **Example outcome** when a user or operator sees something different: the real screen text, log line or command output. -5. **Verified**: what ran and what did not. Say "not run" out loud. A pull request merges only when its `all gates` check on GitHub is green. The check (`.github/workflows/gates.yml`) runs every test on GitHub's runners as parallel jobs: typecheck, lint and format, the scenario values, the PDF fill, the unit tests, the integration tests and the full required-inputs check. No job calls a model or needs a secret. GitHub tests the head merged with `main` as of the last push; when `main` moved since, `gh pr update-branch ` runs the check again. The commit hook is only a quick check (format, lint, scenario values, unit tests), so the pull request's check is the one place where the filled PDFs, the integration tests and every removed answer are checked. `gh pr checks --watch` waits for the result. +5. **Verified**: what ran and what did not. Say "not run" out loud. A pull request merges only when its `all gates` check on GitHub is green. The check (`.github/workflows/gates.yml`) runs every test on GitHub's runners as parallel jobs: typecheck, lint and format, the scenario values, the PDF fill, the unit tests, the integration tests and the full required-inputs check. No job calls a model or needs a secret. GitHub tests the head merged with `main` as of the last push; when `main` moved since, `gh pr update-branch ` runs the check again. The commit hook is only a quick check (format, lint, scenario values, and the unit tests without the whole-return tests of forms the commit does not change: `scripts/hook-unit-tests.mjs`), so the pull request's check is the one place where the filled PDFs, the integration tests and every removed answer are checked. `gh pr checks --watch` waits for the result. 6. **PDFs to review** when the PR adds or changes a PDF: one link for each file to its page on the PR's branch, `https://github.com/captaincole/OpenReturn/blob//`. GitHub's diff does not show a PDF; the file's page does. Put the files a person must read first (a made input document, the filled return of a new form), then the other filled returns, then blank forms. `git diff --name-only --diff-filter=AM origin/main...HEAD -- '*.pdf'` lists the files. After a push that changes the list, update it. 7. **Refs**: the issue, the PRs it builds on, the doc that recommends the change. diff --git a/engine/tests/README.md b/engine/tests/README.md index 7ed207f76..c84219608 100644 --- a/engine/tests/README.md +++ b/engine/tests/README.md @@ -16,7 +16,7 @@ are in `cade/tests/`; the corpus tests are in `canon/tests/`. | `scenarios/` | The scenario data. Each folder with an `index.ts` is a scenario; `scenarios/index.ts` finds them. `scenarios/synthetic/` holds the taxpayers we made up with no outside source (its README). | — | | `helpers/` | Shared assertion, fixture, and golden-PDF code. | — | -The pre-commit hook runs the scenario values and the unit tests. Every pull request runs all of them on GitHub (`.github/workflows/gates.yml`), as parallel jobs, and merges only when the `all gates` check is green. All are offline: no network, no AI. +The pre-commit hook runs the scenario values and the unit tests; of the whole-return unit tests (each form's `form.test.ts`, 10 to 40 seconds a file) it runs only those of the forms the commit changes. Every pull request runs all of them on GitHub (`.github/workflows/gates.yml`), as parallel jobs, and merges only when the `all gates` check is green. All are offline: no network, no AI. **Where a new test goes.** A rule, worksheet, or table test goes in `unit/`, next to the same path as its module. Give it small inputs of its diff --git a/package.json b/package.json index de6186ad8..087ce6cb3 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "test:pdf": "npm -w @openreturn/engine run test:pdf", "test:inputs": "npm -w @openreturn/engine run test:inputs", "test:unit": "npm -w @openreturn/engine run test:unit && npm -w canon run test:unit && npm -w cade run test:unit && npm --prefix web run test:unit", + "test:unit:hook": "node scripts/hook-unit-tests.mjs && npm -w canon run test:unit && npm -w cade run test:unit && npm --prefix web run test:unit", "test:integration": "npm -w @openreturn/engine run test:integration && npm -w cade run test:integration", "coverage": "npm -w @openreturn/engine run coverage", "form-graph": "npm -w @openreturn/engine run form-graph", diff --git a/scripts/hook-unit-tests.mjs b/scripts/hook-unit-tests.mjs new file mode 100644 index 000000000..6e92887eb --- /dev/null +++ b/scripts/hook-unit-tests.mjs @@ -0,0 +1,62 @@ +#!/usr/bin/env node +/* + * The engine's unit tests for the commit hook. + * + * Each form has two kinds of unit test. `lines.test.ts` and the like check + * arithmetic and are fast. `form.test.ts` computes a whole return in each + * test, to check how the form connects to the others, and one file takes 10 + * to 40 seconds: the 100 or so of them were 93% of the unit test time. + * + * The hook runs every fast test, and the `form.test.ts` files only of the + * forms that the commit changes. GitHub runs all of them for each pull + * request (.github/workflows/gates.yml), and `npm run test:unit` runs all of + * them here. + */ +import { execFileSync, spawnSync } from "node:child_process"; +import { existsSync } from "node:fs"; +import { dirname, join } from "node:path"; +import { fileURLToPath } from "node:url"; + +const root = join(dirname(fileURLToPath(import.meta.url)), ".."); +const SLOW_TEST = "form.test.ts"; + +/* The folders that hold forms, each with the test folder that mirrors it. */ +const FORM_ROOTS = [ + { source: "engine/src/federal/", tests: "engine/tests/unit/federal/" }, + { source: "engine/src/state/ca/", tests: "engine/tests/unit/state/ca/" }, +]; + +const stagedFiles = () => + execFileSync("git", ["diff", "--cached", "--name-only", "--diff-filter=ACMR"], { + cwd: root, + encoding: "utf8", + }) + .split("\n") + .filter((line) => line.length > 0); + +/* The form.test.ts of the form a staged file belongs to, when it has one. */ +const slowTestOf = (file) => + FORM_ROOTS.flatMap(({ source, tests }) => + [source, tests] + .filter((prefix) => file.startsWith(prefix)) + .map((prefix) => file.slice(prefix.length).split("/")) + .filter((parts) => parts.length > 1) + .map((parts) => join(tests, parts[0], SLOW_TEST)), + ).filter((testFile) => existsSync(join(root, testFile))); + +const vitest = (args) => + spawnSync("npx", ["vitest", "run", ...args], { cwd: join(root, "engine"), stdio: "inherit" }) + .status ?? 1; + +const fromEngine = (file) => file.slice("engine/".length); + +const fast = vitest(["tests/unit", "--exclude", `**/${SLOW_TEST}`]); +if (fast !== 0) process.exit(fast); + +const slow = [...new Set(stagedFiles().flatMap(slowTestOf))].map(fromEngine); +if (slow.length === 0) { + console.log(`No form changed: no ${SLOW_TEST} to run.`); + process.exit(0); +} +console.log(`Whole-return tests of the forms this commit changes: ${slow.join(", ")}`); +process.exit(vitest(slow));