import test from 'node:test' import assert from 'node:assert/strict' import { readFile } from 'node:fs/promises' import vm from 'node:vm' import { parse } from '@vue/compiler-sfc' import { computed, effectScope, nextTick, ref, watch } from 'vue' import * as operations from '../src/views/subSystem/alarm/common/js/alarmOperationsModel.js' import * as review from '../src/views/subSystem/alarm/common/js/alarmReviewModel.js' import { HOME_QUICK_ENTRIES, WATER_DEVICE_ROOT_TYPE_ID, buildHomeGisOverview } from '../src/utils/waterHomeModel.js' const { descriptor } = parse(await readFile(new URL('../src/views/subSystem/waterSupply/alarm/alarmGIS.vue', import.meta.url), 'utf8')) const script = descriptor.scriptSetup.content.replace(/^import .+$/gm, '') .replace('import.meta.env.VITE_APP_BASE_API', "''") const alarms = [ { id: 'w1', equipmentId: 'eq1', deviceCode: 'W-1', deviceName: '供水压力设备', alarmLevel: 1, alarmStatus: 0, actualValue: 0, longitude: 110.4, latitude: 28.4, createTime: '2026-09-24 10:00:00' }, { id: 'w2', equipmentId: 'eq1', deviceCode: 'W-1', alarmLevel: 2, alarmStatus: 0, actualValue: null, longitude: 110.4, latitude: 28.4, createTime: '2026-09-24 09:00:00' }, { id: 'w3', equipmentId: 'eq2', deviceCode: 'W-2', alarmLevel: 3, alarmStatus: 0, actualValue: null } ] async function flush() { for (let i = 0; i < 6; i += 1) await Promise.resolve() await nextTick() } function mountMap(t, rootTypeId = 1) { const calls = { map: [], options: [], history: [] }, scope = ref({ rootTypeId }) let unmount const context = { ref, computed, watch, nextTick, ...operations, ...review, WATER_DEVICE_ROOT_TYPE_ID, onBeforeUnmount: callback => { unmount = callback }, useAlarmScope: () => ({ scope, scopeKey: computed(() => JSON.stringify(scope.value)) }), getAlarmMap: async params => { calls.map.push(params); return { data: alarms } }, getAlarmOptions: async params => { calls.options.push(params) return { data: { alarmLevels: [1, 2, 3, 4].map(value => ({ value, label: `${value}级报警` })) } } }, getAlarms: async params => { calls.history.push(params) return { data: { records: alarms.filter(row => row.deviceCode === params.deviceCode), total: 2 } } } } const effects = effectScope() effects.run(() => vm.runInNewContext(`${script} globalThis.page = { error, devices, points, missingCoordinates, levelMetrics, selected, detailVisible, curveChoices, currentCurve, historyPage, historySize, choose, loadHistory } `, context)) t.after(() => { unmount(); effects.stop() }) return { calls, page: context.page, scope } } test('water GIS requests current water alarms and groups them by equipment', async t => { const { page, calls } = mountMap(t) await flush() assert.equal(calls.map.length, 1) assert.equal(calls.map[0].rootTypeId, 1) assert.equal(calls.map[0].alarmStatus, 0) assert.equal(calls.options[0].rootTypeId, 1) assert.equal(page.devices.value.length, 2) assert.equal(page.points.value.length, 1) assert.equal(page.missingCoordinates.value, 1) assert.equal(page.levelMetrics.value[0].count, 1) assert.equal(page.levelMetrics.value[1].count, 0) }) test('water GIS rejects a drainage scope before fetching any alarm data', async t => { const { page, calls } = mountMap(t, 2) await flush() assert.equal(calls.map.length, 0) assert.equal(calls.options.length, 0) assert.match(page.error.value, /供水子系统/) }) test('device history carries the water scope and curve choices retain zero but exclude null', async t => { const { page, calls } = mountMap(t) await flush() page.choose(page.devices.value[0]) await flush() assert.equal(page.detailVisible.value, true) assert.equal(calls.history[0].rootTypeId, 1) assert.equal(calls.history[0].deviceCode, 'W-1') assert.equal(page.curveChoices.value.length, 1) assert.equal(page.currentCurve.value.id, 'w1') page.historyPage.value = 2 await page.loadHistory() assert.equal(calls.history[1].pageNum, 2) assert.equal(calls.history[1].pageSize, 10) }) test('home map shortcut includes the required water scope', () => { const entry = HOME_QUICK_ENTRIES.find(item => item.key === 'map') assert.equal(entry.suffix, '/waterSupplyAlarmGIS') assert.deepEqual(entry.query, { rootTypeId: 1 }) }) test('home overview accepts unified alarm records without duplicating device points', () => { const overview = buildHomeGisOverview({ alarmPoints: alarms }) assert.equal(overview.points.length, 1) assert.equal(overview.points[0].id, 'w1') assert.equal(overview.points[0].statusKey, 'alarm') })