docs(p4): testing.md rules: a hold and until across slow work, the BUILDS guard and the gates' heavy files, click what is drawn for good (ERNDL-1516)

This commit is contained in:
Mario Zechner 2026-12-04 03:32:39 +00:00
parent b5117fe6f4
commit e803a270a1

View file

@ -63,10 +63,22 @@ A things module's test imports `@earendil-works/p4/v1` first.
1. **One case per rule of the plan, named after the rule.** The file header lists what it pins; one `describe` per
mechanism; each case sets up in `beforeEach` (a server per case), never in an earlier case.
2. **Wait on the effect.** Use `until` for a change made elsewhere, `eventually` only for what no watchable shows. No
sleeps, no frame counts.
sleeps, no frame counts. **Click what is drawn for good:** `click` aims at the element's box, then clicks; a row or
line drawn later above it (a list's late rows, a loading line's final text, a menu completing) moves it, and the
click lands on whatever is there. First wait for what the page still draws around the target (`formDrawn`,
`plugins/sessions/test/web.test.ts:37`).
3. **No deadlines.** A wait has no timeout of its own: on a loaded machine a correct wait takes many times as long.
The case timeout (`--test-timeout=120000`) is the one hang guard, and a timed-out case prints the waits it began.
A timed rule (a debounce, an idle stop) paces its steps by what the code did; elapsed time is only a lower bound.
- A client's `get` waits the host's default 60 s: across slow work (an apply, a runner reload, a worker's start
under load) use `client.hold(…)` plus `until(ref.state, (s) => s.kind === "live", within(…))`
(`plugins/runner/test/runner-e2e.test.ts:162-172`).
- A case whose work is builds (a launched copy of p4, an apply building plugins), measured to pass in seconds alone
and to need over 120 s at the gates' load, runs as `it(name, BUILDS, fn)` with `const BUILDS = { timeout:
600_000 }` and a comment giving the measured times; its socket asks use `BUILDS.timeout`. Its file goes into
`/home/badlogic/rework-notes/gates-heavy-files.txt`, which the gates run apart, two at once
(`runner-e2e.test.ts:42`; [ERNDL-1516 p4 tests that fail under the gates' load, round
2](https://linear.app/earendil/issue/ERNDL-1516)).
4. **Access.** Every object refuses whom it does not serve (`refused(p, "forbidden")`): each tick alone, the owner,
the agent, an outsider, and a grant changed while held.
5. **Reload** every facet you touch while in use (a list open, a worker held): nothing lost, no error.
@ -86,9 +98,8 @@ A things module's test imports `@earendil-works/p4/v1` first.
its own copy of p4, so several run side by side.
- The runner applies it in the copy (`find` exactly once) and runs `tests` (type-checks them for `types: true`);
one must fail and print `fails`, the rule's own assertion message (`"the workspace's Settings shows its
General"`). Without it, a page that never loads counts as caught; a failure elsewhere counts as `ELSEWHERE`. A `fails` text the passing run prints already is refused.
- **A case timeout:** each case times out at two minutes, as in `npm test`, so a mutant that hangs a wait fails
its case and the file prints the rule's failure before the check's kill at five (`test/mutate.ts`).
General"`); a failure elsewhere counts as `ELSEWHERE`, not caught. A `fails` text the passing run prints is refused.
- Each case times out at two minutes, as in `npm test`: a mutant that hangs a wait prints its failure.
## A kind's tests
@ -118,8 +129,8 @@ afterEach(() => n.close());
held as the session's worker; run tools with `runTool`. A regrant doesn't build it again: call `giving` again to
assert the tools it offers then. For a tool offered and then taken back, wait on `fn`'s third argument (`held[i].ref`)
until the member is gone, then call (`plugins/notes/test/worker.test.ts:146-158`). A prompt section: call its
`render` ([harness.md](harness.md) "Prompt sections"). The part in a real conversation with code mode: `giving`,
then `extensionOf` (`harness/agent-parts.ts`) installed in Durable (`plugins/harness/test/codemode.test.ts:649-721`;
`render` ([harness.md](harness.md)). In a real conversation with code mode: `giving`, then `extensionOf`
(`harness/agent-parts.ts`) in Durable (`plugins/harness/test/codemode.test.ts:649-721`;
its helpers `durableWith`, `askDurable`, `scriptResult` are private to that file: copy them). `harnessWorld` covers
the runtime (the part called again as the holding changes, a reload, a restart) with stand-in `HeldThing`s only,
so a section that reads a reference's values can't render there.
@ -131,21 +142,20 @@ afterEach(() => n.close());
(`"<store>:<table>:<index>"`, or `undefined` for the key; `first` for a keyed read, `pages` for a paged one). Model:
`plugins/sessions/test/plans.test.ts:118-127`; the notes queries: `plugins/things/test/plans.test.ts:130-132`.
- **The store alone** with a mocked clock (`plugins/notes/test/store.test.ts`); messages with `checkCatalog`
(`plugins/things/test/catalog.ts`); the view in a browser ("UI tests").
(`plugins/things/test/catalog.ts`).
## UI tests
- **The driver** (`Browser`, `src/api/v1/browser.ts`): `wait(expression)`, `click(selector)` (waits until enabled,
visible and on top), `type(selector, text)` (inserts text, no key events), `settled`, `evaluate`, `back`/`history()`;
it keeps `console`, `errors`. `back` returns before the navigation lands: wait with `at`. A `wait` expression
that throws counts as "not yet": a typo hangs until the timeout.
that throws counts as "not yet".
- **Every look:** `PHONE`, `DESKTOP`, light and dark; each case ends with `page.browser.errors` and
`page.server.errors` empty, and closes the page in `finally`. `onEveryPage` runs a component on its probe without the chrome;
`page.server.errors` empty. `onEveryPage` runs a component on its probe without the chrome;
an app loops over `LOOKS` (`page-world.ts:387`) with `openPage`.
- **Visibility, not existence.** `querySelector(...) !== null` passes on a page rendered hidden or blank, and
`x?.closest(...) !== null` passes when `x` is missing. Wait for the element itself, then assert
`checkVisibility({ visibilityProperty: true, opacityProperty: true })`: `click` and `settled` hit-test, but don't
check an ancestor's opacity.
`checkVisibility({ visibilityProperty: true, opacityProperty: true })` (`click` and `settled` only hit-test).
- **Exact history steps.** `recordHistory(page)` before `page.open`; take the baseline (`historyMoves(page)`) once
the page shows; assert each step's new calls exactly, never a count of entries (`plugins/chrome/test/history-moves.test.ts:21-118`;
its `moved` helper is local: copy it):
@ -162,18 +172,16 @@ afterEach(() => n.close());
To prove nothing more moved, `evaluate(TRACK_TRAVERSALS)`, then `evaluate(TRAVERSALS_SETTLED)`
(`test/held-traversals.ts`), never a count of frames.
- **What the page sends:** after `recordFrames(page)`, `sentHolds` (id, contract, params) and `sentFrames` (holds,
releases, calls, follows). A `follow` frame after its hold means the view read a member
its hold didn't name: one more wave. `gateFrames`/`releaseFrames` keep a hold loading as long as the case needs.
releases, calls, follows; a `follow` after its hold is one more wave). `gateFrames`/`releaseFrames` keep a hold loading as long as the case needs.
`recordAttribute` catches every value an attribute takes, also before paint.
- **The first wave** (the base's waves counter is local to `test/open-request-waves.test.ts`): open the workspace's
- **The first wave:** open the workspace's
`/settings` (on a phone, click `[data-things-section-toggle="what"]`), `gateFrames(page, "")` to hold back every
frame, click the row (`[data-things-open="<id>"]`), then `eventually(() => heldFrames(page), …)` until it holds
`things.locate` and the thing's hold, sent before any answer (`gatedFrames` returns too early);
`releaseFrames(page)`; assert `sentFrames` has no `follow` for those hold ids.
release; assert `sentFrames` has no `follow` for those hold ids.
- **An app from the switcher:** `openPage` with `plugins: []` for a built-in app, its rows made in `as(server, ctx)`;
open `/me`; on a desktop click `[data-switcher]` (`[data-switcher-popup]`), on a phone `[data-place]`
(`[data-places-sheet]`); then `[data-switcher-root="<id>"]` (also `-app`, `-home`, `-pi`); assert a visible element
of what opened.
open `/me`; click `[data-switcher]` (desktop) or `[data-place]` (phone), then `[data-switcher-root="<id>"]` (also
`-app`, `-home`, `-pi`); assert what opened is visible.
- **Switching sessions in one document:** a second `page.open` reloads and can't catch state kept across a switch.
Go with `history.pushState({ p4entry: crypto.randomUUID() }, "", path)` in `evaluate`
(`plugins/transcript/test/session-view.test.ts:180-188`), or `test/plugins/page-opener` (`openTarget`); entries at
@ -207,18 +215,13 @@ const ref = await ctx.get(NotesKind.contract, params, { readyTimeout: 500 });
const ref = await ctx.get(NotesKind.contract, params);
```
**A click without real input, or an assertion right after Back.** `element.click()` "works" on a hidden or covered
button.
**A click without real input** "works" on a hidden or covered button.
```ts
// wrong
await browser.evaluate(`document.querySelector("[data-tab-close]").click()`);
await browser.back();
assert.equal(await browser.evaluate("location.pathname"), start);
// right
await browser.click("[data-tab-close]"); // visible, on top, real mouse events
await browser.back();
await at(page, start); // test-app-page.ts: waits for the address
// right: visible, on top, real mouse events
await browser.click("[data-tab-close]");
```
## Reference code
@ -245,7 +248,8 @@ await at(page, start); // test-app-page.ts: waits for the address
- `plugins/notes/test/notes-view.test.ts:26-83`: the page API stubbed in a `vm`; a stand-in, not the page.
- `plugins/coding/test/server.test.ts:28-40`: asserts on the things store's SQLite tables, not its API.
- `plugins/transcript/test/transcript-view.test.ts:127-185`: one world shared by about 40 cases in `before`.
- `plugins/coding/test/mutations.ts` (and sessions', providers'): no `fails`.
- `plugins/coding/test/mutations.ts` (and sessions', providers'): no `fails`, so a page that never loads counts as
caught.
## Performance and benches
@ -263,32 +267,29 @@ worker reads about twice as much, so only the daily run's release bench checks i
### Rules
- No work on a hot path that nobody asked for: no walks, copies, compilations or allocations per call.
- Plugins load whole at start: keep worker facets small, and heavy, rarely used code on the server. A heavy library
in a worker facet needs its cost in the plan and Mario's yes. Two plugins inlining one library load it twice: share
it through a contract or a registry.
- Keep worker facets small; heavy, rarely used code goes on the server. A heavy worker library needs its cost in the
plan and Mario's yes. Two plugins inlining one library load it twice: share it through a contract.
- Open natives (connections, sandboxes) on first use, one per worker.
- No new dependency, flag or process without its expected cost in the plan.
- No `--enable-source-maps` in workers (the whole map parsed at the first stack trace).
**Readings:** memory as settled USS (`settled`, `bench/support/memory.ts`), never right after the work; heap with
no-op telemetry after GC (the in-memory recorder keeps every span: a false leak); time as medians, cold run apart.
**Readings:** settled USS (`settled`, `bench/support/memory.ts`); heap with no-op telemetry (the in-memory recorder
keeps every span: a false leak).
**A kind's targets** (design targets its issue reports against the daily run): heap per held thing at most 2 KiB over Chord's own
(200 holders, after GC, against the things plugin's bare test kind); its worker facet idle under 0.1 MiB; a change
reaching the last of 100 holders under 10 ms; a thing's first page one index read.
**A kind's design targets**, reported in its issue: heap per held thing at most 2 KiB over Chord's own (200 holders,
after GC, against the things plugin's bare test kind); the worker facet idle under 0.1 MiB; a change reaching the last
of 100 holders under 10 ms; a first page one index read.
### Benches
A bench is `apps/p4/bench/<name>.ts` (helpers in `bench/support/`, its plugins in `bench/plugins/`), with a quick test
in `bench/test/` pinning its metrics. Model it on `bench/skeleton.ts` and record through `bench/support/result.ts`
(`time`, `memory`, `allocated`, `write`; `runs(20, 10)` for full and quick).
A bench is `apps/p4/bench/<name>.ts`, with a quick test in `bench/test/` pinning its metrics. Model it on
`bench/skeleton.ts`; record through `bench/support/result.ts`.
- **While building, reviewing and gating, benches only compile.** `npm run check` type-checks `bench/**`; a change
that breaks a bench fixes it to compile. No bench, `npm run test:bench` or `bench:compare` runs then.
- **The daily run** (`/home/badlogic/rework-notes/daily-bench.sh`, 02:00 UTC on origin/main): the suite serially with
browser tests, `test:bench`, every bench through `heavy.sh bench`, the release bench, into
`/home/badlogic/rework-notes/bench-daily/<date>/` (`summary.txt`, a log per bench). A regression is chased from
there and fixed in an item of its own.
`/home/badlogic/rework-notes/bench-daily/<date>/`. A regression is chased from there, in an item of its own.
- **Chasing it:** `bench:compare`, an A/B of a ref against your checkout (exit 0 clean, 1 regressed, 2 undecided):
`--prepare` under `heavy.sh test`, then the compare under `heavy.sh bench`, the only numbers that count.