Skip to content

test(web): the unit tier the coverage number was missing - #201

Merged
athvin merged 1 commit into
mainfrom
feat/frontend-unit-tier
Oct 20, 2026
Merged

athvin merged 1 commit into
mainfrom
feat/frontend-unit-tier

Conversation

@athvin

@athvin athvin commented Oct 20, 2026

Copy link
Copy Markdown
Owner

Stacked on #190 (merged).

Why

Coverage over src/lib was 30.9% of lines, and the gap wasn't spread evenly — it was the security-critical half:

Module LOC Coverage before
bff/proxy.ts — requireOrgAccess 203 0%
bff/members.ts — the last-admin guard 349 0%
api/client.ts 77 0%
domain/{run-kinds,master-election,config-templates} 110 0%

requireOrgAccess is the entire tenant-isolation, RBAC and impersonation model in one function, and it had no unit test. This brings src/lib to 49.3% of lines by testing those, not by testing what was easy.

The centrepiece

tests/unit/bff-access.test.ts — three held roles × three required minimums, crossed with membership, super-admin-without-membership, and active impersonation. The Playwright tier drives the happy paths through real screens but can't enumerate this, and the interesting cases are the denials — specifically which denial, since a 403 where a 404 belongs tells a stranger an org exists.

Verified by mutation, not by passing:

Mutation Result
non-member 404 → 403 (isolation leak) 4 cases fail
delete the impersonation org confinement — an impersonating super admin could reach every other tenant exactly the case written for it fails

Real Postgres, not mocks

tests/unit/bff-members.test.ts runs against PGlite (in-process WASM Postgres). db() already supports it and the Playwright tier relies on the same path, so this needed no new infrastructure.

It matters here because the last-admin guard is a COUNT query — mocking drizzle would test the mock. Removing the guard's org predicate, so it counts admins across every workspace, fails the cross-org case.

Covers: the last-admin guard (demote and remove, both directions, org-scoped), single-use invites, single-use password resets, and that neither invites nor resets store the token itself — only its hash, so a leaked table isn't a set of usable links.

The component tier now exists

tests/component/ has been in vitest.config.ts's include glob with @testing-library/react installed — and the directory didn't exist. Its first subject is the evidence waterfall, whose bar width, side and sign all derive from the blob.

One trap found and closed: tests/setup.ts now registers afterEach(cleanup). testing-library only auto-registers that under globals: true, which this project doesn't set — so without it every render() accumulated in one document and getByText started failing with "found multiple elements" in whichever test happened to run second.

Also

The browser's fetch helper, both failure paths — providers.tsx reads BffRequestError.status to decline retrying 401/403/404, and screens render recovery copy from error.error.code, so a non-enveloped failure means an infinite retry or a blank error state. And the closed vocabularies the UI shares with the engine, where the risk is drift, so the tests pin exact members rather than shapes.

One src change: isBffError now uses Object.hasOwn rather than in, which walked the prototype chain. Not a live bug — it only ever sees JSON.parse output — but "carries a code" is what a trust-boundary guard means to check.

Verification

pnpm check green: 272 tests across 18 files, up from 62 before this program. Coverage: lines 30.9% → 49.3%, branches 38.6% → 50.1%, functions 18.3% → 34.9%.

Thresholds are not ratcheted here — that lands with the PR-path coverage gate, so the floor moves once, against a settled number.

Note on the 70% I estimated

The plan floated "roughly 70%+ lines". 49.3% is the honest number, and the reason is structural: ~1,300 of src/lib's LOC is src/lib/query/** — TanStack Query hooks that need a QueryClient and a backend to say anything, and which the Playwright functional tier already owns. Pushing past ~50% means either duplicating that tier in jsdom or widening the coverage scope, and neither buys real confidence. I'd rather report 49.3% and say why than inflate it.

🤖 Generated with Claude Code

Coverage over src/lib was 30.9% of lines, and the gap was not spread
evenly — it was the security-critical half. requireOrgAccess, the whole
tenant-isolation/RBAC/impersonation model in one function, had no unit
test at all. So did bff/members.ts, the largest module in src/lib, which
owns the last-admin guard. This brings src/lib to 49.3% of lines by
testing those, not by testing what was easy.

tests/unit/bff-access.test.ts is the centrepiece: three held roles x
three required minimums, crossed with membership, super-admin-without-
membership, and active impersonation. The Playwright tier drives the
happy paths through real screens but cannot enumerate this, and the
interesting cases are the denials — in particular WHICH denial, since a
403 where a 404 belongs tells a stranger that an org exists.

Verified by mutation rather than by passing: changing the non-member 404
to a 403 fails 4 cases, and deleting the impersonation org confinement
(which would let an impersonating super admin reach every other tenant)
fails exactly the case written for it.

tests/unit/bff-members.test.ts runs against PGlite — in-process WASM
Postgres — not mocks. db() already supports it and the Playwright tier
relies on the same path, so this needs no new infrastructure. It matters
here because the last-admin guard is a COUNT query: mocking drizzle
would test the mock. Removing the guard's org predicate, so it counts
admins across every workspace, fails the cross-org case.

tests/component/ is created, having been in vitest.config.ts's include
glob with @testing-library/react installed and the directory absent. Its
first subject is the evidence waterfall, whose bar width, side and sign
all derive from the blob. Note that tests/setup.ts now registers
`afterEach(cleanup)`: testing-library only auto-registers that under
`globals: true`, which this project does not set, so without it every
render accumulated in one document and getByText began failing with
"found multiple elements" in whichever test ran second.

Also here: the browser's fetch helper (both failure paths, since
providers.tsx reads BffRequestError.status to decline retrying 401/403/
404, and screens render recovery copy from error.error.code), and the
closed vocabularies the UI shares with the engine — where the risk is
drift, so the tests pin exact members rather than shapes.

One src change: isBffError now uses Object.hasOwn rather than `in`,
which walked the prototype chain. Not a live bug — it only ever sees
JSON.parse output — but "carries a code" is what a trust-boundary guard
means to check.

272 tests, up from 62 before this program.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@athvin
athvin merged commit 2af2d6b into main Oct 20, 2026
21 of 22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants