Skip to content

test(web): cover usdcToMicros and formatUsdcMicrosValue - #239

Open
dirtybits wants to merge 1 commit into
mainfrom
test/agentvouch-usdc-money-conversions
Open

dirtybits wants to merge 1 commit into
mainfrom
test/agentvouch-usdc-money-conversions

Conversation

@dirtybits

Copy link
Copy Markdown
Owner

Summary

Adds behavioral tests for the two USDC money-conversion helpers in
web/lib/agentvouchUsdc.ts:

  • usdcToMicros(amountUsdc) — converts a human decimal USDC amount to the
    6-decimal micro-unit the app and on-chain contract use.
  • formatUsdcMicrosValue(micros, minimumFractionDigits) — formats a micro
    value back to a human-readable USDC string.

These functions sit on every USDC amount a user sees and every micro amount the
app converts between the decimal world and the on-chain 6-decimal micro-unit,
so a subtle rounding bug here corrupts displayed balances, purchase amounts, and
the on-chain transfer amount.

Why

The only prior reference for these helpers was a source-text grep in
web/__tests__/components/wallet-detection-source.test.ts, which asserts the
import exists but says nothing about behavior. Per AGENTS.md §3.24, pure helpers
get real behavioral tests, not source-text assertions.

What was verified

  • Value pins: every expected value below was confirmed against the live
    implementation with a throwaway probe run before the assertions were written,
    so the tests pin exact behavior, not just shape.
  • usdcToMicros: 0 -> 0, 1 -> 1_000_000n, 0.01 -> 10_000n,
    0.1 -> 100_000n, 12345.6789 -> 12_345_678_900n, 0.0000005 -> 1n
    (halfway rounds up via Math.round), 0.0000004 -> 0n (below half truncates),
    1.23456789 -> 1_234_568n, sub-micro -> 0n, and rejection of negative and
    NaN/Infinity/-Infinity inputs with the clear message.
  • formatUsdcMicrosValue: null/undefined -> "0.00", 0 -> "0.00",
    10_000n -> "0.01", 1_500_000n -> "1.50", 999_999n -> "0.999999",
    12_345_678n -> "12.345678", 1_000_000_000_000n -> "1,000,000.00",
    identical bigint/number/string input handling (1_000_000 / 1000000 ->
    "1.00"), and minimumFractionDigits overrides ("0.0100" for 4,
    "0.010000" for 6, "1.5" for 0).

Verification

  • npx vitest run __tests__/lib/agentvouchUsdc.test.ts — 10 tests passed.
  • Full local gate on this change:
    • npm run format:check — all files Prettier-clean.
    • npm run lint — passed.
    • npm run typecheck — passed (tsc --noEmit after next typegen).
    • npm run test — 153 test files / 1321 tests passed (web + cli workspaces).
    • npm exec --workspace @agentvouch/web -- next build --webpack — not
      runnable in this cron environment
      because web/.env.local / DATABASE_URL
      is absent; the build fails during prerendering on DATABASE_URL before it
      could compile the new file. This is a local-environment limitation (documented
      in AGENTS.md §1.3), not caused by this change — the change is a
      single, pure test file under web/__tests__/lib/ that no route or app
      module imports, so it cannot affect bundling or output.

Scope

One new file: web/__tests__/lib/agentvouchUsdc.test.ts. No source, config, or
dependency changes.

Rollback

Delete the new test file; nothing else was touched.

Notes

These two money-conversion helpers in web/lib/agentvouchUsdc.ts had no
behavioral test. The only existing reference was a source-text grep in
web/__tests__/components/wallet-detection-source.test.ts, which asserts the
import, not the behavior. Per AGENTS.md §3, pure helpers need real
behavioral tests.

Adds a behavioral suite pinning:
- usdcToMicros: whole/fractional conversion, 6-decimal rounding
  (halfway rounds up, sub-micro truncates to 0), and the rejection of
  negative/non-finite inputs.
- formatUsdcMicrosValue: null/undefined defaults to '0.00', bigint/number/
  string input equivalence, minimum/maximum decimal formatting, thousand
  grouping, and the minimumFractionDigits override.

Every value is the exact output confirmed against the implementation.

This branch was successfully deployed

1 active deployment
Preview — ac118904 Deployed Oct 13, 2026 by vercel[bot]
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