Skip to content

test(web): run page tests on the real app router - #343

Closed
BenSheridanEdwards wants to merge 3 commits into
feat/todo-example-appfrom
test/page-tests-real-router
Closed

BenSheridanEdwards wants to merge 3 commits into
feat/todo-example-appfrom
test/page-tests-real-router

Conversation

@BenSheridanEdwards

@BenSheridanEdwards BenSheridanEdwards commented Nov 8, 2026 •

Copy link
Copy Markdown
Owner

Why does this feature exist?

  • In six of nine agent rebuilds, TodosPage.test.tsx carried a 30 to 40 line in-memory api that re-coded replace, filter and clearDone (SLOP finding R2), and in two the fake invented a blank to-do for a missing id (B2). The reference did the same. Those tests checked the fake, not the feature: a broken procedure, use case or domain rule could still pass them.
  • Issue test: page tests run the real feature router instead of a hand-written fake api #339 asks for a documented helper that serves the feature's real tRPC router with an in-memory store to the page under test, so agents have no reason to write a fake.

Stacked PR: this is based on feat/todo-example-app (#335) and merges after #335. Its diff is against #335's head f2ff9e6, which includes main (#340) and the final-check fixes (the addTodo double-add guard, the filter-colour and re-read E2E tests, trial ports).

Closes #339

What changed?

This is one idea above 400 lines: making TestApiProvider run the real router needs the api to build a router per test, the provider and its handlers to serve it, the page tests to drop their fake, and the guidance to say so; none of them works without the others.

  • The api builds a router per call (0b9e75d): createTodoUseCases() makes a new in-memory store and returns the use cases over it; createTodosRouter() and createAppRouter() build routers the same way. The server still builds appRouter once, so api behaviour is unchanged. Application tests call the factory instead of jest.resetModules, and a new router test proves two app routers keep separate lists. Public API change: apps/api/etc/api.api.md gains createAppRouter; features-todos.api.md exports createTodosRouter in place of todosRouter. The api-client export names are unchanged.
  • TestApiProvider serves the real app router (a66b596): each render gets a new app router over empty stores, run in memory through tRPC's unstable_localLink, so a page test runs the real procedures, input schemas, application and domain code, and outputs cross a JSON round trip as they would over HTTP. seed adds data through the router before the page's first call is answered. A handler in handlers still answers its procedure, so failure injection stays: it can fail, hold or record one call, and its new second argument serve(input) passes the call on to the router.
  • How the router reaches the test without loosening a gate: web code imports the api by type only (Fallow, the types-only @repo/api export, contract/*), and route tests may import only their folder, @repo/ui and the api-client barrel. So the web Jest project gains one setup file, tooling/jest/serve-app-router.ts, which finds the result under test and hands its createAppRouter to that result's api client (serveAppRouterInTests in apps/web/src/api-client/testAppRouter.ts). Tooling sits outside the role table. No boundary, contract rule, route co-location rule, budget or guarded file changes; the branch adds no web production code, and size-limit read 108.69 kB before and after on a26be0c.
  • The to-do page tests drop the fake api and the restated Todo interface (P3). They seed through the router and keep every failure case (todos.list, add, setDone, rename, delete, clearDone failing), the held list and held setDone, and recorded add, list, rename and clearDone calls, now passed through to the router. Same test declarations as the base (11).
  • Guidance (4ea68a9): the write-unit-tests skill (.agents and .claude copies identical, via pnpm skills:sync), .agents/templates/component.test.tsx.template, ARCHITECTURE.md (api file roles, mounting, how page tests reach the router) and TECH_STACK.md point to the router-backed provider and say to write no fake api.
  • New configuration: jest.config.ts adds the setup file to the web project only; tsconfig.node.json typechecks it.

Behavioural Proof (with video and screenshots)

  • Video: Not applicable: test infrastructure only; the page and the api behave exactly as before.
  • Screenshots: Not applicable: no user-visible change, and the production bundle size is unchanged at 108.69 kB.
  • Behaviour tests: apps/web/src/api-client/TestApiProvider.test.tsx (real router answers, seed first, a new empty router per render, a handler passing a call on, a failed seed failing each call), testApiHandlers.test.ts, testAppRouter.test.ts (fresh router per call; names the Jest setup when none is served), apps/api/src/router.test.ts and application.test.ts (separate lists), and the rewritten apps/web/src/routes/todos/TodosPage.test.tsx. Each new test failed first (api: 9 failed before the factories existed; api-client: 4 provider tests and 2 suites failed before the router-backed provider).
  • E2E mapping: unchanged; all 45 Playwright tests, including the 40 in todos.spec.ts, pass in the pre-push hook at 4ea68a9.

Verification Summary

  • Head commit: 4ea68a9
  • PRDs checked: docs/prd/todos.md
  • PRD impact: No behaviour change: the api factories keep the server's single in-memory list, and the other changes are tests, test helpers and docs.
  • North Star commit: a3c434dcf47d2373db530d3e206bd6a15e8b76eb
  • Commands run (e.g. pnpm verify, pnpm e2e): pnpm install --frozen-lockfile, pnpm test:coverage on the base and on this branch, pnpm lint:anti-slop, the real pre-commit hook on each commit, and the real pre-push hook (pnpm verify then pnpm e2e) on the push of 4ea68a9, with PACT_DIR in a private scratch folder.
  • Results: pre-push at 4ea68a9: pnpm verify passed (81 suites, 3118 Jest tests, contract, integration, changed-file coverage, build, size: JS 108.72 kB of 110 kB, CSS 2.65 kB of 5 kB) and pnpm e2e passed 45 of 45. The branch was rebased three times as feat: replace the greeting with a to-do reference app #335 moved (to d10b37f, c19faad, then f2ff9e6); pnpm verify and pnpm e2e passed at each step. The last rebase had no conflicts: feat: replace the greeting with a to-do reference app #335's new useTodos guard and E2E tests touch no file this branch changes, and the router-backed page tests pass against the guard. Coverage before (base a26be0c, 2878 tests) and after (this branch on a26be0c, 2888 tests; again in the pre-push at 4ea68a9): 100% statements, branches, functions and lines for all files, and 100% in apps/api/src/features/todos, apps/web/src/api-client and apps/web/src/routes/todos. TodosPage.test.tsx now takes about 9 s instead of 5 s, because it runs the real router and Effect schemas.
  • Definition of Done (.agents/project/PR_QUALITY.md) satisfied, or each gap named: satisfied locally; hosted checks run on this head.
  • pnpm verify
  • pnpm e2e
  • Behaviour is covered by the E2E map when user-visible behaviour changed.
  • Logic is unit-tested where logic changed.
  • Accessibility considered and verified for interactive/UI changes.
  • The diff follows .agents/project/ARCHITECTURE.md and CONVENTIONS.md; new decisions recorded as ADRs.
  • No gates were bypassed or weakened.
  • Known risks or skipped checks (with reason, risk, and rollback):
    • No ADR: no constraint changes. The setup file is the one indirection a reviewer should judge: it keeps serveAppRouterInTests state in a module-level Map inside the api client, which only Jest calls. The alternatives each needed a gate change: a boundary allowance for a web value import of the api, which would also put api code in the dev server's module graph, or a second import path for route tests, which needs oxlint.config.ts and an AGENTS.md change.
    • TestApiHandlers handlers now receive a second argument, serve. Existing one-argument handlers still type-check.
    • A recorded showcase result copies the api client, so its page tests get its own router from the same setup; its api must export createAppRouter, which the scaffold-exports check now requires because the reference does.
    • The local run used Node 26.12.1 while .nvmrc pins 22; CI runs the pinned version.
    • Rollback: revert the three commits; the fake-api tests return with them.

PRD Behaviours to Review

  • docs/prd/todos.md: Shows a loading message until the list arrives. Evidence: todos.spec.ts test "shows a loading message until the list arrives" passes in the pre-push hook at 4ea68a9 (45 passed); this PR changes no page or api behaviour.
  • docs/prd/todos.md: Opens on the All filter, with an empty message and a disabled Clear done when there are no to-dos. Evidence: todos.spec.ts test "starts on the All filter with an empty message when there are no to-dos"; todos.spec.ts test "returns to the All filter when the page reloads" passes in the pre-push hook at 4ea68a9 (45 passed); this PR changes no page or api behaviour.
  • docs/prd/todos.md: Says so when the list fails to load. Evidence: todos.spec.ts test "says so when the list fails to load" passes in the pre-push hook at 4ea68a9 (45 passed); this PR changes no page or api behaviour.
  • docs/prd/todos.md: Shows only the parts listed above. Evidence: todos.spec.ts test "shows only the parts the PRD lists"; todos.spec.ts test "shows a title message and a failure where the PRD puts them" passes in the pre-push hook at 4ea68a9 (45 passed); this PR changes no page or api behaviour.
  • docs/prd/todos.md: Adds a to-do to the end of the list. Evidence: todos.spec.ts test "adds a to-do to the end of the list" passes in the pre-push hook at 4ea68a9 (45 passed); this PR changes no page or api behaviour.
  • docs/prd/todos.md: Stores and shows each title normalised. Evidence: todos.spec.ts test "stores and shows a title with its whitespace normalised" passes in the pre-push hook at 4ea68a9 (45 passed); this PR changes no page or api behaviour.
  • docs/prd/todos.md: The api normalises every title it receives. Evidence: todos.spec.ts test "the api normalises a title from any client" passes in the pre-push hook at 4ea68a9 (45 passed); this PR changes no page or api behaviour.
  • docs/prd/todos.md: Rejects a blank or over-long title and sends nothing. Evidence: todos.spec.ts test "asks for a title and sends nothing when the title is blank"; todos.spec.ts test "rejects a title over 100 characters and sends nothing" passes in the pre-push hook at 4ea68a9 (45 passed); this PR changes no page or api behaviour.
  • docs/prd/todos.md: Clears the title message once the title is fixed. Evidence: todos.spec.ts test "clears the title message once the title is fixed" passes in the pre-push hook at 4ea68a9 (45 passed); this PR changes no page or api behaviour.
  • docs/prd/todos.md: Accepts a title of exactly 100 characters, counted after normalising. Evidence: todos.spec.ts test "accepts a title of exactly 100 characters after normalising" passes in the pre-push hook at 4ea68a9 (45 passed); this PR changes no page or api behaviour.
  • docs/prd/todos.md: The api rejects an invalid title as a bad request. Evidence: todos.spec.ts test "the api rejects a blank or over-long title as a bad request" passes in the pre-push hook at 4ea68a9 (45 passed); this PR changes no page or api behaviour.
  • docs/prd/todos.md: The api answers each procedure as The api table says. Evidence: todos.spec.ts test "the api lists to-dos oldest first"; todos.spec.ts test "the api answers each change with its result"; todos.spec.ts test "the api answers not found for an unknown to-do" passes in the pre-push hook at 4ea68a9 (45 passed); this PR changes no page or api behaviour.
  • docs/prd/todos.md: Shows an add only once the api has answered and the list is re-read. Evidence: todos.spec.ts test "shows an add only once the api has answered and the list is re-read" passes in the pre-push hook at 4ea68a9 (45 passed); this PR changes no page or api behaviour.
  • docs/prd/todos.md: Pressing Add or Enter again while an add waits sends nothing. Evidence: todos.spec.ts test "stores one to-do when Add is pressed twice quickly" passes in the pre-push hook at 4ea68a9 (45 passed); this PR changes no page or api behaviour.
  • docs/prd/todos.md: Keeps the typed title and says so when adding fails. Evidence: todos.spec.ts test "keeps the typed title and says so when adding fails" passes in the pre-push hook at 4ea68a9 (45 passed); this PR changes no page or api behaviour.
  • docs/prd/todos.md: Marks a to-do done and active again, in place. Evidence: todos.spec.ts test "marks a to-do done and active again in place" passes in the pre-push hook at 4ea68a9 (45 passed); this PR changes no page or api behaviour.
  • docs/prd/todos.md: Disables every change control while a change waits. Evidence: todos.spec.ts test "disables every change control while a change waits" passes in the pre-push hook at 4ea68a9 (45 passed); this PR changes no page or api behaviour.
  • docs/prd/todos.md: Leaves the to-do unchanged and says so when marking it fails. Evidence: todos.spec.ts test "leaves the to-do unchanged and says so when marking it fails" passes in the pre-push hook at 4ea68a9 (45 passed); this PR changes no page or api behaviour.
  • docs/prd/todos.md: Edits a title in place, normalised. Evidence: todos.spec.ts test "edits a title in place and normalises it" passes in the pre-push hook at 4ea68a9 (45 passed); this PR changes no page or api behaviour.
  • docs/prd/todos.md: Cancels an edit without saving. Evidence: todos.spec.ts test "cancels an edit without saving" passes in the pre-push hook at 4ea68a9 (45 passed); this PR changes no page or api behaviour.
  • docs/prd/todos.md: Checks an edited title like a new one. Evidence: todos.spec.ts test "checks an edited title like a new one" passes in the pre-push hook at 4ea68a9 (45 passed); this PR changes no page or api behaviour.
  • docs/prd/todos.md: Edits one to-do at a time. Evidence: todos.spec.ts test "edits one to-do at a time" passes in the pre-push hook at 4ea68a9 (45 passed); this PR changes no page or api behaviour.
  • docs/prd/todos.md: Keeps the edit open and says so when saving fails. Evidence: todos.spec.ts test "keeps the edit open with the typed title and says so when saving fails" passes in the pre-push hook at 4ea68a9 (45 passed); this PR changes no page or api behaviour.
  • docs/prd/todos.md: Deletes a to-do at once. Evidence: todos.spec.ts test "deletes a to-do at once" passes in the pre-push hook at 4ea68a9 (45 passed); this PR changes no page or api behaviour.
  • docs/prd/todos.md: Keeps the to-do and says so when deleting fails. Evidence: todos.spec.ts test "keeps the to-do and says so when deleting fails" passes in the pre-push hook at 4ea68a9 (45 passed); this PR changes no page or api behaviour.
  • docs/prd/todos.md: Filters to active or done to-dos and back to all, and shows which filter is selected. Evidence: todos.spec.ts test "filters to active or done to-dos and back to all"; todos.spec.ts test "shows the selected filter in the focus colour"; todos.spec.ts test "hides a to-do from the Active filter once it is marked done"; todos.spec.ts test "shows the empty message when a filter matches nothing" passes in the pre-push hook at 4ea68a9 (45 passed); this PR changes no page or api behaviour.
  • docs/prd/todos.md: Clears every done to-do, whatever the filter, and only while one is done. Evidence: todos.spec.ts test "clears every done to-do whatever the filter"; todos.spec.ts test "enables Clear done only while a to-do is done" passes in the pre-push hook at 4ea68a9 (45 passed); this PR changes no page or api behaviour.
  • docs/prd/todos.md: Keeps the done to-dos and says so when clearing fails. Evidence: todos.spec.ts test "keeps the done to-dos and says so when clearing fails" passes in the pre-push hook at 4ea68a9 (45 passed); this PR changes no page or api behaviour.
  • docs/prd/todos.md: Shows only the latest failure, until the next change starts. Evidence: todos.spec.ts test "shows only the latest failure and clears it when the next change starts" passes in the pre-push hook at 4ea68a9 (45 passed); this PR changes no page or api behaviour.
  • docs/prd/todos.md: Says so when the list fails to load after a change. Evidence: todos.spec.ts test "says so when the list fails to load after a change" passes in the pre-push hook at 4ea68a9 (45 passed); this PR changes no page or api behaviour.
  • docs/prd/todos.md: Reads the list when the page opens and after each successful change. Evidence: todos.spec.ts test "reads the list only when the page opens and after a successful change" passes in the pre-push hook at 4ea68a9 (45 passed); this PR changes no page or api behaviour.
  • docs/prd/todos.md: Passes the automated WCAG 2.1 A/AA scan. Evidence: accessibility.spec.ts test "the empty to-do page has no detectable WCAG A/AA violations"; accessibility.spec.ts test "the to-do list has no detectable WCAG A/AA violations"; accessibility.spec.ts test "the validation error state has no detectable WCAG A/AA violations"; accessibility.spec.ts test "an open edit has no detectable WCAG A/AA violations" passes in the pre-push hook at 4ea68a9 (45 passed); this PR changes no page or api behaviour.

PR Proof Law

  • I followed .agents/project/PR_QUALITY.md and .agents/skills/pr-quality-contract/SKILL.md; the PR quality check verifies the proof format.

@BenSheridanEdwards
BenSheridanEdwards force-pushed the test/page-tests-real-router branch from 295668b to 7ccbc30 Compare November 8, 2026 23:40
@github-actions github-actions Bot added confidence: red A required gate is failing or pending, or evidence is unavailable. confidence: amber Required gates passed, with an E2E, PRD, unit coverage or import blast-radius gap. and removed confidence: red A required gate is failing or pending, or evidence is unavailable. labels Nov 8, 2026
createTodoUseCases() makes a new in-memory store and returns the use
cases over it; createTodosRouter() and createAppRouter() build routers
the same way. The server still builds appRouter once, so behaviour is
unchanged, and a page test for issue 339 can now build an app router
with an empty list of its own.

Application tests call the factory instead of resetting modules, and a
router test proves two app routers keep separate lists. The api reports
gain createAppRouter and createTodosRouter.
TestApiProvider now serves a new app router per render, in memory
through tRPC's local link, so a page test runs the feature's real
procedures, application and domain code. seed adds data through the
router before the page's first call is answered. A handler still
answers its procedure: it can fail, hold or record a call, and its
serve(input) passes the call on to the router.

Web code imports the api by type only, so the web Jest project's setup,
tooling/jest/serve-app-router.ts, hands the router factory of the
result under test to that result's api client. No boundary, contract
rule or bundle changes.

The to-do page tests drop their 40-line fake api. They seed through
the router and keep failure injection, the held list and setDone calls,
and the recorded add, list, rename and clearDone calls.
The write-unit-tests skill (both copies), the component test template,
ARCHITECTURE.md and TECH_STACK.md now say that TestApiProvider runs the
real app router: seed data through it, write no fake api, and give a
handler only to fail, hold or record one call. ARCHITECTURE.md names
createTodoUseCases, createTodosRouter and createAppRouter in the api
file roles and explains how the Jest setup hands the router over.
@BenSheridanEdwards
BenSheridanEdwards force-pushed the test/page-tests-real-router branch from 7ccbc30 to 4ea68a9 Compare November 9, 2026 00:15
@github-actions github-actions Bot added confidence: red A required gate is failing or pending, or evidence is unavailable. confidence: amber Required gates passed, with an E2E, PRD, unit coverage or import blast-radius gap. and removed confidence: amber Required gates passed, with an E2E, PRD, unit coverage or import blast-radius gap. confidence: red A required gate is failing or pending, or evidence is unavailable. labels Nov 9, 2026
@BenSheridanEdwards

Copy link
Copy Markdown
Owner Author

Closing as superseded, per Ben's decision on 12 Nov 2026 ("Yes, restart from main and close both"). It was stacked on #335, which is also closed. The same change, page tests on the real app router (closes #339), will be re-cut from main in phase B as "test(web): run page tests on the real app router", once the to-do api is on main. The decision record is ADR 0123 in #359. This branch is kept as the source.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

confidence: amber Required gates passed, with an E2E, PRD, unit coverage or import blast-radius gap.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant