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('server dashboard shell does not embed an operational snapshot', () => { const html = readFileSync(new URL('../dashboard/public/jarvis.html', import.meta.url), 'utf8'); assert.match(html, /let D = createDashboardShellData\(\);/); assert.doesNotMatch(html, /2026-04-03T16:18:10\.188Z/); assert.doesNotMatch(html, /Trump announced new strikes on Iran/); }); test('server dashboard fetches api data before initialization', () => { const html = readFileSync(new URL('../dashboard/public/jarvis.html', import.meta.url), 'utf8'); const serverMode = html.indexOf('if (canProbeApi)'); const apiFetch = html.indexOf("fetch('/api/data')"); const firstInitAfterServerMode = html.indexOf('init();', serverMode); assert.ok(serverMode > -1); assert.ok(apiFetch > serverMode); assert.ok(firstInitAfterServerMode > apiFetch); });