Skip to content

Add unit tests for web x402 client request and receipt helpers #263

Description

@AnkanMisra

Summary

The web x402 client module owns the browser-side HTTP request shape, payment-challenge parsing, signed retry headers, and receipt-header decoding, but it has no direct unit tests. These helpers are used by the paid summarize flow and can be tested without a wallet or running service.

Current behavior

The module currently:

  • builds the gateway summarize request;
  • reads the paymentContext from a 402 response;
  • converts a payment signature into the three X-402-* retry headers;
  • reads successful summary responses;
  • drops missing or malformed receipt headers without discarding the successful summary.

Those contracts are only exercised indirectly by higher-level code.

Desired behavior

Add deterministic Bun tests that lock down the existing request, challenge, header, and response behavior. Production behavior should remain unchanged.

Key interfaces

  • getGatewayUrl() — configured URL and localhost fallback.
  • postSummarize() — request URL, method, JSON body, content type, and caller-supplied headers.
  • readPaymentChallenge() — valid challenge extraction and missing-context rejection.
  • buildSignedHeaders() — exact signature, nonce, and numeric timestamp header values.
  • readSummarizeSuccess() and safeDecodeReceiptHeader() — valid, absent, malformed, and wrong-shape receipt headers.

Acceptance criteria

  • Tests verify the configured gateway URL and the localhost fallback.
  • Tests verify the exact summarize request method, URL, JSON body, content type, and custom-header preservation using a mocked fetch.
  • Tests verify valid payment-context parsing and rejection when paymentContext is missing.
  • Tests verify the exact three signed retry headers, including timestamp string conversion.
  • Tests verify successful responses with a valid receipt header and with no receipt header.
  • Malformed base64, invalid JSON, incomplete receipt objects, and wrong field types return a null receipt without losing the summary.
  • Tests do not require a live gateway, verifier, wallet, Redis, or AI provider.
  • cd web && bun run test:unit && bun run typecheck passes.

Out of scope

  • Changing the x402 wire contract or EIP-712 fields.
  • Testing wallet prompts or React hook state transitions.
  • Refactoring the SDK implementation.
  • Changing how malformed receipts are reported to users.

Blocked by

None — can start immediately.

Metadata

Metadata

Assignees

No one assigned

    Labels

    TypeScriptTypeScript codeenhancementNew feature or requestgood first issueGood for newcomersgssoc:approvedApproved for GSSoC contributionlevel:beginnerSmall, well-scoped work suitable for newer contributors.type:testingTests, coverage, fixtures, or validation-only work.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions