test(cli): unit tests for token command handler - #12
Merged
Conversation
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Continuation of the CLI handler test stack:
tokenis 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 maxencodesmaxUint256in approve calldata.--amount 12345encodes the exact12345nin approve calldata.defi token transferdefi token balance/allowance— pre-RPC guardsbalance: errors when neither--ownernorDEFI_WALLET_ADDRESSis set.allowance: same guard.approve: requires--chainat the global level — no dry-run preview is emitted when--chainis missing.Strategy
new Executor(false)— the dry-run branch inexecutor.ts:354falls through to the clean calldata-preview path. Zero network traffic, deterministic.viem.decodeFunctionDataagainst the realerc20Abi. Tests assert both the function selector and the encoded bigint args, so any accidental ABI mismatch surfaces immediately.balanceandallowanceagainst 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 --noEmitclean.pnpm -C ts -r test— defi-core 32/32, defi-protocols 39/39, defi-cli 75/75 (was 69; +6 token tests).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