# Testing doctrine — when to test, where, and what the framework covers Companion to [`change-placement.md`](change-placement.md) and [`coding-standards.md`](coding-standards.md). The commands are the `test:*` scripts in the root `package.json` (`pnpm run test` runs `test:ui`, `test:api`, `test:e2e` and `test:framework`; `test:regression:*` drives the stack suites). This is the *judgement layer*: which layer owns a test, what the framework already covers, and how to write a regression test that fits the setup. Write a test only for a seam **you own** that nothing else pins — the rest is *Unnecessary tests*. ## The layers, fast → slow | Layer | Where | Owns | |---|---|---| | **Unit** — no stack; PGlite where a database is needed | `packages/*/tests/unit`, colocated `ui/src/**/*.test.ts(x)` | pure logic, service behavior, reducers | | **Integration** — the real plugin router in-process, auth context injected by the harness (`api/tests/setup.ts`) | `api/tests`, `plugins/*/src/__tests__`, `packages/*/tests/integration`, `packages/everything-dev/tests/ui` | contract behavior: who may do what to which resource, error shapes, transactions | | **Regression** — the full composed `bos dev` stack, real browser, real remote auth UI | `tests/regression/browser/specs` | rendered journeys (sessions, SSR/CSR compose, navigation, admin flows) and anything jsdom cannot reproduce (cookie handling, hydration, loading states, module loading) | ## When each tier runs The matrix runs exactly where a decision is made — never everywhere. T2 is this repository's train: `regression-full.yml` is root-only and never synced to a generated app, whose `ci.yml` calls the framework's `.github/workflows/consumer-ci.yml` for lint, typecheck and test. | Tier | Where | What runs | |---|---|---| | **T0 local** | lefthook hooks (`lefthook.yml`, installed by `pnpm run prepare`) | pre-commit: Biome on staged files · pre-push: `typecheck` + `lint:effect` + `lint:comments` | | **T1 PR** | `.github/workflows/ci.yml` on pull requests | lint/typecheck, path-gated unit suites, ui-only app-load smoke; init scaffold matrix + boot gate (`ui-api` changes); slow stack suites skip | | **T2 gate** | the regression job of the deploy train (`.github/workflows/deploy.yml`), before its npm leg, on release legs only | unit + start regression (parallel planes — `tier: release`) | | **T2 nightly** | nightly on the main tip (`.github/workflows/backcompat-nightly.yml`) via `.github/workflows/regression-full.yml` (`tier: nightly`) | the full matrix: unit + dev + start + backcompat + framework, parallel planes. The backcompat plane was failing at boot in the Nov-08..12 nightlies (see the comment in `.github/workflows/regression-full.yml`); check a red nightly against that before treating it as a new regression | The gate proves the production artifact (start); the published-chain contract (backcompat) rides the nightly tier; the dev and framework planes pin the `bos dev` machinery, which app-code changes cannot break — so they canary nightly. The slow stack suites are `workflow_dispatch`-only triage jobs in `.github/workflows/ci.yml` and run for real only in T2 — a journey broken by a UI PR surfaces at the nightly or the release gate, not on the PR itself. ## Test shape — which layer asserts what A renderer swap deletes exactly one test layer; prefer the two that survive it ([ADR 0050](../adr/0050-renderer-agnostic-ui-readiness.md)): | Layer | Survives a renderer swap? | Why | |---|---|---| | Node tests of renderer-free logic (`ui/src/lib/queries/*.test.ts` is the model) | ✅ | no renderer in the test at all | | Behavioral browser specs (`data-testid` journeys) | ✅ | any DOM host renders the same testids | | jsdom component tests (`*.test.tsx`, `@testing-library/*`) | ❌ | bound to React rendering specifics | Choose the lowest layer that pins the behavior; a mid-layer jsdom test needs a stated React-specific reason (hook wiring, portal behavior). Extract the logic first: state transitions and derived computations live in renderer-free modules with zero React imports, and data access flows through `lib/queries/*` (ADR 0050 §3). ## Test value rules — property tests run on recorded seeds Every `fc.assert` in a CI-run path passes a pinned `seed` constant, so a given commit always tests the same cases (`packages/everything-dev/tests/unit/merge-properties.test.ts` is the model). A counterexample a seed uncovers becomes a pinned deterministic regression test in the same file — it survives every later run, never a seed bump. Exploratory random-seed runs are a deliberate local action; an intentional exception carries a `fc-random: allow` comment on the call site. The drift guard, `packages/everything-dev/tests/unit/property-seed-pin.test.ts`, scans `packages/*/tests` and `plugins/*/src/__tests__` and fails any `fc.assert` without a `seed`. ## Coverage — what the framework covers, and what an app covers itself The framework's suites live under `packages/*/tests` — `unit`, `integration`, and `packages/everything-dev/tests/ui` — and run with `pnpm run test:framework`. Here they are local; in a generated app they are not: npm packages carry no tests (nothing under `node_modules`), and the suites run against the release an app pins with `release:`. ### The born-healthy contract (init scaffold) Every gate that `bos init` claims for a newborn app lives in one ordered sequence; the scaffold matrix (`packages/everything-dev/tests/integration/init.matrix.test.ts`, `init.boot.test.ts`) is its only enforcer: 1. `bos init` exits 0 — snapshot, `bos.app.ts`, `bos.dev.ts`, `.env` exist; no stray lockfiles 2. `pnpm install` exits 0; nothing awaiting pnpm approval 3. `bos types gen` exits 0; generated types non-stub 4. `pnpm run typecheck` exits 0 per workspace 5. `bos build` exits 0, zero warnings (allowlist empty; entries need owner + issue) 6. `bos dev` boots: ui + api answer 200; boot log free of known failure classes 7. Browser: scaffolded routes render + hydrate, zero console errors, rendered anchors resolve 8. `bos upgrade --check` is well-formed (exit 0/1, resolved semver) The fixture (`packages/everything-dev/tests/helpers/child-fixture.ts`) drives the **real** `InitFlowService`; when init delivers something new, update the rows in the matrix, not a per-file copy. ### Assume covered by the framework - **The composition engine** — `constructTree`, its precedence, mount guards and client bootstrap (`packages/everything-dev/tests/ui/`, `packages/every-plugin/tests/unit/construct-precedence.test.ts`) and the runtime map's parity with the real engine (`packages/everything-dev/tests/unit/runtime-map.test.ts`) - **The client machinery** — `packages/every-plugin/tests/unit/remote-entry-retry.test.ts` (MF loader retry chain) and `packages/everything-dev/tests/ui/session-recovery.test.ts` (session recovery) - **The verification spine** ([ADR 0046](../adr/0046-verification-spine.md), `GLOSSARY.md` — "verification spine") — each suite pins one layer, so a regression has an address: | Spine layer | Suite (`packages/everything-dev/tests/unit`) | What it pins | |---|---|---| | bytes ↔ digest | `verify-engine.test.ts` | the `Verifier` engine: SRI fetches, `Verified` branding, the digest cache, the tagged errors | | config → manifest → files | `verify-chain.test.ts` | the walker's three depths and per-leg reporting (`pointer`/`entries`/`files`) | | config → manifest → files | `manifest-signature.test.ts` | attribution: signing, unregistered keys, rotation, tampered documents, every-depth failure | | composition identities | `digest-identity.test.ts` | pointer vs deployment digests and their documented relation | | served bytes in the MF pipeline | `federation-integrity.test.ts` | the loader verifies every registered byte at fetch time | | identity wiring | `boot-snapshot-schema.test.ts` · `composition-lockfile-schema.test.ts` | the stored-state planes recomputing `pointerDigest` | ### Cover in the app - plugin contracts and permission edges (`api/tests`, `plugins/*/src/__tests__`) - composed journeys — regression specs under `tests/regression/browser/specs` - generated artifacts staying honest: anything you generate gets a test that diffs it against the real engine ## Fit with the regression setup - **Each invocation may boot the whole stack** — `reuseExistingServer: true` (a stack already on the ports is reused), a 5-minute webServer budget (`BOS_WEBSERVER_TIMEOUT_MS`), `BOS_NO_WATCH=1`. One journey per spec that needs the stack is fine; five one-test spec files are five stack boots. Batch journeys by theme. - **The cache is real**: `globalSetup` seeds users/orgs once over HTTP and writes cookies to `.bos/regression/*.json`; specs `injectCookies` (`tests/regression/browser/helpers/seeded.ts`). Mint a new state only when the fixtures don't cover it, seeding through `tests/regression/lib/http-seed.mjs` with the bootstrap-admin API key (`AUTH_BOOTSTRAP_ADMIN_API_KEY`) — the stack database is in-process pglite, so direct DB writes are impossible. - **Serial by design**: `workers: 1`, `fullyParallel: false`, `retries: 1` — specs share ports and the seeded cookie cache. Don't assume a clean global state another spec breathed on; seed per-spec fixtures. - **The stall watchdog** (`tests/regression/browser/helpers/stall-watchdog.mjs`) fails once ~120 s pass with zero test progress: a spec that can hang waits explicitly (bounded `waitFor`, assertions with timeout). ## Writing a good regression test 1. Name it for the journey ("owner sees whether visitors see each event"), not the component. 2. Assert through the UI — rendered text, role selectors, real navigation — never internals the journey doesn't produce. 3. Seed only what differs from the fixtures; lean on the cached session otherwise. 4. Tolerant in styling/locale (class names and translations change), strict in data (ids, counts, membership, errors). 5. A screen another plugin or a remote source owns (`/login`, `/settings/*`) is still a valid journey — assert the outcome, not that owner's internals. 6. Run it once in isolation (`--grep`) and once in the batch; both green is the completion criterion — the serial runner is where shared-state surprises show up.