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

230 lines
13 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.
## A fake that does not do what the real thing refuses hides the bug
**What happened.** The UI tests for the sunset town used a fake `save` that pushed the
value into an array. The real save goes to IndexedDB, which clones the value and refuses
a Proxy. The fake never cloned, so 10 UI tests passed while saving was broken in every
browser (`code.md`). The same feature's e2e tests then raced the IndexedDB commit by
reloading milliseconds after a click, and passed or failed on alternate runs.
**Why it bit.** A fake is a claim about the real dependency. When the real one rejects
inputs, a fake that accepts everything tests a different program.
**How to apply it.** A fake for a boundary does the boundary's checks: a storage fake
calls `structuredClone` on what it is given. An e2e test that reloads after a write first
waits for the write to be readable (`e2e/stored.ts`), never for the screen alone.
## A test that fails for another reason proves nothing about this one
**What happened.** On 2026-11-02 two bug drills on the rose passed. Making too much water
"help" was not caught: every too-full test poured on a rose that did not need water, so
it failed for that reason and never reached the too-full rule. Replaying the log in
stored order instead of time order was not caught either: the shuffle property rarely
built a log in which order changes the outcome.
**Why it bit.** A guard with two conditions is tested only when one input passes the
first and fails the second. A random property can pass a hundred runs and never build
the one case that matters.
**How to apply it.** For each condition, write the case where only it decides (from
0.39 a full can overshoots: needed, too full). For an order property, add one fixed
log where order changes the answer. Then put the bug back and watch the test fail.
## `toEqual` cannot see a field that is `undefined`
**What happened.** On 2026-11-02 a drill made the rose's export file carry an extra field
taken from the identity. The test passed: it kept no identity, so the field was
`undefined`; `toEqual` treats a missing property and an undefined one as equal, and
`JSON.stringify` drops it, so the text check saw nothing either.
**Why it bit.** "Nothing else in it" is a claim about absent fields, and the default
equality is blind to exactly the field that is absent by accident.
**How to apply it.** For "holds this and nothing else", use `toStrictEqual`, and give the
test something real to leak (here, a kept identity).
## A helper that searches for a moment can land on a boundary
**What happened.** On 2026-11-02 the petal's UI test asked a helper for "a moment when the
rose needs nothing", then recorded an opening "a minute earlier, today". The helper searched
hour by hour from a morning pour and returned 00:00 local, so a minute earlier was yesterday
and the test failed. The component was right; the first suspicion fell on it.
**Why it bit.** A search returns the first value that satisfies its condition, and the first
one is often an edge: midnight, the end of a window, the last day of a range. The test then
asserts something the edge makes false.
**How to apply it.** Bound every search to the region the test means (here: daytime on the
pour's own local day), and when a test built on a search fails, print what the search
returned before debugging the code under test.
## A constant the minifier folds cannot be found in the bundle
**What happened.** The first shell-build test (`apps/rosea-tauri/test/shell-build.test.ts`, 2026-11-17)
asserted that the string `"tauri"` was in the built JavaScript, to prove `__ROSEA_SHELL__` was
`'tauri'`. Vite replaces the constant, and the minifier then folds `'tauri' === 'tauri'` to `!0`:
the string is gone whether the build was right or wrong.
**Why it bit.** A test that reads build output tests every step of the build, the minifier included.
An assertion about a literal that only exists before minification is about the source, not the bundle.
**How to apply it.** Assert what the bundle must not contain (the constant's name, which would throw
in the webview), and prove the behaviour where it runs: here, the device check.
## A window after a match can read the neighbour's value
**What happened.** The first APK check looked for a WebView switch's value in the 300 characters
after its name in `aapt2 dump xmltree`. The next `<meta-data>` element starts within those 300
characters, so a switch turned the wrong way could pass on its neighbour's value.
**Why it bit.** A fixed window feels safe because the right answer is inside it; so is a wrong one.
**How to apply it.** Read structured output by its structure (here: the value attribute on the line
after the name attribute), and keep a test that flips exactly one value on real output and expects
that one name back.
## A dropped device link is not a failed test, and must not look like a pass either
**What happened.** On 2026-11-17 the USB link to the Android tablet dropped at least twice within the
hour (`device offline`; adb's transport id went 1 → 4 → 5), once between the two visits of the
garden check, which then failed on `am force-stop`.
**Why it bit.** A real device is a second machine with its own life. Retrying everything hides real
failures; retrying nothing makes the check a coin toss.
**How to apply it.** Retry only the failure that names the link (`offline`, `closed`, `no devices`),
after `adb wait-for-device`, a bounded number of times; find the device handle again after a drop;
let every other failure end the check at once. Report how many runs failed and why.
## jsdom has no `PointerEvent`, so a test pointer carries no coordinates
**What happened.** On 2026-11-17 the planet garden's gesture tests fired `pointerDown` and
`pointerMove` with `clientX`/`clientY`, and every gesture read as a tap at (0, 0). jsdom has no
`PointerEvent`; testing-library falls back to a plain `Event`, which drops the coordinates.
**Why it bit.** The test looked like a finger and measured nothing. A missing browser API in the
test environment does not fail loudly; it quietly hands the code a poorer event.
**How to apply it.** Before testing pointer gestures in jsdom, polyfill `PointerEvent` on
`MouseEvent` (`packages/ui/test/planet.test.ts`) and assert one coordinate-dependent outcome first
(a sideways swipe does *not* pull a weed). Check real fingers on the device (`pnpm test:android`).
## A device test's clean-up must cover its very first step
**What happened.** On 2026-11-17 `scripts/android-smoke.ts` connected to Chrome on the tablet
*before* its `try`. Chrome was still cold-starting, the connect failed with *socket hang up*, and
the `finally` that stops Chrome and removes the adb mappings never ran: Chrome stayed open on a
device the author asked to leave as it was found. It was stopped by hand.
**Why it bit.** Clean-up written for the failures you expect (a step fails) misses the one that
happens first (the setup fails). On a shared device, a leftover is somebody else's problem.
**How to apply it.** Open the `try` before the first command that changes the device; make the
clean-up tolerate half-done setup (`page?.close()`, `browser?.close()`); retry connections that
race a cold start (the script tries for 30 s). After every device run, check: `pidof` the app is
empty, `reverse --list` and `forward --list` are empty.
## When a hand-run probe passes and the check fails, the check is the bug
**What happened.** On 2026-11-17 the Android round trip (`e2e/apps/check-garden.ts`) failed three
times at *Bring her back from a file*, while a probe doing the same steps by hand passed. Two
differences, both in the check: it tapped the picker while the screen was still sliding in, and it
matched nodes by description too, so it tapped DocumentsUI's *Preview the file …* button, which
comes before the file's own name in the tree.
**Why it bit.** A check that drives someone else's screen (Android's picker) carries assumptions
about that screen. When it fails, the app is the first suspect, and the check's own guesses are not.
**How to apply it.** When a device check fails, do the steps once by hand with screenshots before
changing the app. On a system screen, match the exact visible text, and tap only a node that stands
still across two readings.