# Outil Local — manual testing

How to exercise this module by hand. **Keep this file in sync** with the code: every new
mode / flag / behaviour gets a row here in the same PR.

A more visual companion, [`MANUAL-TESTING.html`](MANUAL-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.

## Setup (once)

```bash
cd poc-code/outil-local
npm install
npm run build          # tsc -> dist/
```

Use a throwaway key dir so your real `~/.veritia` stays clean:

```bash
export NP_HOME=/tmp/np-test
rm -rf "$NP_HOME"
```

## Coverage

| # | What | State |
|---|---|---|
| `init` | generate agent Ed25519 keypair, print public JWK + `kid`; `--force` mandate-breakage warning (contracts §9); `--show` re-prints the existing key without touching it (added 2026-10-12, spec §3.2) | ✅ testable |
| runtime proxy | stdio ↔ Claude Desktop, DPoP-signed forward to provider; `NP_HOME`/`NP_MANDATE` folder resolution (contracts §9.5) | ✅ testable |
| `setup` | redeem a one-time setup code into `${NP_HOME}/<slug>/config.json` (contracts §9, build step 5b) | ✅ testable |

## `init` — test matrix

Run each from `poc-code/outil-local/` after `npm run build`.

| # | Command | Expected | Exit |
|---|---|---|---|
| 1 | `node dist/index.js init` | key written to `$NP_HOME/agent.key`; prints one-liner `{"kty":"OKP","crv":"Ed25519","x":"…"}` + `kid` | 0 |
| 2 | `ls -la "$NP_HOME"` | `-rw------- agent.key` inside `drwx------` dir | — |
| 3 | `head -1 "$NP_HOME/agent.key"` | `-----BEGIN PRIVATE KEY-----` (PKCS#8 PEM) | — |
| 4 | `node dist/index.js init` (again) | `refusing to overwrite …` on **stderr**, nothing rewritten | 1 |
| 5 | `node dist/index.js init --force` | new keypair (different `x`), file replaced | 0 |
| 6 | `NP_HOME=/tmp/np-other node dist/index.js init` | writes under `/tmp/np-other`, not the default | 0 |
| 7 | `node dist/index.js` (zero mandate folders under `NP_HOME`) | `no mandate configured — run "mcp-server setup <url>" first` on stderr (O-6) | 1 |
| 8 | `node dist/index.js frobnicate` | `unknown subcommand: frobnicate …` on stderr | 1 |
| 9 | `node dist/index.js init --show` (no key yet) | `no agent key found … run "mcp-server init" first` on stderr (O-1f) | 1 |
| 10 | `node dist/index.js init` then `node dist/index.js init --show` | both print the *same* one-liner + `kid` (O-1g); `sha256sum "$NP_HOME/agent.key"` unchanged before/after `--show` | 0 |

Confirmed 2026-10-12: both verified live (including the `sha256sum` before/after check, and
`--show --force` together — shows, does not regenerate).

### `--force` mandate-breakage warning (contracts §9, one `agent.key` per machine)

Since a regenerated key breaks *every* mandate folder under `NP_HOME` at once, `--force` warns
first. Setup: create at least one mandate folder (either via real `setup <url>` below, or by
hand-writing `${NP_HOME}/<label>/config.json` with a real `AGENT_DELEGATION_TOKEN`).

| # | Command | Expected | Exit |
|---|---|---|---|
| O-1b | `init --force`, answer `N` at the prompt | lists every mandate folder + its expiry, `aborted — key untouched…`; `agent.key` and all folders unchanged | 1 |
| O-1c | `init --force`, answer `y` | same listing, then a fresh keypair is written; mandate folders and their `config.json`s are left on disk untouched (now orphaned — useless until re-`setup`) | 0 |
| O-1d | `init --force --yes` | warning printed to stderr, **no prompt**, proceeds straight to a fresh keypair | 0 |
| O-1e | `init --force` with **zero** mandate folders under `NP_HOME` | proceeds exactly like a plain `--force` always did — no warning at all | 0 |

Confirmed 2026-09-25: all four verified live against real `AGENT_DELEGATION_TOKEN`s (decoded
`act.sub` for the label, `exp` for the timestamp — no signature check, matching spec §3).

Invariants to eyeball on test 1:

- Private key bytes appear **only** in the file — never in stdout, never in a log line.
- The printed one-liner has keys in order `kty, crv, x` and **no whitespace**.

## Keypair verification (round-trip)

Proves the printed public key matches the stored private key and the `kid` is correct.

```bash
node --input-type=module -e '
import { readFileSync } from "node:fs";
import { createPrivateKey, createPublicKey, sign, verify } from "node:crypto";
import { calculateJwkThumbprint, importJWK } from "jose";
const priv = createPrivateKey(readFileSync(process.env.NP_HOME + "/agent.key", "utf8"));
const x = createPublicKey(priv).export({ format: "jwk" }).x;
const oneLiner = `{"kty":"OKP","crv":"Ed25519","x":"${x}"}`;
console.log("one-liner :", oneLiner);
console.log("kid       :", await calculateJwkThumbprint({ kty:"OKP", crv:"Ed25519", x }, "sha256"));
const pub = await importJWK(JSON.parse(oneLiner), "EdDSA");
const sig = sign(null, Buffer.from("msg"), priv);
console.log("PoP ok    :", verify(null, Buffer.from("msg"), pub, sig));
console.log("tamper no :", verify(null, Buffer.from("xxx"), pub, sig) === false);
'
```

| Line | Must match |
|---|---|
| `one-liner` | identical to what `init` printed |
| `kid` | identical to the `kid` `init` printed |
| `PoP ok` | `true` |
| `tamper no` | `true` |

## runtime — test matrix

Needs a real provider to discover tools from. Either the real PDS `/mcp` route — **fixed
2026-09-19 by step 6's isolated `/mcp/**` filter**: the PDS's global `oauth2ResourceServer`
filter used to 401 any `Authorization: DPoP …` request before it reached the `/mcp/**`
`permitAll` rule (Spring Security 6.5's native DPoP-scheme recognition intercepting it); the
isolated filter's separate chain sidesteps that entirely, and the full happy path (real mandate
→ real outil-local → real PDS → real balance data) is now live-verified against the real PDS,
not just refusal/discovery-failure scenarios — or a throwaway MCP server built with the same
`@modelcontextprotocol/sdk` (`Server` + `StreamableHTTPServerTransport` from
`@modelcontextprotocol/sdk/server/*`) for the happy path.

```bash
cd poc-code/outil-local
npm run build
export NP_HOME=/tmp/np-test   # from the init matrix above, real keypair already on disk
```

| # | Setup | Command | Expected | Exit |
|---|---|---|---|---|
| O-2 | no key at `$NP_HOME/agent.key` | `AGENT_DELEGATION_TOKEN=x PROVIDER_API_ENDPOINT=http://localhost:8081/mcp node dist/index.js` | stderr: `agent key not found … run "mcp-server init" first` | 1 |
| O-3 | `chmod 644 $NP_HOME/agent.key` | same as O-2 | stderr: `… is readable by group/other (mode 644) — refusing to start …` | 1 |
| O-3 (Windows) | n/a — the check is skipped entirely on `win32` (no POSIX mode bits on NTFS; `fs.stat()` always reports 0666/0444 there regardless of real ACLs — found live 2026-10-09, refused to start on every launch before this exemption was added) | server starts normally | 0 |
| — | key present, both env vars set | `AGENT_DELEGATION_TOKEN=x PROVIDER_API_ENDPOINT=http://localhost:8081/mcp node dist/index.js` | env wins outright, no mandate folder needed at all (spec §4.1, manual-testing escape hatch) — proceeds straight to discovery | — |
| O-6 | key present, **zero** mandate folders under `NP_HOME`, no env override | `node dist/index.js` (no env) | stderr: `no mandate configured — run "mcp-server setup <url>" first` | 1 |
| O-7 | key present, **≥2** mandate folders under `NP_HOME`, `NP_MANDATE` unset | `node dist/index.js` | stderr: `multiple mandates configured — set NP_MANDATE to one of: <name>, <name>` (sorted) | 1 |
| O-8 | `NP_MANDATE=does-not-exist` | `NP_MANDATE=does-not-exist node dist/index.js` | stderr: `no mandate folder named "does-not-exist" under <NP_HOME>` | 1 |
| O-4 | key + config present, nothing listening | `PROVIDER_API_ENDPOINT=http://localhost:9/mcp node dist/index.js` | stderr: `discovery failed against http://localhost:9/mcp: …` — names the endpoint, no tools served | 1 |
| happy path | real MCP server up, valid delegation token | drive over stdio with an `@modelcontextprotocol/sdk` `Client` + `StdioClientTransport` pointed at `node dist/index.js` | `tools/list` returns the server's tools verbatim; `tools/call` relays the result unchanged; every outbound request (POST *and* the GET SSE stream) carries `Authorization: DPoP <token>` + a fresh `DPoP: <proof>` (unique `jti`/`iat` each time) | — |
| S2–S4 | provider returns 401/403 with `{"error":"<code>"}` on a `tools/call` | same stdio client, call the tool that triggers the rejection | MCP result `isError:true`, `content[0].text` = `provider rejected the call: <code> (HTTP <status>)` — call does **not** throw, the process keeps running | — |
| O-5 | call a tool name never returned by `tools/list` | same stdio client | MCP protocol error `-32601 unknown tool: <name>` — never forwarded to the provider (confirm no log line on the provider side) | — |

Confirmed 2026-09-25: O-6/O-7/O-8 live-verified with hand-built mandate folders under a
throwaway `NP_HOME` (real 2-folder listing, sorted names in the O-7 message).

Confirmed 2026-09-18 against a real PDS (steps 1–4, `poc-code/pds` jar +
`poc-code/platform/stub`, real minted delegation token bound to a freshly-`init`'d key) for
O-2/O-3/O-4/config-missing, and against a throwaway loopback MCP server (same SDK) for the
happy path + S2–S4 + O-5 — see the note above on why the PDS itself can't carry the happy path
yet.

## `setup` — test matrix

Needs the real stub up (`poc-code/platform/stub`) with a real minted mandate (contracts §5.3
now also returns `setup_redeem_url`). `init` must have already run once for this `NP_HOME`.

```bash
cd poc-code/outil-local
npm run build
export NP_HOME=/tmp/np-test
node dist/index.js init   # if not already done for this NP_HOME
```

| # | Setup | Command | Expected | Exit |
|---|---|---|---|---|
| U-1 | no `agent.key` yet | `NP_HOME=/tmp/np-nokey node dist/index.js setup <url>` | stderr: `agent key not found … run "mcp-server init" first` | 1 |
| U-2 | valid, unredeemed `setup_redeem_url` from a real `POST /mandates` response | `node dist/index.js setup <url>` | `${NP_HOME}/<slug>/config.json` written, mode `0600`; stdout names the mandate + `NP_MANDATE=<slug>` | 0 |
| U-3 | same URL, run again | `node dist/index.js setup <url>` (second time) | stderr: `this setup link was already used, or never existed — get a new one from the platform` | 1 |
| U-4 | code past its 10-minute TTL | `node dist/index.js setup <url>` | stderr: `this setup link has expired (10 minute TTL) — get a new one from the platform` | 1 |
| U-5 | platform unreachable | `node dist/index.js setup http://localhost:1/setup-codes/x/redeem` | stderr: `could not reach …: fetch failed` | 1 |
| U-6 | two mandates, two different `agent_label`s, `setup` run once each | — | two separate folders under `NP_HOME`; `runtime` with `NP_MANDATE` unset then refuses and lists both (O-7) | — |
| U-7 | a second mandate reusing an existing `agent_label`, then `setup` for it | — | overwrites that one folder's `config.json` (confirm via checksum); no third folder appears | — |

Confirmed 2026-09-25 against the real stub (`platform/stub/`) + a throwaway PDS assert/JWKS
stand-in (same fake used by the stub's own `node:test` suite) for a real `provider_account_ref`
assertion: U-1 through U-7 all live-verified end to end, including two real mandates with
distinct labels and a third reusing one of them.

## Cleanup

```bash
rm -rf "$NP_HOME" /tmp/np-other
```

## When you change the module

| Change | Update |
|---|---|
| new subcommand / flag | add a row to the `init` matrix (or a new matrix) |
| key path / perms / format | tests 2–3 and the round-trip block |
| public-key output format | the "invariants" list + round-trip `one-liner` check |
| runtime proxy behaviour | the runtime matrix above |
