import { App } from 'tapimo' import * as Store from '../../internal/Store.js' import * as Viem from '../../internal/Viem.js' import * as TestApp from '../../../test/App.js' import { funding } from './App.js' const sandbox = { environment: 'sandbox', id: 'key_sandbox_funding', orgId: 'org_test', scopes: ['data:read'], token: 'secret_sandbox_funding', } satisfies TestApp.kvStore.Key function as(key: { token: string }) { return { headers: { authorization: `Bearer ${key.token}` } } as const } describe('funding access', () => { test('rejects sandbox keys on a mainnet-default deployment', async () => { const app = App.create({ auth: {}, db: TestApp.database(), defaultChainId: Viem.chainId.mainnet, kv: { store: TestApp.kvStore({ keys: [sandbox] }) }, }).route('/', funding()) const response = await app.request('/v1/funding/chains', as(sandbox)) expect(response.status).toBe(403) expect(((await response.json()) as { error: { code: string } }).error.code).toBe( 'api_key_forbidden', ) }) test('requires a matching Zone scope and bypasses the edge cache', async () => { const chainId = 421_700_001 const reader = { id: 'key_zone_funding', orgId: 'org_test', scopes: ['data:read', `zone:${chainId}:read`], token: 'secret_zone_funding', } satisfies TestApp.kvStore.Key const app = App.create({ auth: {}, cache: { store: Store.memory() }, db: TestApp.database(), defaultChainId: chainId, kv: { store: TestApp.kvStore({ keys: [reader] }) }, zones: [TestApp.zone({ chainId, rpcUrl: `https://${chainId}.rpc.test` })], }).route('/', funding()) const allowed = await app.request('/v1/funding/chains', as(reader)) const anonymous = await app.request('/v1/funding/chains') expect(allowed.status).toBe(200) expect(allowed.headers.get('cache-control')).toContain('private') expect(anonymous.status).toBe(403) }) test('keeps public-chain inventory edge-cacheable on a Zone deployment', async () => { const chainId = 421_700_001 const app = App.create({ auth: false, cache: { store: Store.memory() }, db: TestApp.database(), zones: [TestApp.zone({ chainId, rpcUrl: `https://${chainId}.rpc.test` })], }).route('/', funding()) const first = await app.request('/v1/funding/chains') const second = await app.request('/v1/funding/chains') expect(first.status).toBe(200) expect(second.status).toBe(200) expect(second.headers.get('cache-control')).toContain('public') }) })