# Testing the Angular SPA (`platform/spa/`) — manual guide

How to start this module and exercise every screen by hand. There is **no automated test
suite for the SPA yet** (see [Automated tests](#automated-tests-there-arent-any-yet)) — this
file is the only test coverage today, so **keep it in sync** when a route, a field, or a
guard's behaviour changes (see [Maintenance](#maintenance)).

Routes/flows: [`../spec.md`](../spec.md) Part B (B3 routes, B4 deterministic parse, B5 mandate
form, B6 scenario table). Wire contracts: [`../../contracts.md`](../../contracts.md) §5.

A more visual companion, [`TESTING.html`](TESTING.html), sits next to this file — same
content, sticky TOC, collapsible sections, copy buttons. Self-contained (no external
libraries), open it directly in a browser. Regenerate it by hand if this file changes; there
is no committed conversion script yet.

## Prerequisites

| Need | Version | Note |
|---|---|---|
| Node | **20.19+**, 22.12+, or 24+ | this module's actual minimum — stricter than "Node 20 LTS+" (architecture.md §12.0), which the stub/outil-local are fine with but the Angular CLI (pinned to **v21** here specifically so plain Node 20.x still works — v22 of the CLI needs Node ≥22.22.3) is not. `node --version` < 20.19 fails `npm start` with `The Angular CLI requires a minimum Node.js version of…` — upgrade Node (`nvm install 20.19` or later), don't chase the CLI version down further |
| the platform stub running | — | `http://localhost:3000` — the SPA talks to it directly, no proxy. See [`../stub/TESTING.md`](../stub/TESTING.md) |
| the `fake-pds` fixture (recommended) | — | `http://localhost:8081` — unblocks screens that otherwise dead-end on a `discovery_failed` / `assertion_fetch_failed` banner because the real PDS (build steps 3/4) doesn't exist yet. See [below](#the-fake-pds-fixture-optional-but-recommended) |
| Podman | — | **not needed.** The SPA always runs via `ng serve` on the host, even once the PDS is containerised (architecture.md §5.1) |

---

## Starting the SPA

### 1. Start its dependency, the stub

```bash
cd poc-code/deploy && ./gen-keys.sh          # once, if not already done
cd ../platform/stub && npm install && npm run build && npm start   # http://localhost:3000
```
Full detail: [`../stub/TESTING.md`](../stub/TESTING.md).

### 2. (Recommended) start the fake-pds fixture

```bash
cd poc-code/platform/stub && node dev/fake-pds.mjs        # http://localhost:8081
```
Run from **`poc-code/platform/stub/`** (it resolves `express`/`jose` from that package's
`node_modules`, already installed in step 1). Without this, everything **up to and including**
"Discover" / "Configurer un agent IA" still renders correctly — you're then testing the
*expected-failure* paths (S12, B-5) rather than the happy path. See
[What needs the PDS](#what-needs-the-pds) below.

### 3. Start the SPA

```bash
cd poc-code/platform/spa
npm install --legacy-peer-deps   # see Troubleshooting — plain `npm install` currently fails here
npm start                        # = ng serve, http://localhost:4200, auto-reloads on edit
```
Run from **`poc-code/platform/spa/`**. Open `http://localhost:4200` — lands on the **landing
page** (`/`), not a login screen (changed 2026-09-14 — see the walkthrough below).

Production build check (what a demo machine would actually serve):
```bash
cd poc-code/platform/spa
npm run build             # -> dist/spa, fails the whole build on any template/type error
```

---

## What needs the PDS

**Status as of 2026-09-14: PDS build steps 3 *and* 4 have landed** (`poc-code/pds/`) — so if
the real PDS is up on `:8081`, Discover and account-linking now genuinely succeed against it,
same as with the fixture. The table below still applies whenever *nothing* answers on `:8081`
(real PDS not started, and no fixture either):

| With nothing at `:8081` | With `fake-pds.mjs`, **or** the real PDS now that steps 3/4 are done |
|---|---|
| `/admin/login`, `/itsme` fully work (no backend calls at all) | same |
| `/admin/register` → Discover → **`502 discovery_failed`** banner (S12) — correct, expected | Discover succeeds → review screen → J'accepte → `provider_id` issued (S11) |
| `/pds-space` → "Configurer un agent IA" → **error banner** (`assertion_fetch_failed`, B-5) — correct, expected | succeeds → routed to `/itsme` → mandate form |
| `/mandate/new?provider=<id>` reachable only via its **guard redirects** (B-1) — can't reach the form itself, since that needs a real link session | full form → submit → signed mandate + the setup command / raw config / client-recipes accordion (see the walkthrough below) |
| `/dashboard` shows the **empty state** for any identity (`GET /mandates` genuinely returns `[]`) | shows real rows → Revoke (S4) → expand → Trace Plateforme **and** Trace Prestataire (B-4) — the second panel stays empty with `fake-pds.mjs` too (it has no `/mcp-audit/:jti`), only the **real** PDS with a real tool call populates it |

So: everything **client-side** (routing, guards, validation) is testable right now, with or
without anything at `:8081`. Everything that needs a **signed mandate to exist** needs
*something* PDS-shaped there — the fixture or the real thing, doesn't matter which.

---

## Screen-by-screen walkthrough

`B6`/`S*` IDs refer to platform/spec.md B6 and contracts.md §7.

### `/` — landing page
Public, no backend calls. Hero + 3 feature bullets + one CTA (**Se connecter** → `/connexion`).
The point of this screen is purely explanatory — check it actually reads clearly to someone
with no context, not just that it renders.

### `/connexion` — persona chooser
Two cards, plain-language labels (no "PDS"/"user" jargon): **Particulier** → `/itsme`;
**Professionnel** → `/admin/login`. Public, no backend calls, no guard.

### `/admin/login`
1. Wrong password → inline "Identifiants invalides.", stays on the page.
2. `admin@bqt.example` / `demo` → redirects to `/admin/register`; the top-bar badge shows
   `Admin : admin@bqt.example`.

### `/admin/register` (Phase 0 — B4 deterministic parse)
Guarded by `adminGuard`: opening this URL directly without having logged in at `/admin/login`
first redirects straight back there — no registration flow is reachable unauthenticated.

1. Endpoint field defaults to `http://pds:8081/mcp` (changed 2026-10-09 — discovery runs
   server-side, *inside* the `web-platform-stub` container in the primary containerized
   topology, so the default has to be the compose-network address; see `core/constants.ts` and
   `contracts.md` §6). Click **Découvrir**.
   - Running the **simpler non-compose dev loop** instead (bare PDS or `fake-pds.mjs` on the
     host, stub run locally via `npm run dev` — see Prerequisites above)? Retype the field to
     `http://localhost:8081/mcp` first, or this step always 502s even with the fixture up.
   - *No fixture/PDS reachable* → red banner `discovery_failed — …ECONNREFUSED…` (**S12**). Retry
     works once something answers.
   - *Fixture running* → review screen appears: one row `get_balance`, **read-only** badge,
     "Aucun paramètre — aucun plafond de gouvernance à définir." — **no** amount/ceiling field
     (**S11**).
2. Click **J'accepte** → banner with `provider_id` (the fixed demo UUID,
   `9b7c1e40-…-000000000bqt`) + the mandate link, a working **Copier le lien** button.
3. **Nouvelle inscription** resets the form; **Tableau de bord** navigates to `/dashboard`.

### `/pds-space?provider=<uuid>`
Reach it via the mandate link from step 2 above, or type it directly with any provider id.
1. No `provider` query param at all → red banner, no crash.
2. Pick a customer (Pieter / Sofie / Noah), click **Configurer un agent IA**.
   - *No fixture* → red banner (`assertion_fetch_failed`, **B-5**), stays on the page.
   - *Fixture running* → navigates to `/itsme?provider=<uuid>`.

### `/itsme`
Pick any of the 3 identities → top-bar badge updates (`itsme : <name>`). Navigates to
`/mandate/new?provider=…` if a `provider` param was carried, else to `/dashboard`.

### `/mandate/new?provider=<uuid>` — guard chain (**B-1**, testable without any backend)
1. Open it **directly** with no prior `/pds-space` visit → redirected to
   `/pds-space?provider=<uuid>` (no active link session).
2. Complete `/pds-space` (needs the fixture) but skip `/itsme` by typing the mandate-form URL
   again → redirected to `/itsme?provider=<uuid>` (link session present, no identity).
3. Complete both → lands on **step 1** ("Installer l'outil local"), not the form directly —
   added 2026-10-10, platform §B5-pre. A `step-indicator` (1/2) shows above the card.

### Step 1 — "Installer l'outil local" (new 2026-10-10, platform §B5-pre)
- Shows the `npx @veritia/mcp-server init` command with a copy button, and copy
  saying an already-installed user can skip ahead.
- **Always shown, every visit** (revised 2026-10-11 — an earlier version remembered "already
  installed" in `localStorage` and skipped step 1 on a returning visit; dropped, since the
  platform can't actually observe what's on the end user's machine and the real gate — "does
  this human already have a mandate" — is server-side and out of scope for the demo). There is
  no server check here at all; it's purely instructional, not a gate.
- **Continuer** → moves to step 2.
- From step 2, click the step-indicator's **"1"** tab (not a separate button — it becomes
  clickable once step 2 is active) to return to step 1 without losing anything already typed
  into the form.

On the form (step 2):
- **B-2**: paste garbage into the agent-key textarea → inline error, **Valider** stays
  disabled. Paste a valid `{"kty":"OKP","crv":"Ed25519","x":"…"}` (e.g. the RFC 8037 test
  vector in `../stub/TESTING.md`, or real output from `outil-local init`) → error clears.
- Agent label pre-fills as `<provider-name>-<firstname>` (e.g. `banque-test-sofie`) once
  **both** the itsme identity and the provider record have loaded — changed 2026-09-19/
  implemented 2026-09-25 from the earlier `claude-desktop-<firstname>` default (contracts §9.4:
  `agent_label` is unique per human across every provider, not just within one, so the default
  needs the provider's name baked in).
- Tool checkboxes come from the provider's confirmed tools (`get_balance`, pre-ticked).
- **B-8** (contracts §9.4, implemented 2026-09-25): before **Valider** actually submits, the
  form checks `GET /mandates?itsme_sub=` for an existing `VALID`, unexpired mandate under the
  same `agent_label` (any provider). If found: a prompt names it (label, issued/expires dates,
  provider) with **Remplacer** / **Changer l'étiquette** / **Annuler** — no "keep both" option.
  **Remplacer** revokes the old `jti` first, then proceeds; **Changer l'étiquette** just closes
  the prompt so the human can retype the field; **Annuler** aborts the submit entirely. To
  trigger it manually: mint one mandate under a label, then mint a second under the *same*
  label (same or a different provider) — the prompt should appear on the second submit.
- **Valider** → (needs the fixture/PDS — mints the mandate) → a success screen. **This screen
  is the one dead end for "go back to step 1"** — it renders whenever `mandate.lastCreated()`
  is set, independently of the `step` signal, so the step-indicator's clickable "1" tab isn't
  reachable from here anymore (the indicator itself is only shown while `!mandate.lastCreated()`).
  Fixed 2026-10-10 with its own small
  disclosure, **"Besoin de revoir comment installer l'outil local ?"**, right under the `jti`
  banner — expands to the same `init` command + copy button, collapsed by default. Below it,
  **two**
  onboarding paths (implemented 2026-09-25, contracts §9/platform §B5): a one-line
  `npx @veritia/mcp-server setup <url>` command (copy button, valid 10 min /
  single-use) *and* the raw config block (`AGENT_DELEGATION_TOKEN=…` /
  `PROVIDER_API_ENDPOINT=…`, copy button), captioned with the exact
  `~/.veritia/<agent_label>/config.json` folder it belongs in. Below both: an
  independently-expandable **client recipes accordion** (§B5bis — Claude Desktop / Claude Code
  / GitHub Copilot CLI), each panel showing that client's config path + launch recipe with
  `NP_MANDATE=<agent_label>` — never the raw token, since `config.json` already holds it.

### `/dashboard`
Guarded by `itsmeGuard`: opening this URL directly with no itsme identity picked yet redirects
to `/itsme` (no `provider` param, so it returns straight to `/dashboard` after picking) —
same "not reachable unauthenticated" property as `/admin/register`.

1. No itsme identity yet → redirected to `/itsme`.
2. Empty state ("Aucun mandat pour cette identité.") is real — reflects a genuine
   `GET /mandates?itsme_sub=…` `200 {mandates:[]}`, not a placeholder.
3. With a mandate minted (needs the fixture): row shows `jti` (truncated), purpose, tools,
   expiry, `VALID` badge.
   - Click the row → expands to **two** panels side by side — **Trace Plateforme**
     (`GET /audit/:jti`, from the stub) and **Trace Prestataire** (the PDS's own
     `GET /mcp-audit/:jti`, contracts §8.6, fetched directly by this browser — added step 9b,
     2026-10-09) — **B-4**. The PDS must actually be reachable at `http://localhost:8081` for
     the second panel; it fails open to "Aucun appel enregistré" rather than an error banner if
     not (or if the mandate's tool was never actually called yet).
   - **Révoquer** → badge flips to `REVOKED`, button disables, trace (if expanded) refreshes
     and gains a `MANDATE_REVOKED` entry — **S4**.
4. **B-3**: reload the page — the list re-fetches from the stub and is still there (it is not
   client-only state).
5. Past `exp` with no explicit revoke → badge shows `EXPIRED`, not `VALID`. The stub's own
   `status` field never flips on its own (only `/mandates/:jti/revoke` changes it — see
   `contracts.md` §5.6), so the SPA computes this client-side in `dashboard.ts`'s
   `displayStatus()` (`status === 'VALID' && exp > now`), the same check `mandate-new.ts`'s
   collision logic already relies on. Fixed 2026-10-14 — previously showed `VALID` forever past
   expiry. The **Révoquer** button stays enabled on an expired-but-unrevoked row (deliberate —
   still useful to put an explicit `MANDATE_REVOKED` audit entry on record).

### Logout
**Se déconnecter** in the top bar (visible whenever either persona is active) calls
`SessionService.logout()` — clears both `identity` and `isPdsAdmin` regardless of which one
was set — then returns to `/`. Check afterward that `/admin/register` and `/dashboard` are no
longer reachable without going through `/connexion` again (the guards, not just the nav, are
what's actually enforcing this — see below).

### Access control — what should NOT be reachable while logged out
Confirm these directly, by URL, in a fresh/incognito tab (no session at all):
- `/admin/register` → redirected to `/admin/login` (**`adminGuard`**).
- `/dashboard` → redirected to `/itsme` (**`itsmeGuard`**).
- `/mandate/new?provider=<any-uuid>` → redirected to `/pds-space?provider=<uuid>` (**`mandateFormGuard`**, unchanged, B-1).

And that the top bar itself shows **no** persona-specific links at all while logged out — just
the brand and a single **Se connecter** button.

---

## The `fake-pds` fixture — optional but recommended

`poc-code/platform/stub/dev/fake-pds.mjs` — a ~100-line throwaway Express server implementing
just enough of the PDS's contract to unblock the flows above: `POST /mcp` (`initialize` +
`tools/list` → `get_balance`, no tool *calls*), `GET /pds-jwks.json`, `POST
/account-link/assert`. **Not** a PDS implementation — no DPoP, no real auth, no persistence,
fresh signing key every restart. It exists because platform/spec.md B1 explicitly anticipates
this gap ("use a canned tools/list fixture… until the PDS MCP route is reachable").

```bash
cd poc-code/platform/stub
node dev/fake-pds.mjs        # port 8081, or: node dev/fake-pds.mjs 8090
```

Verified end-to-end (2026-09-11) with the real stub via curl: discover → register → link →
mint → list → audit → revoke → introspect, full chain, all responses matching
contracts.md §5 shapes exactly — see git history / ask for the transcript if needed.

**Status as of 2026-09-25 (corrected — this note was stale since 2026-09-11's own step-4
landing): PDS build steps 3, 4, 4b, 5, 5b and 6 have all landed** (`poc-code/pds/`,
`poc-code/outil-local/`) — the real PDS now genuinely answers `tools/list` (`get_balance` +
`list_transactions`) itself, behind its own isolated `/mcp/**` mandate filter. This fixture is
now **optional, not required**: it's still convenient for a fast SPA-only dev loop (no JDK/
Maven, no mandate needed to see a tool list), but Discover no longer needs it once the real PDS
jar is up. **Never run both on `:8081` at once** — pick one (see Troubleshooting).

---

## Automated tests — there aren't any yet

Angular's default Vitest scaffold is present (`npm test` runs it) but there are currently
**no spec files** — the seed one the `ng new` scaffold generated referenced API this app no
longer has and was deleted rather than left stale. This file is the test coverage until that
changes. If/when component or service specs are added, add a section here (or a pointer) the
same way `../stub/TESTING.md` documents `npm test` for that module.

## Troubleshooting

| Symptom | Cause / fix |
|---|---|
| `npm install` fails with `Cannot read properties of null (reading 'edgesOut')` | a known npm/Arborist bug against this Angular 22 template's `vitest`/`msw` peer-dep graph — use `npm install --legacy-peer-deps` |
| `/admin/register` Discover → `502 discovery_failed` | expected with nothing at `:8081` — start `dev/fake-pds.mjs` (or the real PDS later) |
| `/pds-space` → `assertion_fetch_failed` | same — no PDS-shaped server at `:8081` yet |
| `/pds-space` → `422 assertion_invalid` even with the fixture running | check the fixture and stub agree on `PDS_ASSERT_DEV_SECRET` (both default to `poc-dev-secret` — don't override one without the other) |
| Dashboard stays empty after minting a mandate | wrong itsme identity picked vs. the one used at `/mandate/new` — the list is filtered by `itsme_sub`, not global |
| Mandate form's Valider stays disabled | either the agent-key textarea doesn't parse (B-2), or the link session expired (`LINK_SESSION_TTL_SECONDS`, default 600s) — redo `/pds-space` |
| Changes to `src/` don't show up | `ng serve`'s dev server should auto-reload — hard-refresh the browser; if still stale, check the terminal running `npm start` for a compile error |
| Stub or fixture restarted mid-session | both are in-memory — re-run registration + account-link + mandate (same as `S13` in contracts.md §7) |
| `EADDRINUSE` starting `fake-pds.mjs`, or the real PDS won't bind `:8081` | only one thing may listen on `:8081` at a time — stop whichever of `fake-pds.mjs` / the real PDS (`poc-code/pds/`) you're not using |
| `npm start` → `The Angular CLI requires a minimum Node.js version of v22.22.3 or v24.15.0 or v26.0.0` | your Node is below **20.19** (the CLI's real minimum for the 20.x line) — check with `node --version`, then `nvm install 20.19` (or newer) + `nvm use`. This is a Node-version problem, not a project-version problem — `package.json` here is already pinned to Angular 21, the newest release that still supports Node 20.x at all |

## Maintenance

Update this file in the **same change** that touches:

| Change | Update here |
|---|---|
| add/remove/rename a route (`app.routes.ts`) | Screen-by-screen walkthrough + Prerequisites table if it changes backend dependencies |
| change `mandateFormGuard`'s redirect logic | the guard-chain steps under `/mandate/new` |
| change a form field, validation rule, or default | the relevant screen's bullet list |
| change `dev/fake-pds.mjs`'s endpoints or responses | The fixture section above |
| add real automated tests | replace "Automated tests — there aren't any yet" |
