waterSupplyModel.test.mjs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import assert from 'node:assert/strict'
  2. import { normalizePage, normalizeMonitoringRecord, normalizeMonitoringTypeName, normalizeGeoJson, normalizeMapPoints } from '../src/utils/waterSupplyModel.js'
  3. const page = normalizePage({ data: { records: [{ id: 1 }], total: 4, current: 2, size: 10 } })
  4. assert.equal(page.records.length, 1)
  5. assert.equal(page.total, 4)
  6. assert.equal(page.current, 2)
  7. const record = normalizeMonitoringRecord({ equipmentId: 'eq-1', deviceName: '一号泵', currentValue: '2.5' }, 'pressure')
  8. assert.equal(record.id, 'eq-1')
  9. assert.equal(record.value, 2.5)
  10. assert.equal(record.unit, 'MPa')
  11. const flowRecord = normalizeMonitoringRecord({ equipmentId: 'flow-1', instantFlow: '18.6' }, 'flow')
  12. assert.equal(flowRecord.value, 18.6)
  13. const pressureRecord = normalizeMonitoringRecord({ equipmentId: 'pressure-1', pressureValue: 0.42 }, 'pressure')
  14. assert.equal(pressureRecord.value, 0.42)
  15. const typedRecord = normalizeMonitoringRecord({ equipmentId: 'flow-2', typeName: '流量监测(测试)', instantFlow: 8.2 }, 'flow')
  16. assert.equal(typedRecord.equipmentTypeName, '流量监测(测试)')
  17. const fallbackTypeRecord = normalizeMonitoringRecord({ equipmentId: 'flow-3', equipmentTypeName: ' ', instantFlow: 1.2 }, 'flow', 'Flow Monitor')
  18. assert.equal(fallbackTypeRecord.equipmentTypeName, 'Flow Monitor')
  19. assert.equal(normalizeMonitoringTypeName({ typeName: 'Pressure Monitor' }, 'Flow Monitor'), 'Pressure Monitor')
  20. assert.equal(normalizeGeoJson({ data: { type: 'FeatureCollection', features: [] } }).type, 'FeatureCollection')
  21. assert.equal(normalizeGeoJson({ data: [] }).features.length, 0)
  22. const mapPoints = normalizeMapPoints([
  23. { id: 1, longitude: 110.1, latitude: 28.1 },
  24. { id: 2, longitude: 110.3, latitude: 28.4 }
  25. ])
  26. assert.deepEqual(mapPoints.map(item => [item.mapX, item.mapY]), [[6, 90], [94, 8]])
  27. assert.deepEqual(normalizeMapPoints([{ id: 1, longitude: 110.2, latitude: 28.2 }]).map(item => [item.mapX, item.mapY]), [[50, 50]])
  28. assert.deepEqual(normalizeMapPoints([{ id: 1, longitude: null, latitude: undefined }]), [])
  29. console.log('waterSupplyModel tests passed')