import assert from 'node:assert/strict'
|
import { readFile } from 'node:fs/promises'
|
import { test } from 'node:test'
|
import ts from 'typescript'
|
import { formatTestDisplayValue, toTestApiValue } from '../src/utils/testDataUnits.ts'
|
import { consoleImportColumns } from '../src/utils/consoleDataImport.ts'
|
import { parseConsoleImport } from '../src/utils/consoleDataImport.ts'
|
import XLSX from 'xlsx'
|
|
const source = await readFile(new URL('../src/components/ConsoleDataListPanel.vue', import.meta.url), 'utf8')
|
// 执行页面实际行映射,检查甲方文件数值经过导入后的展示边界。
|
const mappings = source.slice(source.indexOf('function emptyRow('), source.indexOf('async function loadRecords('))
|
const script = ts.transpileModule(`${mappings}\nreturn { mapRecord, mapStep, mapCycle }`, {
|
compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.None },
|
}).outputText
|
const { mapRecord, mapStep, mapCycle } = new Function('formatTime', 'formatTestDisplayValue', script)(value => String(value ?? '-'), formatTestDisplayValue)
|
|
test('实时记录按甲方 mA/mAh/mWh 显示,电压、电位和温度不缩放', () => {
|
const row = mapRecord({ sourceType: 'SNAPSHOT', current: -8.1651, capacity: 0.1361, energy: 0.1694, voltage: 1.3349, temperature: 25.1, positivePotential: 1.501, negativePotential: -0.101 })
|
assert.equal(Number(row.current), -8165.1)
|
assert.equal(Number(row.capacity), 136.1)
|
assert.equal(Number(row.energy), 169.4)
|
assert.equal(Number(row.voltage), 1.3349)
|
assert.equal(Number(row.temperature), 25.1)
|
assert.equal(Number(row.negativePotential), -0.101)
|
})
|
|
test('工步与循环显示甲方毫单位,保留真实零和空值', () => {
|
const step = mapStep({ capacity: 8.5122, energy: 9.7178, startVoltage: 1.3349, endVoltage: 0.8999 })
|
assert.equal(Number(step.capacity), 8512.2)
|
assert.equal(Number(step.energy), 9717.8)
|
assert.equal(Number(step.endVoltage), 0.8999)
|
const cycle = mapCycle({ chargeCapacity: 0, dischargeCapacity: 8.5122, chargeEnergy: null, dischargeEnergy: 9.7178, efficiency: 100 })
|
assert.equal(Number(cycle.dischargeCapacity), 8512.2)
|
assert.equal(Number(cycle.dischargeEnergy), 9717.8)
|
assert.equal(Number(cycle.chargeCapacity), 0)
|
assert.equal(cycle.chargeEnergy, '-')
|
assert.equal(Number(cycle.efficiency), 100)
|
})
|
|
test('标准模板与三子页统一使用 mA/mAh/mWh 列头', () => {
|
assert.equal(consoleImportColumns.REALTIME.current, '电流(mA)')
|
assert.equal(consoleImportColumns.STEP.capacity, '容量(mAh)')
|
assert.equal(consoleImportColumns.CYCLE.dischargeEnergy, '放电能量(mWh)')
|
})
|
|
test('新旧标准模板按文件列头换算,展示、编辑再提交不重复缩放', () => {
|
for (const legacy of [false, true]) {
|
for (const type of ['REALTIME', 'STEP', 'CYCLE']) {
|
const fields = consoleImportColumns[type]
|
const values = { recordNo: '1', batteryNo: 'BAT-1', recordedAt: '2023-05-08 11:42:11', cycleNo: 1, stepNo: 1, stepName: '恒流放电', sequenceId: 1, current: legacy ? -8.1651 : -8165.1, capacity: legacy ? 8.5122 : 8512.2, energy: legacy ? 9.7178 : 9717.8, dischargeCapacity: legacy ? 8.5122 : 8512.2, dischargeEnergy: legacy ? 9.7178 : 9717.8 }
|
const book = XLSX.utils.book_new()
|
const headers = Object.values(fields).map(header => legacy ? header.replace(/\(m(A|Ah|Wh)\)/, '($1)') : header)
|
XLSX.utils.book_append_sheet(book, XLSX.utils.aoa_to_sheet([headers, Object.keys(fields).map(field => values[field] ?? '')]), '单页')
|
const row = parseConsoleImport(book, type, ['BAT-1'])[0]
|
assert.deepEqual(row.errors, [])
|
const field = type === 'CYCLE' ? 'dischargeCapacity' : 'capacity'
|
assert.equal(row.data[field], 8.5122)
|
assert.equal(formatTestDisplayValue(field, row.data[field]), '8512.2000')
|
assert.equal(toTestApiValue(field, '8512.2000'), '8.5122')
|
if (type === 'REALTIME') assert.equal(toTestApiValue('current', formatTestDisplayValue('current', row.data.current)), '-8.1651')
|
}
|
}
|
})
|
|
test('编辑边界保留负数、零、清空、非法输入及非毫单位字段', () => {
|
for (const field of ['current', 'capacity', 'energy', 'chargeCapacity', 'dischargeCapacity', 'chargeEnergy', 'dischargeEnergy']) assert.equal(toTestApiValue(field, '9717.8'), '9.7178')
|
assert.equal(toTestApiValue('current', '-8167.4'), '-8.1674')
|
assert.equal(toTestApiValue('capacity', '0'), '0')
|
assert.equal(toTestApiValue('energy', '-'), '-')
|
assert.equal(toTestApiValue('energy', ' '), ' ')
|
assert.equal(toTestApiValue('energy', 'abc'), 'abc')
|
assert.equal(toTestApiValue('voltage', '1.3349'), '1.3349')
|
assert.equal(formatTestDisplayValue('current', 0.00001234), '0.0123')
|
})
|