Skip to content

test: cover full x402 paid message lifecycle - #602

Open
esreekarreddy wants to merge 5 commits into
GetBindu:mainfrom
esreekarreddy:test/full-http-x402-lifecycle
Open

esreekarreddy wants to merge 5 commits into
GetBindu:mainfrom
esreekarreddy:test/full-http-x402-lifecycle

Conversation

@esreekarreddy

@esreekarreddy esreekarreddy commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Describe the problem and fix in 2-5 bullets:

  • Problem: x402 had failure-mode integration coverage, but no full HTTP message/send success-path test proving payment context survived middleware, A2A routing, scheduling, worker execution, and settlement.
  • Why it matters: regressions in that handoff can silently accept paid requests without settling, lose receipt metadata, or persist internal payment context.
  • What changed: added a deterministic full HTTP paid message/send integration test with real X402Middleware, agent_run_endpoint, TaskManager, InMemoryScheduler, ManifestWorker, and InMemoryStorage, while mocking facilitator verification/settlement at the external boundary.
  • Additional guardrail: strengthened the x402 middleware happy-path unit test so it asserts the verified request state actually reaches the downstream agent.
  • What did NOT change: no production behavior, protocol schemas, network payment calls, storage schema, scheduler behavior, or docs were changed.

Change Type (select all that apply)

  • Bug fix
  • Feature
  • Refactor
  • Documentation
  • Security hardening
  • Tests
  • Chore/infra

Scope (select all touched areas)

  • Server / API endpoints
  • Extensions (DID, x402, etc.)
  • Storage backends
  • Scheduler backends
  • Observability / monitoring
  • Authentication / authorization
  • CLI / utilities
  • Tests
  • Documentation
  • CI/CD / infra

Linked Issue/PR

User-Visible / Behavior Changes

None.

Security Impact (required)

  • New permissions/capabilities? (Yes/No): No
  • Secrets/credentials handling changed? (Yes/No): No
  • New/changed network calls? (Yes/No): No
  • Database schema/migration changes? (Yes/No): No
  • Authentication/authorization changes? (Yes/No): No
  • If any Yes, explain risk + mitigation: N/A

Verification

Environment

  • OS: macOS
  • Python version: 3.12.9 locally; GitHub Actions covers 3.12 and 3.13
  • Storage backend: InMemoryStorage
  • Scheduler backend: InMemoryScheduler

Steps to Test

  1. uv run pytest tests/unit/server/middleware/x402/ -q
  2. uv run pytest tests/integration/x402/ -q
  3. uv run pytest tests/unit/ -q
  4. uv run pytest tests/integration/ -q
  5. uv run pre-commit run --files tests/integration/x402/test_e2e_scenarios.py tests/unit/server/middleware/x402/test_x402_middleware.py
  6. git diff --check

Expected Behavior

  • The paid HTTP message/send request verifies x402 payment, reaches the A2A endpoint, schedules a task, settles before manifest execution, completes the task, persists receipt metadata, and does not leak the internal _payment_context into task history.
  • The x402 middleware happy path attaches payment_payload, payment_requirements, verify_response, and payer to request state before forwarding downstream.

Actual Behavior

  • uv run pytest tests/unit/server/middleware/x402/ -q -> 30 passed
  • uv run pytest tests/integration/x402/ -q -> 5 passed, 2 skipped
  • uv run pytest tests/unit/ -q -> 1087 passed, 3 skipped
  • uv run pytest tests/integration/ -q -> 20 passed, 3 skipped
  • uv run pre-commit run --files tests/integration/x402/test_e2e_scenarios.py tests/unit/server/middleware/x402/test_x402_middleware.py -> passed
  • git diff --check -> passed

Evidence (attach at least one)

  • Failing test before + passing after
  • Test output / logs
  • Screenshot / recording
  • Performance metrics (if relevant)

Human Verification (required)

What you personally verified (not just CI):

  • Verified scenarios: full paid HTTP message/send happy path; existing x402 integration scenarios for insufficient balance/settlement failure, settlement timeout, parallel nonce double-spend, and replay rejection; middleware happy path state handoff.
  • Edge cases checked: payment context is consumed internally, every persisted task-history entry omits _payment_context, receipt metadata is attached, mocked facilitator verify_payment and settle are each awaited once, settle receives the expected nonce/network, and middleware exposes verified state to downstream handlers.
  • What you did NOT verify: live chain settlement or live facilitator behavior; those remain mocked so the test is deterministic and CI-safe.

Compatibility / Migration

  • Backward compatible? (Yes/No): Yes
  • Config/env changes? (Yes/No): No
  • Database migration needed? (Yes/No): No
  • If yes, exact upgrade steps: N/A

Failure Recovery (if this breaks)

  • How to disable/revert this change quickly: revert this test-only PR or skip tests/integration/x402/test_e2e_scenarios.py::test_full_http_paid_message_send_settles_and_completes.
  • Files/config to restore: tests/integration/x402/test_e2e_scenarios.py, tests/unit/server/middleware/x402/test_x402_middleware.py
  • Known bad symptoms reviewers should watch for: flaky terminal-task polling, missing task completion, missing x402 receipt metadata, unexpected live network calls, or missing request-state payment fields.

