Skip to content

test(cli): unit tests for token command handler - #12

Merged
Hiksang merged 1 commit into
mainfrom
test/token-handler-coverage
May 6, 2026
Merged

test(cli): unit tests for token command handler#12
Hiksang merged 1 commit into
mainfrom
test/token-handler-coverage

Conversation

@Hiksang

@Hiksang Hiksang commented May 6, 2026

Copy link
Copy Markdown
Collaborator

Summary

Continuation of the CLI handler test stack:

token is the next-smallest untested handler (105 lines, 4 subcommands: approve / transfer / balance / allowance).

What's added

ts/packages/defi-cli/src/commands/token.test.ts (+272 lines, 6 tests):

defi token approve

  • --amount max encodes maxUint256 in approve calldata.
  • --amount 12345 encodes the exact 12345n in approve calldata.

defi token transfer

  • Encodes the recipient address + exact amount as bigint in transfer calldata.

defi token balance / allowance — pre-RPC guards

  • balance: errors when neither --owner nor DEFI_WALLET_ADDRESS is set.
  • allowance: same guard.
  • approve: requires --chain at the global level — no dry-run preview is emitted when --chain is missing.

Strategy

  • Executor with no rpcUrl: new Executor(false) — the dry-run branch in executor.ts:354 falls through to the clean calldata-preview path. Zero network traffic, deterministic.
  • Calldata decoding: viem.decodeFunctionData against the real erc20Abi. Tests assert both the function selector and the encoded bigint args, so any accidental ABI mismatch surfaces immediately.
  • Happy paths deferred: balance and allowance against a real ERC20 need on-chain reads — same boundary the wallet.test.ts and status.test.ts tests draw around --verify / live RPC. Integration / Anvil-fork suite covers those.

Test plan

  • pnpm -C ts -r build — clean.
  • pnpm -C ts -r lint — 3 packages, tsc --noEmit clean.
  • pnpm -C ts -r test — defi-core 32/32, defi-protocols 39/39, defi-cli 75/75 (was 69; +6 token tests).
  • Reviewer: confirm calldata-preview assertions cover the ABI shapes you care about (esp. approve(spender,uint256) order).

Next handlers in this stack (separate PRs)

  • swap — needs aggregator API HTTP mocking (more involved).
  • lending — needs viem RPC mocking for read paths.
  • lp (1803 LOC) — biggest coverage win; will likely split across multiple PRs.

🤖 Generated with Claude Code

Continues the CLI handler test stack. PR #6 added status (4) and
schema (2). PR #11 added wallet (5). This commit adds token (6),
the next-smallest untested handler (105 lines, 4 subcommands).

6 new tests in src/commands/token.test.ts:

  defi token approve:
    - --amount max encodes maxUint256 in approve calldata
    - --amount 12345 encodes the exact bigint in approve calldata

  defi token transfer:
    - encodes recipient + exact amount in transfer calldata

  defi token balance / allowance — pre-RPC guards:
    - balance: errors when neither --owner nor DEFI_WALLET_ADDRESS is set
    - allowance: errors when neither --owner nor DEFI_WALLET_ADDRESS is set
    - approve: requires --chain at the global level (no preview emitted
      when --chain is missing)

Strategy:
  - Executor is constructed with `new Executor(false)` — no rpcUrl —
    so the dry-run branch in executor.ts:354 falls through to the
    clean calldata-preview path. Zero network traffic, deterministic.
  - Calldata is decoded with viem.decodeFunctionData against the
    real erc20 ABI; tests assert both the function selector and the
    encoded bigint args, so accidental ABI mismatches surface
    immediately.
  - The happy paths for `balance` / `allowance` (real ERC20 reads)
    are intentionally deferred to integration / Anvil-fork tests —
    same boundary the wallet.test.ts and status.test.ts tests draw
    around live RPC.

Verified:
  - pnpm -C ts -r build  — clean.
  - pnpm -C ts -r lint   — 3 packages, tsc --noEmit clean.
  - pnpm -C ts -r test   — defi-core 32/32, defi-protocols 39/39,
                           defi-cli 75/75 (was 69; +6 token tests).
@Hiksang
Hiksang merged commit 477c17c into main May 6, 2026
4 checks passed
@Hiksang
Hiksang deleted the test/token-handler-coverage branch May 6, 2026 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant