import assert from 'node:assert/strict'
|
import { readFile } from 'node:fs/promises'
|
import { test } from 'node:test'
|
import ts from 'typescript'
|
import { toTestDisplayValue } from '../src/utils/testDataUnits.ts'
|
|
async function componentFunctions(path, names, bindings) {
|
const source = (await readFile(new URL(`../src/${path}`, import.meta.url), 'utf8')).split('<script setup lang="ts">')[1].split('</script>')[0]
|
const ast = ts.createSourceFile(path, source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS)
|
const functions = ast.statements.filter(node => ts.isFunctionDeclaration(node) && names.includes(node.name?.text)).map(node => node.getText(ast)).join('\n')
|
const script = ts.transpileModule(`${functions}\nreturn { ${names.join(', ')} }`, { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText
|
return new Function(...Object.keys(bindings), 'toTestDisplayValue', script)(...Object.values(bindings), toTestDisplayValue)
|
}
|
|
async function measurementModule() {
|
const name = 'reportMeasurementChart'
|
const source = await readFile(new URL(`../src/utils/${name}.ts`, import.meta.url), 'utf8')
|
const script = ts.transpileModule(source, { compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS } }).outputText
|
const exports = {}
|
new Function('exports', 'require', script)(exports, id => {
|
assert.equal(id, './testDataUnits')
|
return { toTestDisplayValue }
|
})
|
return exports
|
}
|
const { measurementChartOption } = await measurementModule()
|
|
test('工步图电流序列和纵轴同步为 mA,保留负数与零', async () => {
|
const records = [-8.1651, 0, -8.1674].map((current, index) => ({ batteryNo: 'BAT-1', current, deviceTime: index * 60000, sourceType: 'SNAPSHOT' }))
|
const { metricValue, chartOption } = await componentFunctions('components/ConsoleChartPanel.vue', ['metricValue', 'recordTime', 'chartOption'], {
|
props: { cells: [{ no: 'BAT-1' }] }, selectedRecords: () => records, selectedCells: { value: [{ no: 'BAT-1' }] }, metricMatches: () => true, seriesColors: { value: ['#3978f6'] },
|
})
|
const option = chartOption({ key: 'current', unit: 'mA' })
|
assert.deepEqual(option.series[0].data, [-8165.1, 0, -8167.4])
|
assert.equal(option.yAxis[0].name, 'mA')
|
assert.ok(option.yAxis[0].min < -8167.4)
|
assert.ok(option.yAxis[0].max > 0)
|
assert.equal(metricValue('voltage', { voltage: 1.3349 }), 1.3349)
|
})
|
|
test('项目图表以 mAh 除以 F,缺失和无效 F 不造点,电压不缩放', async () => {
|
const capacityFactor = { value: 2 }
|
const funcs = await componentFunctions('components/ConsoleProjectChartPanel.vue', ['numericValue', 'lastStepVoltage', 'dischargeCapacityPerFactor', 'seriesValues', 'chartOption'], {
|
props: { cells: [{ no: 'BAT-1' }], cycles: [{ batteryNo: 'BAT-1', cycleNo: 1, dischargeCapacity: 8.5122 }, { batteryNo: 'BAT-1', cycleNo: 2, dischargeCapacity: 0 }], steps: [{ batteryNo: 'BAT-1', cycleNo: 1, stepNo: 1, endVoltage: 0.8999 }] },
|
capacityFactor, cycleLabels: { value: ['1', '2', '3'] }, seriesColors: { value: ['#3978f6'] },
|
})
|
const option = funcs.chartOption({ key: 'dischargeCapacityF', unit: 'mAh / F' })
|
assert.deepEqual(option.series[0].data, [4256.1, 0, null])
|
assert.equal(option.yAxis.name, 'mAh / F')
|
assert.equal(option.yAxis.axisLabel.formatter(7366.332499999995), '7366.33')
|
assert.equal(funcs.lastStepVoltage('BAT-1', 1), 0.8999)
|
capacityFactor.value = null
|
assert.equal(funcs.dischargeCapacityPerFactor('BAT-1', 1), null)
|
})
|
|
test('保存报表共用测量图按真实时间和单位绘制,显隐不改变完整数据范围', () => {
|
const items = [
|
{ name: '测试甲 · 1号', zeroTime: 1000, records: [
|
{ deviceTime: 1000, collectorDeviceTime: 9000, current: -0.125, voltage: 3.2, temperature: 0 },
|
{ deviceTime: 18000, collectorDeviceTime: 22000, current: null, voltage: null, temperature: null },
|
{ deviceTime: 181000, collectorDeviceTime: 182000, current: 0, voltage: 0, temperature: -5 },
|
] },
|
{ name: '测试乙 · 1号', zeroTime: 3000, records: [{ deviceTime: 7203000, current: 0.25 }] },
|
]
|
const before = structuredClone(items)
|
const metric = { key: 'current', unit: 'mA' }
|
const option = measurementChartOption(metric, items, ['#123456', '#abcdef'])
|
assert.deepEqual(option.series[0].data, [[0, -125], [17, null], [180, 0]])
|
assert.equal(option.series[0].connectNulls, false)
|
assert.equal(option.xAxis.max, 7200)
|
assert.equal(option.xAxis.axisLabel.formatter(1800), '0.5')
|
assert.deepEqual(option.yAxis.map(axis => [axis.name, axis.min, axis.max]), [['mA', -143.75, 250], ['mA', -143.75, 250]])
|
const hidden = measurementChartOption(metric, [{ ...items[0], hidden: true }, items[1]], ['#123456', '#abcdef'])
|
assert.equal(hidden.series.length, 1)
|
assert.equal(hidden.series[0].lineStyle.color, '#abcdef')
|
assert.equal(hidden.xAxis.max, option.xAxis.max)
|
assert.equal(hidden.yAxis[0].min, option.yAxis[0].min)
|
assert.deepEqual(items, before)
|
const voltage = measurementChartOption({ key: 'voltage', unit: 'V' }, [items[0]], ['#123456'])
|
assert.deepEqual(voltage.series[0].data, [[0, 3.2], [17, null], [180, 0]])
|
const temperature = measurementChartOption({ key: 'temperature', unit: '℃' }, [items[0]], ['#123456'])
|
assert.deepEqual(temperature.series[0].data, [[8, 0], [21, null], [181, -5]])
|
})
|