test(cli): unit tests for wallet command handler - #11
Merged
Conversation
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).
4 tasks
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).
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 PR #6's CLI handler test stack. PR #6 added 6 tests for
status+schema. This PR extends the same in-process commander pattern towallet, 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{address: null, source: "none"}when no env vars are set.DEFI_WALLET_ADDRESSwithsource=env.DEFI_PRIVATE_KEYwithsource=private_key. Uses Hardhat's well-known account #0 (0xac09…ff80→0xf39f…2266); public knowledge, no funds at risk.defi wallet balanceviem.getBalance()so the test runs offline.--chain(multi-chain balance fan-out is intentionally not implemented). Asserts the no-chain path surfaces achain-related diagnostic on either output channel.Strategy notes
beforeEach/afterEachsnapshot+restore the two env varsresolveWalletWithSigner()consults (DEFI_WALLET_ADDRESS,DEFI_PRIVATE_KEY), so each test runs in isolation regardless of host shell.wallet balance(wallet + chain set) hits live RPC viaviem.createPublicClientand 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 --noEmitclean.pnpm -C ts -r test— defi-core 32/32, defi-protocols 39/39, defi-cli 69/69 (was 64; +5 wallet tests).0xof zero key +viem.privateKeyToAccounttest 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