import { afterEach, describe, expect, it, vi } from 'vitest'; import { extractFromText } from './extraction-client.js'; describe('extraction client request propagation', () => { afterEach(() => { vi.unstubAllGlobals(); }); it('sends x-request-id to extraction-service when provided', async () => { const fetchMock = vi.fn(async () => new Response(JSON.stringify({ summary: 'ok' }), { status: 200 })); vi.stubGlobal('fetch', fetchMock); await extractFromText('hello', 'summarization', { requestId: 'req-propagated' }); expect(fetchMock).toHaveBeenCalledOnce(); const init = fetchMock.mock.calls[0]?.[1] as RequestInit; expect(init.headers).toMatchObject({ 'Content-Type': 'application/json', 'x-request-id': 'req-propagated', }); }); it('maps extraction-service failures to a structured dependency error', async () => { vi.stubGlobal( 'fetch', vi.fn(async () => new Response(JSON.stringify({ error: 'down' }), { status: 503, statusText: 'Service Unavailable' })), ); await expect(extractFromText('hello', 'summarization')).rejects.toMatchObject({ statusCode: 502, message: 'Extraction service failed', details: { code: 'EXTRACTION_SERVICE_FAILURE', dependency: 'extraction-service', }, }); }); });