warningIndicator.test.mjs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import test from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import { indicatorName, setIndicatorNames, normalizeCode } from '../src/utils/warningIndicator.js'
  4. test('优先使用后端下发的监测指标中文名', () => {
  5. setIndicatorNames([])
  6. assert.equal(indicatorName({ warningName: '窨井倾斜预警', warningCode: 'WARN-TILT' }), '窨井倾斜预警')
  7. })
  8. test('没有 warningName 时使用 alarm_data 的中文类型名', () => {
  9. setIndicatorNames([])
  10. assert.equal(indicatorName({ warningType: '倾斜预警', warningCode: 'WARN-TILT' }), '倾斜预警')
  11. })
  12. test('通过 warning_code 从 warning_type 字典取 warning_name 显示', () => {
  13. setIndicatorNames([
  14. { dictValue: 'WARN-TILT', dictLabel: '窨井倾斜预警' },
  15. { typeCode: 'WARN_PRESSURE', typeName: '压力预警' }
  16. ])
  17. assert.equal(indicatorName({ warningCode: 'WARN-TILT' }, '监测指标'), '窨井倾斜预警')
  18. assert.equal(indicatorName({ warningCode: 'WARN_PRESSURE' }, '监测指标'), '压力预警')
  19. })
  20. test('warning_type 与 alarm_data 编码 -/_、大小写混用也能关联上', () => {
  21. setIndicatorNames([{ dictValue: 'WARN_TILT', dictLabel: '窨井倾斜预警' }])
  22. assert.equal(indicatorName({ warningCode: ' warn-tilt ' }, '监测指标'), '窨井倾斜预警')
  23. assert.equal(normalizeCode(' WARN-Tilt '), 'WARN_TILT')
  24. })
  25. test('任何 WARN-*/WARN_* 编码都不作为展示文案', () => {
  26. setIndicatorNames([])
  27. assert.equal(indicatorName({ warningName: 'WARN-TILT' }, '监测指标'), '监测指标')
  28. assert.equal(indicatorName({ warningType: 'WARN_PRESSURE' }, '监测指标'), '监测指标')
  29. assert.equal(indicatorName({ warningType: 'UNKNOWN_EVENT_TYPE' }, '监测指标'), '监测指标')
  30. })
  31. test('全大写下划线编码也不当作名称', () => {
  32. setIndicatorNames([])
  33. assert.equal(indicatorName({ warningType: 'DRAINAGE_DEVICE' }, '监测指标'), '监测指标')
  34. })
  35. test('字段缺失或字典未命中时返回兜底文案而不是编码', () => {
  36. setIndicatorNames([])
  37. assert.equal(indicatorName({ warningCode: 'WARN-TILT' }), '—')
  38. assert.equal(indicatorName({ warningCode: 'WARN-TILT' }, '其他告警'), '其他告警')
  39. assert.equal(indicatorName(), '—')
  40. setIndicatorNames([{ dictValue: 'WARN-PRESSURE', dictLabel: '压力预警' }])
  41. assert.equal(indicatorName({ warningCode: 'WARN-TILT' }, '监测指标'), '监测指标')
  42. })