waterHomeModel.test.mjs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import test from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import {
  4. HOME_QUICK_ENTRIES,
  5. buildHomeWorkOrderQuery,
  6. normalizeFacilityTotal,
  7. normalizeHomeKpis,
  8. normalizeWorkOrderStatusStat,
  9. normalizeAlarmOverviewCards,
  10. normalizeAlarmTrend,
  11. normalizePieRatio,
  12. normalizeRealtimeAlarmRows,
  13. realtimeAlarmJumpTarget,
  14. normalizeAbnormalTodoRows,
  15. normalizeDeviceSummary,
  16. normalizeWorkOrderRows,
  17. buildHomeGisOverview
  18. } from '../src/utils/waterHomeModel.js'
  19. test('home work order scope is constrained to the water-supply type tree', () => {
  20. assert.deepEqual(buildHomeWorkOrderQuery(), {
  21. deviceTypeParentId: '1',
  22. pageNum: 1,
  23. pageSize: 5
  24. })
  25. assert.deepEqual(buildHomeWorkOrderQuery({ pageSize: 8 }), {
  26. deviceTypeParentId: '1',
  27. pageNum: 1,
  28. pageSize: 8
  29. })
  30. })
  31. test('facility total excludes the aggregated total row', () => {
  32. const rows = [
  33. { facilityType: 'source', totalCount: 3 },
  34. { facilityType: 'plant', totalCount: 2 },
  35. { facilityType: 'pipe', totalCount: 996 },
  36. { facilityType: 'total', totalCount: 1000 }
  37. ]
  38. assert.equal(normalizeFacilityTotal(rows), 1001)
  39. assert.equal(normalizeFacilityTotal(null), 0)
  40. assert.equal(normalizeFacilityTotal([]), 0)
  41. })
  42. test('home KPIs map api payloads to workbench cards', () => {
  43. const kpis = normalizeHomeKpis({
  44. facilityRows: [{ facilityType: 'pipe', totalCount: 996 }, { facilityType: 'total', totalCount: 1000 }],
  45. ledgerStats: { total: 15, online: 13 },
  46. alarmStats: { realtimeAlarmCount: 3 }
  47. })
  48. assert.deepEqual(kpis, [
  49. { key: 'facility', label: '设施总数', value: 996, unit: '处', tone: 'primary' },
  50. { key: 'device', label: '监测设备', value: 15, unit: '台', tone: 'primary' },
  51. { key: 'online', label: '在线设备', value: 13, unit: '台', tone: 'primary' },
  52. { key: 'alarm', label: '实时报警', value: 3, unit: '条', tone: 'danger' }
  53. ])
  54. })
  55. test('home KPIs keep realtime alarm card neutral when no alarm is active', () => {
  56. const alarm = normalizeHomeKpis({ alarmStats: { realtimeAlarmCount: 0 } }).find(item => item.key === 'alarm')
  57. assert.equal(alarm.tone, 'primary')
  58. })
  59. test('work order status statistic groups by authoritative status contract', () => {
  60. const stat = normalizeWorkOrderStatusStat({
  61. total: 10,
  62. counts: [
  63. { status: 0, count: 2 },
  64. { status: 1, count: 1 },
  65. { status: 3, count: 2 },
  66. { status: 4, count: 2 },
  67. { status: 5, count: 2 },
  68. { status: 7, count: 1 }
  69. ]
  70. })
  71. assert.equal(stat.total, 10)
  72. assert.equal(stat.pendingCount, 3)
  73. assert.equal(stat.processingCount, 2)
  74. assert.equal(stat.verifyingCount, 2)
  75. assert.equal(stat.finishedCount, 3)
  76. assert.equal(stat.byStatus.find(item => item.status === 6).count, 0)
  77. })
  78. test('alarm overview cards derive online rate and verifying orders', () => {
  79. const cards = normalizeAlarmOverviewCards({
  80. dashboard: { totalCount: 16, alarmingDeviceCount: 4 },
  81. deviceStats: { onlineRate: 86.666666 },
  82. workOrderStat: {
  83. counts: [{ status: 4, count: 3 }]
  84. }
  85. })
  86. assert.deepEqual(cards, [
  87. { key: 'alarmTotal', label: '报警总数', value: 16, unit: '条', tone: 'primary' },
  88. { key: 'alarmingDevice', label: '报警中设备', value: 4, unit: '台', tone: 'danger' },
  89. { key: 'verifyingOrder', label: '待验收工单', value: 3, unit: '单', tone: 'warning' },
  90. { key: 'onlineRate', label: '在线率', value: 86.7, unit: '%', tone: 'primary' }
  91. ])
  92. })
  93. test('alarm trend returns four level series with zero-filled missing days', () => {
  94. const trend = normalizeAlarmTrend([
  95. { date: '2026-09-15', level1Count: 1, level2Count: 2 },
  96. { date: '2026-09-16' }
  97. ])
  98. assert.deepEqual(trend.dates, ['2026-09-15', '2026-09-16'])
  99. assert.equal(trend.levels.length, 4)
  100. const level2 = trend.levels.find(item => item.name === '级2')
  101. assert.deepEqual(level2.data, [2, 0])
  102. assert.equal(normalizeAlarmTrend(null).levels.length, 4)
  103. })
  104. test('ratio pie drops empty values and caps the number of segments', () => {
  105. const items = [
  106. { name: ' ' },
  107. { name: '压力', count: '8' },
  108. { name: '流量', count: '5' },
  109. { name: '离线', count: '3' },
  110. { name: '低', count: 0 }
  111. ]
  112. assert.deepEqual(normalizePieRatio(items, '未分类', 3), [
  113. { name: '压力', count: 8 },
  114. { name: '流量', count: 5 },
  115. { name: '离线', count: 3 }
  116. ])
  117. assert.deepEqual(normalizePieRatio(undefined, '未分类'), [])
  118. })
  119. test('realtime alarm rows expose chinese friendly display fields', () => {
  120. const rows = normalizeRealtimeAlarmRows([
  121. {
  122. id: 'a1',
  123. deviceCode: 'CN-GS-202609020001',
  124. deviceName: '一号压力点',
  125. warningType: '压力过低',
  126. alarmLevel: 1,
  127. alarmTime: '2026-09-15 10:00:00',
  128. equipmentLocation: '城南',
  129. displayStatus: 1
  130. }
  131. ])
  132. assert.equal(rows.length, 1)
  133. assert.equal(rows[0].deviceName, '一号压力点')
  134. assert.equal(rows[0].alarmLevelText, '一级报警')
  135. assert.equal(rows[0].location, '城南')
  136. assert.equal(rows[0].displayStatusText, '待审核')
  137. assert.equal(rows[0].displayStatusType, 'warning')
  138. assert.equal(normalizeRealtimeAlarmRows([], 5).length, 0)
  139. })
  140. test('realtime alarm rows drop released alarms and keep status for routing', () => {
  141. const rows = normalizeRealtimeAlarmRows([
  142. { id: 'a-pending', deviceCode: 'CN-GS-202609020001', displayStatus: 1, alarmTime: '2026-09-15 10:00:00' },
  143. { id: 'a-order', deviceCode: 'CN-GS-202609020002', displayStatus: 2, orderNo: 'WO202609151100001', alarmTime: '2026-09-15 09:00:00' },
  144. { id: 'a-released', deviceCode: 'CN-GS-202609020003', displayStatus: 3, alarmTime: '2026-09-15 08:00:00' }
  145. ], 5)
  146. assert.deepEqual(rows.map(item => item.id), ['a-pending', 'a-order'])
  147. assert.equal(rows.find(item => item.id === 'a-order').displayStatusText, '已转工单')
  148. assert.equal(rows.find(item => item.id === 'a-order').orderNo, 'WO202609151100001')
  149. assert.equal(rows.find(item => item.id === 'a-pending').displayStatusType, 'warning')
  150. })
  151. test('realtime alarm jumps split between audit and work order by display status', () => {
  152. assert.deepEqual(realtimeAlarmJumpTarget({
  153. id: 'alarm-1',
  154. deviceCode: 'CN-GS-202609020001',
  155. displayStatus: 1
  156. }), {
  157. path: '/monitorAlarm/audit',
  158. query: { alarmId: 'alarm-1', deviceCode: 'CN-GS-202609020001' }
  159. })
  160. assert.deepEqual(realtimeAlarmJumpTarget({
  161. id: 'alarm-2',
  162. deviceCode: 'CN-GS-202609020002',
  163. displayStatus: 2,
  164. orderNo: 'WO202609151100001'
  165. }), {
  166. path: '/device/maintain',
  167. query: { keyword: 'WO202609151100001' }
  168. })
  169. // 已转工单但缺少工单号时回落到报警审核,避免无效跳转
  170. const fallback = realtimeAlarmJumpTarget({ id: 'alarm-3', displayStatus: 2 })
  171. assert.equal(fallback.path, '/monitorAlarm/audit')
  172. })
  173. test('abnormal todo rows reuse the shared device normalization contract', () => {
  174. const rows = normalizeAbnormalTodoRows([{
  175. equipmentId: 'eq-1',
  176. equipmentCode: 'CN-GS-202609020002',
  177. equipmentName: '二号流量点',
  178. alarmStatus: 1,
  179. onlineStatus: 1,
  180. currentStatus: 1,
  181. alertLevel: 2,
  182. relatedWorkOrderNo: 'WO202609150001',
  183. relatedWorkOrderStatus: 3
  184. }], 1)
  185. assert.equal(rows.length, 1)
  186. assert.equal(rows[0].alarmStatusText, '报警')
  187. assert.equal(rows[0].onlineStatusText, '在线')
  188. assert.equal(rows[0].alarmLevelText, '二级报警')
  189. assert.equal(rows[0].relatedOrderNo, 'WO202609150001')
  190. assert.equal(rows[0].workOrderStatusText, '处理中')
  191. })
  192. test('device summary keeps key text fields for the workbench detail panel', () => {
  193. const summary = normalizeDeviceSummary({
  194. equipmentId: 'eq-9',
  195. equipmentCode: 'CB-GS-202609020003',
  196. equipmentName: '三号漏失点',
  197. equipmentLocation: '城北',
  198. district: '城北',
  199. alarmStatus: 0,
  200. onlineStatus: 0,
  201. currentStatus: 3,
  202. equipmentTypeName: '漏失在线监测仪'
  203. })
  204. assert.equal(summary.name, '三号漏失点')
  205. assert.equal(summary.alarmStatusText, '正常')
  206. assert.equal(summary.onlineStatusText, '离线')
  207. assert.equal(summary.currentStatusText, '维修')
  208. assert.equal(summary.abnormalReasonText, '设备维修中')
  209. assert.equal(summary.typeName, '漏失在线监测仪')
  210. })
  211. test('work order rows translate status and level codes to chinese', () => {
  212. const rows = normalizeWorkOrderRows([
  213. { orderId: 5, orderNo: 'WO202609150001', orderStatus: 4, orderLevel: 0, deviceCode: 'CN-GS-202609020001', createTime: '2026-09-15 10:00:00' }
  214. ], 5)
  215. assert.equal(rows[0].orderStatusText, '待验收')
  216. assert.equal(rows[0].orderStatusType, 'primary')
  217. assert.equal(rows[0].orderLevelText, '紧急')
  218. assert.equal(rows[0].orderLevelType, 'danger')
  219. })
  220. test('home GIS overview aggregates facilities, devices and alarms with alarm priority', () => {
  221. const overview = buildHomeGisOverview({
  222. facilityPoints: [
  223. { id: 'f-1', type: 'source', name: '一水源地', code: 'S-001', lng: 110.08, lat: 28.05 }
  224. ],
  225. facilityLines: [
  226. { id: 'pipe-1', name: '城南主管', type: 'pipe', points: [{ lng: 110.1, lat: 28.1 }, { lng: 110.2, lat: 28.2 }] }
  227. ],
  228. devicePoints: [
  229. { equipmentId: 'eq-1', equipmentCode: 'CN-GS-202609020001', equipmentName: '一号压力点', longitude: 110.1, latitude: 28.1, alarmStatus: 0, onlineStatus: 1, currentStatus: 0, equipmentTypeName: '压力计' }
  230. ],
  231. alarmPoints: [
  232. { alarmId: 'alarm-1', deviceCode: 'CN-GS-202609020001', deviceName: '一号压力点', longitude: 110.1, latitude: 28.1, warningType: '压力过低', alarmLevel: 1 }
  233. ]
  234. })
  235. assert.equal(overview.lines.length, 1)
  236. assert.equal(overview.lines[0].color, '#f1c40f')
  237. assert.equal(overview.lines[0].points.length, 2)
  238. assert.ok(overview.points.some(point => point.id === 'f-1' && point.statusKey === 'normal'))
  239. const device = overview.points.find(point => point.code === 'CN-GS-202609020001')
  240. assert.ok(device)
  241. assert.equal(device.statusKey, 'alarm')
  242. assert.equal(device.meta, '压力过低')
  243. assert.ok(overview.points.every(point =>
  244. Number.isFinite(point.lng) && Number.isFinite(point.lat) && point.name && point.iconUrl))
  245. const empty = buildHomeGisOverview({})
  246. assert.deepEqual(empty, { points: [], lines: [] })
  247. })
  248. test('home quick entries cover the four workbench jumps', () => {
  249. assert.deepEqual(HOME_QUICK_ENTRIES.map(item => item.label), ['设备异常管理', '报警管理', '报警审核', '监测报警一张图'])
  250. assert.ok(HOME_QUICK_ENTRIES.every(item => item.suffix.startsWith('/')))
  251. })