# Testing

Live end-to-end tests in `tests/` — real Tychi API, real local signing. Not unit tests. Each file is a **reference scenario** for how the SDK should be used.

Run from `client/agent-finance/typescript/`:

```bash
npm run test:native
npm run test:tokens
npm run test:mint
npm run test:erc8004
npm run test:x402:buyer
npm run test:x402:seller
```

Scripts use `.env.sdk` and `TYI_RUN_DEBUG=1` (see `package.json`).

---

## `native.test.ts` — send native flow

**What it proves:** Full agent money loop on native coin — portfolio read, transfer, local sign, broadcast, resume.

**Flow (Monad testnet):**

1. Onboard with policy allowing `get_portfolio` and `transfer_native`
2. User asks wallet balance (Spanish prompt in file) → agent calls `get_portfolio`
3. User goal: send a **percent** of native balance to a fixed `0x` recipient
4. Agent confirms amount + recipient (may pause for your reply in terminal)
5. Agent calls `transfer_native` → `pending_tx`
6. SDK signs locally → broadcast → exec/result resume
7. Agent confirms with `tx_hash`

**Edit in-file:** `MSG_BALANCE_ES`, `TRANSFER_PERCENT`, `TRANSFER_TO`  
**Needs:** Tychi API, `.env.sdk`, **funded wallet**

---

## `tokens.test.ts` — send tokens flow

**What it proves:** Same pause/resume loop for **ERC-20** transfers.

**Flow:**

1. Onboard with `get_portfolio`, `transfer_tokens`
2. User asks balance → `get_portfolio`
3. User goal: send a **percent** of an ERC-20 to `TRANSFER_TO`
4. You paste the **token contract address** in chat when the agent asks
5. Confirm → `transfer_tokens` → `pending_tx` → sign → broadcast → resume

**Edit in-file:** `MSG_BALANCE_ES`, `TRANSFER_PERCENT`, `TRANSFER_TO` (token address via chat, not file)  
**Needs:** Funded wallet + token balance on chosen ERC-20

---

## `mint.test.ts` — create token flow

**What it proves:** Policy-scoped tools + multi-turn chat → deploy ERC-20 via agent run.

**Flow:**

1. Onboard with `get_portfolio`, `create_token`
2. Balance ask → agent uses portfolio tool
3. Multi-turn chat toward deploy goal (default: create `AgentCoin` / `AGNT`)
4. Agent reaches `create_token` → `pending_tx`
5. Sign → broadcast → exec/result

**Edit in-file / env:** `TOKEN_NAME`, `TOKEN_SYMBOL`, `TYI_RUN_GOAL`, `TYI_USER_REPLIES`  
**Needs:** Funded wallet for deploy gas

---

## `erc8004.test.ts` — ERC-8004 flow

**What it proves:** Agent registration on Tychi → onboard → ERC-8004 profile/register → public manifest → balance run.

**Flow:**

1. Register agent on Tychi (fresh or existing identity)
2. Onboard session with read-only portfolio policy
3. Set ERC-8004 profile (description, image, x402 flag) — or full on-chain register + broadcast
4. Validate public manifest at `/v1/public/agents/<agent_id>/erc8004.json`
5. Run agent: balance / portfolio question → expects tool steps

**Skip options:** `SKIP_ERC8004_BROADCAST` (profile only), skips mint if already registered  
**Needs:** `.env.sdk`; optional `TYI_EVM_PRIVATE_KEY` to import wallet

---

## `x402_buyer.test.ts` — x402 buyer (in-session)

**What it proves:** Locked **agent-to-agent x402 v2 buyer** path — vendor 402 → Tychi prepares payment → local sign → pay vendor → resume run.

**Flow:**

1. Mock vendor returns HTTP 402 + `PAYMENT-REQUIRED` (no x402 logic in client beyond fetch)
2. Agent chats to Tychi with raw header + URL
3. Tychi policy → `x402_prepare_payment` → `pending_x402`
4. SDK signs EIP-3009 locally → retries vendor with `PAYMENT-SIGNATURE`
5. Posts vendor 200 body to exec/result → agent confirms

**No** `/v1/tx/broadcast` on this path. Payment terms come from the vendor header.

**Env:** `.env.sdk` + `TYI_X402_USDC` (token in mock header; buyer must hold balance)  
**Optional:** `SKIP_X402_SIGN=1`

---

## `x402_seller.test.ts` — x402 seller settlement

**What it proves:** **Seller side** — external plain-EVM buyer pays mock vendor; Tychi agent settles on-chain.

**Flow:**

1. External buyer wallet (not on Tychi) hits mock vendor → 402 → signs EIP-3009 → 200
2. Mock captures payment headers
3. Seller agent chats Tychi → `x402_prepare_settlement` → `pending_tx`
4. Seller `signBroadcastAndResume` → `transferWithAuthorization` on-chain

**Wallets in `.env.sdk`:**

| Key | Role |
| --- | --- |
| `TYI_EVM_PRIVATE_KEY` | Seller — Tychi agent, receives USDC, pays gas |
| `TYI_2_EVM_PRIVATE_KEY` | Buyer stunt double (~1 USDC, not on Tychi) |

Buyer-only path: run `test:x402:buyer` separately.

---

## Shared requirements

| Need | Detail |
| --- | --- |
| Tychi API | `TYI_API_BASE_URL` (default localhost or production) |
| Credentials | `TYI_VAULT_PASSWORD`, `TYI_SESSION_PASSWORD`, `TYI_LLM_API_KEY` in `.env.sdk` |
| Chain | `TYI_CHAIN_ID` (default `10143` Monad testnet) |
| Provider | `TYI_LLM_PROVIDER` (default `openai`) |
| State | Vault/wallet/bind/ERC-8004 may already exist from prior runs |
| Funds | Transfers, mint, x402 need funded wallets |
