deviceStatusStats.test.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * 票 133:设备状态统计池的唯一计数入口。
  3. *
  4. * 钉住三条规则(CONTEXT.md「设备预警/报警统计」「图例入桶规则」):
  5. * 1. 报警/预警/工单是**可叠加标签**,一台设备可同时进多个池,池之和可大于台数;
  6. * 2. 在线/离线是**互斥**基础属性,只按台账状态入桶,不用「总数减离线」倒推;
  7. * 3. 缺状态/认不出的状态单列 UNKNOWN_STATE,不并入在线。
  8. */
  9. import assert from 'node:assert/strict'
  10. import { test } from 'node:test'
  11. import { deviceStatusStats, DEVICE_POOL_KEYS } from '../src/modules/cockpit/domain/deviceStatusStats.js'
  12. test('标签可叠加:一台设备可同时进报警/预警/工单三个池', () => {
  13. const { available, total, counts } = deviceStatusStats([
  14. { code: 'D1', name: '一号', status: 'ONLINE', tags: ['ALARM', 'WARNING', 'WORK_ORDER'] },
  15. { code: 'D2', status: 'ONLINE', tags: ['ALARM'] },
  16. { code: 'D3', status: 'OFFLINE', tags: ['WARNING'] },
  17. ])
  18. assert.equal(available, true)
  19. assert.equal(total, 3)
  20. assert.equal(counts.ALARM, 2)
  21. assert.equal(counts.WARNING, 2)
  22. assert.equal(counts.WORK_ORDER, 1)
  23. // 在线/离线互斥,且与标签无关(离线设备照样可以带预警标签)
  24. assert.equal(counts.NORMAL, 2)
  25. assert.equal(counts.OFFLINE, 1)
  26. assert.equal(counts.UNKNOWN_STATE, 0)
  27. // 池之和允许大于台数 —— 这是规则本身,不是重复计数
  28. assert.ok(counts.ALARM + counts.WARNING + counts.WORK_ORDER > total)
  29. })
  30. test('在线/离线只按台账状态入桶;缺状态与认不出的状态单列未知', () => {
  31. const { counts } = deviceStatusStats([
  32. { code: 'A', status: 'ONLINE', tags: [] },
  33. { code: 'B', status: 'OFFLINE', tags: [] },
  34. { code: 'C', status: '', tags: [] },
  35. { code: 'D', tags: [] },
  36. { code: 'E', status: 'STALE', tags: [] },
  37. ])
  38. assert.equal(counts.NORMAL, 1)
  39. assert.equal(counts.OFFLINE, 1)
  40. assert.equal(counts.UNKNOWN_STATE, 3, '缺状态不得并入在线')
  41. })
  42. test('标签大小写归一、重复只计一次、认不出的丢弃', () => {
  43. const { counts } = deviceStatusStats([
  44. { code: 'X', status: 'ONLINE', tags: ['alarm', 'ALARM', 'ALARM', 'UNASSOCIATED', 'nonsense'] },
  45. ])
  46. assert.equal(counts.ALARM, 1)
  47. assert.equal(counts.WARNING, 0)
  48. assert.equal(counts.WORK_ORDER, 0)
  49. assert.equal(counts.NORMAL, 1)
  50. })
  51. test('空台账 → available:false,不补 0', () => {
  52. assert.deepEqual(deviceStatusStats([]), { available: false, total: null, counts: {}, devices: [] })
  53. assert.deepEqual(deviceStatusStats(null), { available: false, total: null, counts: {}, devices: [] })
  54. })
  55. test('逐设备事实供详情/筛选消费:deviceCode + base + tags', () => {
  56. const { devices } = deviceStatusStats([
  57. { code: 'D9', name: '九号', status: 'OFFLINE', tags: ['WARNING'] },
  58. ])
  59. assert.deepEqual(devices, [{ deviceCode: 'D9', deviceName: '九号', base: 'OFFLINE', tags: ['WARNING'] }])
  60. })
  61. test('计数桶顺序固定(环图与图例共用一份行序)', () => {
  62. assert.deepEqual(DEVICE_POOL_KEYS, ['ALARM', 'WARNING', 'WORK_ORDER', 'NORMAL', 'OFFLINE'])
  63. })