import test from 'node:test'; import assert from 'node:assert/strict'; import { buildEventEnvelope } from '../src/core/event-envelope.mjs'; import { routeHistoryRecord } from '../src/core/history-records.mjs'; import { insertEnvironmentStatusChange } from '../src/lib/postgres.mjs'; function buildEnvironmentEvent({ fingerprint, status = 'disrupted', changedAt = '2026-04-16T12:40:00.000Z' }) { return buildEventEnvelope({ source: 'ops-sentinel', venue: 'near_intents', eventType: 'environment_status', observedAt: changedAt, ingestedAt: changedAt, payload: { environment_status_id: `status-${fingerprint}-${changedAt}`, environment_key: 'near_intents_status_page', source: 'near_intents_status_page', status, label: status === 'operational' ? 'operational' : 'upstream paused', status_fingerprint: fingerprint, previous_status_fingerprint: null, observed_at: changedAt, changed_at: changedAt, decisive_reason: status === 'operational' ? 'No active incident.' : '1Click quoting paused.', current_incident_count: status === 'operational' ? 0 : 1, current_incidents: [], affected_services: [], quoting_stopped: status !== 'operational', }, }); } function createDedupePool() { const rows = []; return { rows, async query(_sql, params) { const environmentKey = params[9]; const payload = JSON.parse(params[10]); const fingerprint = params[12]; const latest = rows.filter((row) => row.decision_key === environmentKey).at(-1); if (latest?.payload.status_fingerprint === fingerprint) { return { rowCount: 0, rows: [] }; } rows.push({ event_id: params[0], decision_key: environmentKey, payload, }); return { rowCount: 1, rows: [{ event_id: params[0] }] }; }, }; } test('environment status events route into their own durable history table', () => { const event = buildEnvironmentEvent({ fingerprint: 'fp-1' }); const routed = routeHistoryRecord({ topic: 'ops.environment_status', event, }); assert.equal(routed.table, 'environment_status_events'); assert.equal(routed.record.decision_key, 'near_intents_status_page'); assert.equal(routed.record.quote_id, null); }); test('environment status persistence stores only fingerprint changes', async () => { const pool = createDedupePool(); const first = buildEnvironmentEvent({ fingerprint: 'fp-1', changedAt: '2026-04-16T12:40:00.000Z' }); const duplicate = buildEnvironmentEvent({ fingerprint: 'fp-1', changedAt: '2026-04-16T12:41:00.000Z' }); const changed = buildEnvironmentEvent({ fingerprint: 'fp-2', status: 'operational', changedAt: '2026-04-16T12:42:00.000Z' }); const firstRoute = routeHistoryRecord({ topic: 'ops.environment_status', event: first }); const duplicateRoute = routeHistoryRecord({ topic: 'ops.environment_status', event: duplicate }); const changedRoute = routeHistoryRecord({ topic: 'ops.environment_status', event: changed }); assert.equal((await insertEnvironmentStatusChange(pool, { topic: 'ops.environment_status', event: first, record: firstRoute.record })).inserted, true); assert.equal((await insertEnvironmentStatusChange(pool, { topic: 'ops.environment_status', event: duplicate, record: duplicateRoute.record })).inserted, false); assert.equal((await insertEnvironmentStatusChange(pool, { topic: 'ops.environment_status', event: changed, record: changedRoute.record })).inserted, true); assert.deepEqual(pool.rows.map((row) => row.payload.status_fingerprint), ['fp-1', 'fp-2']); });