import test from 'node:test'; import assert from 'node:assert/strict'; import { safeFetch, safeFetchText, getFetchMetrics } from '../apis/utils/fetch.mjs'; import { formatStaleAlert, shouldSendStaleAlert } from '../lib/stale-alerts.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('stale alert is skipped for fresh health and resets active key', () => { const state = { lastStaleAlertKey: 'old', lastStaleAlertAt: 100 }; const decision = shouldSendStaleAlert({ stale: false }, state, { now: 200 }); assert.equal(decision.send, false); assert.equal(decision.reason, 'not_stale'); assert.equal(state.lastStaleAlertKey, null); }); test('stale alert sends once and deduplicates during cooldown', () => { const state = {}; const health = { stale: true, lastSuccessfulSweep: '2026-05-17T08:00:00.000Z', lastSweepError: 'network timeout', sourcesFailed: 2, sourcesDegraded: 1, }; const first = shouldSendStaleAlert(health, state, { now: 1_000, cooldownMs: 60_000 }); const second = shouldSendStaleAlert(health, state, { now: 2_000, cooldownMs: 60_000 }); assert.equal(first.send, true); assert.equal(second.send, false); assert.equal(second.reason, 'cooldown'); }); test('stale alert repeats after cooldown', () => { const state = {}; const health = { stale: true, lastSuccessfulSweep: 'a', lastSweepError: 'timeout', sourcesFailed: 1 }; assert.equal(shouldSendStaleAlert(health, state, { now: 1_000, cooldownMs: 60_000 }).send, true); assert.equal(shouldSendStaleAlert(health, state, { now: 62_000, cooldownMs: 60_000 }).send, true); }); test('stale alert message includes operator context and affected sources', () => { const message = formatStaleAlert({ status: 'stale', stale: true, dataAgeSeconds: 7200, lastSuccessfulSweep: '2026-05-17T08:00:00.000Z', lastSweep: '2026-05-17T10:00:00.000Z', lastSweepError: 'GDELT timeout', sourcesOk: 20, sourcesDegraded: 3, sourcesFailed: 2, sourceHealth: [ { name: 'GDELT', status: 'degraded', error: 'timeout' }, { name: 'Reddit', status: 'no_credentials' }, ], }, { dashboardUrl: 'https://terminal.example.test', context: 'failed sweep' }); assert.match(message, /CRUCIX STALE DATA ALERT/); assert.match(message, /Data age: 120 minutes/); assert.match(message, /GDELT: degraded \(timeout\)/); assert.match(message, /Dashboard: https:\/\/terminal\.example\.test/); });