import { describe, expect, test } from 'bun:test'; import type { ModuleManifest } from '../../manifest/schema'; import { effectiveHealthCheckCadence, isScheduled } from './health-cadence'; function manifestWith(interval?: string): ModuleManifest { return { hooks: { health_check: { script: './health.ts', ...(interval ? { interval } : {}) } }, } as unknown as ModuleManifest; } describe('effectiveHealthCheckCadence', () => { test("the manifest's suggestion applies when nobody has overridden", () => { expect(effectiveHealthCheckCadence(manifestWith('15m'), undefined)).toEqual({ minutes: 15 }); }); test('the operator override wins', () => { expect(effectiveHealthCheckCadence(manifestWith('15m'), '1h')).toEqual({ minutes: 60 }); }); // The failure this whole change exists for: the cadence used to be seeded onto // the monitor row at first deploy and never reconsulted, so an author who // corrected a bad interval never reached an existing install. test('a corrected suggestion reaches an un-overridden module', () => { const before = effectiveHealthCheckCadence(manifestWith('1h'), undefined); const afterUpgrade = effectiveHealthCheckCadence(manifestWith('15m'), undefined); expect(before).toEqual({ minutes: 60 }); expect(afterUpgrade).toEqual({ minutes: 15 }); }); test('a corrected suggestion does NOT disturb an overridden module', () => { expect(effectiveHealthCheckCadence(manifestWith('15m'), '1h')).toEqual({ minutes: 60 }); }); test('`manual` stops it being scheduled, and is not the same as unset', () => { expect(effectiveHealthCheckCadence(manifestWith('15m'), 'manual')).toBe('manual'); expect(isScheduled('manual')).toBe(false); expect(isScheduled(null)).toBe(false); expect(isScheduled({ minutes: 15 })).toBe(true); }); test('unsetting the override resumes the manifest cadence', () => { // `undefined` is what the accessor sees once the row is deleted. expect(effectiveHealthCheckCadence(manifestWith('15m'), undefined)).toEqual({ minutes: 15 }); }); test('nobody naming a cadence is null — a gap, not an opt-out', () => { expect(effectiveHealthCheckCadence(manifestWith(), undefined)).toBeNull(); }); test('an operator can set a cadence a module never suggested', () => { expect(effectiveHealthCheckCadence(manifestWith(), 'daily')).toEqual({ minutes: 1440 }); }); test('an unparseable override falls back to the manifest rather than going quiet', () => { expect(effectiveHealthCheckCadence(manifestWith('15m'), 'fifteen')).toEqual({ minutes: 15 }); }); });