Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,4 +20,4 @@
npm run format:check
npm run lint
npm test
npm run test:unit
npm run test:unit:hook
2 changes: 1 addition & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 — <what could break and for whom>`; add `Complex: <why>` when the reader needs more than a glance, and `Pattern change: <old → new>` 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 <number>` 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 <number> --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 <number>` 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 <number> --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/<branch>/<path>`. 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.

Expand Down
2 changes: 1 addition & 1 deletion engine/tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
62 changes: 62 additions & 0 deletions scripts/hook-unit-tests.mjs
Original file line number Diff line number Diff line change
@@ -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));
Loading