Risks and Mitigations

  • Risk: the new integration test is broader than a unit test and could become flaky if task scheduling semantics change.
    • Mitigation: it uses in-memory scheduler/storage, mocked external facilitator calls, and explicit polling for terminal task state.
  • Risk: the mocked facilitator boundary may not catch live chain/facilitator incompatibilities.
    • Mitigation: the PR intentionally keeps CI deterministic; live facilitator checks are covered separately by existing live-smoke tests.

Checklist

  • Tests pass (uv run pytest)
  • Pre-commit hooks pass for touched files
  • Documentation updated (not needed; tests only)
  • Security impact assessed
  • Human verification completed
  • Backward compatibility considered

AI assistance

This patch was prepared with AI assistance and manually reviewed/validated before submission.

Summary by CodeRabbit

  • Tests
    • Added end-to-end coverage for paid HTTP message delivery.
    • Verifies payment processing, request routing, task scheduling and execution, settlement, completion, metadata, and facilitator interactions.
    • Added polling support to reliably wait for tasks to reach a terminal state.
    • Expanded middleware coverage to verify payment details are propagated throughout request processing.

@coderabbitai

coderabbitai Bot commented Aug 5, 2026 •

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

The tests verify X402 payment state propagation and a complete paid HTTP message/send lifecycle. The lifecycle covers verification, routing, scheduling, worker execution, task completion, metadata, receipt storage, history filtering, and settlement arguments.

Changes

X402 payment coverage

Layer / File(s) Summary
Middleware payment state propagation
tests/unit/server/middleware/x402/test_x402_middleware.py
The test app records payment payload, requirements, verification response, and payer. The valid-payment test verifies the propagated values.
Paid HTTP lifecycle validation
tests/integration/x402/test_e2e_scenarios.py
The integration test sends a paid message/send request through the Starlette and X402 middleware stack. It waits for task completion and verifies worker execution, artifacts, payment metadata, receipt storage, history filtering, and facilitator arguments.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant HTTPClient
  participant X402Middleware
  participant A2ARouting
  participant TaskScheduler
  participant Worker
  participant PaymentFacilitator
  HTTPClient->>X402Middleware: Send paid message/send request
  X402Middleware->>PaymentFacilitator: Verify payment
  X402Middleware->>A2ARouting: Forward verified request
  A2ARouting->>TaskScheduler: Schedule task
  TaskScheduler->>Worker: Execute task
  Worker->>PaymentFacilitator: Settle payment
  Worker-->>HTTPClient: Return completed task and artifact
Loading

Possibly related PRs

Suggested reviewers: raahulrahl

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The PR satisfies [#348] with successful paid-task lifecycle coverage and existing integration coverage for the three requested failure scenarios.
Out of Scope Changes check ✅ Passed All changes are limited to the requested x402 integration and middleware tests, with no unrelated production or schema changes.
Docstring Coverage ✅ Passed Docstring coverage is 80.00% which is sufficient. The required threshold is 80.00%.
Title check ✅ Passed The title clearly summarizes the main change: coverage for the complete x402 paid message lifecycle.
Description check ✅ Passed The description is detailed and covers the required scope, verification, security, compatibility, risks, and recovery information.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
tests/integration/x402/test_e2e_scenarios.py (1)

533-534: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Check metadata on every history entry.

The assertion checks only task["history"][0]. A later history entry can expose _payment_context while this test passes. Assert the condition for every entry in task["history"].

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/integration/x402/test_e2e_scenarios.py` around lines 533 - 534, Update
the history metadata assertion in the test to iterate over every entry in
task["history"], retrieving each entry’s metadata and asserting that
"_payment_context" is absent. Preserve the existing empty-metadata fallback for
entries without metadata.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/integration/x402/test_e2e_scenarios.py`:
- Around line 419-420: Replace the two diagnostic print calls near the
end-to-end scenario with module logging: import get_logger from
bindu.utils.logging, define a module-level logger using get_logger(__name__),
and emit both messages through logger.info(...).

---

Nitpick comments:
In `@tests/integration/x402/test_e2e_scenarios.py`:
- Around line 533-534: Update the history metadata assertion in the test to
iterate over every entry in task["history"], retrieving each entry’s metadata
and asserting that "_payment_context" is absent. Preserve the existing
empty-metadata fallback for entries without metadata.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: b85917a9-7072-42db-abc3-17e11f4da4c8

📥 Commits

Reviewing files that changed from the base of the PR and between 7b1ff75 and 24e5f07.

📒 Files selected for processing (1)
  • tests/integration/x402/test_e2e_scenarios.py

This branch has not been deployed

No deployments
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.

[Feature]: Add Integration Tests for Payment Flows

1 participant