import { alertHistoryRange, mergeAlertHistory } from './alert-history'; describe('historical alert observation cycles', () => { it('uses seven Shanghai calendar days and rejects invalid or excessive dates', () => { expect(alertHistoryRange(undefined, undefined, new Date('2026-09-09T16:30:00Z'))).toMatchObject({ startDate: '2026-09-04', endDate: '2026-09-10', }); for (const [from, to] of [ ['2026-02-30', '2026-03-01'], ['2026-09-09', '2026-09-08'], ['2026-07-01', '2026-09-09'], ]) { expect(() => alertHistoryRange(from, to)).toThrow(); } }); it('retains distinct cycles, merges daily boundaries and excludes stale/nonpositive samples', () => { const result = new Map(); const metric = { __name__: 'ALERTS_FOR_STATE', alertname: 'CPUHigh', instance: 'host', severity: 'warning' }; mergeAlertHistory( result, [ { metric, values: [ [110, '100'], [120, '100'], [130, '0'], [140, 'NaN'], [150, '145'], [200, '145'], ], }, ], 110, 200, ); mergeAlertHistory(result, [{ metric, values: [[160, '145']] }], 110, 200); expect(result.size).toBe(2); expect([...result.values()].map((item) => item.lastObservedAt)).toEqual([ new Date(120000).toISOString(), new Date(160000).toISOString(), ]); }); });