@codespar/cli 0.14.1 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -17
- package/dist/commands/audit-replay.js +267 -0
- package/dist/commands/audit-replay.js.map +1 -0
- package/dist/commands/init.js +28 -0
- package/dist/commands/init.js.map +1 -1
- package/dist/commands/mandate-verify.js +98 -19
- package/dist/commands/mandate-verify.js.map +1 -1
- package/dist/index.js +17 -3
- package/dist/index.js.map +1 -1
- package/dist/mandate-codec.js +31 -20
- package/dist/mandate-codec.js.map +1 -1
- package/dist/surface.js +4 -1
- package/dist/surface.js.map +1 -1
- package/package.json +1 -1
- package/templates/bills-agent/README.md +8 -5
- package/templates/bills-agent/agents/bills-agent/.env.example +3 -3
- package/templates/bills-agent/agents/bills-agent/AGENTS.md +1 -1
- package/templates/bills-agent/agents/bills-agent/CLAUDE.md +1 -1
- package/templates/bills-agent/agents/bills-agent/README.md +62 -22
- package/templates/bills-agent/agents/bills-agent/SYSTEM_PROMPT.md +1 -1
- package/templates/bills-agent/agents/bills-agent/agent.yaml +2 -2
- package/templates/bills-agent/agents/bills-agent/package.json +14 -11
- package/templates/bills-agent/agents/bills-agent/src/kit.ts +193 -0
- package/templates/bills-agent/agents/bills-agent/test/adversarial.test.ts +8 -7
- package/templates/bills-agent/agents/bills-agent/test/check.test.ts +19 -2
- package/templates/bills-agent/agents/bills-agent/test/cli.test.ts +72 -24
- package/templates/bills-agent/agents/bills-agent/test/fixtures/excursao-300.transcript.jsonl +2 -0
- package/templates/bills-agent/agents/bills-agent/test/fixtures/material-400.transcript.jsonl +2 -0
- package/templates/bills-agent/agents/bills-agent/test/inspect.test.ts +124 -0
- package/templates/bills-agent/agents/bills-agent/test/provider.test.ts +25 -0
- package/templates/bills-agent/agents/bills-agent/test/scenarios.test.ts +10 -9
- package/templates/bills-agent/agents/bills-agent/tsconfig.json +0 -1
- package/templates/bills-agent/package.json +5 -2
- package/templates/bills-agent/packages/agent-core/README.md +2 -0
- package/templates/bills-agent/packages/agent-core/package.json +1 -1
- package/templates/bills-agent/packages/agent-core/src/api/charge-rail.ts +27 -6
- package/templates/bills-agent/packages/agent-core/src/api/rail.ts +16 -0
- package/templates/bills-agent/packages/agent-core/src/api/sandbox-payer.ts +20 -7
- package/templates/bills-agent/packages/agent-core/src/approval.ts +16 -2
- package/templates/bills-agent/packages/agent-core/src/bundle.ts +50 -4
- package/templates/bills-agent/packages/agent-core/src/channels.ts +140 -0
- package/templates/bills-agent/packages/agent-core/src/check.ts +90 -8
- package/templates/bills-agent/packages/agent-core/src/clock.ts +42 -0
- package/templates/bills-agent/packages/agent-core/src/engine.ts +119 -18
- package/templates/bills-agent/packages/agent-core/src/hash.ts +13 -0
- package/templates/bills-agent/packages/agent-core/src/index.ts +3 -0
- package/templates/bills-agent/packages/agent-core/src/manifest.ts +1 -1
- package/templates/bills-agent/packages/agent-core/src/rail.ts +16 -2
- package/templates/bills-agent/packages/agent-core/src/receipt-verification.ts +401 -0
- package/templates/bills-agent/packages/agent-core/src/state-machine.ts +3 -1
- package/templates/bills-agent/packages/agent-core/src/stubs/charge-rail.ts +2 -0
- package/templates/bills-agent/packages/agent-core/src/stubs/rail.ts +19 -2
- package/templates/bills-agent/packages/agent-core/src/types.ts +28 -0
- package/templates/bills-agent/packages/agent-core/test/approval.test.ts +36 -2
- package/templates/bills-agent/packages/agent-core/test/bundle.test.ts +85 -0
- package/templates/bills-agent/packages/agent-core/test/clock.test.ts +25 -0
- package/templates/bills-agent/packages/agent-core/test/engine.test.ts +73 -1
- package/templates/bills-agent/packages/agent-core/test/fixtures/staging-receipt-keys.json +18 -0
- package/templates/bills-agent/packages/agent-core/test/fixtures/staging-receipt.json +24 -0
- package/templates/bills-agent/packages/agent-core/test/helpers.ts +1 -1
- package/templates/bills-agent/packages/agent-core/test/manifest.test.ts +2 -2
- package/templates/bills-agent/packages/agent-core/test/receipt-verification.test.ts +316 -0
- package/templates/bills-agent/packages/agent-core/test/receivables.test.ts +51 -0
- package/templates/bills-agent/packages/agent-runtime/README.md +68 -0
- package/templates/bills-agent/packages/agent-runtime/bin.mjs +32 -0
- package/templates/bills-agent/packages/agent-runtime/package.json +23 -0
- package/templates/bills-agent/{agents/bills-agent → packages/agent-runtime}/src/adversarial.ts +36 -44
- package/templates/bills-agent/packages/agent-runtime/src/agent.ts +68 -0
- package/templates/bills-agent/packages/agent-runtime/src/channels/contact.ts +13 -0
- package/templates/bills-agent/packages/agent-runtime/src/channels/index.ts +72 -0
- package/templates/bills-agent/packages/agent-runtime/src/channels/rules.ts +120 -0
- package/templates/bills-agent/packages/agent-runtime/src/channels/types.ts +124 -0
- package/templates/bills-agent/packages/agent-runtime/src/channels/whatsapp/cloud-api.ts +358 -0
- package/templates/bills-agent/packages/agent-runtime/src/channels/whatsapp/emulator.ts +206 -0
- package/templates/bills-agent/packages/agent-runtime/src/channels/whatsapp/evidence.ts +109 -0
- package/templates/bills-agent/packages/agent-runtime/src/channels/whatsapp/index.ts +225 -0
- package/templates/bills-agent/packages/agent-runtime/src/channels/whatsapp/open.ts +143 -0
- package/templates/bills-agent/packages/agent-runtime/src/channels/whatsapp/present.ts +47 -0
- package/templates/bills-agent/packages/agent-runtime/src/channels/whatsapp/run.ts +113 -0
- package/templates/bills-agent/packages/agent-runtime/src/channels/whatsapp/session.ts +113 -0
- package/templates/bills-agent/packages/agent-runtime/src/cli.ts +98 -0
- package/templates/bills-agent/packages/agent-runtime/src/commands/check.ts +15 -0
- package/templates/bills-agent/packages/agent-runtime/src/commands/decide.ts +63 -0
- package/templates/bills-agent/packages/agent-runtime/src/commands/eval.ts +58 -0
- package/templates/bills-agent/packages/agent-runtime/src/commands/inspect.ts +100 -0
- package/templates/bills-agent/packages/agent-runtime/src/commands/poll-whatsapp.ts +317 -0
- package/templates/bills-agent/packages/agent-runtime/src/commands/poll.ts +144 -0
- package/templates/bills-agent/packages/agent-runtime/src/commands/reconcile.ts +42 -0
- package/templates/bills-agent/packages/agent-runtime/src/commands/rerun.ts +86 -0
- package/templates/bills-agent/packages/agent-runtime/src/commands/resume.ts +43 -0
- package/templates/bills-agent/packages/agent-runtime/src/commands/start-whatsapp.ts +131 -0
- package/templates/bills-agent/packages/agent-runtime/src/commands/start.ts +245 -0
- package/templates/bills-agent/packages/agent-runtime/src/commands/verify.ts +129 -0
- package/templates/bills-agent/packages/agent-runtime/src/commands/webhook.ts +39 -0
- package/templates/bills-agent/packages/agent-runtime/src/default-kit.ts +83 -0
- package/templates/bills-agent/packages/agent-runtime/src/index.ts +12 -0
- package/templates/bills-agent/packages/agent-runtime/src/inspect.ts +797 -0
- package/templates/bills-agent/packages/agent-runtime/src/kit.ts +206 -0
- package/templates/{collections-agent/agents/collections-agent → bills-agent/packages/agent-runtime}/src/poll.ts +22 -11
- package/templates/{collections-agent/agents/collections-agent → bills-agent/packages/agent-runtime}/src/scenarios.ts +50 -34
- package/templates/bills-agent/{agents/bills-agent → packages/agent-runtime}/src/setup.ts +88 -66
- package/templates/bills-agent/packages/agent-runtime/src/terminal.ts +154 -0
- package/templates/{collections-agent/agents/collections-agent/channels/webhook/index.ts → bills-agent/packages/agent-runtime/src/webhook.ts} +1 -1
- package/templates/bills-agent/packages/agent-runtime/test/inspect.test.ts +336 -0
- package/templates/bills-agent/packages/agent-runtime/test/verify-cli.test.ts +127 -0
- package/templates/bills-agent/packages/agent-runtime/test/whatsapp-cloud-api.test.ts +232 -0
- package/templates/bills-agent/packages/agent-runtime/test/whatsapp-emulator.integration.test.ts +204 -0
- package/templates/bills-agent/packages/agent-runtime/test/whatsapp-evidence.test.ts +94 -0
- package/templates/bills-agent/packages/agent-runtime/test/whatsapp-rules.test.ts +174 -0
- package/templates/bills-agent/packages/agent-runtime/test/whatsapp-session.test.ts +135 -0
- package/templates/bills-agent/packages/agent-runtime/tsconfig.json +9 -0
- package/templates/bills-agent/vitest.config.ts +1 -1
- package/templates/collections-agent/README.md +8 -5
- package/templates/collections-agent/agents/collections-agent/.env.example +14 -0
- package/templates/collections-agent/agents/collections-agent/AGENTS.md +6 -4
- package/templates/collections-agent/agents/collections-agent/CLAUDE.md +6 -4
- package/templates/collections-agent/agents/collections-agent/README.md +184 -16
- package/templates/collections-agent/agents/collections-agent/agent.yaml +3 -3
- package/templates/collections-agent/agents/collections-agent/channels/whatsapp/README.md +111 -0
- package/templates/collections-agent/agents/collections-agent/channels/whatsapp/acordo-1042.json +11 -0
- package/templates/collections-agent/agents/collections-agent/channels/whatsapp/acordo-1103.json +11 -0
- package/templates/collections-agent/agents/collections-agent/channels/whatsapp/templates.json +23 -0
- package/templates/collections-agent/agents/collections-agent/package.json +12 -10
- package/templates/collections-agent/agents/collections-agent/runbook.md +52 -1
- package/templates/collections-agent/agents/collections-agent/src/kit.ts +265 -0
- package/templates/collections-agent/agents/collections-agent/test/adversarial.test.ts +9 -8
- package/templates/collections-agent/agents/collections-agent/test/check.test.ts +43 -1
- package/templates/collections-agent/agents/collections-agent/test/cli.test.ts +56 -18
- package/templates/collections-agent/agents/collections-agent/test/fixtures/agreed-1042-awaiting-payer.transcript.jsonl +4 -0
- package/templates/collections-agent/agents/collections-agent/test/scenarios.test.ts +11 -10
- package/templates/collections-agent/agents/collections-agent/test/webhook.test.ts +4 -7
- package/templates/collections-agent/agents/collections-agent/test/whatsapp-poll.test.ts +271 -0
- package/templates/collections-agent/agents/collections-agent/test/whatsapp.test.ts +213 -0
- package/templates/collections-agent/agents/collections-agent/tsconfig.json +0 -1
- package/templates/collections-agent/package.json +3 -1
- package/templates/collections-agent/packages/agent-core/README.md +2 -0
- package/templates/collections-agent/packages/agent-core/package.json +1 -1
- package/templates/collections-agent/packages/agent-core/src/api/charge-rail.ts +27 -6
- package/templates/collections-agent/packages/agent-core/src/api/rail.ts +16 -0
- package/templates/collections-agent/packages/agent-core/src/api/sandbox-payer.ts +20 -7
- package/templates/collections-agent/packages/agent-core/src/approval.ts +16 -2
- package/templates/collections-agent/packages/agent-core/src/bundle.ts +50 -4
- package/templates/collections-agent/packages/agent-core/src/channels.ts +140 -0
- package/templates/collections-agent/packages/agent-core/src/check.ts +90 -8
- package/templates/collections-agent/packages/agent-core/src/clock.ts +42 -0
- package/templates/collections-agent/packages/agent-core/src/engine.ts +119 -18
- package/templates/collections-agent/packages/agent-core/src/hash.ts +13 -0
- package/templates/collections-agent/packages/agent-core/src/index.ts +3 -0
- package/templates/collections-agent/packages/agent-core/src/manifest.ts +1 -1
- package/templates/collections-agent/packages/agent-core/src/rail.ts +16 -2
- package/templates/collections-agent/packages/agent-core/src/receipt-verification.ts +401 -0
- package/templates/collections-agent/packages/agent-core/src/state-machine.ts +3 -1
- package/templates/collections-agent/packages/agent-core/src/stubs/charge-rail.ts +2 -0
- package/templates/collections-agent/packages/agent-core/src/stubs/rail.ts +19 -2
- package/templates/collections-agent/packages/agent-core/src/types.ts +28 -0
- package/templates/collections-agent/packages/agent-core/test/approval.test.ts +36 -2
- package/templates/collections-agent/packages/agent-core/test/bundle.test.ts +85 -0
- package/templates/collections-agent/packages/agent-core/test/clock.test.ts +25 -0
- package/templates/collections-agent/packages/agent-core/test/engine.test.ts +73 -1
- package/templates/collections-agent/packages/agent-core/test/fixtures/staging-receipt-keys.json +18 -0
- package/templates/collections-agent/packages/agent-core/test/fixtures/staging-receipt.json +24 -0
- package/templates/collections-agent/packages/agent-core/test/helpers.ts +1 -1
- package/templates/collections-agent/packages/agent-core/test/manifest.test.ts +2 -2
- package/templates/collections-agent/packages/agent-core/test/receipt-verification.test.ts +316 -0
- package/templates/collections-agent/packages/agent-core/test/receivables.test.ts +51 -0
- package/templates/collections-agent/packages/agent-runtime/README.md +68 -0
- package/templates/collections-agent/packages/agent-runtime/bin.mjs +32 -0
- package/templates/collections-agent/packages/agent-runtime/package.json +23 -0
- package/templates/collections-agent/{agents/collections-agent → packages/agent-runtime}/src/adversarial.ts +29 -50
- package/templates/collections-agent/packages/agent-runtime/src/agent.ts +68 -0
- package/templates/collections-agent/packages/agent-runtime/src/channels/contact.ts +13 -0
- package/templates/collections-agent/packages/agent-runtime/src/channels/index.ts +72 -0
- package/templates/collections-agent/packages/agent-runtime/src/channels/rules.ts +120 -0
- package/templates/collections-agent/packages/agent-runtime/src/channels/types.ts +124 -0
- package/templates/collections-agent/packages/agent-runtime/src/channels/whatsapp/cloud-api.ts +358 -0
- package/templates/collections-agent/packages/agent-runtime/src/channels/whatsapp/emulator.ts +206 -0
- package/templates/collections-agent/packages/agent-runtime/src/channels/whatsapp/evidence.ts +109 -0
- package/templates/collections-agent/packages/agent-runtime/src/channels/whatsapp/index.ts +225 -0
- package/templates/collections-agent/packages/agent-runtime/src/channels/whatsapp/open.ts +143 -0
- package/templates/collections-agent/packages/agent-runtime/src/channels/whatsapp/present.ts +47 -0
- package/templates/collections-agent/packages/agent-runtime/src/channels/whatsapp/run.ts +113 -0
- package/templates/collections-agent/packages/agent-runtime/src/channels/whatsapp/session.ts +113 -0
- package/templates/collections-agent/packages/agent-runtime/src/cli.ts +98 -0
- package/templates/collections-agent/packages/agent-runtime/src/commands/check.ts +15 -0
- package/templates/collections-agent/packages/agent-runtime/src/commands/decide.ts +63 -0
- package/templates/collections-agent/packages/agent-runtime/src/commands/eval.ts +58 -0
- package/templates/collections-agent/packages/agent-runtime/src/commands/inspect.ts +100 -0
- package/templates/collections-agent/packages/agent-runtime/src/commands/poll-whatsapp.ts +317 -0
- package/templates/collections-agent/packages/agent-runtime/src/commands/poll.ts +144 -0
- package/templates/collections-agent/packages/agent-runtime/src/commands/reconcile.ts +42 -0
- package/templates/collections-agent/packages/agent-runtime/src/commands/rerun.ts +86 -0
- package/templates/collections-agent/packages/agent-runtime/src/commands/resume.ts +43 -0
- package/templates/collections-agent/packages/agent-runtime/src/commands/start-whatsapp.ts +131 -0
- package/templates/collections-agent/packages/agent-runtime/src/commands/start.ts +245 -0
- package/templates/collections-agent/packages/agent-runtime/src/commands/verify.ts +129 -0
- package/templates/collections-agent/packages/agent-runtime/src/commands/webhook.ts +39 -0
- package/templates/collections-agent/packages/agent-runtime/src/default-kit.ts +83 -0
- package/templates/collections-agent/packages/agent-runtime/src/index.ts +12 -0
- package/templates/collections-agent/packages/agent-runtime/src/inspect.ts +797 -0
- package/templates/collections-agent/packages/agent-runtime/src/kit.ts +206 -0
- package/templates/collections-agent/packages/agent-runtime/src/poll.ts +105 -0
- package/templates/{bills-agent/agents/bills-agent → collections-agent/packages/agent-runtime}/src/scenarios.ts +98 -26
- package/templates/collections-agent/packages/agent-runtime/src/setup.ts +229 -0
- package/templates/collections-agent/packages/agent-runtime/src/terminal.ts +154 -0
- package/templates/collections-agent/packages/agent-runtime/src/webhook.ts +131 -0
- package/templates/collections-agent/packages/agent-runtime/test/inspect.test.ts +336 -0
- package/templates/collections-agent/packages/agent-runtime/test/verify-cli.test.ts +127 -0
- package/templates/collections-agent/packages/agent-runtime/test/whatsapp-cloud-api.test.ts +232 -0
- package/templates/collections-agent/packages/agent-runtime/test/whatsapp-emulator.integration.test.ts +204 -0
- package/templates/collections-agent/packages/agent-runtime/test/whatsapp-evidence.test.ts +94 -0
- package/templates/collections-agent/packages/agent-runtime/test/whatsapp-rules.test.ts +174 -0
- package/templates/collections-agent/packages/agent-runtime/test/whatsapp-session.test.ts +135 -0
- package/templates/collections-agent/packages/agent-runtime/tsconfig.json +9 -0
- package/templates/collections-agent/vitest.config.ts +1 -1
- package/templates/hello-agent/LICENSE +21 -0
- package/templates/hello-agent/README.md +23 -0
- package/templates/hello-agent/_gitignore +10 -0
- package/templates/hello-agent/agents/hello-agent/.env.example +5 -0
- package/templates/hello-agent/agents/hello-agent/AGENTS.md +15 -0
- package/templates/hello-agent/agents/hello-agent/CLAUDE.md +15 -0
- package/templates/hello-agent/agents/hello-agent/README.md +33 -0
- package/templates/hello-agent/agents/hello-agent/SYSTEM_PROMPT.md +19 -0
- package/templates/hello-agent/agents/hello-agent/agent.yaml +19 -0
- package/templates/hello-agent/agents/hello-agent/evals/adversarial/beneficiary-swap.json +6 -0
- package/templates/hello-agent/agents/hello-agent/evals/adversarial/beneficiary-swap.transcript.jsonl +2 -0
- package/templates/hello-agent/agents/hello-agent/evals/adversarial/exfiltration.json +6 -0
- package/templates/hello-agent/agents/hello-agent/evals/adversarial/exfiltration.transcript.jsonl +2 -0
- package/templates/hello-agent/agents/hello-agent/evals/adversarial/false-authority.json +6 -0
- package/templates/hello-agent/agents/hello-agent/evals/adversarial/false-authority.transcript.jsonl +2 -0
- package/templates/hello-agent/agents/hello-agent/evals/adversarial/fractioning.json +6 -0
- package/templates/hello-agent/agents/hello-agent/evals/adversarial/fractioning.transcript.jsonl +2 -0
- package/templates/hello-agent/agents/hello-agent/evals/adversarial/model-total.json +6 -0
- package/templates/hello-agent/agents/hello-agent/evals/adversarial/model-total.transcript.jsonl +2 -0
- package/templates/hello-agent/agents/hello-agent/evals/adversarial/prompt-injection.json +6 -0
- package/templates/hello-agent/agents/hello-agent/evals/adversarial/prompt-injection.transcript.jsonl +2 -0
- package/templates/hello-agent/agents/hello-agent/evals/adversarial/webhook-replay.json +6 -0
- package/templates/hello-agent/agents/hello-agent/evals/eval.yaml +7 -0
- package/templates/hello-agent/agents/hello-agent/guardrails.json +8 -0
- package/templates/hello-agent/agents/hello-agent/mandate.example.json +17 -0
- package/templates/hello-agent/agents/hello-agent/package.json +19 -0
- package/templates/hello-agent/agents/hello-agent/runbook.md +10 -0
- package/templates/hello-agent/agents/hello-agent/scenarios/happy-path.json +6 -0
- package/templates/hello-agent/agents/hello-agent/scenarios/happy-path.transcript.jsonl +2 -0
- package/templates/hello-agent/agents/hello-agent/scenarios/prompt-injection.json +6 -0
- package/templates/hello-agent/agents/hello-agent/scenarios/prompt-injection.transcript.jsonl +2 -0
- package/templates/hello-agent/agents/hello-agent/src/bills.ts +23 -0
- package/templates/hello-agent/agents/hello-agent/src/kit.ts +33 -0
- package/templates/hello-agent/agents/hello-agent/tools.json +11 -0
- package/templates/hello-agent/agents/hello-agent/tsconfig.json +9 -0
- package/templates/hello-agent/package.json +30 -0
- package/templates/hello-agent/packages/agent-core/README.md +21 -0
- package/templates/hello-agent/packages/agent-core/package.json +24 -0
- package/templates/hello-agent/packages/agent-core/src/agent.ts +125 -0
- package/templates/hello-agent/packages/agent-core/src/api/charge-rail.ts +175 -0
- package/templates/hello-agent/packages/agent-core/src/api/client.ts +53 -0
- package/templates/hello-agent/packages/agent-core/src/api/mandate-status.ts +58 -0
- package/templates/hello-agent/packages/agent-core/src/api/rail.ts +117 -0
- package/templates/hello-agent/packages/agent-core/src/api/sandbox-payer.ts +58 -0
- package/templates/hello-agent/packages/agent-core/src/approval.ts +123 -0
- package/templates/hello-agent/packages/agent-core/src/bundle.ts +159 -0
- package/templates/hello-agent/packages/agent-core/src/channels.ts +140 -0
- package/templates/hello-agent/packages/agent-core/src/check.ts +253 -0
- package/templates/hello-agent/packages/agent-core/src/clock.ts +42 -0
- package/templates/hello-agent/packages/agent-core/src/engine.ts +885 -0
- package/templates/hello-agent/packages/agent-core/src/escalate.ts +85 -0
- package/templates/hello-agent/packages/agent-core/src/events.ts +40 -0
- package/templates/hello-agent/packages/agent-core/src/guardrails.ts +45 -0
- package/templates/hello-agent/packages/agent-core/src/hash.ts +62 -0
- package/templates/hello-agent/packages/agent-core/src/ids.ts +10 -0
- package/templates/hello-agent/packages/agent-core/src/index.ts +32 -0
- package/templates/hello-agent/packages/agent-core/src/mandate.ts +100 -0
- package/templates/hello-agent/packages/agent-core/src/manifest.ts +76 -0
- package/templates/hello-agent/packages/agent-core/src/providers/anthropic.ts +76 -0
- package/templates/hello-agent/packages/agent-core/src/providers/replay.ts +63 -0
- package/templates/hello-agent/packages/agent-core/src/providers/types.ts +39 -0
- package/templates/hello-agent/packages/agent-core/src/rail.ts +101 -0
- package/templates/hello-agent/packages/agent-core/src/receipt-verification.ts +401 -0
- package/templates/hello-agent/packages/agent-core/src/revocation.ts +30 -0
- package/templates/hello-agent/packages/agent-core/src/secrets.ts +30 -0
- package/templates/hello-agent/packages/agent-core/src/state/store.ts +307 -0
- package/templates/hello-agent/packages/agent-core/src/state-machine.ts +148 -0
- package/templates/hello-agent/packages/agent-core/src/stubs/charge-rail.ts +195 -0
- package/templates/hello-agent/packages/agent-core/src/stubs/mandate-status.ts +57 -0
- package/templates/hello-agent/packages/agent-core/src/stubs/rail.ts +141 -0
- package/templates/hello-agent/packages/agent-core/src/tools.ts +55 -0
- package/templates/hello-agent/packages/agent-core/src/types.ts +134 -0
- package/templates/hello-agent/packages/agent-core/test/approval.test.ts +117 -0
- package/templates/hello-agent/packages/agent-core/test/bundle.test.ts +85 -0
- package/templates/hello-agent/packages/agent-core/test/clock.test.ts +25 -0
- package/templates/hello-agent/packages/agent-core/test/engine.test.ts +596 -0
- package/templates/hello-agent/packages/agent-core/test/escalate.test.ts +42 -0
- package/templates/hello-agent/packages/agent-core/test/fixtures/staging-receipt-keys.json +18 -0
- package/templates/hello-agent/packages/agent-core/test/fixtures/staging-receipt.json +24 -0
- package/templates/hello-agent/packages/agent-core/test/helpers.ts +139 -0
- package/templates/hello-agent/packages/agent-core/test/mandate-status.test.ts +153 -0
- package/templates/hello-agent/packages/agent-core/test/manifest.test.ts +45 -0
- package/templates/hello-agent/packages/agent-core/test/receipt-verification.test.ts +316 -0
- package/templates/hello-agent/packages/agent-core/test/receivables.test.ts +316 -0
- package/templates/hello-agent/packages/agent-core/test/secrets.test.ts +28 -0
- package/templates/hello-agent/packages/agent-core/test/state-machine.test.ts +67 -0
- package/templates/hello-agent/packages/agent-core/test/transitions.type-test.ts +59 -0
- package/templates/hello-agent/packages/agent-core/tsconfig.json +10 -0
- package/templates/hello-agent/packages/agent-runtime/README.md +68 -0
- package/templates/hello-agent/packages/agent-runtime/bin.mjs +32 -0
- package/templates/hello-agent/packages/agent-runtime/package.json +23 -0
- package/templates/hello-agent/packages/agent-runtime/src/adversarial.ts +169 -0
- package/templates/hello-agent/packages/agent-runtime/src/agent.ts +68 -0
- package/templates/hello-agent/packages/agent-runtime/src/channels/contact.ts +13 -0
- package/templates/hello-agent/packages/agent-runtime/src/channels/index.ts +72 -0
- package/templates/hello-agent/packages/agent-runtime/src/channels/rules.ts +120 -0
- package/templates/hello-agent/packages/agent-runtime/src/channels/types.ts +124 -0
- package/templates/hello-agent/packages/agent-runtime/src/channels/whatsapp/cloud-api.ts +358 -0
- package/templates/hello-agent/packages/agent-runtime/src/channels/whatsapp/emulator.ts +206 -0
- package/templates/hello-agent/packages/agent-runtime/src/channels/whatsapp/evidence.ts +109 -0
- package/templates/hello-agent/packages/agent-runtime/src/channels/whatsapp/index.ts +225 -0
- package/templates/hello-agent/packages/agent-runtime/src/channels/whatsapp/open.ts +143 -0
- package/templates/hello-agent/packages/agent-runtime/src/channels/whatsapp/present.ts +47 -0
- package/templates/hello-agent/packages/agent-runtime/src/channels/whatsapp/run.ts +113 -0
- package/templates/hello-agent/packages/agent-runtime/src/channels/whatsapp/session.ts +113 -0
- package/templates/hello-agent/packages/agent-runtime/src/cli.ts +98 -0
- package/templates/hello-agent/packages/agent-runtime/src/commands/check.ts +15 -0
- package/templates/hello-agent/packages/agent-runtime/src/commands/decide.ts +63 -0
- package/templates/hello-agent/packages/agent-runtime/src/commands/eval.ts +58 -0
- package/templates/hello-agent/packages/agent-runtime/src/commands/inspect.ts +100 -0
- package/templates/hello-agent/packages/agent-runtime/src/commands/poll-whatsapp.ts +317 -0
- package/templates/hello-agent/packages/agent-runtime/src/commands/poll.ts +144 -0
- package/templates/hello-agent/packages/agent-runtime/src/commands/reconcile.ts +42 -0
- package/templates/hello-agent/packages/agent-runtime/src/commands/rerun.ts +86 -0
- package/templates/hello-agent/packages/agent-runtime/src/commands/resume.ts +43 -0
- package/templates/hello-agent/packages/agent-runtime/src/commands/start-whatsapp.ts +131 -0
- package/templates/hello-agent/packages/agent-runtime/src/commands/start.ts +245 -0
- package/templates/hello-agent/packages/agent-runtime/src/commands/verify.ts +129 -0
- package/templates/hello-agent/packages/agent-runtime/src/commands/webhook.ts +39 -0
- package/templates/hello-agent/packages/agent-runtime/src/default-kit.ts +83 -0
- package/templates/hello-agent/packages/agent-runtime/src/index.ts +12 -0
- package/templates/hello-agent/packages/agent-runtime/src/inspect.ts +797 -0
- package/templates/hello-agent/packages/agent-runtime/src/kit.ts +206 -0
- package/templates/hello-agent/packages/agent-runtime/src/poll.ts +105 -0
- package/templates/hello-agent/packages/agent-runtime/src/scenarios.ts +268 -0
- package/templates/hello-agent/packages/agent-runtime/src/setup.ts +229 -0
- package/templates/hello-agent/packages/agent-runtime/src/terminal.ts +154 -0
- package/templates/hello-agent/packages/agent-runtime/src/webhook.ts +131 -0
- package/templates/hello-agent/packages/agent-runtime/test/inspect.test.ts +336 -0
- package/templates/hello-agent/packages/agent-runtime/test/verify-cli.test.ts +127 -0
- package/templates/hello-agent/packages/agent-runtime/test/whatsapp-cloud-api.test.ts +232 -0
- package/templates/hello-agent/packages/agent-runtime/test/whatsapp-emulator.integration.test.ts +204 -0
- package/templates/hello-agent/packages/agent-runtime/test/whatsapp-evidence.test.ts +94 -0
- package/templates/hello-agent/packages/agent-runtime/test/whatsapp-rules.test.ts +174 -0
- package/templates/hello-agent/packages/agent-runtime/test/whatsapp-session.test.ts +135 -0
- package/templates/hello-agent/packages/agent-runtime/tsconfig.json +9 -0
- package/templates/hello-agent/tsconfig.base.json +16 -0
- package/templates/hello-agent/vitest.config.ts +12 -0
- package/templates/kits.lock.json +60 -12
- package/templates/supplier-payments-agent/LICENSE +21 -0
- package/templates/supplier-payments-agent/README.md +23 -0
- package/templates/supplier-payments-agent/_gitignore +10 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/.env.example +7 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/AGENTS.md +21 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/CLAUDE.md +21 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/README.md +118 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/SYSTEM_PROMPT.md +29 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/agent.yaml +26 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/evals/adversarial/batch-tampering.json +12 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/evals/adversarial/batch-tampering.transcript.jsonl +3 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/evals/adversarial/beneficiary-swap-mandate.json +9 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/evals/adversarial/beneficiary-swap.json +10 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/evals/adversarial/beneficiary-swap.transcript.jsonl +2 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/evals/adversarial/exfiltration.json +13 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/evals/adversarial/exfiltration.transcript.jsonl +3 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/evals/adversarial/false-authority.json +9 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/evals/adversarial/false-authority.transcript.jsonl +3 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/evals/adversarial/fractioning.json +15 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/evals/adversarial/fractioning.transcript.jsonl +6 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/evals/adversarial/model-total.json +10 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/evals/adversarial/model-total.transcript.jsonl +2 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/evals/adversarial/prompt-injection.json +9 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/evals/adversarial/prompt-injection.transcript.jsonl +2 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/evals/adversarial/webhook-replay.json +9 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/evals/eval.yaml +9 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/guardrails.json +15 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/mandate.example.json +70 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/package.json +26 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/runbook.md +19 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/scenarios/beneficiary-not-allowed.json +14 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/scenarios/beneficiary-not-allowed.transcript.jsonl +2 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/scenarios/cap-exceeded.json +12 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/scenarios/cap-exceeded.transcript.jsonl +2 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/scenarios/escalated-above-threshold.json +20 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/scenarios/escalated-above-threshold.transcript.jsonl +7 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/scenarios/happy-path.json +19 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/scenarios/happy-path.transcript.jsonl +3 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/scenarios/mandate-revoked.json +21 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/scenarios/mandate-revoked.transcript.jsonl +9 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/scenarios/partial-batch-failure.json +21 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/scenarios/partial-batch-failure.transcript.jsonl +3 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/scenarios/prompt-injection.json +12 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/scenarios/prompt-injection.transcript.jsonl +2 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/src/kit.ts +176 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/src/modules/batch-payout.ts +345 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/src/modules/pix-out.ts +118 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/src/payables.ts +76 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/test/adversarial.test.ts +53 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/test/batch.test.ts +384 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/test/check.test.ts +109 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/test/scenarios.test.ts +81 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/tools.json +53 -0
- package/templates/supplier-payments-agent/agents/supplier-payments-agent/tsconfig.json +9 -0
- package/templates/supplier-payments-agent/package.json +36 -0
- package/templates/supplier-payments-agent/packages/agent-core/README.md +21 -0
- package/templates/supplier-payments-agent/packages/agent-core/package.json +24 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/agent.ts +125 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/api/charge-rail.ts +175 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/api/client.ts +53 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/api/mandate-status.ts +58 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/api/rail.ts +117 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/api/sandbox-payer.ts +58 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/approval.ts +123 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/bundle.ts +159 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/channels.ts +140 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/check.ts +253 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/clock.ts +42 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/engine.ts +885 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/escalate.ts +85 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/events.ts +40 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/guardrails.ts +45 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/hash.ts +62 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/ids.ts +10 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/index.ts +32 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/mandate.ts +100 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/manifest.ts +76 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/providers/anthropic.ts +76 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/providers/replay.ts +63 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/providers/types.ts +39 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/rail.ts +101 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/receipt-verification.ts +401 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/revocation.ts +30 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/secrets.ts +30 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/state/store.ts +307 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/state-machine.ts +148 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/stubs/charge-rail.ts +195 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/stubs/mandate-status.ts +57 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/stubs/rail.ts +141 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/tools.ts +55 -0
- package/templates/supplier-payments-agent/packages/agent-core/src/types.ts +134 -0
- package/templates/supplier-payments-agent/packages/agent-core/test/approval.test.ts +117 -0
- package/templates/supplier-payments-agent/packages/agent-core/test/bundle.test.ts +85 -0
- package/templates/supplier-payments-agent/packages/agent-core/test/clock.test.ts +25 -0
- package/templates/supplier-payments-agent/packages/agent-core/test/engine.test.ts +596 -0
- package/templates/supplier-payments-agent/packages/agent-core/test/escalate.test.ts +42 -0
- package/templates/supplier-payments-agent/packages/agent-core/test/fixtures/staging-receipt-keys.json +18 -0
- package/templates/supplier-payments-agent/packages/agent-core/test/fixtures/staging-receipt.json +24 -0
- package/templates/supplier-payments-agent/packages/agent-core/test/helpers.ts +139 -0
- package/templates/supplier-payments-agent/packages/agent-core/test/mandate-status.test.ts +153 -0
- package/templates/supplier-payments-agent/packages/agent-core/test/manifest.test.ts +45 -0
- package/templates/supplier-payments-agent/packages/agent-core/test/receipt-verification.test.ts +316 -0
- package/templates/supplier-payments-agent/packages/agent-core/test/receivables.test.ts +316 -0
- package/templates/supplier-payments-agent/packages/agent-core/test/secrets.test.ts +28 -0
- package/templates/supplier-payments-agent/packages/agent-core/test/state-machine.test.ts +67 -0
- package/templates/supplier-payments-agent/packages/agent-core/test/transitions.type-test.ts +59 -0
- package/templates/supplier-payments-agent/packages/agent-core/tsconfig.json +10 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/README.md +68 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/bin.mjs +32 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/package.json +23 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/adversarial.ts +169 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/agent.ts +68 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/channels/contact.ts +13 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/channels/index.ts +72 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/channels/rules.ts +120 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/channels/types.ts +124 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/channels/whatsapp/cloud-api.ts +358 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/channels/whatsapp/emulator.ts +206 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/channels/whatsapp/evidence.ts +109 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/channels/whatsapp/index.ts +225 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/channels/whatsapp/open.ts +143 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/channels/whatsapp/present.ts +47 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/channels/whatsapp/run.ts +113 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/channels/whatsapp/session.ts +113 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/cli.ts +98 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/commands/check.ts +15 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/commands/decide.ts +63 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/commands/eval.ts +58 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/commands/inspect.ts +100 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/commands/poll-whatsapp.ts +317 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/commands/poll.ts +144 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/commands/reconcile.ts +42 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/commands/rerun.ts +86 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/commands/resume.ts +43 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/commands/start-whatsapp.ts +131 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/commands/start.ts +245 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/commands/verify.ts +129 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/commands/webhook.ts +39 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/default-kit.ts +83 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/index.ts +12 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/inspect.ts +797 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/kit.ts +206 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/poll.ts +105 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/scenarios.ts +268 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/setup.ts +229 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/terminal.ts +154 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/src/webhook.ts +131 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/test/inspect.test.ts +336 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/test/verify-cli.test.ts +127 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/test/whatsapp-cloud-api.test.ts +232 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/test/whatsapp-emulator.integration.test.ts +204 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/test/whatsapp-evidence.test.ts +94 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/test/whatsapp-rules.test.ts +174 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/test/whatsapp-session.test.ts +135 -0
- package/templates/supplier-payments-agent/packages/agent-runtime/tsconfig.json +9 -0
- package/templates/supplier-payments-agent/tsconfig.base.json +16 -0
- package/templates/supplier-payments-agent/vitest.config.ts +12 -0
- package/templates/bills-agent/agents/bills-agent/channels/terminal/index.ts +0 -108
- package/templates/bills-agent/agents/bills-agent/src/commands/approve.ts +0 -9
- package/templates/bills-agent/agents/bills-agent/src/commands/check.ts +0 -13
- package/templates/bills-agent/agents/bills-agent/src/commands/consent.ts +0 -26
- package/templates/bills-agent/agents/bills-agent/src/commands/decide.ts +0 -45
- package/templates/bills-agent/agents/bills-agent/src/commands/deny.ts +0 -7
- package/templates/bills-agent/agents/bills-agent/src/commands/eval.ts +0 -37
- package/templates/bills-agent/agents/bills-agent/src/commands/reconcile.ts +0 -38
- package/templates/bills-agent/agents/bills-agent/src/commands/rerun.ts +0 -65
- package/templates/bills-agent/agents/bills-agent/src/commands/resume.ts +0 -30
- package/templates/bills-agent/agents/bills-agent/src/main.ts +0 -185
- package/templates/collections-agent/agents/collections-agent/channels/terminal/index.ts +0 -178
- package/templates/collections-agent/agents/collections-agent/src/commands/approve.ts +0 -10
- package/templates/collections-agent/agents/collections-agent/src/commands/check.ts +0 -13
- package/templates/collections-agent/agents/collections-agent/src/commands/decide.ts +0 -48
- package/templates/collections-agent/agents/collections-agent/src/commands/deny.ts +0 -7
- package/templates/collections-agent/agents/collections-agent/src/commands/eval.ts +0 -39
- package/templates/collections-agent/agents/collections-agent/src/commands/poll.ts +0 -35
- package/templates/collections-agent/agents/collections-agent/src/commands/reconcile.ts +0 -38
- package/templates/collections-agent/agents/collections-agent/src/commands/rerun.ts +0 -55
- package/templates/collections-agent/agents/collections-agent/src/commands/resume.ts +0 -30
- package/templates/collections-agent/agents/collections-agent/src/commands/webhook.ts +0 -36
- package/templates/collections-agent/agents/collections-agent/src/main.ts +0 -193
- package/templates/collections-agent/agents/collections-agent/src/setup.ts +0 -250
package/templates/supplier-payments-agent/packages/agent-core/test/receipt-verification.test.ts
ADDED
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The wave-5 contract: a party with the receipt JSON and the published key set
|
|
3
|
+
* reaches the right answer, and the wrong answers stay apart from each other.
|
|
4
|
+
*
|
|
5
|
+
* The signatures here are made with stock `node:crypto` and a key generated in
|
|
6
|
+
* the test, never with the module under test: a test that signed with the
|
|
7
|
+
* verifier's own helper would prove the helper agrees with itself. The signed
|
|
8
|
+
* string is written out literally for the same reason — if the enterprise ever
|
|
9
|
+
* changes the shape, this line fails rather than following it.
|
|
10
|
+
*
|
|
11
|
+
* No network: the key set is a document, or a fetcher the test supplies.
|
|
12
|
+
*/
|
|
13
|
+
import { createPublicKey, generateKeyPairSync, sign as signDetached, verify as nodeVerify, type KeyObject } from "node:crypto";
|
|
14
|
+
import { readFileSync } from "node:fs";
|
|
15
|
+
import { join } from "node:path";
|
|
16
|
+
import { describe, expect, it } from "vitest";
|
|
17
|
+
import {
|
|
18
|
+
DEFAULT_RECEIPT_KEYS_URL,
|
|
19
|
+
RECEIPT_SIGNATURE_DOMAIN,
|
|
20
|
+
VERDICT_EXIT_CODES,
|
|
21
|
+
receiptKeysUrl,
|
|
22
|
+
receiptSigningString,
|
|
23
|
+
verifyReceipt,
|
|
24
|
+
verifyReceiptWithKeys,
|
|
25
|
+
type PublishedReceiptKey,
|
|
26
|
+
} from "../src/receipt-verification.js";
|
|
27
|
+
|
|
28
|
+
const KID = "did:web:id.codespar.dev#1";
|
|
29
|
+
const CHAIN = "9f2c4a1b6d0e8f3a5c7b9d1e2f4a6b8c0d2e4f6a8b0c2d4e6f8a0b2c4d6e8f01";
|
|
30
|
+
const RECEIPT_ID = "rcpt_V1StGXR8Z5jdHi6BmyT0sw";
|
|
31
|
+
|
|
32
|
+
function keypair() {
|
|
33
|
+
const { privateKey, publicKey } = generateKeyPairSync("ed25519");
|
|
34
|
+
const raw = publicKey.export({ format: "der", type: "spki" }).subarray(-32);
|
|
35
|
+
return { privateKey, x: Buffer.from(raw).toString("base64url") };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function publishedKey(x: string, over: Partial<PublishedReceiptKey> = {}): PublishedReceiptKey {
|
|
39
|
+
return { kid: KID, kty: "OKP", crv: "Ed25519", x, alg: "EdDSA", use: "sig", status: "active", ...over };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function keySet(...keys: PublishedReceiptKey[]) {
|
|
43
|
+
return { issuer: "did:web:id.codespar.dev", algorithm: "Ed25519", signing_string: `${RECEIPT_SIGNATURE_DOMAIN}:<receipt_id>:<chain>`, keys };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function seal(privateKey: KeyObject, receiptId = RECEIPT_ID, chain = CHAIN): string {
|
|
47
|
+
return signDetached(null, Buffer.from(`codespar-receipt:v1:${receiptId}:${chain}`, "utf8"), privateKey).toString("base64url");
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function receipt(over: Record<string, unknown> = {}) {
|
|
51
|
+
return {
|
|
52
|
+
receipt_id: RECEIPT_ID,
|
|
53
|
+
state: "paid",
|
|
54
|
+
mandate: { id: "mnd_1" },
|
|
55
|
+
payment: { amount_minor: 12345, payee: "es***@exemplo.com.br", attempt_id: "att_1", money_moved: false, sandbox: true, at: "2026-09-24T12:00:00.000Z" },
|
|
56
|
+
chain: CHAIN,
|
|
57
|
+
receipt_sig: "hmac-half-not-read-here",
|
|
58
|
+
receipt_sig_ed25519: null,
|
|
59
|
+
receipt_sig_kid: null,
|
|
60
|
+
...over,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
describe("the signed string", () => {
|
|
65
|
+
it("is the enterprise's, byte for byte", () => {
|
|
66
|
+
expect(receiptSigningString(RECEIPT_ID, CHAIN)).toBe(`codespar-receipt:v1:${RECEIPT_ID}:${CHAIN}`);
|
|
67
|
+
expect(RECEIPT_SIGNATURE_DOMAIN).toBe("codespar-receipt:v1");
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("names the key set of a deployment from its base url", () => {
|
|
71
|
+
expect(receiptKeysUrl("https://api.codespar.dev")).toBe(DEFAULT_RECEIPT_KEYS_URL);
|
|
72
|
+
expect(receiptKeysUrl("https://staging.example/v1/anything")).toBe("https://staging.example/.well-known/codespar-receipt-keys.json");
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe("verified", () => {
|
|
77
|
+
it("accepts a receipt CodeSpar sealed, and says which key signed it", () => {
|
|
78
|
+
const { privateKey, x } = keypair();
|
|
79
|
+
const report = verifyReceiptWithKeys(receipt({ receipt_sig_ed25519: seal(privateKey), receipt_sig_kid: KID }), keySet(publishedKey(x)));
|
|
80
|
+
expect(report.verdict).toBe("verified");
|
|
81
|
+
expect(report.receipt_id).toBe(RECEIPT_ID);
|
|
82
|
+
expect(report.kid).toBe(KID);
|
|
83
|
+
expect(report.key_status).toBe("active");
|
|
84
|
+
expect(report.signing_string).toBe(`codespar-receipt:v1:${RECEIPT_ID}:${CHAIN}`);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("accepts a signature made by a RETIRED key: rotation retires, it does not revoke", () => {
|
|
88
|
+
const { privateKey, x } = keypair();
|
|
89
|
+
const retired = publishedKey(x, { status: "retired", retired_at: "2026-09-01T00:00:00.000Z" });
|
|
90
|
+
const report = verifyReceiptWithKeys(receipt({ receipt_sig_ed25519: seal(privateKey), receipt_sig_kid: KID }), keySet(retired, publishedKey("ignored", { kid: "did:web:id.codespar.dev#2" })));
|
|
91
|
+
expect(report.verdict).toBe("verified");
|
|
92
|
+
expect(report.key_status).toBe("retired");
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("still verifies the proof bundle's masked copy: the signature covers the id and the chain, not the body", () => {
|
|
96
|
+
const { privateKey, x } = keypair();
|
|
97
|
+
const sealed = receipt({ receipt_sig_ed25519: seal(privateKey), receipt_sig_kid: KID });
|
|
98
|
+
const masked = { ...sealed, payment: { ...sealed.payment, payee: "es***@exemplo.com.br" }, raw: undefined };
|
|
99
|
+
expect(verifyReceiptWithKeys(masked, keySet(publishedKey(x))).verdict).toBe("verified");
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
describe("tampered", () => {
|
|
104
|
+
it("refuses a receipt whose chain was changed after it was sealed", () => {
|
|
105
|
+
const { privateKey, x } = keypair();
|
|
106
|
+
const report = verifyReceiptWithKeys(receipt({ receipt_sig_ed25519: seal(privateKey), receipt_sig_kid: KID, chain: CHAIN.replace(/^9/, "8") }), keySet(publishedKey(x)));
|
|
107
|
+
expect(report.verdict).toBe("tampered");
|
|
108
|
+
expect(report.reason).toBe("signature_does_not_match");
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("refuses a signature lifted from another receipt: the id is in the signed string", () => {
|
|
112
|
+
const { privateKey, x } = keypair();
|
|
113
|
+
const other = seal(privateKey, "rcpt_somebodyelsesreceipt", CHAIN);
|
|
114
|
+
const report = verifyReceiptWithKeys(receipt({ receipt_sig_ed25519: other, receipt_sig_kid: KID }), keySet(publishedKey(x)));
|
|
115
|
+
expect(report.verdict).toBe("tampered");
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("refuses a signature made by a key that is not the published one, and names the deployment case", () => {
|
|
119
|
+
const { privateKey } = keypair();
|
|
120
|
+
const other = keypair();
|
|
121
|
+
// The shape of the staging-receipt-against-production-keys mistake: both
|
|
122
|
+
// deployments publish a key called `did:web:id.codespar.dev#1` and the two
|
|
123
|
+
// are not the same key, so the message has to offer that reading.
|
|
124
|
+
const report = verifyReceiptWithKeys(receipt({ receipt_sig_ed25519: seal(privateKey), receipt_sig_kid: KID }), keySet(publishedKey(other.x)));
|
|
125
|
+
expect(report.verdict).toBe("tampered");
|
|
126
|
+
expect(report.message).toContain("deployment");
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("refuses a signature that is not 64 base64url bytes", () => {
|
|
130
|
+
const { x } = keypair();
|
|
131
|
+
const report = verifyReceiptWithKeys(receipt({ receipt_sig_ed25519: "not-a-signature", receipt_sig_kid: KID }), keySet(publishedKey(x)));
|
|
132
|
+
expect(report.verdict).toBe("tampered");
|
|
133
|
+
expect(report.reason).toBe("signature_malformed");
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
describe("unsigned", () => {
|
|
138
|
+
it("is not a failure: it says the receipt predates the capability, and that the HMAC is unaffected", () => {
|
|
139
|
+
const report = verifyReceiptWithKeys(receipt(), keySet());
|
|
140
|
+
expect(report.verdict).toBe("unsigned");
|
|
141
|
+
expect(report.message).toContain("never will");
|
|
142
|
+
expect(report.message).toContain("HMAC");
|
|
143
|
+
expect(report.kid).toBeNull();
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("answers the same when the two fields are absent rather than null", () => {
|
|
147
|
+
const { receipt_sig_ed25519: _sig, receipt_sig_kid: _kid, ...older } = receipt();
|
|
148
|
+
expect(verifyReceiptWithKeys(older, keySet()).verdict).toBe("unsigned");
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("answers without a key set at all, and without fetching one", async () => {
|
|
152
|
+
let fetched = 0;
|
|
153
|
+
const report = await verifyReceipt(receipt(), {
|
|
154
|
+
url: "https://example.invalid/keys.json",
|
|
155
|
+
fetch: async () => {
|
|
156
|
+
fetched += 1;
|
|
157
|
+
return keySet();
|
|
158
|
+
},
|
|
159
|
+
});
|
|
160
|
+
expect(report.verdict).toBe("unsigned");
|
|
161
|
+
expect(fetched).toBe(0);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("covers a paid charge, which the API seals nothing for", () => {
|
|
165
|
+
const charge = { receipt_id: "chg_1", kind: "charge", state: "paid", chain: null, receipt_sig: null, receipt_sig_ed25519: null, receipt_sig_kid: null };
|
|
166
|
+
expect(verifyReceiptWithKeys(charge, keySet()).verdict).toBe("unsigned");
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
describe("unknown_key", () => {
|
|
171
|
+
it("refuses to guess when the kid is not published", () => {
|
|
172
|
+
const { privateKey, x } = keypair();
|
|
173
|
+
const report = verifyReceiptWithKeys(receipt({ receipt_sig_ed25519: seal(privateKey), receipt_sig_kid: "did:web:id.codespar.dev#9" }), keySet(publishedKey(x)));
|
|
174
|
+
expect(report.verdict).toBe("unknown_key");
|
|
175
|
+
expect(report.kid).toBe("did:web:id.codespar.dev#9");
|
|
176
|
+
expect(report.message).toContain("retired keys published");
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it("does not fall back to the only key in the set", () => {
|
|
180
|
+
const { privateKey, x } = keypair();
|
|
181
|
+
const report = verifyReceiptWithKeys(receipt({ receipt_sig_ed25519: seal(privateKey), receipt_sig_kid: "did:web:other.example#1" }), keySet(publishedKey(x)));
|
|
182
|
+
expect(report.verdict).toBe("unknown_key");
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
describe("unreachable", () => {
|
|
187
|
+
it("is what a failed fetch answers — never `tampered`", async () => {
|
|
188
|
+
const { privateKey } = keypair();
|
|
189
|
+
const report = await verifyReceipt(receipt({ receipt_sig_ed25519: seal(privateKey), receipt_sig_kid: KID }), {
|
|
190
|
+
url: "https://example.invalid/keys.json",
|
|
191
|
+
fetch: async () => {
|
|
192
|
+
throw new Error("getaddrinfo ENOTFOUND example.invalid");
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
|
+
expect(report.verdict).toBe("unreachable");
|
|
196
|
+
expect(report.reason).toBe("fetch_failed");
|
|
197
|
+
expect(report.message).toContain("Nothing is proved and nothing is disproved");
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("is what a document that is not a key set answers", () => {
|
|
201
|
+
const { privateKey } = keypair();
|
|
202
|
+
const report = verifyReceiptWithKeys(receipt({ receipt_sig_ed25519: seal(privateKey), receipt_sig_kid: KID }), { error: "not found" });
|
|
203
|
+
expect(report.verdict).toBe("unreachable");
|
|
204
|
+
expect(report.reason).toBe("key_document_unreadable");
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it("is what an unusable published key answers: a key we cannot read tells us nothing about the signature", () => {
|
|
208
|
+
const { privateKey } = keypair();
|
|
209
|
+
const report = verifyReceiptWithKeys(receipt({ receipt_sig_ed25519: seal(privateKey), receipt_sig_kid: KID }), keySet(publishedKey("AAAA")));
|
|
210
|
+
expect(report.verdict).toBe("unreachable");
|
|
211
|
+
expect(report.reason).toBe("key_unusable");
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it("fetches the published set and verifies against it when the fetch works", async () => {
|
|
215
|
+
const { privateKey, x } = keypair();
|
|
216
|
+
const seen: string[] = [];
|
|
217
|
+
const report = await verifyReceipt(receipt({ receipt_sig_ed25519: seal(privateKey), receipt_sig_kid: KID }), {
|
|
218
|
+
fetch: async (url) => {
|
|
219
|
+
seen.push(url);
|
|
220
|
+
return keySet(publishedKey(x));
|
|
221
|
+
},
|
|
222
|
+
});
|
|
223
|
+
expect(seen).toEqual([DEFAULT_RECEIPT_KEYS_URL]);
|
|
224
|
+
expect(report.verdict).toBe("verified");
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
describe("malformed", () => {
|
|
229
|
+
it("does not call an arbitrary JSON file an unsigned receipt", () => {
|
|
230
|
+
const report = verifyReceiptWithKeys({ hello: "world" }, keySet());
|
|
231
|
+
expect(report.verdict).toBe("malformed");
|
|
232
|
+
expect(report.reason).toBe("receipt_id_missing");
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it("refuses a signature whose kid was deleted, instead of reading it as unsigned", () => {
|
|
236
|
+
const { privateKey } = keypair();
|
|
237
|
+
const report = verifyReceiptWithKeys(receipt({ receipt_sig_ed25519: seal(privateKey), receipt_sig_kid: null }), keySet());
|
|
238
|
+
expect(report.verdict).toBe("malformed");
|
|
239
|
+
expect(report.reason).toBe("kid_missing");
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it("refuses a kid whose signature was deleted", () => {
|
|
243
|
+
const report = verifyReceiptWithKeys(receipt({ receipt_sig_kid: KID }), keySet());
|
|
244
|
+
expect(report.verdict).toBe("malformed");
|
|
245
|
+
expect(report.reason).toBe("signature_missing");
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it("refuses a signed receipt whose chain was deleted: the signed string cannot be rebuilt", () => {
|
|
249
|
+
const { privateKey } = keypair();
|
|
250
|
+
const report = verifyReceiptWithKeys(receipt({ receipt_sig_ed25519: seal(privateKey), receipt_sig_kid: KID, chain: null }), keySet());
|
|
251
|
+
expect(report.verdict).toBe("malformed");
|
|
252
|
+
expect(report.reason).toBe("chain_missing");
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it("refuses a JSON array or a string", () => {
|
|
256
|
+
expect(verifyReceiptWithKeys([], keySet()).verdict).toBe("malformed");
|
|
257
|
+
expect(verifyReceiptWithKeys("rcpt_1", keySet()).verdict).toBe("malformed");
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
describe("the exit codes", () => {
|
|
262
|
+
it("keep every verdict apart, and do not call an unsigned receipt a success", () => {
|
|
263
|
+
expect(VERDICT_EXIT_CODES.verified).toBe(0);
|
|
264
|
+
expect(new Set(Object.values(VERDICT_EXIT_CODES)).size).toBe(Object.keys(VERDICT_EXIT_CODES).length);
|
|
265
|
+
expect(Object.values(VERDICT_EXIT_CODES)).not.toContain(2); // 2 is the usage error every other command uses
|
|
266
|
+
});
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
describe("the check a third party reimplements", () => {
|
|
270
|
+
it("is four lines of node:crypto over the published JWK", () => {
|
|
271
|
+
const { privateKey, x } = keypair();
|
|
272
|
+
const signature = seal(privateKey);
|
|
273
|
+
// Exactly what someone outside CodeSpar writes, with no code of ours.
|
|
274
|
+
const ok = nodeVerify(
|
|
275
|
+
null,
|
|
276
|
+
Buffer.from(`codespar-receipt:v1:${RECEIPT_ID}:${CHAIN}`, "utf8"),
|
|
277
|
+
createPublicKey({ key: { kty: "OKP", crv: "Ed25519", x }, format: "jwk" }),
|
|
278
|
+
Buffer.from(signature, "base64url"),
|
|
279
|
+
);
|
|
280
|
+
expect(ok).toBe(true);
|
|
281
|
+
expect(verifyReceiptWithKeys(receipt({ receipt_sig_ed25519: signature, receipt_sig_kid: KID }), keySet(publishedKey(x))).verdict).toBe("verified");
|
|
282
|
+
});
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* The one fixture that is not this test's own: a receipt the CodeSpar sandbox
|
|
287
|
+
* actually sealed (bills-agent, staging, 2026-09-24), as the proof bundle
|
|
288
|
+
* wrote it, and the key set that deployment publishes, as it was served. Every
|
|
289
|
+
* other case here signs with a key the test made, which proves the module is
|
|
290
|
+
* self-consistent; this one proves it agrees with the signer.
|
|
291
|
+
*
|
|
292
|
+
* Offline: both are files. If the API ever changes the signed string, this is
|
|
293
|
+
* the case that fails.
|
|
294
|
+
*/
|
|
295
|
+
describe("a receipt the sandbox really sealed", () => {
|
|
296
|
+
const dir = join(import.meta.dirname, "fixtures");
|
|
297
|
+
const real = JSON.parse(readFileSync(join(dir, "staging-receipt.json"), "utf8")) as Record<string, unknown>;
|
|
298
|
+
const realKeys = JSON.parse(readFileSync(join(dir, "staging-receipt-keys.json"), "utf8")) as unknown;
|
|
299
|
+
|
|
300
|
+
it("verifies against the key set that deployment publishes", () => {
|
|
301
|
+
const report = verifyReceiptWithKeys(real, realKeys, "staging");
|
|
302
|
+
expect(report.verdict).toBe("verified");
|
|
303
|
+
expect(report.kid).toBe("did:web:id.codespar.dev#1");
|
|
304
|
+
expect(report.signing_string).toBe(`codespar-receipt:v1:${real["receipt_id"] as string}:${real["chain"] as string}`);
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
it("is refused when one character of the chain changes", () => {
|
|
308
|
+
const chain = real["chain"] as string;
|
|
309
|
+
const edited = { ...real, chain: chain.slice(0, -1) + (chain.endsWith("0") ? "1" : "0") };
|
|
310
|
+
expect(verifyReceiptWithKeys(edited, realKeys, "staging").verdict).toBe("tampered");
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
it("is refused when the receipt id changes, which is why the id is in the signed string", () => {
|
|
314
|
+
expect(verifyReceiptWithKeys({ ...real, receipt_id: "rcpt_0000000000000000000000" }, realKeys, "staging").verdict).toBe("tampered");
|
|
315
|
+
});
|
|
316
|
+
});
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The receivable side of the core (collections-agent): a charge is ISSUED,
|
|
3
|
+
* not settled, when the rail accepts it; the execution stays `executing`
|
|
4
|
+
* until the payer acts, and closes from a lookup (the poll) or from a
|
|
5
|
+
* `commerce.charge.*` event (the webhook). Never twice, never on prose.
|
|
6
|
+
*/
|
|
7
|
+
import { mkdtempSync } from "node:fs";
|
|
8
|
+
import { tmpdir } from "node:os";
|
|
9
|
+
import { join } from "node:path";
|
|
10
|
+
import { describe, expect, it } from "vitest";
|
|
11
|
+
import { outcomeOf, type ChargeView } from "../src/api/charge-rail.js";
|
|
12
|
+
import { hmacSigner } from "../src/approval.js";
|
|
13
|
+
import { ProofBundle } from "../src/bundle.js";
|
|
14
|
+
import { ExecutionEngine, type EngineDeps, type PolicyExtension } from "../src/engine.js";
|
|
15
|
+
import { itemsHash } from "../src/hash.js";
|
|
16
|
+
import { StateStore } from "../src/state/store.js";
|
|
17
|
+
import { LocalMandateStatusStub } from "../src/stubs/mandate-status.js";
|
|
18
|
+
import { StubChargeRail, type StubChargeRailOptions } from "../src/stubs/charge-rail.js";
|
|
19
|
+
import { testGuardrails, testManifest, testMandate } from "./helpers.js";
|
|
20
|
+
|
|
21
|
+
const DEBTOR = "11144477735";
|
|
22
|
+
const OTHER = "22233344450";
|
|
23
|
+
|
|
24
|
+
function receivables(options: { mode?: "human" | "mandate"; rail?: StubChargeRailOptions; policyExtension?: PolicyExtension; dir?: string } = {}) {
|
|
25
|
+
const dir = options.dir ?? mkdtempSync(join(tmpdir(), "agent-core-recv-"));
|
|
26
|
+
let now = new Date("2026-09-23T18:00:00Z");
|
|
27
|
+
const clock = () => now;
|
|
28
|
+
const store = new StateStore(join(dir, "state.db"));
|
|
29
|
+
const gate = new LocalMandateStatusStub(store, clock);
|
|
30
|
+
const rail = new StubChargeRail(store, { clock, ...(options.rail ?? {}) });
|
|
31
|
+
const bundle = new ProofBundle(join(dir, "runs"), "run_recv");
|
|
32
|
+
const mode = options.mode ?? "mandate";
|
|
33
|
+
const deps: EngineDeps = {
|
|
34
|
+
store,
|
|
35
|
+
rail,
|
|
36
|
+
status: gate,
|
|
37
|
+
signer: hmacSigner("test", Buffer.alloc(32, 2)),
|
|
38
|
+
manifest: testManifest({ name: "collections-agent", escalate_above: {}, maturity: { "bolepix-receivables": "sandbox" } }),
|
|
39
|
+
guardrails: testGuardrails({ approval: mode, escalate_above: {} }),
|
|
40
|
+
mandate: testMandate({
|
|
41
|
+
agent_id: "collections-agent",
|
|
42
|
+
purpose: "cobranca",
|
|
43
|
+
merchant_pin_kind: "document",
|
|
44
|
+
merchant_allowlist: [DEBTOR, OTHER],
|
|
45
|
+
beneficiaries: [{ alias: "acordo-1", name: "Joana Devedora", payee: DEBTOR }],
|
|
46
|
+
}),
|
|
47
|
+
bundle,
|
|
48
|
+
mode,
|
|
49
|
+
runId: "run_recv",
|
|
50
|
+
onBehalfOf: "merchant_demo",
|
|
51
|
+
clock,
|
|
52
|
+
policyExtension: options.policyExtension,
|
|
53
|
+
};
|
|
54
|
+
const engine = new ExecutionEngine(deps);
|
|
55
|
+
return { dir, store, rail, bundle, engine, setNow: (d: Date) => (now = d) };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
describe("a receivable is accepted, then settled by the payer", () => {
|
|
59
|
+
it("execute leaves the execution executing (awaiting_settlement) with the instrument; two looks later it is settled with the charge as receipt", async () => {
|
|
60
|
+
const h = receivables();
|
|
61
|
+
const d = await h.engine.draft({ items: [{ payee: "acordo-1", amount: 108000, due_date: "2026-09-30", description: "acordo 1042, a vista" }] });
|
|
62
|
+
if (!d.ok) throw new Error("refused");
|
|
63
|
+
expect(d.execution.state).toBe("approved");
|
|
64
|
+
expect(d.execution.items[0]?.due_date).toBe("2026-09-30");
|
|
65
|
+
|
|
66
|
+
const issued = await h.engine.execute(d.execution.id);
|
|
67
|
+
expect(issued.state).toBe("executing");
|
|
68
|
+
expect(issued.reason).toBe("awaiting_settlement");
|
|
69
|
+
expect(issued.outcomes[0]).toMatchObject({ status: "accepted", instrument: { payable: false, status: "PROCESSING" } });
|
|
70
|
+
expect(h.store.getOutbox(issued.idempotency_key)!.status).toBe("sent");
|
|
71
|
+
|
|
72
|
+
const registered = await h.engine.reconcile(issued.id);
|
|
73
|
+
expect(registered.state).toBe("executing");
|
|
74
|
+
expect(registered.outcomes[0]?.instrument).toMatchObject({ payable: true, status: "PENDING" });
|
|
75
|
+
expect(registered.outcomes[0]?.instrument?.pix_copy_paste).toContain("br.gov.bcb.pix");
|
|
76
|
+
|
|
77
|
+
const paid = await h.engine.reconcile(issued.id);
|
|
78
|
+
expect(paid.state).toBe("settled");
|
|
79
|
+
expect(paid.history.map((t) => t.to)).toEqual(["approved", "executing", "settled"]);
|
|
80
|
+
expect(paid.outcomes[0]).toMatchObject({ status: "settled", receipt_id: paid.outcomes[0]!.transaction_id });
|
|
81
|
+
expect(h.store.getOutbox(issued.idempotency_key)!.status).toBe("done");
|
|
82
|
+
expect(h.bundle.listReceipts()).toHaveLength(1);
|
|
83
|
+
const events = h.bundle.readEvents();
|
|
84
|
+
expect(events.filter((e) => e["type"] === "commerce.charge.paid")).toHaveLength(1);
|
|
85
|
+
expect(events.filter((e) => e["type"] === "charge.instrument").map((e) => (e["payload"] as { payable: boolean }).payable)).toEqual([false, true]);
|
|
86
|
+
// A third look changes nothing and issues nothing.
|
|
87
|
+
expect((await h.engine.reconcile(issued.id)).state).toBe("settled");
|
|
88
|
+
expect(h.rail.issueCount).toBe(1);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("a receivable nobody pays closes failed with charge_expired, never settled", async () => {
|
|
92
|
+
const h = receivables({ rail: { payer: "expires" } });
|
|
93
|
+
const d = await h.engine.draft({ items: [{ payee: "acordo-1", amount: 50000, due_date: "2026-09-25" }] });
|
|
94
|
+
if (!d.ok) throw new Error("refused");
|
|
95
|
+
const issued = await h.engine.execute(d.execution.id);
|
|
96
|
+
await h.engine.reconcile(issued.id);
|
|
97
|
+
const closed = await h.engine.reconcile(issued.id);
|
|
98
|
+
expect(closed.state).toBe("failed");
|
|
99
|
+
expect(closed.reason).toBe("charge_expired");
|
|
100
|
+
expect(h.bundle.listReceipts()).toHaveLength(0);
|
|
101
|
+
expect(h.store.getOutbox(issued.idempotency_key)!.status).toBe("failed");
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("the poll logs an observed state once, however many times it sees it", async () => {
|
|
105
|
+
const h = receivables({ rail: { payer: "never" } });
|
|
106
|
+
const d = await h.engine.draft({ items: [{ payee: "acordo-1", amount: 50000, due_date: "2026-09-25" }] });
|
|
107
|
+
if (!d.ok) throw new Error("refused");
|
|
108
|
+
const issued = await h.engine.execute(d.execution.id);
|
|
109
|
+
for (let i = 0; i < 5; i += 1) await h.engine.reconcile(issued.id);
|
|
110
|
+
const events = h.store.listEvents({ execution_id: issued.id });
|
|
111
|
+
// Five looks, one observed state (PENDING, payable): one line. The instrument log names the two states the charge went through.
|
|
112
|
+
expect(events.filter((e) => e.type === "rail.reconcile")).toHaveLength(1);
|
|
113
|
+
expect(events.filter((e) => e.type === "charge.instrument")).toHaveLength(2);
|
|
114
|
+
expect(events.filter((e) => e.type === "execution.awaiting_settlement")).toHaveLength(1);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("killed after the issuance and resumed: one charge, one settlement", async () => {
|
|
118
|
+
const h = receivables();
|
|
119
|
+
const d = await h.engine.draft({ items: [{ payee: "acordo-1", amount: 50000, due_date: "2026-09-25" }] });
|
|
120
|
+
if (!d.ok) throw new Error("refused");
|
|
121
|
+
let died = false;
|
|
122
|
+
const flaky = receivables({ dir: h.dir, rail: { afterDispatch: () => { died = true; throw new Error("killed"); } } });
|
|
123
|
+
await expect(flaky.engine.execute(d.execution.id)).rejects.toThrow("killed");
|
|
124
|
+
expect(died).toBe(true);
|
|
125
|
+
// The row says executing and the outbox says sent: resume reconciles, never re-issues.
|
|
126
|
+
const again = receivables({ dir: h.dir });
|
|
127
|
+
const stuck = again.store.getExecution(d.execution.id)!;
|
|
128
|
+
expect(stuck.state).toBe("executing");
|
|
129
|
+
const first = await again.engine.resumePending(stuck.id);
|
|
130
|
+
expect(first.state).toBe("executing");
|
|
131
|
+
expect(first.outcomes[0]?.status).toBe("accepted");
|
|
132
|
+
const settled = await again.engine.reconcile(stuck.id);
|
|
133
|
+
expect(settled.state).toBe("settled");
|
|
134
|
+
expect(again.rail.issueCount).toBe(0);
|
|
135
|
+
expect((again.store as unknown as { db: { prepare(sql: string): { get(): { n: number } } } }).db.prepare("SELECT COUNT(*) AS n FROM stub_rail_attempts").get().n).toBe(1);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("an instalment agreement is N receivables; the execution settles when every parcel is paid", async () => {
|
|
139
|
+
const h = receivables();
|
|
140
|
+
const d = await h.engine.draft({ items: [{ payee: "acordo-1", amount: 40000, due_date: "2026-09-30" }, { payee: "acordo-1", amount: 40000, due_date: "2026-10-30" }, { payee: "acordo-1", amount: 40000, due_date: "2026-11-30" }] });
|
|
141
|
+
if (!d.ok) throw new Error("refused");
|
|
142
|
+
expect(d.execution.total).toBe(120000);
|
|
143
|
+
const issued = await h.engine.execute(d.execution.id);
|
|
144
|
+
expect(issued.outcomes.map((o) => o.status)).toEqual(["accepted", "accepted", "accepted"]);
|
|
145
|
+
expect(h.rail.issueCount).toBe(3);
|
|
146
|
+
const a2 = issued.outcomes[2]!.attempt_id;
|
|
147
|
+
h.rail.decide(a2, "never");
|
|
148
|
+
await h.engine.reconcile(issued.id);
|
|
149
|
+
const partly = await h.engine.reconcile(issued.id);
|
|
150
|
+
expect(partly.state).toBe("executing");
|
|
151
|
+
expect(partly.outcomes.map((o) => o.status)).toEqual(["settled", "settled", "accepted"]);
|
|
152
|
+
h.rail.decide(a2, "pays");
|
|
153
|
+
const done = await h.engine.reconcile(issued.id);
|
|
154
|
+
expect(done.state).toBe("settled");
|
|
155
|
+
expect(h.bundle.listReceipts()).toHaveLength(3);
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
describe("the webhook path: commerce.charge.* by attempt id or by the rail's charge id", () => {
|
|
160
|
+
it("paid twice settles once; paid before created is fine; expired after paid moves nothing", async () => {
|
|
161
|
+
const h = receivables({ rail: { payer: "never" } });
|
|
162
|
+
const d = await h.engine.draft({ items: [{ payee: "acordo-1", amount: 50000, due_date: "2026-09-25" }] });
|
|
163
|
+
if (!d.ok) throw new Error("refused");
|
|
164
|
+
const issued = await h.engine.execute(d.execution.id);
|
|
165
|
+
const chargeId = issued.outcomes[0]!.transaction_id!;
|
|
166
|
+
expect(h.engine.ingestExternalEvent({ event_id: "evt_paid_1", type: "commerce.charge.paid", transaction_id: chargeId })).toEqual({ applied: true, reason: "settled" });
|
|
167
|
+
expect(h.engine.ingestExternalEvent({ event_id: "evt_paid_1", type: "commerce.charge.paid", transaction_id: chargeId })).toEqual({ applied: false, reason: "duplicate event id" });
|
|
168
|
+
expect(h.engine.ingestExternalEvent({ event_id: "evt_created_late", type: "commerce.charge.created", transaction_id: chargeId })).toMatchObject({ applied: false });
|
|
169
|
+
expect(h.engine.ingestExternalEvent({ event_id: "evt_paid_2", type: "commerce.charge.paid", transaction_id: chargeId })).toMatchObject({ applied: false, reason: "execution already settled" });
|
|
170
|
+
expect(h.engine.ingestExternalEvent({ event_id: "evt_expired_late", type: "commerce.charge.expired", transaction_id: chargeId })).toMatchObject({ applied: false, reason: "execution already settled" });
|
|
171
|
+
const settled = h.store.getExecution(issued.id)!;
|
|
172
|
+
expect(settled.state).toBe("settled");
|
|
173
|
+
expect(settled.outcomes[0]).toMatchObject({ status: "settled", transaction_id: chargeId, receipt_id: chargeId });
|
|
174
|
+
expect(h.store.listEvents({ execution_id: issued.id }).filter((e) => e.type === "execution.transition" && (e.payload as { to: string }).to === "settled")).toHaveLength(1);
|
|
175
|
+
// The settlement came by event, so the bundle holds no receipt yet; collectReceipts fetches it once.
|
|
176
|
+
expect(h.bundle.listReceipts()).toHaveLength(0);
|
|
177
|
+
expect(await h.engine.collectReceipts(issued.id)).toEqual([chargeId]);
|
|
178
|
+
expect(await h.engine.collectReceipts(issued.id)).toEqual([]);
|
|
179
|
+
expect(h.bundle.listReceipts()).toHaveLength(1);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("expired by event closes failed with charge_expired; a later paid for the same charge moves nothing", async () => {
|
|
183
|
+
const h = receivables({ rail: { payer: "never" } });
|
|
184
|
+
const d = await h.engine.draft({ items: [{ payee: "acordo-1", amount: 50000, due_date: "2026-09-25" }] });
|
|
185
|
+
if (!d.ok) throw new Error("refused");
|
|
186
|
+
const issued = await h.engine.execute(d.execution.id);
|
|
187
|
+
const attempt = issued.outcomes[0]!.attempt_id;
|
|
188
|
+
expect(h.engine.ingestExternalEvent({ event_id: "evt_exp", type: "commerce.charge.expired", attempt_id: attempt })).toEqual({ applied: true, reason: "failed" });
|
|
189
|
+
expect(h.store.getExecution(issued.id)!.reason).toBe("charge_expired");
|
|
190
|
+
expect(h.engine.ingestExternalEvent({ event_id: "evt_paid_after", type: "commerce.charge.paid", attempt_id: attempt })).toMatchObject({ applied: false });
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it("an event naming a charge this kit never issued is not applied", () => {
|
|
194
|
+
const h = receivables();
|
|
195
|
+
expect(h.engine.ingestExternalEvent({ event_id: "evt_x", type: "commerce.charge.paid", transaction_id: "chg_someone_elses" })).toEqual({ applied: false, reason: "unknown attempt" });
|
|
196
|
+
expect(h.engine.ingestExternalEvent({ event_id: "evt_y", type: "commerce.charge.paid" })).toMatchObject({ applied: false });
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
describe("the kit's envelope runs at every gate, and only tightens", () => {
|
|
201
|
+
const envelope: PolicyExtension = (execution) => {
|
|
202
|
+
const principal = 120000;
|
|
203
|
+
if (execution.total < principal * 0.85) return { reason: "outside_envelope", detail: `total ${execution.total} is below the floor of ${principal * 0.85} (15% off ${principal})` };
|
|
204
|
+
return undefined;
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
it("a proposal below the discount floor is denied at draft, even in human mode", async () => {
|
|
208
|
+
const h = receivables({ mode: "human", policyExtension: envelope });
|
|
209
|
+
const d = await h.engine.draft({ items: [{ payee: "acordo-1", amount: 1000, due_date: "2026-09-30" }] });
|
|
210
|
+
if (!d.ok) throw new Error("refused");
|
|
211
|
+
expect(d.execution.state).toBe("denied");
|
|
212
|
+
expect(d.execution.reason).toBe("outside_envelope");
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it("a debtor without an open agreement is not a payee: denied in mandate, blocked for the human, never approvable", async () => {
|
|
216
|
+
const h = receivables({ mode: "human", policyExtension: envelope });
|
|
217
|
+
const d = await h.engine.draft({ items: [{ payee: "99988877766", amount: 120000, due_date: "2026-09-30" }] });
|
|
218
|
+
if (!d.ok) throw new Error("refused");
|
|
219
|
+
expect(d.execution.state).toBe("awaiting_approval");
|
|
220
|
+
expect(d.execution.blocking_reasons).toEqual(["beneficiary_not_allowed"]);
|
|
221
|
+
expect(h.engine.approve(d.execution.id, { id: "usr_operator", channel: "terminal" }).state).toBe("denied");
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it("the due date is part of the items_hash: moving it after approval is another charge", () => {
|
|
225
|
+
const a = itemsHash([{ beneficiary: "x", payee: DEBTOR, amount: 100, currency: "BRL", due_date: "2026-09-30" }]);
|
|
226
|
+
const b = itemsHash([{ beneficiary: "x", payee: DEBTOR, amount: 100, currency: "BRL", due_date: "2026-10-30" }]);
|
|
227
|
+
const c = itemsHash([{ beneficiary: "x", payee: DEBTOR, amount: 100, currency: "BRL" }]);
|
|
228
|
+
expect(a).not.toBe(b);
|
|
229
|
+
expect(a).not.toBe(c);
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
describe("outcomeOf: the API's charge view, mapped by fields and never by prose", () => {
|
|
234
|
+
const base: ChargeView = {
|
|
235
|
+
id: "chg_1",
|
|
236
|
+
status: "PENDING",
|
|
237
|
+
local_status: "pending",
|
|
238
|
+
status_conflict: false,
|
|
239
|
+
method: "boleto",
|
|
240
|
+
currency: "BRL",
|
|
241
|
+
amount: 1080.5,
|
|
242
|
+
amount_minor: 108050,
|
|
243
|
+
due_date: "2026-09-30",
|
|
244
|
+
payable: true,
|
|
245
|
+
boleto_bar_code: "1".repeat(44),
|
|
246
|
+
boleto_bank_line: "2".repeat(47),
|
|
247
|
+
pix_copy_paste: "000201...",
|
|
248
|
+
credit_correlation_armed: true,
|
|
249
|
+
payment_in_flight: false,
|
|
250
|
+
settlement: null,
|
|
251
|
+
issuance_unconfirmed: false,
|
|
252
|
+
};
|
|
253
|
+
it("maps the five states", () => {
|
|
254
|
+
expect(outcomeOf(base)).toMatchObject({ status: "accepted", transaction_id: "chg_1", instrument: { payable: true, status: "PENDING", pix_copy_paste: "000201..." } });
|
|
255
|
+
expect(outcomeOf({ ...base, status: "PROCESSING", payable: false, pix_copy_paste: null })).toMatchObject({ status: "accepted", instrument: { payable: false } });
|
|
256
|
+
expect(outcomeOf({ ...base, local_status: "settled", status: "CONFIRMED", settlement: "confirmed" })).toMatchObject({ status: "settled", transaction_id: "chg_1", receipt_id: "chg_1", money_moved: false });
|
|
257
|
+
expect(outcomeOf({ ...base, local_status: "expired", status: "CANCELLED" })).toMatchObject({ status: "failed", code: "charge_expired" });
|
|
258
|
+
expect(outcomeOf({ ...base, status: "EXPIRED" })).toMatchObject({ status: "failed", code: "charge_expired" });
|
|
259
|
+
expect(outcomeOf({ ...base, status: "CANCELLED" })).toMatchObject({ status: "failed", code: "charge_cancelled" });
|
|
260
|
+
expect(outcomeOf({ ...base, id: null, issuance_unconfirmed: true })).toEqual({ status: "in_flight" });
|
|
261
|
+
});
|
|
262
|
+
it("a payment notified and not yet credited is still accepted: nothing settles on a notification", () => {
|
|
263
|
+
expect(outcomeOf({ ...base, payment_in_flight: true, settlement: "pending" })).toMatchObject({ status: "accepted" });
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
describe("CodeSparChargeRail: what crosses the wire on a create", () => {
|
|
268
|
+
it("sends consumer_id, method boleto, due_date, the attempt id as idempotency_key and the amount in major units; refuses without a consumer", async () => {
|
|
269
|
+
const { CodeSparChargeRail } = await import("../src/api/charge-rail.js");
|
|
270
|
+
const calls: Array<{ path: string; body: Record<string, unknown> }> = [];
|
|
271
|
+
const api = {
|
|
272
|
+
post: async (path: string, opts: { body: Record<string, unknown> }) => {
|
|
273
|
+
calls.push({ path, body: opts.body });
|
|
274
|
+
return { id: "chg_wire_1", status: "PROCESSING", local_status: "pending", status_conflict: false, method: "boleto", currency: "BRL", amount: 1080, amount_minor: 108000, due_date: "2026-09-30", payable: false, boleto_bar_code: null, boleto_bank_line: null, pix_copy_paste: null, credit_correlation_armed: false, payment_in_flight: false, settlement: null, issuance_unconfirmed: false };
|
|
275
|
+
},
|
|
276
|
+
get: async () => { throw new Error("not called"); },
|
|
277
|
+
};
|
|
278
|
+
const rail = new CodeSparChargeRail(api as never);
|
|
279
|
+
const base = { attempt_id: "att_x_0", mandate_id: "pol_1", amount_minor: 108000, currency: "BRL", payee: DEBTOR, beneficiary: "Joana Ribeiro", purpose: "cobranca", agent_id: "collections-agent", description: "Acordo #1042 - parcela 1/1", due_date: "2026-09-30", actor: { type: "agent" as const, agent: "collections-agent@0.1.0", on_behalf_of: "merchant_demo_loja" } };
|
|
280
|
+
expect(await rail.pay(base)).toMatchObject({ status: "failed", code: "consumer_id_required" });
|
|
281
|
+
expect(calls).toHaveLength(0);
|
|
282
|
+
const out = await rail.pay({ ...base, consumer_id: "merchant_demo_loja" });
|
|
283
|
+
expect(out).toMatchObject({ status: "accepted", transaction_id: "chg_wire_1", instrument: { payable: false, status: "PROCESSING" } });
|
|
284
|
+
expect(calls[0]).toEqual({ path: "/v1/charges", body: { consumer_id: "merchant_demo_loja", amount: 1080, currency: "BRL", method: "boleto", description: "Acordo #1042 - parcela 1/1", buyer: { name: "Joana Ribeiro", document: DEBTOR }, due_date: "2026-09-30", idempotency_key: "att_x_0" } });
|
|
285
|
+
});
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
describe("paySandboxCharge: the SDK's typed call to the payer route", () => {
|
|
289
|
+
it("posts to /v1/test/charges/{id}/pay with the key, the project header and the amount; refuses a live key before any request", async () => {
|
|
290
|
+
const { paySandboxCharge } = await import("../src/api/sandbox-payer.js");
|
|
291
|
+
const seen: Array<{ url: string; init: RequestInit }> = [];
|
|
292
|
+
const realFetch = globalThis.fetch;
|
|
293
|
+
globalThis.fetch = (async (url: string | URL | Request, init?: RequestInit) => {
|
|
294
|
+
seen.push({ url: String(url), init: init ?? {} });
|
|
295
|
+
return new Response(JSON.stringify({ charge_id: "chg_1", status: "paid", local_status: "settled", simulated: true, settled_against: "sandbox_fixture", money_moved: false, quoted_minor: 1000, paid_minor: 1000, payment: "full", idempotent_replay: false }), { status: 200, headers: { "content-type": "application/json" } });
|
|
296
|
+
}) as typeof fetch;
|
|
297
|
+
try {
|
|
298
|
+
const live = await paySandboxCharge({ apiKey: ["csk", "live", "0000000000"].join("_") }, "chg_1").catch((e: Error) => e);
|
|
299
|
+
expect(live).toBeInstanceOf(Error);
|
|
300
|
+
expect(seen).toHaveLength(0);
|
|
301
|
+
const out = await paySandboxCharge({ apiKey: "csk_test_unit_0000", baseUrl: "https://api.example.test/", projectId: "prj_1" }, "chg_1", 1000);
|
|
302
|
+
expect(out).toMatchObject({ ok: true, state: { charge_id: "chg_1", status: "paid", simulated: true, money_moved: false } });
|
|
303
|
+
expect(seen[0]?.url).toBe("https://api.example.test/v1/test/charges/chg_1/pay");
|
|
304
|
+
const headers = new Headers(seen[0]?.init.headers);
|
|
305
|
+
expect(headers.get("authorization")).toBe("Bearer csk_test_unit_0000");
|
|
306
|
+
expect(headers.get("x-codespar-project")).toBe("prj_1");
|
|
307
|
+
expect(headers.get("content-type")).toBe("application/json");
|
|
308
|
+
expect(seen[0]?.init.body).toBe(JSON.stringify({ amount_minor: 1000 }));
|
|
309
|
+
const bare = await paySandboxCharge({ apiKey: "csk_test_unit_0000", baseUrl: "https://api.example.test/", projectId: "prj_1" }, "chg_1");
|
|
310
|
+
expect(bare).toMatchObject({ ok: true });
|
|
311
|
+
expect(seen[1]?.init.body).toBe("{}");
|
|
312
|
+
} finally {
|
|
313
|
+
globalThis.fetch = realFetch;
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { createCodeSparClient } from "../src/api/client.js";
|
|
3
|
+
import { assertTestKey, isTestKey, NotATestKeyError, redactKey } from "../src/secrets.js";
|
|
4
|
+
|
|
5
|
+
// Built at runtime so the secret scan (which refuses key-shaped literals) stays honest about this file.
|
|
6
|
+
const TEST = ["csk", "test", "0123456789abcdef"].join("_");
|
|
7
|
+
const LIVE = ["csk", "live", "0123456789"].join("_");
|
|
8
|
+
|
|
9
|
+
describe("sandbox by construction", () => {
|
|
10
|
+
it("accepts only csk_test_ keys", () => {
|
|
11
|
+
expect(isTestKey(TEST)).toBe(true);
|
|
12
|
+
expect(isTestKey(LIVE)).toBe(false);
|
|
13
|
+
expect(isTestKey("csk_test_")).toBe(false);
|
|
14
|
+
expect(isTestKey(undefined)).toBe(false);
|
|
15
|
+
expect(() => assertTestKey(LIVE)).toThrow(NotATestKeyError);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("refuses to build an API client with a live key, before any network", () => {
|
|
19
|
+
expect(() => createCodeSparClient({ apiKey: LIVE })).toThrow(NotATestKeyError);
|
|
20
|
+
expect(() => createCodeSparClient({ apiKey: undefined })).toThrow(NotATestKeyError);
|
|
21
|
+
expect(createCodeSparClient({ apiKey: TEST })).toBeDefined();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("redacts keys for logs", () => {
|
|
25
|
+
expect(redactKey(TEST)).toBe("csk_test_…cdef");
|
|
26
|
+
expect(redactKey(TEST)).not.toContain("0123456789");
|
|
27
|
+
});
|
|
28
|
+
});
|