Skip to content
SprecherEast
Issue 129

tech-debt: Add Vitest and unit tests for the rules the site depends on

Back to our plan
Done
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, a test script, and a Unit tests step in the CI workflow.
  • Unit tests for existing pure or easily isolated logic, next to the code as *.test.ts:
    • utilities/feeds.ts: parsePageNumber, isEventScope
    • utilities/turnstile.ts: token verification (success, wrong action, wrong hostname, test secrets, network error, timeout) with fetch mocked
    • hooks/limitSubmissionSize.ts: field count, name length, value length, total length, malformed data
    • hooks/formSubmissionGuards.ts: dropping emails to submitted addresses, From and Reply-To handling, rate limit, hourly email cap
    • utilities/email.ts: SMTP config validation (port, single address, user and password together)
    • utilities/timezone.ts and utilities/formatDateTime.ts: site-timezone "today", day-only dates
    • utilities/clientIp.ts
  • No network calls and no database in unit tests.

Acceptance criteria

  • Not done: npm test runs 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 & Build check.
  • 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

See it on GitHub, opens in a new tab