|
| 1 | +import test from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | + |
| 4 | +import { |
| 5 | + ALERT_ISSUE_TITLE, |
| 6 | + buildAlertBody, |
| 7 | + cronCadenceMs, |
| 8 | + discoverScheduledLanes, |
| 9 | + evaluateLaneHealth, |
| 10 | + expandCronField, |
| 11 | + parseScheduledLane, |
| 12 | + type LaneRun, |
| 13 | +} from './model.ts'; |
| 14 | + |
| 15 | +const DAY_MS = 24 * 60 * 60 * 1000; |
| 16 | +const HOUR_MS = 60 * 60 * 1000; |
| 17 | + |
| 18 | +function isoAgo(now: number, ms: number): string { |
| 19 | + return new Date(now - ms).toISOString(); |
| 20 | +} |
| 21 | + |
| 22 | +test('expandCronField expands wildcards, lists, ranges, and steps', () => { |
| 23 | + assert.deepEqual(expandCronField('*', 0, 6), [0, 1, 2, 3, 4, 5, 6]); |
| 24 | + assert.deepEqual(expandCronField('1,15', 0, 59), [1, 15]); |
| 25 | + assert.deepEqual(expandCronField('0-3', 0, 59), [0, 1, 2, 3]); |
| 26 | + assert.deepEqual(expandCronField('*/6', 0, 23), [0, 6, 12, 18]); |
| 27 | + // Unknown syntax fails open to the full range (never under-counts fires). |
| 28 | + assert.deepEqual(expandCronField('garbage', 0, 2), [0, 1, 2]); |
| 29 | +}); |
| 30 | + |
| 31 | +test('cronCadenceMs estimates common cadences', () => { |
| 32 | + assert.equal(cronCadenceMs('0 5 * * *'), DAY_MS); // daily |
| 33 | + assert.equal(cronCadenceMs('0 * * * *'), HOUR_MS); // hourly |
| 34 | + // Weekly (Sundays) is ~7 days. |
| 35 | + assert.equal(cronCadenceMs('0 5 * * 0'), 7 * DAY_MS); |
| 36 | + // Twice daily → ~12h. |
| 37 | + assert.equal(cronCadenceMs('0 0,12 * * *'), 12 * HOUR_MS); |
| 38 | +}); |
| 39 | + |
| 40 | +test('parseScheduledLane picks up schedule-triggered workflows and skips others', () => { |
| 41 | + const scheduled = parseScheduledLane( |
| 42 | + 'nightly.yml', |
| 43 | + ['name: Nightly Sweep', 'on:', ' schedule:', " - cron: '0 5 * * *'", 'jobs: {}'].join('\n'), |
| 44 | + ); |
| 45 | + assert.ok(scheduled); |
| 46 | + assert.equal(scheduled.name, 'Nightly Sweep'); |
| 47 | + assert.equal(scheduled.cadenceMs, DAY_MS); |
| 48 | + |
| 49 | + const pushOnly = parseScheduledLane('ci.yml', ['name: CI', 'on:', ' push: {}'].join('\n')); |
| 50 | + assert.equal(pushOnly, undefined); |
| 51 | +}); |
| 52 | + |
| 53 | +test('discoverScheduledLanes excludes the watcher itself', () => { |
| 54 | + const files = [ |
| 55 | + { file: 'a.yml', content: ['on:', ' schedule:', " - cron: '0 5 * * *'"].join('\n') }, |
| 56 | + { file: 'scheduled-lane-health.yml', content: ['on:', ' schedule:', " - cron: '0 8 * * *'"].join('\n') }, |
| 57 | + { file: 'ci.yml', content: ['on:', ' push: {}'].join('\n') }, |
| 58 | + ]; |
| 59 | + const lanes = discoverScheduledLanes(files, 'scheduled-lane-health.yml'); |
| 60 | + assert.deepEqual( |
| 61 | + lanes.map((l) => l.file), |
| 62 | + ['a.yml'], |
| 63 | + ); |
| 64 | +}); |
| 65 | + |
| 66 | +test('evaluateLaneHealth: a fresh lane is healthy', () => { |
| 67 | + const now = Date.now(); |
| 68 | + const lane = { file: 'nightly.yml', name: 'Nightly', cadenceMs: DAY_MS }; |
| 69 | + const runs: LaneRun[] = [{ conclusion: 'success', createdAt: isoAgo(now, 3 * HOUR_MS) }]; |
| 70 | + assert.equal(evaluateLaneHealth({ lane, runs, now }).healthy, true); |
| 71 | +}); |
| 72 | + |
| 73 | +test('evaluateLaneHealth: a lane gone dark (no runs for two cadences) is unhealthy', () => { |
| 74 | + const now = Date.now(); |
| 75 | + const lane = { file: 'nightly.yml', name: 'Nightly', cadenceMs: DAY_MS }; |
| 76 | + // Last success was 3 days ago and nothing since — two cadences missed. |
| 77 | + const runs: LaneRun[] = [{ conclusion: 'success', createdAt: isoAgo(now, 3 * DAY_MS) }]; |
| 78 | + const health = evaluateLaneHealth({ lane, runs, now }); |
| 79 | + assert.equal(health.healthy, false); |
| 80 | + assert.match(health.reason, /two/); |
| 81 | +}); |
| 82 | + |
| 83 | +test('evaluateLaneHealth: a lane with no successful run ever is unhealthy', () => { |
| 84 | + const now = Date.now(); |
| 85 | + const lane = { file: 'nightly.yml', name: 'Nightly', cadenceMs: DAY_MS }; |
| 86 | + const runs: LaneRun[] = [ |
| 87 | + { conclusion: 'failure', createdAt: isoAgo(now, HOUR_MS) }, |
| 88 | + { conclusion: 'failure', createdAt: isoAgo(now, DAY_MS) }, |
| 89 | + ]; |
| 90 | + const health = evaluateLaneHealth({ lane, runs, now }); |
| 91 | + assert.equal(health.healthy, false); |
| 92 | + assert.match(health.reason, /failing every cadence/); |
| 93 | +}); |
| 94 | + |
| 95 | +test('buildAlertBody summarizes only unhealthy lanes, or nothing when all healthy', () => { |
| 96 | + const now = Date.now(); |
| 97 | + const healths = [ |
| 98 | + { file: 'a.yml', name: 'A', healthy: true, reason: 'fresh' }, |
| 99 | + { file: 'b.yml', name: 'B', healthy: false, reason: 'went dark' }, |
| 100 | + ]; |
| 101 | + const body = buildAlertBody({ healths, now, runUrl: 'https://example/run/1' }); |
| 102 | + assert.ok(body); |
| 103 | + assert.match(body, /b\.yml/); |
| 104 | + assert.doesNotMatch(body, /a\.yml/); |
| 105 | + assert.match(body, /https:\/\/example\/run\/1/); |
| 106 | + |
| 107 | + const healthy = buildAlertBody({ |
| 108 | + healths: [{ file: 'a.yml', name: 'A', healthy: true, reason: 'fresh' }], |
| 109 | + now, |
| 110 | + }); |
| 111 | + assert.equal(healthy, undefined); |
| 112 | +}); |
| 113 | + |
| 114 | +test('ALERT_ISSUE_TITLE is stable so the watcher pings one issue', () => { |
| 115 | + assert.equal(typeof ALERT_ISSUE_TITLE, 'string'); |
| 116 | + assert.ok(ALERT_ISSUE_TITLE.length > 0); |
| 117 | +}); |
0 commit comments