t116-facility-service.mjs 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import assert from 'node:assert/strict'
  2. import { test } from 'node:test'
  3. import { __setInstanceForTest, syncFacilityMarkersForPlan, syncPointMarkersForPlan, syncAssociatedDevices, clearAssociatedDevices, getEffectsAbnormalBase, unmountMap } from '../src/modules/cockpit/services/mapService.js'
  4. import { planDeviceEffects } from '../src/modules/cockpit/domain/deviceEffects.js'
  5. import { loadCockpitConfig, resetCockpitConfigForTest } from '../src/modules/cockpit/services/cockpitConfig.js'
  6. import { markerTestDocument } from './helpers/marker-dom.mjs'
  7. test('设施数量跨过降级阈值时,所有图层的宿主读数立即同步并能恢复', (t) => {
  8. const document = markerTestDocument()
  9. __setInstanceForTest({ host: document.host })
  10. t.after(() => { __setInstanceForTest(null); unmountMap() })
  11. syncPointMarkersForPlan({ markers: [] })
  12. const rows = Array.from({ length: 31 }, (_, index) => ({
  13. key: `facility-point|WATER_PLANT|${index}`, code: 'WP001', type: 'WATER_PLANT', status: '2',
  14. }))
  15. syncFacilityMarkersForPlan(rows)
  16. assert.equal(getEffectsAbnormalBase(), 31)
  17. assert.equal(document.host.dataset.mapFacilityEffectsAnimated, '31')
  18. assert.equal(document.host.dataset.mapMarkerEffects, 'degraded')
  19. assert.equal(document.host.dataset.mapDeviceEffects, 'degraded')
  20. assert.equal(document.host.dataset.mapMarkerEffectsAbnormal, '31')
  21. syncFacilityMarkersForPlan([])
  22. assert.equal(getEffectsAbnormalBase(), 0)
  23. assert.equal(document.host.dataset.mapMarkerEffects, 'on')
  24. assert.equal(document.host.dataset.mapDeviceEffectsAnimated, '0')
  25. })
  26. test('移除关联设备后,剩余设施立即退出跨图层降级', async (t) => {
  27. resetCockpitConfigForTest()
  28. await loadCockpitConfig({ scope: { __COCKPIT_CONFIG__: { map: { effects: { enabled: true, abnormalLimit: 1 } } } }, env: {} })
  29. const document = markerTestDocument()
  30. __setInstanceForTest({
  31. host: document.host, viewer: {},
  32. engine: {
  33. syncAssociatedDevices: () => ({ rendered: 1, skipped: 0 }),
  34. applyDeviceEffects: () => true,
  35. clearAssociatedDevices() {},
  36. },
  37. })
  38. t.after(() => {
  39. __setInstanceForTest(null)
  40. clearAssociatedDevices()
  41. unmountMap()
  42. resetCockpitConfigForTest()
  43. })
  44. syncFacilityMarkersForPlan([{ key: 'facility-point|PUMP_STATION|P1', type: 'PUMP_STATION', status: '2' }])
  45. const devices = [{ code: 'D1', status: 'ALARM', assetMode: 'shape' }]
  46. syncAssociatedDevices(devices, { effects: planDeviceEffects({ devices, enabled: true, abnormalLimit: 1 }), overlapGroups: [] })
  47. assert.equal(document.host.dataset.mapDeviceEffectsAbnormal, '2')
  48. assert.equal(document.host.dataset.mapDeviceEffects, 'degraded')
  49. clearAssociatedDevices()
  50. assert.equal(document.host.dataset.mapDeviceEffectsAbnormal, '1')
  51. assert.equal(document.host.dataset.mapMarkerEffects, 'on')
  52. })
  53. test('设施门控只读自身状态,重复业务码按实体键计数,开关可关闭再开启', async (t) => {
  54. const document = markerTestDocument()
  55. __setInstanceForTest({ host: document.host })
  56. t.after(() => { __setInstanceForTest(null); unmountMap(); resetCockpitConfigForTest() })
  57. const rows = Object.freeze([
  58. Object.freeze({ key: 'facility-point|WATER_PLANT|A', code: 'WP001', type: 'WATER_PLANT', status: '1', tone: 'lv2' }),
  59. Object.freeze({ key: 'facility-point|WATER_PLANT|B', code: 'WP001', type: 'WATER_PLANT', status: '2' }),
  60. Object.freeze({ key: 'facility-point|PUMP_STATION|C', code: '', type: 'PUMP_STATION', status: '1' }),
  61. Object.freeze({ key: 'facility-point|WATER_PLANT|D', code: '', type: 'WATER_PLANT', status: 'unknown', tone: 'lv1' }),
  62. ])
  63. for (const enabled of [false, true, false]) {
  64. resetCockpitConfigForTest()
  65. await loadCockpitConfig({ scope: { __COCKPIT_CONFIG__: { map: { effects: { enabled } } } }, env: {} })
  66. syncFacilityMarkersForPlan(rows)
  67. assert.equal(document.host.dataset.mapFacilityEffectsAnimated, enabled ? '3' : '0')
  68. assert.equal(document.host.dataset.mapMarkerEffects, enabled ? 'on' : 'off')
  69. assert.equal(document.host.dataset.mapFacilityEffectsAbnormal, '1')
  70. assert.equal(getEffectsAbnormalBase(), 1)
  71. }
  72. assert.equal(rows[0].status, '1')
  73. assert.equal(rows[0].tone, 'lv2')
  74. })