# Development

_See the [README](../README.md) for what pi-keyrouter is and how to install it._

```bash
bun install
bun x tsc --noEmit -p tsconfig.json          # strict + noUncheckedIndexedAccess
node test/fake-openai-server.mjs --selftest  # the fake server's own plan engine
node test/unit-oauth.mjs                     # config parsing, store guards, /keyrouter args (no pi needed)
node test/verify-runtime-credentials.mjs     # probes pi's credential-store behaviour
node test/oauth-fixture-smoke.mjs            # proves the OAuth fixture works WITHOUT keyrouter
node test/unit-identity.mjs                  # credential->account attribution, incl. the incident replay
node test/run.mjs                            # end-to-end, 37 cases
```

Slash commands only exist in the interactive TUI, so `/keyrouter account` cannot be reached from a
`--print` run. Its argument handling is therefore a pure exported function (`parseCommandArgs`)
covered by `unit-oauth.mjs`, and the effect of a switch — including pi's refreshed-blob capture and
the shutdown restore — is exercised live in the TUI and by the automatic-rotation cases.

`test/run.mjs` starts `test/fake-openai-server.mjs` on an OS-assigned free port, spawns real
`pi --print` runs against an **isolated** agent dir and keyrouter config, and asserts on
independent signals: which `Authorization` header each request carried (the proof of rotation),
keyrouter's trace log, pi's final output, and — for OAuth cases — a log written by the fixture
provider's own refresh hook.

> Nothing touches `~/.pi`. The suite sha256s your real `~/.pi/agent/auth.json` before and after
> and **fails the run** if it changed. That check is a release blocker, because the OAuth tests
> write credentials.

The API-key cases use the `krtest` provider (api-key auth). The OAuth cases additionally load
`test/fixture/oauth-provider.ts`, which registers `kroauth` — an **OAuth-only** provider with no
`apiKey` auth method, mirroring `openai-codex`. That is what makes the tests meaningful: an
api-key overlay on such a provider resolves to nothing, so every request proves keyrouter used the
credential store rather than the overlay. The fixture also models **refresh-token rotation**, so a
credential that keyrouter failed to sync back is rejected as already-used instead of silently
working.

The fake server is scriptable — `sequence` for per-request steps, `byKey` to pin a key's fate,
plus presets for `hang`, `drop` and `badJson`:

```bash
node test/fake-openai-server.mjs --port 8791
curl -X POST 127.0.0.1:8791/__admin/plan -d '{"byKey":{"sk-a":429},"default":"ok"}'
curl 127.0.0.1:8791/__admin/log          # shows the key per request
```

Useful runner flags: `node test/run.mjs rotate` (filter), `oauth-` (just the OAuth cases),
`--verbose`, `--keep-open`.

> The `verify-runtime-credentials.mjs` probe documents the pi 1.0.0 behaviour this design rests
> on; keep it green when bumping pi. It also prints which built-in providers are OAuth-only vs
> dual-auth, which is the data behind the skip rules.

```
index.ts          entry point: events, rotation, continuation
rotation.ts       pure key-pick / cooldown logic
config.ts         config load, $ENV expansion, path override
schema.json        JSON Schema for IDE completion and validation
types.ts          shared types
notification.ts   ui.notify lines
test/
  fake-openai-server.mjs  scriptable OpenAI-compatible endpoint
  run.mjs                 e2e suite
  fixture/                isolated agent dir + configs
```

---

## Publishing

Releases go out from `.github/workflows/publish.yml`, triggered by pushing a `v*` tag. The workflow
typechecks first, fails if the tag does not match `package.json`, then publishes with
`--provenance`.

There is **no npm token in this repo**. npm no longer accepts TOTP 2FA, and is retiring
bypass-2FA tokens for direct publishing, so publishing authenticates through **Trusted Publishing**:
the job's OIDC identity (`permissions: id-token: write`) is exchanged by npm for publish rights. To
set it up, add a trusted publisher on npm for the `@jeryjs` scope pointing at this repo and the
`publish.yml` workflow.
