yeg
2026-09-28 6a4ca47df73d99e952d926f6103bcccea1ecea89
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import assert from 'node:assert/strict'
import { test } from 'node:test'
import load from './load-typescript.cjs'
const { formationCalculatedValue } = load(new URL('../src/utils/formationReportCalculations.ts', import.meta.url))
const { formationStepChartOption, formationStepSeries, formationHiddenSeries } = load(new URL('../src/utils/formationStepChart.ts', import.meta.url))
const { formationSavedReportHtml } = load(new URL('../src/utils/formationSavedReportPrint.ts', import.meta.url), {
  '@/api/formationReports': {getFormationPrintVersion:()=>{throw new Error('打印内容不应重新请求来源')}}
})
const source = { selection: { testId: 1, testBatteryId: 11 }, name: '化成甲', batteryNo: '电池1', batteryGroup: '组1' }
const item = { source, cycleNos: [1], metrics: {}, businessFields: Object.entries({H:'2',Z:'3',A:'4',F:'5',B:'6'}).map(([fieldName,fieldValue])=>({fieldName,fieldValue})),
  chargeSteps: {1:[{stepNo:1,direction:'CHARGE',endVoltage:4.1},{stepNo:2,direction:'UNKNOWN'}, {stepNo:3,direction:'CHARGE',endVoltage:4.2},{stepNo:4,direction:'DISCHARGE',capacity:0.25}]},
  curves: {1:{zeroTime:1000,records:[
    {sourceType:'SNAPSHOT',stepNo:1,deviceTime:1000,voltage:3,current:0.1,temperature:25},
    {sourceType:'SNAPSHOT',stepNo:1,deviceTime:2000,voltage:4,current:0.1,temperature:26},
    {sourceType:'SNAPSHOT',stepNo:2,deviceTime:3000,voltage:99,current:99,temperature:99},
    {sourceType:'SNAPSHOT',stepNo:3,deviceTime:5000,voltage:4.1,current:0,temperature:27},
    {sourceType:'SNAPSHOT',stepNo:4,deviceTime:6000,voltage:1,current:-0.1,temperature:28},
  ]}} }
const settings = {fields:[], values:{'1|11|1|manual':'2'},chargeStepSelections:{}}
test('报表字段修改驱动公式,空值不按零且末工步容量不回退',()=>{
  assert.equal(formationCalculatedValue(item,1,'chargePerGram',settings).value,240)
  assert.equal(formationCalculatedValue(item,1,'capacity1vf',settings).value,50)
  const changed = {...settings,values:{...settings.values,'1|11|0|F':'10'}}
  assert.equal(formationCalculatedValue(item,0,'C',changed).value,1440)
  assert.equal(formationCalculatedValue(item,1,'chargePerGram',changed).value,480)
  assert.equal(formationCalculatedValue(item,1,'capacity1vf',changed).value,25)
  assert.equal(formationCalculatedValue(item,1,'chargePerGram',{...settings,values:{}}).value,null)
  assert.equal(formationCalculatedValue(item,1,'capacity1vf',{...settings,values:{'1|11|0|F':'0'}}).value,null)
  const missing = {...item,chargeSteps:{1:[...item.chargeSteps[1],{stepNo:5,direction:'DISCHARGE',capacity:null}]}}
  assert.equal(formationCalculatedValue(missing,1,'capacity1v',settings).value,null)
})
test('同方向按电池单线保留真实间隔,静置断线且放电独立归零',()=>{
  assert.equal(formationStepSeries([item],1,'charge',settings).length,1)
  const charge = formationStepChartOption({key:'voltage',unit:'V'},[item],1,'charge',settings)
  assert.deepEqual(charge.series[0].data.filter(point=>point[1]!==null),[[0,3],[1,4],[4,4.1]])
  assert.ok(charge.series[0].data.some(point=>point[1]===null && point[0]>=1 && point[0]<=4))
  assert.deepEqual(formationStepChartOption({key:'current',unit:'mA'},[item],1,'discharge',settings).series[0].data,[[0,-100]])
  assert.deepEqual(formationStepChartOption({key:'temperature',unit:'℃'},[item],1,'charge',settings).series[0].data.filter(point=>point[1]!==null),[[0,25],[1,26],[4,27]])
})
test('当前循环的图例操作不清除其他循环独有来源的隐藏设置',()=>{
  assert.deepEqual(formationHiddenSeries(['2|22'],{'1|11':false}),['2|22','1|11'])
  assert.deepEqual(formationHiddenSeries(['2|22','1|11'],{'1|11':true}),['2|22'])
})
test('保存版打印完整输出跨组电池和循环,使用冻结结果且转义人工文本',()=>{
  const items=Array.from({length:5},(_,index)=>({...item,source:{...source,selection:{testId:index+1,testBatteryId:11},name:`测试${index+1}`}}))
  const settings={fields:[{key:'F',scope:'detail',kind:'battery',displayName:'F',unit:''},{key:'C',scope:'detail',kind:'calculated',displayName:'C',unit:''},{key:'method',scope:'cycle',kind:'manual',displayName:'测试方法',unit:''},{key:'capacity1v',scope:'cycle',kind:'calculated',displayName:'1.0V容量',unit:'mAh'}],values:{'1|11|0|F':'<自定义>','1|11|1|method':'<script>不能执行</script>'},metrics:['voltage'],chargeStepSelections:{}}
  const html=formationSavedReportHtml({id:1,name:'已保存化成',items,cycleNos:[1],settings,calculatedValues:{'1|11|0|C':777,'1|11|1|capacity1v':222},versionNo:2,createdAt:0,versionCreatedAt:0,creator:'甲',operatorName:'甲',changeSummary:'测试',description:''})
  assert.ok(html.includes('测试5'))
  assert.ok(html.includes('777'))
  assert.ok(html.includes('222'))
  assert.ok(html.includes('&lt;自定义&gt;'))
  assert.ok(html.includes('&lt;script&gt;不能执行&lt;/script&gt;'))
  assert.ok(!html.includes('<script>不能执行'))
  assert.ok(html.includes('data-group="charge"')&&html.includes('data-group="discharge"'))
  assert.equal((html.match(/<td>终止电压<\/td>/g)??[]).length,4)
})