Files
vyndr/tests/unit/opsWatch.test.js
T
builtbykev 044941d406 S8 (a1): ops — the product watches itself
Settlement alarm (settle-pass THROW pages high; morning zero-settle alarm
keyed off the Postgres ledger settle results, once per ET date, never on an
empty yesterday), per-sport 3-consecutive-slot failure pager (pure
opsWatch.createFailureTracker, pages once per losing streak), odds-api >=80%
quota alert (once per day, Redis-deduped), systemHealth (statfs + os mem,
disk>85 / mem>90 pages), daily 9 AM ET pulse (ONE notification: ledger rows
yesterday via ledgerService.countRowsForDate, settles 24h, quota, disk/mem,
desk line), docs/OPS-RUNBOOK.md (Uptime Kuma monitors, Coolify
deploy-failure -> ntfy, phone subscription). All copy VOICE v1.1 — deadpan,
numbers, no exclamation points (tests lint for it). 2398 -> 2437 tests.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-11 14:34:45 -04:00

210 lines
7.9 KiB
JavaScript

'use strict';
// Session 8 (A1 board) — opsWatch: the pure ops-alarm logic.
const {
createFailureTracker,
isBadSnapshotResult,
zeroSettleAlarm,
morningHourUtc,
checkQuotaDaily,
countRecentSettles,
buildPulseMessage,
dateET,
} = require('../../src/services/opsWatch');
function memCache(seed = {}) {
const store = { ...seed };
return {
store,
cacheGet: async (k) => (k in store ? store[k] : null),
cacheSet: async (k, v) => { store[k] = v; return true; },
};
}
describe('isBadSnapshotResult', () => {
test('error and skipped/no-props are bad; ok and skipped/no-grades are not', () => {
expect(isBadSnapshotResult({ status: 'error', reason: 'down' })).toBe(true);
expect(isBadSnapshotResult({ status: 'skipped', reason: 'no props' })).toBe(true);
expect(isBadSnapshotResult({ status: 'ok' })).toBe(false);
expect(isBadSnapshotResult({ status: 'skipped', reason: 'no grades' })).toBe(false);
expect(isBadSnapshotResult(null)).toBe(false);
});
});
describe('createFailureTracker', () => {
test('pages exactly once at the third consecutive failure, stays quiet after', () => {
const t = createFailureTracker(3);
const bad = { status: 'error', reason: 'x' };
expect(t.record('mlb', bad).shouldPage).toBe(false); // 1
expect(t.record('mlb', bad).shouldPage).toBe(false); // 2
const third = t.record('mlb', bad);
expect(third.shouldPage).toBe(true); // 3 -> page once
expect(third.count).toBe(3);
expect(t.record('mlb', bad).shouldPage).toBe(false); // 4 -> no re-page
expect(t.count('mlb')).toBe(4);
});
test('any good outcome resets the counter and re-arms the pager', () => {
const t = createFailureTracker(3);
const bad = { status: 'skipped', reason: 'no props' };
t.record('mlb', bad); t.record('mlb', bad);
t.record('mlb', { status: 'ok' }); // reset
expect(t.count('mlb')).toBe(0);
t.record('mlb', bad); t.record('mlb', bad);
expect(t.record('mlb', bad).shouldPage).toBe(true); // re-armed -> pages again
});
test('counts are independent per sport', () => {
const t = createFailureTracker(3);
const bad = { status: 'error' };
t.record('mlb', bad); t.record('mlb', bad);
expect(t.record('wnba', bad).count).toBe(1);
expect(t.count('mlb')).toBe(2);
});
});
describe('zeroSettleAlarm', () => {
test('alarms when settleable rows existed and none settled', () => {
const z = zeroSettleAlarm([{ sport: 'mlb', settled: 0, pending: 12 }]);
expect(z).toEqual({ alarm: true, settled: 0, pending: 12 });
});
test('never alarms on a genuinely empty yesterday (0 rows fetched)', () => {
expect(zeroSettleAlarm([{ sport: 'mlb', settled: 0, pending: 0 }]).alarm).toBe(false);
expect(zeroSettleAlarm([]).alarm).toBe(false);
expect(zeroSettleAlarm(null).alarm).toBe(false);
});
test('no alarm when anything settled', () => {
expect(zeroSettleAlarm([{ sport: 'mlb', settled: 3, pending: 9 }]).alarm).toBe(false);
});
test('ignores sports without a settled-result feed (WNBA pendings are honest)', () => {
const z = zeroSettleAlarm([
{ sport: 'mlb', settled: 0, pending: 0 },
{ sport: 'wnba', settled: 0, pending: 40 },
]);
expect(z.alarm).toBe(false);
});
});
describe('morningHourUtc', () => {
test('first configured hour >= 06 UTC (1,3 are late-night ET of the prior day)', () => {
expect(morningHourUtc([14, 19, 22, 1, 3])).toBe(14);
expect(morningHourUtc([19, 14])).toBe(14);
});
test('falls back to the smallest hour when nothing is daytime', () => {
expect(morningHourUtc([1, 3])).toBe(1);
expect(morningHourUtc([])).toBe(14);
});
});
describe('checkQuotaDaily', () => {
const notifyCollector = () => {
const msgs = [];
return { msgs, notify: async (m, o) => { msgs.push({ m, o }); return { sent: true }; } };
};
test('below 80% -> no alert', async () => {
const { msgs, notify } = notifyCollector();
const cache = memCache();
const r = await checkQuotaDaily({
getStatus: async () => ({ pct: 0.5, used: 250, limit: 500 }),
...cache, notify, now: () => new Date('2026-07-11T15:00:00Z'),
});
expect(r.alerted).toBe(false);
expect(msgs).toHaveLength(0);
});
test('>= 80% alerts once, then dedupes for the rest of the day', async () => {
const { msgs, notify } = notifyCollector();
const cache = memCache();
const deps = {
getStatus: async () => ({ pct: 0.82, used: 410, limit: 500, quotaType: 'monthly' }),
...cache, notify, now: () => new Date('2026-07-11T15:00:00Z'),
};
expect((await checkQuotaDaily(deps)).alerted).toBe(true);
const second = await checkQuotaDaily(deps);
expect(second.alerted).toBe(false);
expect(second.deduped).toBe(true);
expect(msgs).toHaveLength(1);
expect(msgs[0].m).toContain('82%');
expect(msgs[0].m).toContain('410/500');
expect(msgs[0].o.priority).toBe('high');
expect(msgs[0].m).not.toContain('!');
expect(cache.store['ops:quota_day:odds-api:2026-07-11']).toBe('1');
});
test('fires again the next day (date-keyed dedupe)', async () => {
const { msgs, notify } = notifyCollector();
const cache = memCache();
const base = {
getStatus: async () => ({ pct: 0.9, used: 450, limit: 500 }),
...cache, notify,
};
await checkQuotaDaily({ ...base, now: () => new Date('2026-07-11T15:00:00Z') });
await checkQuotaDaily({ ...base, now: () => new Date('2026-07-12T15:00:00Z') });
expect(msgs).toHaveLength(2);
});
test('never throws — a broken status probe returns { alerted: false }', async () => {
const r = await checkQuotaDaily({ getStatus: async () => { throw new Error('redis gone'); } });
expect(r.alerted).toBe(false);
expect(r.error).toBe('redis gone');
});
});
describe('countRecentSettles', () => {
test('counts settledAt inside the window across multiple sport logs', () => {
const nowMs = Date.parse('2026-07-11T13:00:00Z');
const logs = [
[ { settledAt: '2026-07-11T14:05:00Z' }, { settledAt: '2026-07-09T14:00:00Z' } ], // 1 in, 1 out
[ { settledAt: '2026-07-10T14:30:00Z' }, { noSettledAt: true } ], // 1 in
];
expect(countRecentSettles(logs, nowMs)).toBe(2);
expect(countRecentSettles([], nowMs)).toBe(0);
expect(countRecentSettles(null, nowMs)).toBe(0);
});
});
describe('buildPulseMessage', () => {
test('one message with every field, real numbers rendered', () => {
const msg = buildPulseMessage({
dateEt: '2026-07-11',
ledgerRows: 42,
settles24h: 38,
quota: { pct: 0.12, used: 61, limit: 500 },
health: { disk_pct: 41, mem_pct: 63 },
});
expect(msg).toContain('VYNDR pulse — 2026-07-11');
expect(msg).toContain('ledger rows yesterday: 42');
expect(msg).toContain('settles last 24h: 38');
expect(msg).toContain('odds-api quota: 12% (61/500)');
expect(msg).toContain('disk 41% · mem 63%');
expect(msg).toContain('desk pack: see /desk');
});
test('missing data renders n/a — never a fabricated zero', () => {
const msg = buildPulseMessage({ dateEt: '2026-07-11', ledgerRows: null, settles24h: null, quota: null, health: null });
expect(msg).toContain('ledger rows yesterday: n/a');
expect(msg).toContain('settles last 24h: n/a');
expect(msg).toContain('odds-api quota: n/a');
expect(msg).toContain('disk n/a · mem n/a');
});
test('VOICE v1.1 — no exclamation points, ever', () => {
const loud = buildPulseMessage({ ledgerRows: 999, settles24h: 999, quota: { pct: 0.99, used: 495, limit: 500 }, health: { disk_pct: 99, mem_pct: 99 } });
expect(loud).not.toContain('!');
expect(buildPulseMessage({})).not.toContain('!');
});
});
describe('dateET', () => {
test('renders the America/New_York calendar date (late UTC rolls back)', () => {
// 03:00 UTC Jul 11 = 11:00 PM ET Jul 10.
expect(dateET(new Date('2026-07-11T03:00:00Z'))).toBe('2026-07-10');
expect(dateET(new Date('2026-07-11T15:00:00Z'))).toBe('2026-07-11');
});
});