import test from 'node:test' import assert from 'node:assert/strict' import { readFile } from 'node:fs/promises' import vm from 'node:vm' import { computed, effectScope, nextTick, reactive, ref, watch } from 'vue' import { parse } from '@vue/compiler-sfc' import { rangeParams } from '../src/views/subSystem/alarm/common/js/alarmOperationsModel.js' const { descriptor } = parse(await readFile(new URL('../src/views/subSystem/alarm/common/list.vue', import.meta.url), 'utf8')) const script = descriptor.scriptSetup.content.replace(/^import .+$/gm, '') async function flush() { for (let i = 0; i < 8; i++) await Promise.resolve() await nextTick() } function mount(t) { const requests = { list: [], statistics: [] }, scope = ref({ rootTypeId: 2 }), lifecycle = {} const request = key => params => new Promise((resolve, reject) => requests[key].push({ params, resolve, reject })) const context = { ref, reactive, watch, rangeParams, Document: {}, Warning: {}, CircleCheck: {}, useAlarmScope: () => ({ scope, scopeKey: computed(() => JSON.stringify(scope.value)), route: { query: {} } }), getAlarms: request('list'), getAlarmStatistics: request('statistics'), getAlarmOptions: async () => ({ data: {} }), onBeforeUnmount: callback => { lifecycle.unmount = callback } } const effects = effectScope() effects.run(() => vm.runInNewContext(`${script} globalThis.page = { statistics, statisticsLoading, statisticsError, rows, total, loading, error, filters, dates, pageNum, pageSize, search, reset, load } `, context)) t.after(() => { lifecycle.unmount(); effects.stop() }) return { page: context.page, requests, scope } } const stats = (total, abnormal, recovered) => ({ data: [{ level: 1, total, abnormal, recovered }] }) const snapshot = value => JSON.parse(JSON.stringify(value)) test('statistics use the full filtered result and do not delay table rendering', async t => { const { page, requests } = mount(t) await flush() requests.list[0].resolve({ data: { records: [{ id: 'one' }], total: 120 } }) await flush() assert.equal(page.rows.value.length, 1) assert.equal(page.loading.value, false) assert.equal(page.statisticsLoading.value, true) requests.statistics[0].resolve({ data: [ { level: 1, total: '30', abnormal: '10', recovered: '20' }, { level: null, total: '90', abnormal: '40', recovered: '48' } ] }) await flush() assert.deepEqual(snapshot(page.statistics.value), { total: 120, abnormal: 50, recovered: 68 }) Object.assign(page.filters, { alarmNo: 'BJ', keyword: '设备', alarmLevel: 2, alarmStatus: 1 }) page.dates.value = ['2026-09-01', '2026-09-24'] page.pageNum.value = 3 page.load() const params = requests.statistics[1].params assert.equal(params.rootTypeId, 2) assert.equal(params.keyword, '设备') assert.equal(params.alarmNo, 'BJ') assert.equal(params.alarmLevel, 2) assert.equal(params.alarmStatus, 1) assert.equal(params.startTime, '2026-09-01 00:00:00') assert.equal(params.endTime, '2026-09-24 23:59:59.999999') assert.equal(params.pageNum, undefined) assert.equal(params.pageSize, undefined) assert.equal(requests.list[1].params.pageNum, 3) }) test('late statistics from an old search or subsystem cannot overwrite current cards', async t => { const { page, requests, scope } = mount(t) await flush() page.filters.keyword = '新查询' page.search() requests.statistics[1].resolve(stats(4, 3, 1)) await flush() requests.statistics[0].resolve(stats(120, 50, 70)) await flush() assert.equal(page.statistics.value.total, 4) page.search() scope.value = { rootTypeId: 1 } await flush() assert.equal(page.statistics.value, null) assert.equal(requests.statistics[3].params.rootTypeId, 1) requests.statistics[3].resolve(stats(2, 1, 1)) requests.statistics[2].resolve(stats(10, 8, 2)) await flush() assert.equal(page.statistics.value.total, 2) }) test('statistics failure remains distinct from zero and leaves the list usable', async t => { const { page, requests } = mount(t) await flush() requests.statistics[0].reject(new Error('统计请求失败')) requests.list[0].resolve({ data: { records: [{ id: 'one' }], total: 1 } }) await flush() assert.equal(page.statistics.value, null) assert.equal(page.statisticsError.value, '统计请求失败') assert.equal(page.statisticsLoading.value, false) assert.equal(page.rows.value.length, 1) assert.equal(page.error.value, '') page.reset() requests.statistics[1].resolve({ data: [] }) await flush() assert.deepEqual(snapshot(page.statistics.value), { total: 0, abnormal: 0, recovered: 0 }) assert.equal(page.statisticsError.value, '') })