Skip to content

test(cli): unit tests for wallet command handler - #11

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

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

Conversation

@Hiksang

@Hiksang Hiksang commented May 6, 2026

Copy link
Copy Markdown
Collaborator

Summary

Continuation of PR #6's CLI handler test stack. PR #6 added 6 tests for status + schema. This PR extends the same in-process commander pattern to wallet, the smallest still-untested handler (69 lines, 2 subcommands).

What's added

ts/packages/defi-cli/src/commands/wallet.test.ts (+167 lines, 5 tests):

defi wallet address

  • Returns {address: null, source: "none"} when no env vars are set.
  • Returns the configured DEFI_WALLET_ADDRESS with source=env.
  • Derives the address from DEFI_PRIVATE_KEY with source=private_key. Uses Hardhat's well-known account #0 (0xac09…ff800xf39f…2266); public knowledge, no funds at risk.

defi wallet balance

  • Rejects with a structured error envelope when no wallet is configured — short-circuits before viem.getBalance() so the test runs offline.
  • Requires --chain (multi-chain balance fan-out is intentionally not implemented). Asserts the no-chain path surfaces a chain-related diagnostic on either output channel.

Strategy notes

  • beforeEach/afterEach snapshot+restore the two env vars resolveWalletWithSigner() consults (DEFI_WALLET_ADDRESS, DEFI_PRIVATE_KEY), so each test runs in isolation regardless of host shell.
  • The happy-path wallet balance (wallet + chain set) hits live RPC via viem.createPublicClient and is intentionally deferred to integration / Anvil-fork tests rather than mocked here — same boundary the status.test.ts tests draw around --verify.

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 69/69 (was 64; +5 wallet tests).
  • Reviewer: confirm Hardhat account #0 derivation is the expected smoke case (alternatively, swap for 0x of zero key + viem.privateKeyToAccount test vector).

Next handlers in this stack (separate PRs)

Following PR #6's roadmap:

  • token — small, mostly read-only.
  • swap — needs aggregator API mocking.
  • lending — needs viem RPC mocking.
  • lp — biggest coverage win (1803 LOC).

🤖 Generated with Claude Code

PR #6 added handler tests for status (4) and schema (2). This
commit extends the same in-process commander pattern to `wallet`,
the smallest still-untested handler (69 lines, 2 subcommands).

5 new tests in src/commands/wallet.test.ts:

  defi wallet address:
    - returns null + source=none when no env vars are set
    - returns the configured DEFI_WALLET_ADDRESS with source=env
    - derives the address from DEFI_PRIVATE_KEY with source=private_key
      (uses Hardhat's well-known account #0; public knowledge,
      no funds at risk)

  defi wallet balance:
    - rejects with a structured error envelope when no wallet is
      configured — short-circuits BEFORE viem.getBalance() so the
      test runs offline
    - requires --chain (multi-chain balance fan-out is intentionally
      not implemented; just asserts the no-chain path produces a
      "chain"-related diagnostic on either output channel)

Strategy notes:
  - beforeEach/afterEach snapshot+restore the two env vars
    `resolveWalletWithSigner()` consults so each test runs in
    isolation regardless of host shell.
  - The happy-path `wallet balance` (with wallet + chain set) hits
    live RPC via viem.createPublicClient and is intentionally
    deferred to integration / Anvil-fork tests rather than mocked
    here — same boundary the status.test.ts tests draw around
    `--verify`.

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 69/69 (was 64; +5 wallet tests).
@Hiksang
Hiksang merged commit 303643a into main May 6, 2026
4 checks passed
@Hiksang
Hiksang deleted the test/wallet-handler-coverage branch May 6, 2026 14:34
Hiksang added a commit that referenced this pull request May 6, 2026
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).
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