import assert from 'node:assert/strict' import test from 'node:test' import { HttpInterceptor } from '../src/interceptor/HttpInterceptor' test('HTTP interception removes host, query and request contents before reporting', async () => { const originalFetch = globalThis.fetch const reports: Array<{ error: unknown; metadata?: Record }> = [] globalThis.fetch = async () => new Response(null, { status: 500 }) try { HttpInterceptor.start((error, metadata) => reports.push({ error, metadata })) await globalThis.fetch('https://api.example.com/am/user?phone=13800000000&token=secret', { body: JSON.stringify({ userId: 'secret-user' }), headers: { authorization: 'secret-authorization' }, method: 'POST', }) } finally { HttpInterceptor.stop() globalThis.fetch = originalFetch } assert.equal(reports.length, 1) assert.deepEqual(reports[0]?.metadata, { method: 'POST', path: '/am/user', status: 500, type: 'api_error', }) const serialized = JSON.stringify(reports) assert.doesNotMatch(serialized, /example\.com|13800000000|token|secret|authorization|userId/) }) test('BugCollect transport failures are excluded to prevent recursive reporting', async () => { const originalFetch = globalThis.fetch const reports: unknown[] = [] globalThis.fetch = async () => new Response(null, { status: 503 }) try { HttpInterceptor.start(error => reports.push(error)) const response = await globalThis.fetch( 'https://bugcollect.example.com/bugcollect/v1/issues/batch', { method: 'POST' }, ) assert.equal(response.status, 503) } finally { HttpInterceptor.stop() globalThis.fetch = originalFetch } assert.deepEqual(reports, []) })