Repository navigation
test(web): the unit tier the coverage number was missing - #201
Merged
Merged
Conversation
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #190 (merged).
Why
Coverage over
src/libwas 30.9% of lines, and the gap wasn't spread evenly — it was the security-critical half:bff/proxy.ts—requireOrgAccessbff/members.ts— the last-admin guardapi/client.tsdomain/{run-kinds,master-election,config-templates}requireOrgAccessis the entire tenant-isolation, RBAC and impersonation model in one function, and it had no unit test. This bringssrc/libto 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:
Real Postgres, not mocks
tests/unit/bff-members.test.tsruns 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
COUNTquery — 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 invitest.config.ts's include glob with@testing-library/reactinstalled — 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.tsnow registersafterEach(cleanup). testing-library only auto-registers that underglobals: true, which this project doesn't set — so without it everyrender()accumulated in one document andgetByTextstarted failing with "found multiple elements" in whichever test happened to run second.Also
The browser's fetch helper, both failure paths —
providers.tsxreadsBffRequestError.statusto decline retrying 401/403/404, and screens render recovery copy fromerror.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:
isBffErrornow usesObject.hasOwnrather thanin, which walked the prototype chain. Not a live bug — it only ever seesJSON.parseoutput — but "carries a code" is what a trust-boundary guard means to check.Verification
pnpm checkgreen: 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 issrc/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