/** @module-tag localnet */ import * as TestApp from '../../test/App.js' import * as Runtime from '../../test/runtime.js' import * as Indexer from '../apps/data/routes/indexer.js' import type * as Log from './Log.js' import * as Tidx from './Tidx.js' describe('local TIDX runtime', () => { test.runIf(Runtime.get().mode === 'localnet')( 'observes queries through a disposable TIDX', async () => { const runtime = Runtime.get() const entries: Log.Entry[] = [] const client = TestApp.client({ logger: (entry) => void entries.push(entry) }) const response = await client.v1.indexer.query.$get( { query: { chainId: String(runtime.chainId), sql: 'select 1 as value' } }, TestApp.auth, ) const body = await TestApp.json(response, Indexer.schema.Response) expect(response.status).toMatchInlineSnapshot(`200`) expect(body.ok).toMatchInlineSnapshot(`true`) expect(body.columns).toMatchInlineSnapshot(` [ "value", ] `) expect(body.rows).toMatchInlineSnapshot(` [ [ 1, ], ] `) expect(entries[0]?.provider).toBeUndefined() expect(entries[0]?.timings?.['tidx']).toBeGreaterThan(0) }, ) test.runIf(Runtime.get().mode === 'localnet')( 'does not record caller query rejections as provider failures', async () => { const runtime = Runtime.get() const entries: Log.Entry[] = [] const client = TestApp.client({ logger: (entry) => void entries.push(entry) }) const response = await client.v1.indexer.query.$get( { query: { chainId: String(runtime.chainId), sql: 'select * from missing_observability_table', }, }, TestApp.auth, ) expect(response.status).toBe(422) expect(entries[0]?.provider).toBeUndefined() expect(entries[0]?.timings?.['tidx']).toBeGreaterThan(0) }, ) test.runIf(Runtime.get().mode === 'localnet')( 'clears a handled failure after a successful query', async () => { const runtime = Runtime.get() const failures: (Log.ProviderFailure | undefined)[] = [] const timings: number[] = [] const client = Tidx.observe( Tidx.getClient({ chainId: runtime.chainId, tidx: { baseUrl: runtime.tidxUrl }, }), { record: (failure) => void failures.push(failure), time: async (fn) => { const start = performance.now() try { return await fn() } finally { timings.push(performance.now() - start) } }, }, ) await expect( client.fetch({ chainId: runtime.chainId, query: 'select * from missing_observability_table', }), ).rejects.toThrow() await client.fetch({ chainId: runtime.chainId, query: 'select 1' }) expect(failures).toEqual([ { failure: 'query', id: 'tidx', operation: 'query', status: 422, }, undefined, ]) expect(timings).toHaveLength(2) expect(timings.every((duration) => duration > 0)).toBe(true) }, ) })