import assert from 'node:assert/strict' import { test } from 'node:test' import { __setInstanceForTest, syncFacilityMarkersForPlan, syncPointMarkersForPlan, syncAssociatedDevices, clearAssociatedDevices, getEffectsAbnormalBase, unmountMap } from '../src/modules/cockpit/services/mapService.js' import { planDeviceEffects } from '../src/modules/cockpit/domain/deviceEffects.js' import { loadCockpitConfig, resetCockpitConfigForTest } from '../src/modules/cockpit/services/cockpitConfig.js' import { markerTestDocument } from './helpers/marker-dom.mjs' test('设施数量跨过降级阈值时,所有图层的宿主读数立即同步并能恢复', (t) => { const document = markerTestDocument() __setInstanceForTest({ host: document.host }) t.after(() => { __setInstanceForTest(null); unmountMap() }) syncPointMarkersForPlan({ markers: [] }) const rows = Array.from({ length: 31 }, (_, index) => ({ key: `facility-point|WATER_PLANT|${index}`, code: 'WP001', type: 'WATER_PLANT', status: '2', })) syncFacilityMarkersForPlan(rows) assert.equal(getEffectsAbnormalBase(), 31) assert.equal(document.host.dataset.mapFacilityEffectsAnimated, '31') assert.equal(document.host.dataset.mapMarkerEffects, 'degraded') assert.equal(document.host.dataset.mapDeviceEffects, 'degraded') assert.equal(document.host.dataset.mapMarkerEffectsAbnormal, '31') syncFacilityMarkersForPlan([]) assert.equal(getEffectsAbnormalBase(), 0) assert.equal(document.host.dataset.mapMarkerEffects, 'on') assert.equal(document.host.dataset.mapDeviceEffectsAnimated, '0') }) test('移除关联设备后,剩余设施立即退出跨图层降级', async (t) => { resetCockpitConfigForTest() await loadCockpitConfig({ scope: { __COCKPIT_CONFIG__: { map: { effects: { enabled: true, abnormalLimit: 1 } } } }, env: {} }) const document = markerTestDocument() __setInstanceForTest({ host: document.host, viewer: {}, engine: { syncAssociatedDevices: () => ({ rendered: 1, skipped: 0 }), applyDeviceEffects: () => true, clearAssociatedDevices() {}, }, }) t.after(() => { __setInstanceForTest(null) clearAssociatedDevices() unmountMap() resetCockpitConfigForTest() }) syncFacilityMarkersForPlan([{ key: 'facility-point|PUMP_STATION|P1', type: 'PUMP_STATION', status: '2' }]) const devices = [{ code: 'D1', status: 'ALARM', assetMode: 'shape' }] syncAssociatedDevices(devices, { effects: planDeviceEffects({ devices, enabled: true, abnormalLimit: 1 }), overlapGroups: [] }) assert.equal(document.host.dataset.mapDeviceEffectsAbnormal, '2') assert.equal(document.host.dataset.mapDeviceEffects, 'degraded') clearAssociatedDevices() assert.equal(document.host.dataset.mapDeviceEffectsAbnormal, '1') assert.equal(document.host.dataset.mapMarkerEffects, 'on') }) test('设施门控只读自身状态,重复业务码按实体键计数,开关可关闭再开启', async (t) => { const document = markerTestDocument() __setInstanceForTest({ host: document.host }) t.after(() => { __setInstanceForTest(null); unmountMap(); resetCockpitConfigForTest() }) const rows = Object.freeze([ Object.freeze({ key: 'facility-point|WATER_PLANT|A', code: 'WP001', type: 'WATER_PLANT', status: '1', tone: 'lv2' }), Object.freeze({ key: 'facility-point|WATER_PLANT|B', code: 'WP001', type: 'WATER_PLANT', status: '2' }), Object.freeze({ key: 'facility-point|PUMP_STATION|C', code: '', type: 'PUMP_STATION', status: '1' }), Object.freeze({ key: 'facility-point|WATER_PLANT|D', code: '', type: 'WATER_PLANT', status: 'unknown', tone: 'lv1' }), ]) for (const enabled of [false, true, false]) { resetCockpitConfigForTest() await loadCockpitConfig({ scope: { __COCKPIT_CONFIG__: { map: { effects: { enabled } } } }, env: {} }) syncFacilityMarkersForPlan(rows) assert.equal(document.host.dataset.mapFacilityEffectsAnimated, enabled ? '3' : '0') assert.equal(document.host.dataset.mapMarkerEffects, enabled ? 'on' : 'off') assert.equal(document.host.dataset.mapFacilityEffectsAbnormal, '1') assert.equal(getEffectsAbnormalBase(), 1) } assert.equal(rows[0].status, '1') assert.equal(rows[0].tone, 'lv2') })