Skip to content

0016. Page view models live in core; mock scenarios are switches ​

  • Status: Accepted
  • Date: 2026-11-30

Context ​

Two pages (Partner's "Can I spend this?" and the admin's dashboard) show money answers, and the API doesn't exist yet. Money math must not live in a page (ADR 0006), and the same numbers must come out of the Worker and the browser.

Decision ​

  • A view model is a pure function in packages/core. It takes a snapshot (plain JSON) and now, and returns the data a page shows, with the words: partnerHome, partnerCheck, partnerAdd, dashboardHome, dashboardAddCharge. It composes the other core modules (forecast, spend check, envelopes, mortgage pot, freshness) and only adds or subtracts cents. The time zone and now are arguments. Pixels (a chart's size, a colour) stay in the page.
  • A snapshot is what the API returns. One shape per page (the dashboard's extends Partner's), JSON only: no dates as objects, no class instances. The Worker redacts what a role may not see (the trust balance, another person's private envelope) before a snapshot leaves it.
  • Writes are plans. Core says what to write (AddPlan) and re-checks it; the data source applies it. The mock applies it to an in-memory store; the API will apply it to D1.
  • The mock is a data source with scenario switches. ?mock=<state>, ?as=member|admin and ?now=<instant> build the household in each state a page has to show (stale, empty, over, locked, tight…) from packages/mocks. They work only while the source is the mock, so a build on the API ignores them. Each state has a golden example in packages/mocks/test/.
  • Golden fixtures cover the rules, not the pages. Each money rule keeps its id and worked example in docs/money-rules.md; a guard fails when one has no fixture.

Consequences ​

  • A page is thin: a controller, components, no money math. Its tests are about states, not numbers.
  • The numbers are the same in the Worker, the browser and the tests, because it is the same function.
  • A rule change is one edit in core plus its example in docs/money-rules.md.
  • Snapshots carry more than a page shows (the whole ledger), so a page cannot ask the API for less; fine at household size, to be revisited if a household's ledger gets large.