--- name: adding-tests-and-ci description: >- Where a new test belongs, how to write a browser E2E test that fails when the feature breaks, and how to scope CI jobs, steps, and workflow triggers so they run only when a change can affect them. Use when adding, moving, or writing a test (including Design E2E specs), adding or changing a CI job, step, or workflow, or when a job is slow or runs on unrelated changes. scope: dev metadata: internal: true --- # Adding Tests and CI ## Why this is a cost question Every agent-native workflow draws on one org-wide pool of GitHub-hosted runners. On the Team plan it held 60 concurrent jobs, and on 2026-09-29 the `Fast tests` gate waited a median 445 s just for a slot. BuilderIO moved to Enterprise Cloud in October 2026, which raises the pool to 500 jobs (50 macOS). Minutes are free for this OSS repo; slots are shared with every private repo. Scope still matters, because a job a change cannot affect delays other PRs once the pool fills, but parallelism that cuts the critical path is now worth a slot. `scripts/ci-change-scope.ts` is the one place that decides what a change runs. It classifies the changed paths into a full or targeted run and emits one output per check. `ci.yml` jobs gate on those outputs. A `.github/**` change, the lockfile, root config, or a `scripts/**` change other than a guard script or root script test forces a full run. ## Adding a test First decide whether the test should exist: `test-audit` owns that gate and its junk patterns. This section covers where it lives and what runs it. - **Put it in the workspace of the code it proves.** Targeted runs test the changed packages and their dependents (`...{packages/core}`), so a test in another package either runs on unrelated changes or never runs on the one that breaks it. - **Prove it at the cheapest tier that can fail for the bug:** a pure function, then a component or `.db.test.ts`, then browser E2E last. Some E2E suites, such as Design's, run only after merge and cannot fail a PR. Do not restate one assertion at several tiers. `templates/design/.agents/skills/design-editor-architecture/SKILL.md` covers failing-first proof and mutation audits. - **A new test joins an existing lane or job.** It never gets a job of its own. A changed root `scripts/*.test.ts`, or the test beside a changed guard script, runs in `Security guards` through the `script_tests` output. - **Mind what `vitest --changed` cannot see.** Targeted Core lanes run only the tests whose import graph reaches a changed file. A test that reads a file from disk (a skill, a fixture, a template) will not rerun when only that file changes. Import it, or teach `requiresFullCoreFastTests` in `scripts/ci-test-lanes.ts` about the path. - **Prefer an oracle to hand-picked expected values.** Sometimes a reference decides the right answer: a public spec through a library (RFC 5545 via `rrule`), the schema, or an older implementation. Then write a fast-check property against it. `templates/calendar/server/lib/series-recurrence.oracle.spec.ts` found three recurrence bugs that the example tests had passed over. Mark a confirmed bug `it.fails` with an `// oracle finding:` comment. Never loosen the property instead. - **A surviving mutant is a line no test can fail on.** `pnpm mutation:changed` runs Stryker on the lines this branch added in `packages/core/src`. CI runs it as `Mutation test changed Core lines`. It runs only specs named `.*` beside the mutated file (`poll.ts` runs `poll.spec.ts` and `poll.settings-scope.spec.ts`, not `poll-settings-scope.spec.ts`; a dash names a different module). A spec with the wrong prefix passes locally but counts as no coverage there. - Snapshot output is for reading; reviewers read its diff beside the code diff. - Never skip, disable, or quarantine a test to get green. `guard:no-oracle-loosening` fails a branch that adds `.skip`/`.only`, test retries, or `passWithNoTests`, or that removes more assertions from a test file than it adds. The exception is when an `oracle-change: ` comment says what behaviour changed. ## Writing a browser E2E test A browser test is the most expensive tier, so it has to fail when the feature breaks the way a user would see it. A test that only checks that something happened stays green while the result is wrong. - **Assert the exact end state, stated literally:** the saved parent, order, position, and size. A fired message, `toContain`, a file over N bytes, or a loose `closeTo` passes over the bug. - **Read persisted state, not only the DOM.** Wait for the save to settle, then read what the server stored. An edit that never saved must fail. - **Fail on collateral change.** Diff the saved document before and after, and allow only the nodes the step meant to touch. - **Fail on app errors by default, in a fixture:** console and page errors, app responses of 400 or more, error toasts, a save still in flight at teardown, and page loads the test did not start. A request that fails and is then retried still counts. - **Own the data.** Create a fresh record per test and delete it after the page leaves. Never mutate a shared seed or delete a record under an open editor. - **Drive the real input path at any zoom.** Click, drag, and type at positions computed from the app's own geometry; constant page-pixel offsets break when the default zoom changes. - **One flow per test, a guard before each step, no conditionals, no sleeps.** In a serial chain, one failure skips everything after it. - **Keep artifacts out of the watched source tree.** A file written there reloads the dev server mid-test; use `testInfo.outputPath`. - **Check intent before calling it a bug.** Read the source first; some saved attributes that look like leaks are deliberate metadata. A test that asserts correct behaviour and fails is a bug report: never loosen it to pass. Design's harness (`templates/design/e2e/rails.ts`) and its traps are in [references/design-e2e.md](references/design-e2e.md). ## Test workers Fast-test lanes run Vitest with `VITEST_CONCURRENCY=100%`, one worker per core. The shared config's 25% default is for laptops, and on CI's 4-vCPU runners it meant one worker: a core shard took 542 s at one worker and 201 s at four, on the same CPU time. On the 60-slot pool that let two full-width lanes replace five, but each lane then ran ~18 min of packages one after another. With 500 slots CI plans twelve lanes (`LANES` in `ci.yml`): each costs ~100 s of setup, and the planner balances lanes by test-file count. - **Large packages are sharded too.** `splitLargePackages` in `scripts/ci-test-lanes.ts` splits a package heavier than a fair lane share into Vitest `--shard`s, so Design no longer sets the floor for every lane. Only a `vitest` test script (optionally behind a `pnpm