import { Hono } from 'hono' import { Tidx } from 'tidx.ts' import * as Response from './Response.js' describe('upstream', () => { test('returns a stable message and preserves the cause on context', async () => { const app = new Hono() const cause = new Error('provider URL contains secret credentials') let captured: Error | undefined app.use(async (c, next) => { await next() captured = c.error }) app.get('/', (c) => Response.upstream(c, cause)) const response = await app.request('/') expect(await response.json()).toMatchInlineSnapshot(` { "error": { "code": "upstream_error", "message": "Upstream service could not complete the request", }, } `) expect(captured).toBe(cause) }) test('returns a query error when a positional page times out', async () => { const app = new Hono() const cause = new Tidx.FetchRequestError( 'Request timeout', new globalThis.Response(null, { status: 408 }), ) let captured: Error | undefined app.use(async (c, next) => { await next() captured = c.error }) app.get('/', (c) => Response.upstream(c, cause, { page: 2 })) const response = await app.request('/') expect(response.status).toMatchInlineSnapshot(`400`) expect(await response.json()).toMatchInlineSnapshot(` { "error": { "code": "query_invalid", "message": "The requested page timed out. Use cursor pagination for deep traversal.", }, } `) expect(captured).toBe(cause) }) test('returns a query error when a positional page is rejected', async () => { const app = new Hono() const cause = new Tidx.FetchRequestError( 'Query rejected', new globalThis.Response(null, { status: 422 }), ) let captured: Error | undefined app.use(async (c, next) => { await next() captured = c.error }) app.get('/', (c) => Response.upstream(c, cause, { page: 584 })) const response = await app.request('/') expect(response.status).toMatchInlineSnapshot(`400`) expect(await response.json()).toMatchInlineSnapshot(` { "error": { "code": "query_invalid", "message": "The requested page was rejected. Use cursor pagination for deep traversal.", }, } `) expect(captured).toBe(cause) }) test('keeps timeouts without a deep positional page as upstream errors', async () => { const app = new Hono() const cause = new Tidx.FetchRequestError( 'Request timeout', new globalThis.Response(null, { status: 408 }), ) app.get('/', (c) => Response.upstream(c, cause, { page: undefined })) const response = await app.request('/') expect(response.status).toMatchInlineSnapshot(`502`) expect(await response.json()).toMatchInlineSnapshot(` { "error": { "code": "upstream_error", "message": "Upstream service could not complete the request", }, } `) }) test('keeps query rejections without a deep positional page as upstream errors', async () => { const app = new Hono() const cause = new Tidx.FetchRequestError( 'Query rejected', new globalThis.Response(null, { status: 422 }), ) app.get('/', (c) => Response.upstream(c, cause, { page: undefined })) const response = await app.request('/') expect(response.status).toMatchInlineSnapshot(`502`) expect(await response.json()).toMatchInlineSnapshot(` { "error": { "code": "upstream_error", "message": "Upstream service could not complete the request", }, } `) }) })