|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { UptoBillingPolicy } from './UptoBillingPolicy.js'; |
| 3 | + |
| 4 | +describe('UptoBillingPolicy', () => { |
| 5 | + it('tracks max authorization and exposes reservation ledger delta', () => { |
| 6 | + const policy = new UptoBillingPolicy(); |
| 7 | + const auth = policy.authorize({ |
| 8 | + authorizationId: 'auth-1', |
| 9 | + service: 'api.example.com', |
| 10 | + resource: 'GET /inference', |
| 11 | + network: 'base:8453', |
| 12 | + asset: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913', |
| 13 | + payTo: '0xabc', |
| 14 | + maxAmount: 1_000_000n, |
| 15 | + }); |
| 16 | + |
| 17 | + expect(auth.maxAmount).toBe(1_000_000n); |
| 18 | + expect(auth.remainingAmount).toBe(1_000_000n); |
| 19 | + expect(policy.getReservedAmount('auth-1')).toBe(1_000_000n); |
| 20 | + |
| 21 | + const deltas = policy.getWalletLedgerDeltas('auth-1'); |
| 22 | + expect(deltas).toHaveLength(1); |
| 23 | + expect(deltas[0].type).toBe('authorization'); |
| 24 | + expect(deltas[0].reservedDelta).toBe(1_000_000n); |
| 25 | + expect(deltas[0].netWalletDelta).toBe(0n); |
| 26 | + }); |
| 27 | + |
| 28 | + it('records actual settlement and wallet delta', () => { |
| 29 | + const policy = new UptoBillingPolicy(); |
| 30 | + policy.authorize({ |
| 31 | + authorizationId: 'auth-2', |
| 32 | + service: 'api.example.com', |
| 33 | + network: 'base:8453', |
| 34 | + asset: 'usdc', |
| 35 | + payTo: '0xabc', |
| 36 | + maxAmount: 1_000_000n, |
| 37 | + }); |
| 38 | + |
| 39 | + const snapshot = policy.recordSettlement('auth-2', 420_000n, { |
| 40 | + txHash: '0xsettle', |
| 41 | + }); |
| 42 | + |
| 43 | + expect(snapshot.authorization.settledAmount).toBe(420_000n); |
| 44 | + expect(snapshot.authorization.remainingAmount).toBe(580_000n); |
| 45 | + expect(snapshot.authorization.status).toBe('partially_settled'); |
| 46 | + expect(policy.getNetWalletDelta('auth-2')).toBe(-420_000n); |
| 47 | + |
| 48 | + const settlementDelta = snapshot.ledgerDeltas[1]; |
| 49 | + expect(settlementDelta.type).toBe('settlement'); |
| 50 | + expect(settlementDelta.reservedDelta).toBe(-420_000n); |
| 51 | + expect(settlementDelta.settledDelta).toBe(420_000n); |
| 52 | + expect(settlementDelta.netWalletDelta).toBe(-420_000n); |
| 53 | + }); |
| 54 | + |
| 55 | + it('releases unused authorization capacity on finalize', () => { |
| 56 | + const policy = new UptoBillingPolicy(); |
| 57 | + policy.authorize({ |
| 58 | + authorizationId: 'auth-3', |
| 59 | + service: 'api.example.com', |
| 60 | + network: 'base:8453', |
| 61 | + asset: 'usdc', |
| 62 | + payTo: '0xabc', |
| 63 | + maxAmount: 1_000_000n, |
| 64 | + }); |
| 65 | + |
| 66 | + policy.recordSettlement('auth-3', 250_000n, { finalize: true }); |
| 67 | + const auth = policy.getAuthorization('auth-3'); |
| 68 | + const deltas = policy.getWalletLedgerDeltas('auth-3'); |
| 69 | + |
| 70 | + expect(auth?.settledAmount).toBe(250_000n); |
| 71 | + expect(auth?.releasedAmount).toBe(750_000n); |
| 72 | + expect(auth?.remainingAmount).toBe(0n); |
| 73 | + expect(auth?.status).toBe('settled'); |
| 74 | + expect(policy.getReservedAmount('auth-3')).toBe(0n); |
| 75 | + expect(deltas.map((delta) => delta.type)).toEqual([ |
| 76 | + 'authorization', |
| 77 | + 'settlement', |
| 78 | + 'release', |
| 79 | + ]); |
| 80 | + }); |
| 81 | + |
| 82 | + it('rejects settlement above the authorized amount', () => { |
| 83 | + const policy = new UptoBillingPolicy(); |
| 84 | + policy.authorize({ |
| 85 | + authorizationId: 'auth-4', |
| 86 | + service: 'api.example.com', |
| 87 | + network: 'base:8453', |
| 88 | + asset: 'usdc', |
| 89 | + payTo: '0xabc', |
| 90 | + maxAmount: 100n, |
| 91 | + }); |
| 92 | + |
| 93 | + expect(() => policy.recordSettlement('auth-4', 101n)).toThrow( |
| 94 | + /exceeds remaining authorized amount/i, |
| 95 | + ); |
| 96 | + }); |
| 97 | + |
| 98 | + it('supports multiple settlements up to the authorized cap', () => { |
| 99 | + const policy = new UptoBillingPolicy(); |
| 100 | + policy.authorize({ |
| 101 | + authorizationId: 'auth-5', |
| 102 | + service: 'api.example.com', |
| 103 | + network: 'base:8453', |
| 104 | + asset: 'usdc', |
| 105 | + payTo: '0xabc', |
| 106 | + maxAmount: 1_000n, |
| 107 | + }); |
| 108 | + |
| 109 | + policy.recordSettlement('auth-5', 250n); |
| 110 | + policy.recordSettlement('auth-5', 750n); |
| 111 | + |
| 112 | + const auth = policy.getAuthorization('auth-5'); |
| 113 | + expect(auth?.settledAmount).toBe(1_000n); |
| 114 | + expect(auth?.remainingAmount).toBe(0n); |
| 115 | + expect(auth?.status).toBe('settled'); |
| 116 | + expect(policy.getNetWalletDelta('auth-5')).toBe(-1_000n); |
| 117 | + }); |
| 118 | +}); |
0 commit comments