thresholdAlarmWindow.test.mjs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import assert from 'node:assert/strict'
  2. import { test } from 'node:test'
  3. import { getWindowThresholdAlarm } from '../src/utils/thresholdAlarmWindow.ts'
  4. const second = (value) => value * 1000
  5. test('第 25 秒检查第 16~25 秒,窗口内任一点超限就告警', () => {
  6. const points = Array.from({ length: 10 }, (_, index) => ({
  7. ts: second(index + 16),
  8. value: index === 0 ? 1101 : 800
  9. }))
  10. assert.deepEqual(getWindowThresholdAlarm(points, second(25), 10, 500, 1000, 100), {
  11. value: 1101,
  12. threshold: 1100,
  13. baseThreshold: 1000,
  14. direction: 'high'
  15. })
  16. })
  17. test('第 15 秒的越限点不属于第 25 秒的 10 秒窗口', () => {
  18. const points = [
  19. { ts: second(15), value: 1101 },
  20. { ts: second(25), value: 800 }
  21. ]
  22. assert.equal(getWindowThresholdAlarm(points, second(25), 10, 500, 1000, 100), undefined)
  23. })
  24. test('窗口随当前时间右移,旧越限点退出后告警解除', () => {
  25. const points = [
  26. { ts: second(16), value: 1101 },
  27. { ts: second(25), value: 800 },
  28. { ts: second(26), value: 800 }
  29. ]
  30. assert.equal(getWindowThresholdAlarm(points, second(26), 10, 500, 1000, 100), undefined)
  31. })
  32. test('下限按阈值减去范围检查,优先展示最近一次越限点', () => {
  33. const points = [
  34. { ts: second(16), value: 1101 },
  35. { ts: second(22), value: 399 },
  36. { ts: second(25), value: 800 }
  37. ]
  38. assert.deepEqual(getWindowThresholdAlarm(points, second(25), 10, 500, 1000, 100), {
  39. value: 399,
  40. threshold: 400,
  41. baseThreshold: 500,
  42. direction: 'low'
  43. })
  44. })
  45. test('范围边界相等时不触发', () => {
  46. const points = [
  47. { ts: second(24), value: 1100 },
  48. { ts: second(25), value: 400 }
  49. ]
  50. assert.equal(getWindowThresholdAlarm(points, second(25), 10, 500, 1000, 100), undefined)
  51. })
  52. test('乱序数据中晚于当前时间的点不参与检查', () => {
  53. const points = [
  54. { ts: second(25), value: 800 },
  55. { ts: second(26), value: 1101 }
  56. ]
  57. assert.equal(getWindowThresholdAlarm(points, second(25), 10, 500, 1000, 100), undefined)
  58. })
  59. test('未设置窗口时只检查当前点,保留原有立即告警语义', () => {
  60. const points = [
  61. { ts: second(24), value: 1001 },
  62. { ts: second(25), value: 800 }
  63. ]
  64. assert.equal(getWindowThresholdAlarm(points, second(25), 0, 500, 1000), undefined)
  65. })