import assert from 'node:assert/strict' import { test } from 'node:test' import { getWindowThresholdAlarm } from '../src/utils/thresholdAlarmWindow.ts' const second = (value) => value * 1000 test('第 25 秒检查第 16~25 秒,窗口内任一点超限就告警', () => { const points = Array.from({ length: 10 }, (_, index) => ({ ts: second(index + 16), value: index === 0 ? 1101 : 800 })) assert.deepEqual(getWindowThresholdAlarm(points, second(25), 10, 500, 1000, 100), { value: 1101, threshold: 1100, baseThreshold: 1000, direction: 'high' }) }) test('第 15 秒的越限点不属于第 25 秒的 10 秒窗口', () => { const points = [ { ts: second(15), value: 1101 }, { ts: second(25), value: 800 } ] assert.equal(getWindowThresholdAlarm(points, second(25), 10, 500, 1000, 100), undefined) }) test('窗口随当前时间右移,旧越限点退出后告警解除', () => { const points = [ { ts: second(16), value: 1101 }, { ts: second(25), value: 800 }, { ts: second(26), value: 800 } ] assert.equal(getWindowThresholdAlarm(points, second(26), 10, 500, 1000, 100), undefined) }) test('下限按阈值减去范围检查,优先展示最近一次越限点', () => { const points = [ { ts: second(16), value: 1101 }, { ts: second(22), value: 399 }, { ts: second(25), value: 800 } ] assert.deepEqual(getWindowThresholdAlarm(points, second(25), 10, 500, 1000, 100), { value: 399, threshold: 400, baseThreshold: 500, direction: 'low' }) }) test('范围边界相等时不触发', () => { const points = [ { ts: second(24), value: 1100 }, { ts: second(25), value: 400 } ] assert.equal(getWindowThresholdAlarm(points, second(25), 10, 500, 1000, 100), undefined) }) test('乱序数据中晚于当前时间的点不参与检查', () => { const points = [ { ts: second(25), value: 800 }, { ts: second(26), value: 1101 } ] assert.equal(getWindowThresholdAlarm(points, second(25), 10, 500, 1000, 100), undefined) }) test('未设置窗口时只检查当前点,保留原有立即告警语义', () => { const points = [ { ts: second(24), value: 1001 }, { ts: second(25), value: 800 } ] assert.equal(getWindowThresholdAlarm(points, second(25), 0, 500, 1000), undefined) })