|
| 1 | +package agents_engine.x402 |
| 2 | + |
| 3 | +import agents_engine.agui.AgUiServer |
| 4 | +import agents_engine.core.agent |
| 5 | +import agents_engine.core.skill |
| 6 | +import agents_engine.nlweb.NlWebServer |
| 7 | +import java.net.URI |
| 8 | +import java.net.http.HttpClient |
| 9 | +import java.net.http.HttpRequest |
| 10 | +import java.net.http.HttpResponse |
| 11 | +import kotlin.test.Test |
| 12 | +import kotlin.test.assertEquals |
| 13 | +import kotlin.test.assertTrue |
| 14 | + |
| 15 | +// #4527 — the X402PaymentGate `payment =` wiring on the serve surfaces. Hermetic: a fake facilitator (no |
| 16 | +// chain) gates a real agent served over NLWeb and AG-UI. All four serve surfaces wrap their invocation |
| 17 | +// handler identically (payment?.gate(handler) ?: handler), so NLWeb + AG-UI cover the pattern. |
| 18 | +class X402ServeIntegrationTest { |
| 19 | + |
| 20 | + private val http = HttpClient.newHttpClient() |
| 21 | + |
| 22 | + private val okFacilitator = object : FacilitatorClient { |
| 23 | + override fun verify(paymentHeader: String, requirements: PaymentRequirements) = |
| 24 | + FacilitatorVerification(isValid = true, payer = "0xPayer") |
| 25 | + |
| 26 | + override fun settle(paymentHeader: String, requirements: PaymentRequirements) = |
| 27 | + FacilitatorSettlement(success = true, transaction = "0xTX", network = requirements.network) |
| 28 | + } |
| 29 | + |
| 30 | + private fun gate(resource: String) = X402PaymentGate( |
| 31 | + PaymentRequirements( |
| 32 | + network = "base", maxAmountRequired = "1", payTo = "0xSeller", asset = "0xUSDC", resource = resource, |
| 33 | + ), |
| 34 | + okFacilitator, |
| 35 | + ) |
| 36 | + |
| 37 | + private fun paidAgent() = agent<String, String>("paid") { |
| 38 | + skills { skill<String, String>("answer", "") { implementedBy { "answer: $it" } } } |
| 39 | + } |
| 40 | + |
| 41 | + private fun post(url: String, body: String, payment: String? = null): HttpResponse<String> { |
| 42 | + val b = HttpRequest.newBuilder().uri(URI.create(url)).POST(HttpRequest.BodyPublishers.ofString(body)) |
| 43 | + payment?.let { b.header("X-PAYMENT", it) } |
| 44 | + return http.send(b.build(), HttpResponse.BodyHandlers.ofString()) |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + fun `NlWebServer gated by x402 demands payment then serves`() { |
| 49 | + val server = NlWebServer.from(paidAgent(), payment = gate("/ask")).start() |
| 50 | + try { |
| 51 | + assertEquals(402, post(server.url, """{"query":"hi"}""").statusCode()) |
| 52 | + val paid = post(server.url, """{"query":"hi"}""", payment = "dummy") |
| 53 | + assertEquals(200, paid.statusCode()) |
| 54 | + assertTrue("answer: hi" in paid.body(), paid.body()) |
| 55 | + } finally { |
| 56 | + server.stop() |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + fun `AgUiServer gated by x402 demands payment then streams`() { |
| 62 | + val server = AgUiServer.from(paidAgent(), payment = gate("/agent")).start() |
| 63 | + try { |
| 64 | + val body = """{"messages":[{"role":"user","content":"hi"}]}""" |
| 65 | + assertEquals(402, post(server.url, body).statusCode()) |
| 66 | + val paid = post(server.url, body, payment = "dummy") |
| 67 | + assertEquals(200, paid.statusCode()) |
| 68 | + assertTrue("RUN_STARTED" in paid.body(), paid.body()) |
| 69 | + } finally { |
| 70 | + server.stop() |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments