import test from 'node:test'; import assert from 'node:assert/strict'; import { readFileSync } from 'node:fs'; import { safeFetch, safeFetchText, getFetchMetrics } from '../apis/utils/fetch.mjs'; test('safeFetch reports HTML as degraded JSON response', async () => { const originalFetch = globalThis.fetch; globalThis.fetch = async () => ({ ok: true, status: 200, headers: { get: () => 'text/html' }, text: async () => 'not json', }); try { const data = await safeFetch('https://example.test/json', { retries: 0, source: 'unit' }); assert.match(data.error, /Expected JSON/); assert.ok(getFetchMetrics().bySource.unit.requests >= 1); } finally { globalThis.fetch = originalFetch; } }); test('safeFetchText returns text and byte count', async () => { const originalFetch = globalThis.fetch; globalThis.fetch = async () => ({ ok: true, status: 200, text: async () => 'hello', }); try { const data = await safeFetchText('https://example.test/rss', { retries: 0, source: 'rss-unit' }); assert.equal(data.text, 'hello'); assert.equal(data.bytes, 5); } finally { globalThis.fetch = originalFetch; } }); test('SSE endpoint sends reconnect guidance and clears heartbeat timer', () => { const server = readFileSync(new URL('../server.mjs', import.meta.url), 'utf8'); const config = readFileSync(new URL('../crucix.config.mjs', import.meta.url), 'utf8'); assert.match(config, /sseHeartbeatIntervalMs/); assert.match(server, /retry: 10000\\n/); assert.match(server, /setInterval\(\(\) =>/); assert.match(server, /: heartbeat/); assert.match(server, /clearInterval\(heartbeat\)/); assert.match(server, /X-Accel-Buffering/); });