|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { normalizeV2Offer, normalizeV2Offers, normalizeV2Receipt } from '../src/normalize-v2.js'; |
| 3 | +import type { |
| 4 | + RawV2PaymentRequired, |
| 5 | + RawV2PaymentRequiredAccept, |
| 6 | + RawV2Resource, |
| 7 | + RawV2SettlementResponse, |
| 8 | +} from '../src/raw-v2.js'; |
| 9 | + |
| 10 | +// --------------------------------------------------------------------------- |
| 11 | +// Fixtures |
| 12 | +// --------------------------------------------------------------------------- |
| 13 | + |
| 14 | +const RESOURCE: RawV2Resource = { |
| 15 | + url: 'https://api.example.com/premium', |
| 16 | + description: 'Premium API access', |
| 17 | + mimeType: 'application/json', |
| 18 | +}; |
| 19 | + |
| 20 | +const ACCEPT_ENTRY: RawV2PaymentRequiredAccept = { |
| 21 | + scheme: 'exact', |
| 22 | + network: 'eip155:84532', |
| 23 | + amount: '100000', |
| 24 | + asset: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', |
| 25 | + payTo: '0x1234567890abcdef1234567890abcdef12345678', |
| 26 | + maxTimeoutSeconds: 300, |
| 27 | + extra: { customField: 'preserved' }, |
| 28 | +}; |
| 29 | + |
| 30 | +const CHALLENGE: RawV2PaymentRequired = { |
| 31 | + x402Version: 2, |
| 32 | + error: 'PAYMENT-SIGNATURE header is required', |
| 33 | + resource: RESOURCE, |
| 34 | + accepts: [ACCEPT_ENTRY], |
| 35 | +}; |
| 36 | + |
| 37 | +const SUCCESS_SETTLEMENT: RawV2SettlementResponse = { |
| 38 | + success: true, |
| 39 | + transaction: '0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890', |
| 40 | + network: 'eip155:84532', |
| 41 | + payer: '0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef', |
| 42 | +}; |
| 43 | + |
| 44 | +const FAILED_SETTLEMENT: RawV2SettlementResponse = { |
| 45 | + success: false, |
| 46 | + errorReason: 'insufficient_funds', |
| 47 | + transaction: '', |
| 48 | + network: 'eip155:84532', |
| 49 | + payer: '0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef', |
| 50 | +}; |
| 51 | + |
| 52 | +// --------------------------------------------------------------------------- |
| 53 | +// Tests: normalizeV2Offer |
| 54 | +// --------------------------------------------------------------------------- |
| 55 | + |
| 56 | +describe('normalizeV2Offer', () => { |
| 57 | + it('maps accept entry fields to NormalizedV2Offer', () => { |
| 58 | + const result = normalizeV2Offer(ACCEPT_ENTRY, RESOURCE); |
| 59 | + expect(result.version).toBe(2); |
| 60 | + expect(result.scheme).toBe('exact'); |
| 61 | + expect(result.network).toBe('eip155:84532'); |
| 62 | + expect(result.asset).toBe('0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'); |
| 63 | + expect(result.payTo).toBe('0x1234567890abcdef1234567890abcdef12345678'); |
| 64 | + expect(result.amount).toBe('100000'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('preserves resource metadata', () => { |
| 68 | + const result = normalizeV2Offer(ACCEPT_ENTRY, RESOURCE); |
| 69 | + expect(result.resource.url).toBe('https://api.example.com/premium'); |
| 70 | + expect(result.resource.description).toBe('Premium API access'); |
| 71 | + expect(result.resource.mimeType).toBe('application/json'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('preserves maxTimeoutSeconds (V2-specific, not epoch timestamp)', () => { |
| 75 | + const result = normalizeV2Offer(ACCEPT_ENTRY, RESOURCE); |
| 76 | + expect(result.maxTimeoutSeconds).toBe(300); |
| 77 | + }); |
| 78 | + |
| 79 | + it('preserves extra data from upstream', () => { |
| 80 | + const result = normalizeV2Offer(ACCEPT_ENTRY, RESOURCE); |
| 81 | + expect(result.extra).toEqual({ customField: 'preserved' }); |
| 82 | + }); |
| 83 | + |
| 84 | + it('preserves empty extra object', () => { |
| 85 | + const entry = { ...ACCEPT_ENTRY, extra: {} }; |
| 86 | + const result = normalizeV2Offer(entry, RESOURCE); |
| 87 | + expect(result.extra).toEqual({}); |
| 88 | + }); |
| 89 | +}); |
| 90 | + |
| 91 | +// --------------------------------------------------------------------------- |
| 92 | +// Tests: normalizeV2Offers |
| 93 | +// --------------------------------------------------------------------------- |
| 94 | + |
| 95 | +describe('normalizeV2Offers', () => { |
| 96 | + it('normalizes all accepts from a challenge', () => { |
| 97 | + const results = normalizeV2Offers(CHALLENGE); |
| 98 | + expect(results).toHaveLength(1); |
| 99 | + expect(results[0].version).toBe(2); |
| 100 | + expect(results[0].resource.url).toBe('https://api.example.com/premium'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('handles multiple accept entries with different timeouts', () => { |
| 104 | + const multiChallenge: RawV2PaymentRequired = { |
| 105 | + ...CHALLENGE, |
| 106 | + accepts: [ |
| 107 | + ACCEPT_ENTRY, |
| 108 | + { ...ACCEPT_ENTRY, network: 'eip155:1', amount: '200000', maxTimeoutSeconds: 600 }, |
| 109 | + ], |
| 110 | + }; |
| 111 | + const results = normalizeV2Offers(multiChallenge); |
| 112 | + expect(results).toHaveLength(2); |
| 113 | + expect(results[0].maxTimeoutSeconds).toBe(300); |
| 114 | + expect(results[1].maxTimeoutSeconds).toBe(600); |
| 115 | + expect(results[1].network).toBe('eip155:1'); |
| 116 | + expect(results[1].amount).toBe('200000'); |
| 117 | + }); |
| 118 | +}); |
| 119 | + |
| 120 | +// --------------------------------------------------------------------------- |
| 121 | +// Tests: normalizeV2Receipt |
| 122 | +// --------------------------------------------------------------------------- |
| 123 | + |
| 124 | +describe('normalizeV2Receipt', () => { |
| 125 | + it('normalizes successful settlement to V2 receipt', () => { |
| 126 | + const result = normalizeV2Receipt( |
| 127 | + SUCCESS_SETTLEMENT, |
| 128 | + 'https://api.example.com/premium', |
| 129 | + 1711900000 |
| 130 | + ); |
| 131 | + expect(result).not.toBeNull(); |
| 132 | + expect(result!.version).toBe(2); |
| 133 | + expect(result!.network).toBe('eip155:84532'); |
| 134 | + expect(result!.payer).toBe('0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef'); |
| 135 | + expect(result!.resourceUrl).toBe('https://api.example.com/premium'); |
| 136 | + expect(result!.issuedAt).toBe(1711900000); |
| 137 | + expect(result!.transaction).toBe( |
| 138 | + '0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890' |
| 139 | + ); |
| 140 | + }); |
| 141 | + |
| 142 | + it('returns null for failed settlement', () => { |
| 143 | + const result = normalizeV2Receipt( |
| 144 | + FAILED_SETTLEMENT, |
| 145 | + 'https://api.example.com/premium', |
| 146 | + 1711900000 |
| 147 | + ); |
| 148 | + expect(result).toBeNull(); |
| 149 | + }); |
| 150 | + |
| 151 | + it('omits transaction when empty string in success', () => { |
| 152 | + const settlement: RawV2SettlementResponse = { |
| 153 | + success: true, |
| 154 | + transaction: '', |
| 155 | + network: 'eip155:84532', |
| 156 | + payer: '0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef', |
| 157 | + }; |
| 158 | + const result = normalizeV2Receipt(settlement, 'https://api.example.com/premium', 1711900000); |
| 159 | + expect(result).not.toBeNull(); |
| 160 | + expect(result!.transaction).toBeUndefined(); |
| 161 | + }); |
| 162 | + |
| 163 | + it('preserves transaction when present', () => { |
| 164 | + const result = normalizeV2Receipt( |
| 165 | + SUCCESS_SETTLEMENT, |
| 166 | + 'https://api.example.com/premium', |
| 167 | + 1711900000 |
| 168 | + ); |
| 169 | + expect(result!.transaction).toBe( |
| 170 | + '0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890' |
| 171 | + ); |
| 172 | + }); |
| 173 | +}); |
0 commit comments