waterSupplyAlarmGIS.test.mjs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import test from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import { readFile } from 'node:fs/promises'
  4. import vm from 'node:vm'
  5. import { parse } from '@vue/compiler-sfc'
  6. import { computed, effectScope, nextTick, ref, watch } from 'vue'
  7. import * as operations from '../src/views/subSystem/alarm/common/utils/alarmOperationsModel.js'
  8. import * as review from '../src/views/subSystem/alarm/common/utils/alarmReviewModel.js'
  9. import { HOME_QUICK_ENTRIES, WATER_DEVICE_ROOT_TYPE_ID, buildHomeGisOverview } from '../src/utils/waterHomeModel.js'
  10. const { descriptor } = parse(await readFile(new URL('../src/views/subSystem/waterSupply/alarm/alarmGIS.vue', import.meta.url), 'utf8'))
  11. const script = descriptor.scriptSetup.content.replace(/^import .+$/gm, '')
  12. .replace('import.meta.env.VITE_APP_BASE_API', "''")
  13. const alarms = [
  14. { id: 'w1', equipmentId: 'eq1', deviceCode: 'W-1', deviceName: '供水压力设备', alarmLevel: 1,
  15. alarmStatus: 0, actualValue: 0, longitude: 110.4, latitude: 28.4, createTime: '2026-09-24 10:00:00' },
  16. { id: 'w2', equipmentId: 'eq1', deviceCode: 'W-1', alarmLevel: 2, alarmStatus: 0, actualValue: null,
  17. longitude: 110.4, latitude: 28.4, createTime: '2026-09-24 09:00:00' },
  18. { id: 'w3', equipmentId: 'eq2', deviceCode: 'W-2', alarmLevel: 3, alarmStatus: 0, actualValue: null }
  19. ]
  20. async function flush() {
  21. for (let i = 0; i < 6; i += 1) await Promise.resolve()
  22. await nextTick()
  23. }
  24. function mountMap(t, rootTypeId = 1) {
  25. const calls = { map: [], options: [], history: [] }, scope = ref({ rootTypeId })
  26. let unmount
  27. const context = {
  28. ref, computed, watch, nextTick, ...operations, ...review, WATER_DEVICE_ROOT_TYPE_ID,
  29. onBeforeUnmount: callback => { unmount = callback },
  30. useAlarmScope: () => ({ scope, scopeKey: computed(() => JSON.stringify(scope.value)) }),
  31. getAlarmMap: async params => { calls.map.push(params); return { data: alarms } },
  32. getAlarmOptions: async params => {
  33. calls.options.push(params)
  34. return { data: { alarmLevels: [1, 2, 3, 4].map(value => ({ value, label: `${value}级报警` })) } }
  35. },
  36. getAlarms: async params => {
  37. calls.history.push(params)
  38. return { data: { records: alarms.filter(row => row.deviceCode === params.deviceCode), total: 2 } }
  39. }
  40. }
  41. const effects = effectScope()
  42. effects.run(() => vm.runInNewContext(`${script}
  43. globalThis.page = { error, devices, points, missingCoordinates, levelMetrics,
  44. selected, detailVisible, curveChoices, currentCurve, historyPage, historySize,
  45. choose, loadHistory }
  46. `, context))
  47. t.after(() => { unmount(); effects.stop() })
  48. return { calls, page: context.page, scope }
  49. }
  50. test('water GIS requests current water alarms and groups them by equipment', async t => {
  51. const { page, calls } = mountMap(t)
  52. await flush()
  53. assert.equal(calls.map.length, 1)
  54. assert.equal(calls.map[0].rootTypeId, 1)
  55. assert.equal(calls.map[0].alarmStatus, 0)
  56. assert.equal(calls.options[0].rootTypeId, 1)
  57. assert.equal(page.devices.value.length, 2)
  58. assert.equal(page.points.value.length, 1)
  59. assert.equal(page.missingCoordinates.value, 1)
  60. assert.equal(page.levelMetrics.value[0].count, 1)
  61. assert.equal(page.levelMetrics.value[1].count, 0)
  62. })
  63. test('water GIS rejects a drainage scope before fetching any alarm data', async t => {
  64. const { page, calls } = mountMap(t, 2)
  65. await flush()
  66. assert.equal(calls.map.length, 0)
  67. assert.equal(calls.options.length, 0)
  68. assert.match(page.error.value, /供水子系统/)
  69. })
  70. test('device history carries the water scope and curve choices retain zero but exclude null', async t => {
  71. const { page, calls } = mountMap(t)
  72. await flush()
  73. page.choose(page.devices.value[0])
  74. await flush()
  75. assert.equal(page.detailVisible.value, true)
  76. assert.equal(calls.history[0].rootTypeId, 1)
  77. assert.equal(calls.history[0].deviceCode, 'W-1')
  78. assert.equal(page.curveChoices.value.length, 1)
  79. assert.equal(page.currentCurve.value.id, 'w1')
  80. page.historyPage.value = 2
  81. await page.loadHistory()
  82. assert.equal(calls.history[1].pageNum, 2)
  83. assert.equal(calls.history[1].pageSize, 10)
  84. })
  85. test('home map shortcut includes the required water scope', () => {
  86. const entry = HOME_QUICK_ENTRIES.find(item => item.key === 'map')
  87. assert.equal(entry.suffix, '/waterSupplyAlarmGIS')
  88. assert.deepEqual(entry.query, { rootTypeId: 1 })
  89. })
  90. test('home overview accepts unified alarm records without duplicating device points', () => {
  91. const overview = buildHomeGisOverview({ alarmPoints: alarms })
  92. assert.equal(overview.points.length, 1)
  93. assert.equal(overview.points[0].id, 'w1')
  94. assert.equal(overview.points[0].statusKey, 'alarm')
  95. })