Files
Rosea/.agents/lessons/testing.md
T

90 lines
4.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Testing
## A pipe hides the exit code of the command you are checking
**What happened.** On 2026-11-01 Phase 0 ran `pnpm -s lint 2>&1 | tail -30; echo "exit=$?"`.
It printed one ESLint error and then `exit=0`. The `0` belonged to `tail`, not to ESLint.
**Why it bit.** Anything piped through `tail`, `grep` or `head` reports the last command's
status. A gate read that way can be red and look green. This is the same lie as
`pnpm test` exiting 0 having run nothing, one layer further out.
**How to apply it.** When the exit code matters, redirect to a file and read `$?`
before filtering: `cmd > log 2>&1; ec=$?; grep … log; echo "exit=$ec"`. You can also use
`${PIPESTATUS[0]}` in bash. Never quote `exit=0` from the far end of a pipe.
## `setOffline` tests the harness, not the service worker *(imported from Ludus)*
**What happened.** In Ludus, `context.setOffline(true)` and `route().abort()` both
failed the navigation before the service worker was consulted. So an "offline" test
passed or failed for reasons that had nothing to do with the cache.
**Why it bit.** Both mechanisms act at the browser's network layer, which sits in
front of service-worker dispatch.
**How to apply it.** Prove offline by **closing the server**: start it, wait for
`navigator.serviceWorker.controller` and for the precache, call `server.close()`,
then navigate. `e2e/offline.spec.ts` does this, and also fetches from Node to prove
the server really is gone. Not re-measured in Rosea. Strike the "imported" mark once
someone has.
## Put the bug back before you trust the test
**What happened.** Phase 0 put 13 bugs back one at a time: `clientsClaim: false`, no
`navigateFallback`, manifest scope `/`, an inline script, a Google Fonts link, a
`localStorage` write, a hard-coded heading, a UTC `localDay`, a disabled length cap
(checked twice), a lost word boundary, a removed Domi rule, and both git hooks.
Every one was caught. The results are recorded in
`.agents/reviews/feature-phase0-workspace.md`.
**Why it bit.** It didn't, this time. But a guard that has only ever been seen not
firing has not been tested. Without the run, a review can only say "I believe".
**How to apply it.** For every guard a branch adds, apply the inverse change, run the
one suite that should catch it, record CAUGHT or MISSED, and restore the file with
`git checkout`. Commit first, so the restore is exact.
## Fill an empty slot for one run, to test the state it will have later
**What happened.** The Letter's e2e test branches: no link while `docs/LETTER.md` is
empty, and the page opens once it is written. Only the empty branch could run, because
the author has not written the letter. One run with a temporary sample letter took the
other branch. It found the stale-cache bug (`technical.md`), and then passed the
written-letter page, axe included, in a real browser. The scaffold was restored
afterwards.
**Why it bit.** A test written for a future state passes trivially until that state
arrives. Then it fails for the first time on the author's machine, with the author's words.
**How to apply it.** When a test branches on content that does not exist yet, fill the
content temporarily, run the suite, restore it, and record the run in the review.
## A one-time cold start is charged to the first case
**What happened.** On 2026-11-01, with the machine at 80% CPU, `pnpm test` failed one engine
case: `purity-lint.test.ts` › *reports Date.now()*, timed out at 5 s. The snippet was fine.
The first `eslint.lintText` call loads the whole config and typescript-eslint, and vitest
charged that load to whichever case ran first. The fix is a `beforeAll` that lints an
empty file once, with its own 60 s timeout.
**Why it bit.** A suite that passes on a quiet machine can still hide a fixed cost inside one
case. It fails only under load, and then it looks like a bug in the code that case tests.
**How to apply it.** When the first case of a file loads something heavy (a linter, a
compiler, a browser), load it in `beforeAll` with an explicit timeout. If only the first case
of a file times out, suspect a cold start before suspecting the code.
## An example of "missing" must stay missing
**What happened.** On 2026-11-01 two art-prep tests used *fox* as their example of a thing not
in the manifest: `unknown character "fox"` and `"fox-sheet" is not a picture`. The fox was the
next character on the roadmap. Adding him made both tests fail. They now name an owl, which no
plan mentions.
**Why it bit.** A negative test takes its example from what is not there yet, and the next
thing on the roadmap is exactly what is not there yet. The test then fails on the day the
feature lands, for a reason that has nothing to do with the feature.
**How to apply it.** For a "does not exist" example, pick a name no plan will ever add (an owl
in a game with a fox, `owl-sheet`), never the next item on the roadmap.