|
1 | 1 | import { describe, expect, it } from "vitest"; |
2 | 2 | import { |
3 | 3 | RpcCheckPaymentResultSchema, |
| 4 | + RpcSubmitPaymentRequestSchema, |
4 | 5 | RpcSubmitPaymentResultSchema, |
5 | 6 | } from "../src/index.js"; |
6 | 7 |
|
@@ -140,4 +141,60 @@ describe("rpc schemas", () => { |
140 | 141 | "https://example.com/payment/pay_01JMVP9QE8XA3BDGM5RN7KWTZ4", |
141 | 142 | ); |
142 | 143 | }); |
| 144 | + |
| 145 | + describe("paymentIdentifier — x402 V2 idempotency parity", () => { |
| 146 | + const STUB_TX_HEX = "0x" + "ab".repeat(32); |
| 147 | + |
| 148 | + it("accepts a submit request with an optional paymentIdentifier", () => { |
| 149 | + const req = RpcSubmitPaymentRequestSchema.parse({ |
| 150 | + txHex: STUB_TX_HEX, |
| 151 | + paymentIdentifier: "pay_01JMVP9QE8XA3BDGM5", |
| 152 | + }); |
| 153 | + expect(req.paymentIdentifier).toBe("pay_01JMVP9QE8XA3BDGM5"); |
| 154 | + }); |
| 155 | + |
| 156 | + it("accepts a submit request without paymentIdentifier (backward compat)", () => { |
| 157 | + const req = RpcSubmitPaymentRequestSchema.parse({ txHex: STUB_TX_HEX }); |
| 158 | + expect(req.paymentIdentifier).toBeUndefined(); |
| 159 | + }); |
| 160 | + |
| 161 | + it("rejects a paymentIdentifier shorter than 16 chars", () => { |
| 162 | + const result = RpcSubmitPaymentRequestSchema.safeParse({ |
| 163 | + txHex: STUB_TX_HEX, |
| 164 | + paymentIdentifier: "short", |
| 165 | + }); |
| 166 | + expect(result.success).toBe(false); |
| 167 | + }); |
| 168 | + |
| 169 | + it("rejects a paymentIdentifier longer than 128 chars", () => { |
| 170 | + const result = RpcSubmitPaymentRequestSchema.safeParse({ |
| 171 | + txHex: STUB_TX_HEX, |
| 172 | + paymentIdentifier: "a".repeat(129), |
| 173 | + }); |
| 174 | + expect(result.success).toBe(false); |
| 175 | + }); |
| 176 | + |
| 177 | + it("rejects a paymentIdentifier with disallowed characters", () => { |
| 178 | + const result = RpcSubmitPaymentRequestSchema.safeParse({ |
| 179 | + txHex: STUB_TX_HEX, |
| 180 | + paymentIdentifier: "invalid identifier!", |
| 181 | + }); |
| 182 | + expect(result.success).toBe(false); |
| 183 | + }); |
| 184 | + |
| 185 | + it("accepts PAYMENT_IDENTIFIER_CONFLICT as a rejected submit error code", () => { |
| 186 | + const result = RpcSubmitPaymentResultSchema.parse({ |
| 187 | + accepted: false, |
| 188 | + error: "Same paymentIdentifier submitted with a different transaction", |
| 189 | + code: "PAYMENT_IDENTIFIER_CONFLICT", |
| 190 | + retryable: false, |
| 191 | + }); |
| 192 | + |
| 193 | + expect(result.accepted).toBe(false); |
| 194 | + if (result.accepted) { |
| 195 | + throw new Error("Expected a rejected RPC submit result"); |
| 196 | + } |
| 197 | + expect(result.code).toBe("PAYMENT_IDENTIFIER_CONFLICT"); |
| 198 | + }); |
| 199 | + }); |
143 | 200 | }); |
0 commit comments