import { describe, it, expect } from 'vitest' import { CRON_STALE_GRACE_SECONDS, CRON_STALE_MULTIPLIER, allowedAgeSeconds, evaluateCronHeartbeats, isFailingStatus, type CronHeartbeatRow, } from '../heartbeat-evaluation' import type { CronScheduleEntry } from '../schedules.generated' const NOW = Date.parse('2026-11-15T12:00:00.000Z') function entry(path: string, maxIntervalSeconds: number): CronScheduleEntry { return { path, schedule: '* * * * *', maxIntervalSeconds } } function row(overrides: Partial & { path: string }): CronHeartbeatRow { return { declaredAt: '2026-11-15T11:00:00.000Z', lastStartedAt: null, lastCompletedAt: null, lastStatus: null, lastHttpStatus: null, lastDurationMs: null, ...overrides, } } function isoSecondsAgo(seconds: number): string { return new Date(NOW - seconds * 1000).toISOString() } describe('allowedAgeSeconds', () => { it('is two intervals plus the grace floor', () => { expect(CRON_STALE_MULTIPLIER).toBe(2) expect(CRON_STALE_GRACE_SECONDS).toBe(120) expect(allowedAgeSeconds(60)).toBe(240) expect(allowedAgeSeconds(86_400)).toBe(2 * 86_400 + 120) }) }) describe('evaluateCronHeartbeats', () => { it('reports ok when the newest run is inside the window', () => { const report = evaluateCronHeartbeats({ entries: [entry('/api/invoices/recurring/cron', 3600)], rows: [row({ path: '/api/invoices/recurring/cron', lastStartedAt: isoSecondsAgo(1800), lastStatus: 'ok' })], now: NOW, }) expect(report.ok).toBe(true) expect(report.checks[0].state).toBe('ok') expect(report.checks[0].ageSeconds).toBe(1800) expect(report.stale).toBe(0) }) it('reports stale when a per-minute schedule has not started for four minutes', () => { const report = evaluateCronHeartbeats({ entries: [entry('/api/webhooks/dispatch/cron', 60)], rows: [ row({ path: '/api/webhooks/dispatch/cron', lastStartedAt: isoSecondsAgo(241), lastStatus: 'ok', }), ], now: NOW, }) expect(report.ok).toBe(false) expect(report.checks[0].state).toBe('stale') expect(report.stale).toBe(1) }) it('does not flag a single skipped tick', () => { const report = evaluateCronHeartbeats({ entries: [entry('/api/webhooks/dispatch/cron', 60)], rows: [row({ path: '/api/webhooks/dispatch/cron', lastStartedAt: isoSecondsAgo(120) })], now: NOW, }) expect(report.ok).toBe(true) expect(report.checks[0].state).toBe('ok') }) it('treats a never-run path inside its declared grace as pending, not stale', () => { const report = evaluateCronHeartbeats({ entries: [entry('/api/new/job/cron', 86_400)], rows: [row({ path: '/api/new/job/cron', declaredAt: isoSecondsAgo(3600) })], now: NOW, }) expect(report.ok).toBe(true) expect(report.pending).toBe(1) expect(report.checks[0].state).toBe('pending') }) it('flags a declared path that never ran once the grace is spent', () => { const report = evaluateCronHeartbeats({ entries: [entry('/api/new/job/cron', 86_400)], rows: [row({ path: '/api/new/job/cron', declaredAt: isoSecondsAgo(3 * 86_400) })], now: NOW, }) expect(report.ok).toBe(false) expect(report.checks[0].state).toBe('stale') }) it('reports a path with no row at all as pending on the first call', () => { const report = evaluateCronHeartbeats({ entries: [entry('/api/unknown/job/cron', 60)], rows: [], now: NOW, }) expect(report.ok).toBe(true) expect(report.checks[0].state).toBe('pending') expect(report.checks[0].ageSeconds).toBeNull() }) it('measures staleness from the last START, so a hung run looks dead', () => { // The run started 3 h ago and (in this row) completed successfully 2 h 59 // min ago — the row is doctored to prove last_started_at wins. After a hang // supercronic stops scheduling, so a completed_at-based check would keep // reporting ok while nothing new runs. const report = evaluateCronHeartbeats({ entries: [entry('/api/invoices/recurring/cron', 3600)], rows: [ row({ path: '/api/invoices/recurring/cron', lastStartedAt: isoSecondsAgo(3 * 3600), lastCompletedAt: isoSecondsAgo(60), }), ], now: NOW, }) expect(report.checks[0].state).toBe('stale') }) it('fails the report when any single path is stale', () => { const report = evaluateCronHeartbeats({ entries: [entry('/api/a/cron', 60), entry('/api/b/cron', 60)], rows: [ row({ path: '/api/a/cron', lastStartedAt: isoSecondsAgo(30) }), row({ path: '/api/b/cron', lastStartedAt: isoSecondsAgo(600) }), ], now: NOW, }) expect(report.ok).toBe(false) expect(report.stale).toBe(1) expect(report.checks.filter((check) => check.state === 'ok')).toHaveLength(1) }) it('carries the last outcome through so a failing-but-running job is visible', () => { const report = evaluateCronHeartbeats({ entries: [entry('/api/a/cron', 60)], rows: [ row({ path: '/api/a/cron', lastStartedAt: isoSecondsAgo(30), lastStatus: 'error', lastHttpStatus: 500, lastDurationMs: 42, }), ], now: NOW, }) expect(report.ok).toBe(true) expect(report.checks[0].lastStatus).toBe('error') expect(report.checks[0].lastHttpStatus).toBe(500) expect(report.checks[0].lastDurationMs).toBe(42) expect(isFailingStatus('error', 500)).toBe(true) }) it('ignores a malformed timestamp instead of throwing', () => { const report = evaluateCronHeartbeats({ entries: [entry('/api/a/cron', 60)], rows: [row({ path: '/api/a/cron', lastStartedAt: 'not-a-date' })], now: NOW, }) expect(report.ok).toBe(true) expect(report.checks[0].state).toBe('pending') }) }) describe('isFailingStatus', () => { it.each([ ['ok', 200, false], // A cron endpoint that redirects did not do its work either; curl -f // accepts a 3xx, which is exactly why this is worth surfacing separately. ['ok', 302, true], ['error', 200, true], ['ok', 500, true], [null, 401, true], [null, null, false], ])('status %s / http %s -> %s', (status, httpStatus, expected) => { expect(isFailingStatus(status as string | null, httpStatus as number | null)).toBe(expected) }) })