import * as StripeCustomers from '../../../db/tables/stripeCustomers.js' import * as TestApp from '../../../../test/App.js' import * as Tempo from '../../../../test/Tempo.js' import * as Zones from '../../Zones.js' import * as Auth from '../../../internal/Auth.js' import * as Mpp from '../Mpp.js' import * as Policy from './Policy.js' const feePayer = Tempo.accounts[2] const zones = new Map(Zones.chains.map((zone) => [zone.id, zone])) const input = { challenge: { id: 'ch_01j3j1k2l3m4n5p6q7r8s9t0u', intent: 'charge', method: 'tempo', realm: 'merchant.example', request: { chainId: 4217, methodDetails: { chainId: 42431, feePayer: true } }, }, payload: {}, } satisfies Mpp.RelayInput const principal = { apiKey: { allowedIps: [], environment: 'production', id: 'key_mpp', orgId: 'org_mpp', scopes: ['mpp:write'], }, environment: 'production', id: 'key_mpp', orgId: 'org_mpp', projectId: 'prj_mpp', type: 'api_key', } satisfies Auth.Principal const { projectId: _, ...principal_projectless } = principal describe('chainId', () => { test('uses the method-specific chain before the request fallback', () => { expect(Policy.chainId(input)).toBe(42431) }) }) describe('requiresFeePayer', () => { test('requires both a fee-payer method detail and an enabled request', () => { expect(Policy.requiresFeePayer(input)).toBe(true) expect( Policy.requiresFeePayer({ ...input, challenge: { ...input.challenge, request: { methodDetails: { feePayer: true }, feePayer: false }, }, }), ).toBe(false) }) }) describe('assertFeeMarketAdmission', () => { test('rejects a transaction below the current base fee', () => { expect(() => Policy.assertFeeMarketAdmission({ baseFeePerGas: 2n, maxFeePerGas: 1n }), ).toThrowErrorMatchingInlineSnapshot( `[Mpp.Policy.PolicyDeniedError: Transaction max fee per gas is below the current base fee.]`, ) }) test('accepts a transaction at the current base fee', () => { expect(() => Policy.assertFeeMarketAdmission({ baseFeePerGas: 1n, maxFeePerGas: 1n }), ).not.toThrow() }) }) describe('assertChainAccess', () => { test('rejects chains outside the deployment allowlist', () => { expect(() => Policy.assertChainAccess({ chainId: 1, supportedChainIds: new Set([4217]), zones }), ).toThrowErrorMatchingInlineSnapshot( `[Mpp.Policy.UnsupportedChainError: Unsupported chain id 1.]`, ) }) }) describe('authorizeFeePayer', () => { test.each([4217, Zones.zoneMainnetInternal.id, Zones.zoneDeel.id])( 'rejects sandbox sponsorship on chain %s', async (chainId) => { await expect( Policy.authorizeFeePayer({ chainId, db: TestApp.database(), feePayer, input, principal: { ...principal, environment: 'sandbox' }, zones, }), ).rejects.toThrowErrorMatchingInlineSnapshot( `[Mpp.Policy.PolicyDeniedError: Mainnet fee sponsorship requires a production API key.]`, ) }, ) test('allows sandbox sponsorship on a testnet Zone', async () => { await expect( Policy.authorizeFeePayer({ chainId: Zones.zoneModeratoInternal.id, db: TestApp.database(), feePayer, input, principal: { ...principal, environment: 'sandbox' }, zones, }), ).resolves.toBe(feePayer) }) test('allows organization attribution for sponsored credentials', async () => { await expect( Policy.authorizeFeePayer({ chainId: 42431, db: TestApp.database(), feePayer, input, principal: principal_projectless, zones, }), ).resolves.toBe(feePayer) }) test.each([4217, Zones.zoneMainnetInternal.id, Zones.zoneDeel.id])( 'requires active billing for chain %s', async (chainId) => { const db = TestApp.database() await expect( Policy.authorizeFeePayer({ chainId, db, feePayer, input, principal: principal_projectless, zones, }), ).rejects.toThrowErrorMatchingInlineSnapshot( `[Mpp.Policy.PolicyDeniedError: Active billing is required for mainnet fee sponsorship.]`, ) await StripeCustomers.create(db, { orgId: principal_projectless.orgId, stripeCustomerId: 'cus_mpp', }) await StripeCustomers.setStatus(db, principal_projectless.orgId, 'active') await expect( Policy.authorizeFeePayer({ chainId, db, feePayer, input, principal: principal_projectless, zones, }), ).resolves.toBe(feePayer) }, ) test('does not require billing when the credential pays its own fee', async () => { await expect( Policy.authorizeFeePayer({ chainId: 4217, db: TestApp.database(), feePayer, input: { ...input, challenge: { ...input.challenge, request: { methodDetails: { feePayer: false } } }, }, principal, zones, }), ).resolves.toBe(feePayer) }) })