import { privateKeyToAccount } from 'viem/accounts' import * as StripeCustomers from '../../../db/tables/stripeCustomers.js' import * as TestApp from '../../../../test/App.js' import * as Auth from '../../../internal/Auth.js' import * as Mpp from '../Mpp.js' import * as Policy from './Policy.js' const feePayer = privateKeyToAccount(`0x${'11'.repeat(32)}`) 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('assertSupportedChain', () => { test('rejects chains outside the deployment allowlist', () => { expect(() => Policy.assertSupportedChain({ chainId: 1, supportedChainIds: new Set([4217]) }), ).toThrowErrorMatchingInlineSnapshot( `[Mpp.Policy.UnsupportedChainError: Unsupported chain id 1.]`, ) }) }) describe('authorizeFeePayer', () => { test('allows organization attribution for sponsored credentials', async () => { await expect( Policy.authorizeFeePayer({ chainId: 42431, db: TestApp.database(), feePayer, input, principal: principal_projectless, }), ).resolves.toBe(feePayer) }) test('bills a projectless mainnet sponsored credential to its organization', async () => { const db = TestApp.database() await expect( Policy.authorizeFeePayer({ chainId: 4217, db, feePayer, input, principal: principal_projectless, }), ).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: 4217, db, feePayer, input, principal: principal_projectless, }), ).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, }), ).resolves.toBe(feePayer) }) })