import { describe, it, expect } from 'vitest' import { describeGap, maxIntervalSeconds, parseCronExpression, } from '../cron-interval' /** * These are the numbers app/api/cron/health turns into alert thresholds, so the * tests assert the actual worst-case gaps rather than "returns a number". A * wrong threshold is not a cosmetic bug: too small and every slow run flaps the * cron container unhealthy, too large and a stopped schedule keeps looking fine * — the exact failure the 2026-11-15 incident was. */ describe('parseCronExpression', () => { it('parses the five fields in order', () => { const spec = parseCronExpression('15 3 * * 1-5') expect(spec.minutes).toEqual([15]) expect(spec.hours).toEqual([3]) expect(spec.daysOfMonth).toHaveLength(31) expect(spec.months).toHaveLength(12) expect(spec.daysOfWeek).toEqual([1, 2, 3, 4, 5]) expect(spec.dayOfMonthRestricted).toBe(false) expect(spec.dayOfWeekRestricted).toBe(true) }) it('expands steps, ranges and lists', () => { expect(parseCronExpression('*/15 * * * *').minutes).toEqual([0, 15, 30, 45]) expect(parseCronExpression('2,4,6-8 * * * *').minutes).toEqual([2, 4, 6, 7, 8]) expect(parseCronExpression('5/20 * * * *').minutes).toEqual([5, 25, 45]) }) it('normalises Sunday 7 to 0', () => { expect(parseCronExpression('0 0 * * 7').daysOfWeek).toEqual([0]) expect(parseCronExpression('0 0 * * 0').daysOfWeek).toEqual([0]) expect(parseCronExpression('0 0 * * 0,7').daysOfWeek).toEqual([0]) }) it('treats a star step as unrestricted, like every cron implementation', () => { expect(parseCronExpression('0 0 */1 * *').dayOfMonthRestricted).toBe(false) }) it.each([ ['* * * *', 'four fields'], ['* * * * * *', 'six fields'], ['60 * * * *', 'minute out of range'], ['* 24 * * *', 'hour out of range'], ['* * 0 * *', 'day-of-month out of range'], ['* * * 13 *', 'month out of range'], ['* * * * 8', 'day-of-week out of range'], ['*/0 * * * *', 'zero step'], ['5-2 * * * *', 'inverted range'], ['a * * * *', 'non-numeric'], ])('rejects %s (%s)', (expression) => { expect(() => parseCronExpression(expression)).toThrow() }) }) describe('maxIntervalSeconds', () => { it('every-minute is exactly 60 s', () => { expect(maxIntervalSeconds('* * * * *')).toBe(60) }) it('*/2 is 120 s and */5 is 300 s', () => { expect(maxIntervalSeconds('*/2 * * * *')).toBe(120) expect(maxIntervalSeconds('*/5 * * * *')).toBe(300) }) it('hourly on the hour is 3600 s', () => { expect(maxIntervalSeconds('0 * * * *')).toBe(3600) }) it('a daily job is 86400 s', () => { expect(maxIntervalSeconds('0 6 * * *')).toBe(86_400) expect(maxIntervalSeconds('30 4 * * *')).toBe(86_400) }) it('every other hour at :30 is 7200 s, not 3600 s', () => { // The gap between 22:30 and 00:30 spans two hours; an average-gap // implementation would say 3600 and alert on the normal cadence. expect(maxIntervalSeconds('30 */2 * * *')).toBe(7200) }) it('weekly is seven days', () => { expect(maxIntervalSeconds('0 3 * * 0')).toBe(7 * 86_400) }) it('yearly on 2 January is 365 or 366 days depending on the leap year it spans', () => { // 2003-01-02 -> 2004-01-02 spans no 29 February, 2004 -> 2005 does not // either; 1 Jan -> 1 Jan crossing a leap day is the 8784-hour case. Either // way the threshold must be at least a year, never 0. expect(maxIntervalSeconds('0 0 2 1 *')).toBeGreaterThanOrEqual(365 * 86_400) }) it('29 February only matches in leap years and still returns a finite gap', () => { const gap = maxIntervalSeconds('0 0 29 2 *') expect(gap).toBeGreaterThanOrEqual(365 * 86_400) expect(Number.isFinite(gap)).toBe(true) }) it('honours the cron day rule: restricted day-of-month OR day-of-week', () => { // "1st of the month OR every Monday" still has a Monday every seven days, // so the worst case is a week — a naive AND would report ~28 days and hide // a fortnight of misses. expect(maxIntervalSeconds('0 0 1 * 1')).toBe(7 * 86_400) }) }) describe('describeGap', () => { it.each([ [60, '60 s'], [120, '2 min'], [3600, '60 min'], [7200, '2 h'], [86_400, '24 h'], [7 * 86_400, '7 dygn'], ])('renders %i s as %s', (seconds, expected) => { expect(describeGap(seconds)).toBe(expected) }) })