import { Hono } from 'hono' import * as Earn from './earn.js' describe('hasZoneAccess', () => { test('matches every Zone granted by read or wildcard scopes', () => { const scopes = ['data:read', 'zone:1424310003:read', 'zone:1424310005:write'] const visible = [1424310003, 1424310004, 1424310005].filter((chainId) => Earn.hasZoneAccess({ chainId, scopes }), ) expect(visible).toStrictEqual([1424310003]) expect(Earn.hasZoneAccess({ chainId: 1424310004, scopes: ['*'] })).toBe(true) expect(Earn.hasZoneAccess({ chainId: 1424310005, scopes })).toBe(false) expect(Earn.hasZoneAccess({ chainId: 1424310003 })).toBe(false) }) }) describe('vaultCacheKey', () => { test('partitions Zone projections by API key after query credentials are stripped', async () => { const app = new Hono() app.use('*', async (c, next) => { c.set('chainId' as never, 42_431 as never) const principal = c.req.header('x-principal') if (principal) c.set('principal' as never, { id: principal, type: 'api_key' } as never) await next() }) app.get('/', (c) => { return c.text(Earn.vaultCacheKey(c as never, Earn.schema.getEarnVault.Query)) }) const [first, second, public_, unscoped] = await Promise.all([ app.request('/?include=zones', { headers: { 'x-principal': 'key_1' } }), app.request('/?include=zones', { headers: { 'x-principal': 'key_2' } }), app.request('/?include=zones'), app.request('/', { headers: { 'x-principal': 'key_1' } }), ]) expect([ new URL(await first.text()).searchParams.get('principal'), new URL(await second.text()).searchParams.get('principal'), new URL(await public_.text()).searchParams.get('principal'), new URL(await unscoped.text()).searchParams.get('principal'), ]).toStrictEqual(['api_key:key_1', 'api_key:key_2', null, null]) }) })