Skip to content

Add unit tests for version-dispatcher and network-utils modules #136

@jolestar

Description

@jolestar

Problem

The version-dispatcher.ts and network-utils.ts modules lack comprehensive unit test coverage, which is critical for these core routing and utility functions.

Files to Test

1. src/network-utils.ts

Critical functions that need test coverage:

  • getCanonicalNetwork() - Network canonicalization logic
  • getNetworkDisplayName() - Network name resolution
  • isCanonicalNetwork() - CAIP-2 format validation
  • getCachedCanonicalNetwork() - Cached network lookup
  • getSupportedCanonicalNetworks() - Returns all supported networks
  • getSupportedHumanReadableNetworks() - Returns human-readable names
  • validateNetwork() - Network validation with error handling
  • determineX402Version() - Version detection from payload/body
  • isVersionSupported() - Version support checking

2. src/version-dispatcher.ts

Critical class methods that need test coverage:

  • VersionDispatcher.verify() - Version routing for verify operations
  • VersionDispatcher.settle() - Version routing for settle operations
  • VersionDispatcher.verifyV1() - V1 verification implementation
  • VersionDispatcher.verifyV2() - V2 verification implementation
  • VersionDispatcher.settleV1() - V1 settlement implementation
  • VersionDispatcher.settleV2() - V2 settlement implementation
  • VersionDispatcher.isV1SettlementRouterMode() - SettlementRouter mode detection

Test Scenarios to Cover

network-utils.ts

  1. Network Canonicalization

    • Valid v1 names → v2 CAIP-2 identifiers
    • Valid v2 CAIP-2 → passthrough
    • Invalid networks → proper error messages
    • Case sensitivity handling
    • Cache functionality in getCachedCanonicalNetwork()
  2. Version Detection

    • Version from paymentPayload.x402Version
    • Version from body.x402Version
    • Default to v1 when not specified
    • Invalid version numbers → error
    • Version support checking with/without FACILITATOR_ENABLE_V2 flag
  3. Network Validation

    • Empty/undefined network strings
    • Non-string inputs
    • Valid networks returning canonical keys

version-dispatcher.ts

  1. Version Routing

    • V1 requests route to v1 implementation
    • V2 requests route to v2 implementation (when enabled)
    • V2 requests rejected when disabled
    • Metrics recording for each version/mode
  2. SettlementRouter Mode Detection

    • V1 with extra.settlementRouter → v1_settlementRouter mode
    • V1 without extra.settlementRouter → v1_standard mode
    • V2 always uses v2_router mode
  3. Error Handling

    • Unsupported versions return appropriate errors
    • Invalid networks handled properly
    • V2 facilitator not initialized errors
    • Metrics recorded for errors

Acceptance Criteria

  1. Test files created:
    • test/unit/network-utils.test.ts
    • test/unit/version-dispatcher.test.ts
  2. All critical functions/methods have test coverage
  3. Edge cases covered (invalid inputs, version mismatches, etc.)
  4. Mock external dependencies (v2 facilitator, pool manager, etc.)
  5. Coverage report shows >80% for both files
  6. All tests pass consistently

Priority

High - These are core modules for v1/v2 routing and need reliable test coverage

Technical Notes

  • Use viem mocks for EVM-related tests
  • Mock x402 package functions
  • Mock @x402x/* package functions
  • Test both with FACILITATOR_ENABLE_V2=true and false
  • Verify metrics recording using spies/mocks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions