import * as TestApp from '../../../../test/App.js' import * as Runtime from '../../../../test/runtime.js' import * as VerifiedTokens from '../../../internal/VerifiedTokens.js' import * as Verified from './verified-tokens.js' /** Reader key for the data routes. */ const reader = { id: 'key_reader', orgId: 'org_test', scopes: ['data:read'], token: 'secret_reader', } satisfies TestApp.kvStore.Key /** An address not present in the seed list. */ const unknownAddress = '0x20c00000000000000000000000000000000000ff' /** A known list published into the store before exercising the read endpoints. */ const seedList = [ { address: '0x20c0000000000000000000000000000000000001', currency: 'USD', decimals: 6, logoUri: 'https://example.com/susd.svg', name: 'Seed USD', symbol: 'sUSD' }, // prettier-ignore { address: '0x20c0000000000000000000000000000000000002', currency: 'EUR', decimals: 2, name: 'Seed EUR', symbol: 'sEUR' }, // prettier-ignore { address: '0x20c0000000000000000000000000000000000003', currency: 'USD', decimals: 6, name: 'Seed USD Two', symbol: 'sUSD2' }, // prettier-ignore ] as const const chainId = Runtime.get().chainId /** RequestInit carrying a bearer token for the given key. */ function as(key: { token: string }) { return { headers: { authorization: `Bearer ${key.token}` } } as const } /** Client whose verified-token store holds a published {@link seedList} snapshot. */ async function seeded() { const db = TestApp.database() await VerifiedTokens.replace(db, chainId, seedList) return TestApp.client({ auth: { keys: [reader] }, db, }) } describe('GET /verified-tokens', () => { test('returns the verified list', async () => { const response = await (await seeded()).v1['verified-tokens'].$get({ query: {} }, as(reader)) expect(response.status).toBe(200) const body = await TestApp.json(response, Verified.schema.listVerifiedTokens.Response) expect(body.data.map((token) => token.address)).toEqual(seedList.map((token) => token.address)) // The curated `logoUri` rides along when set, and is absent otherwise. expect(body.data[0]?.logoUri).toBe('https://example.com/susd.svg') expect(body.data[1]?.logoUri).toBeUndefined() }) test('reads through `db.cached` when configured', async () => { // Seed only the cached source: the list serving from it proves the // snapshot path resolves `cached`, not `source`. `refreshMs: 0` forces a // head check against this app's database (the per-isolate snapshot cell // is keyed by chain id, so a prior test's cell could otherwise satisfy // the read). const cached = TestApp.database() await VerifiedTokens.replace(cached, chainId, seedList) const client = TestApp.client({ auth: { keys: [reader] }, db: { cached, source: TestApp.database() }, verifiedTokens: { refreshMs: 0 }, }) const response = await client.v1['verified-tokens'].$get({ query: {} }, as(reader)) expect(response.status).toBe(200) const body = await TestApp.json(response, Verified.schema.listVerifiedTokens.Response) expect(body.data.map((token) => token.address)).toEqual(seedList.map((token) => token.address)) }) test('filters by currency (case-insensitive)', async () => { const response = await ( await seeded() ).v1['verified-tokens'].$get({ query: { currency: 'usd' } }, as(reader)) const body = await TestApp.json(response, Verified.schema.listVerifiedTokens.Response) expect(body.data.length).toBe(2) expect(body.data.every((token) => token.currency.toLowerCase() === 'usd')).toBe(true) }) test('returns 304 for a matching If-None-Match', async () => { const client = await seeded() const first = await client.v1['verified-tokens'].$get({ query: {} }, as(reader)) const etag = first.headers.get('ETag') expect(etag).toBeTruthy() const second = await client.v1['verified-tokens'].$get( { query: {} }, { headers: { 'if-none-match': etag! } }, ) expect(second.status).toBe(304) }) test('refreshes cached reads after a snapshot publish', async () => { const db = TestApp.database() await VerifiedTokens.replace(db, chainId, seedList) const client = TestApp.client({ auth: { keys: [reader] }, db }) await client.v1['verified-tokens'].$get({ query: {} }, as(reader)) await VerifiedTokens.replace(db, chainId, seedList.slice(1)) const response = await client.v1['verified-tokens'].$get({ query: {} }, as(reader)) const body = await TestApp.json(response, Verified.schema.listVerifiedTokens.Response) expect(body.data.map((token) => token.address)).toEqual( seedList.slice(1).map((token) => token.address), ) }) }) describe('GET /verified-tokens/currencies', () => { test('returns the sorted distinct currencies', async () => { const response = await ( await seeded() ).v1['verified-tokens'].currencies.$get({ query: {} }, as(reader)) const body = await TestApp.json(response, Verified.schema.getVerifiedTokenCurrencies.Response) expect([...body.data]).toEqual(['EUR', 'USD']) }) test('refreshes cached reads after a snapshot publish', async () => { const db = TestApp.database() await VerifiedTokens.replace(db, chainId, seedList) const client = TestApp.client({ auth: { keys: [reader] }, db }) await client.v1['verified-tokens'].currencies.$get({ query: {} }, as(reader)) await VerifiedTokens.replace( db, chainId, seedList.filter((token) => token.currency === 'USD'), ) const response = await client.v1['verified-tokens'].currencies.$get({ query: {} }, as(reader)) const body = await TestApp.json(response, Verified.schema.getVerifiedTokenCurrencies.Response) expect([...body.data]).toEqual(['USD']) }) }) describe('GET /verified-tokens/:address', () => { test('returns a seeded entry', async () => { const client = await seeded() const address = seedList[0].address const response = await client.v1['verified-tokens'][':address'].$get( { param: { address }, query: {} }, as(reader), ) expect(response.status).toBe(200) const body = await TestApp.json(response, Verified.schema.Token) expect(body.address).toBe(address) }) test('returns 404 for an unknown address', async () => { const response = await ( await seeded() ).v1['verified-tokens'][':address'].$get( { param: { address: unknownAddress }, query: {} }, as(reader), ) expect(response.status).toBe(404) const body = (await response.json()) as { error?: { code?: string } } expect(body.error?.code).toBe('verified_token_not_found') }) test('refreshes cached reads after a snapshot publish', async () => { const db = TestApp.database() await VerifiedTokens.replace(db, chainId, seedList) const client = TestApp.client({ auth: { keys: [reader] }, db }) const address = seedList[0].address await client.v1['verified-tokens'][':address'].$get( { param: { address }, query: {} }, as(reader), ) await VerifiedTokens.replace(db, chainId, seedList.slice(1)) const response = await client.v1['verified-tokens'][':address'].$get( { param: { address }, query: {} }, as(reader), ) expect(response.status).toBe(404) }) })