Issue 129
tech-debt: Add Vitest and unit tests for the rules the site depends on
Back to our planDone
citoolingtech-debtpriority: high
Type
tech-debt
Summary
Add automated tests so that the rules the site depends on (spam checks, email safety, paging, dates) are checked on every pull request, instead of by hand. This is the safety net before code gets moved around.
Problem
There is no test runner and no test file in the repo. CI checks types, build, formatting, lint, the dependency audit and PR rules, but not behavior. The checks written for #111, #113 and #114 (page-number parsing, Turnstile verification, submission size limits, email guards, SMTP config) lived only in a scratch folder and were never committed.
Proposed fix
- Add Vitest (TypeScript-native, ESM, works with the
@/path alias) as a dev dependency, atestscript, and aUnit testsstep in the CI workflow. - Unit tests for existing pure or easily isolated logic, next to the code as
*.test.ts:utilities/feeds.ts:parsePageNumber,isEventScopeutilities/turnstile.ts: token verification (success, wrong action, wrong hostname, test secrets, network error, timeout) withfetchmockedhooks/limitSubmissionSize.ts: field count, name length, value length, total length, malformed datahooks/formSubmissionGuards.ts: dropping emails to submitted addresses, From and Reply-To handling, rate limit, hourly email caputilities/email.ts: SMTP config validation (port, single address, user and password together)utilities/timezone.tsandutilities/formatDateTime.ts: site-timezone "today", day-only datesutilities/clientIp.ts
- No network calls and no database in unit tests.
Acceptance criteria
- Not done:
npm testruns all tests locally in under 30 seconds and passes. - Not done: CI runs the tests on every PR that changes code; a failing test fails the
Type-check & Buildcheck. - Not done: Every function listed above has tests for its normal case and its edge cases, including the security-relevant ones (Turnstile hostname/action, anti-relay, size limits).
- Not done: Tests are deterministic: fixed dates, mocked
fetch, no real timers or network. - Not done: No production code changes, except where a function must be exported to be tested.
Depends on
Nothing. Step 2 of #127.
Area
ci, tooling
Priority
high
Generated by Claude Code, opens in a new tab