Files
quality_analyse/web/verification/configuration_workbook.test.mjs

96 lines
10 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { configurationPreviewTable, configurationTables, importConfigurationTables, createConfigurationWorkbook, readConfigurationWorkbook } from '../src/configurationWorkbook.ts';
const rule={onlyUnclassified:true,id:'rule_001',name:'指定分类',priority:-9900,stage:'all',enabled:true,target:'group',value:'G',conditions:[{field:'PDT代码',operator:'EQUALS',value:'00001'},{field:'销售片区',operator:'IS_BLANK',value:''}]};
const base={schemaVersion:3,unknown:{retain:[1,null]},masterData:{departments:[{code:'0001',name:'A',enabled:true}],groups:[{code:'002',name:'G',enabled:true,department:'A',unknown:'keep'}],products:[],projectGroups:[]},
categoryLookup:{'00001|欧洲':'G','00002|空':null},projectGroupLookup:{'00003|G':'P'},departmentLookup:{raw:'A'},productLookup:{RAW:'P'},disabledMappings:{categoryLookup:['00002|空']},matchRules:[rule],organizationHierarchy:[{code:'A',parentCode:'',name:'保留名称'}],effectiveRanges:[{mappingType:'CATEGORY',sourceKey:'00001|欧洲',fromMonth:'2026-01',toMonth:'2026-12',mappedValue:null}]};
test('all configuration sheets round-trip through a real xlsx with codes, nulls, conditions and omitted keys preserved',async()=>{
const source=structuredClone(base);const workbook=await createConfigurationWorkbook(source,'all');
const tables=await readConfigurationWorkbook(await workbook.xlsx.writeBuffer());
for(const mode of ['MERGE','REPLACE'])assert.deepEqual(importConfigurationTables(source,tables,'all',mode).definition,source);
assert.equal(tables.find(t=>t.name==='产业部名录').rows[0][0],'0001');
assert.equal(workbook.getWorksheet('产业部名录').getColumn(1).hidden,true);
assert.deepEqual(configurationPreviewTable(tables.find(t=>t.name==='产业部名录')).headers,['名称','状态']);
assert.deepEqual(source,base);assert.equal(workbook.getWorksheet('产业部名录').getCell('A2').numFmt,'@');
});
test('all four catalogs accept name-only files and preserve existing identity, metadata and ownership',()=>{
for(const kind of ['departments','groups','products','projectGroups']){
const source={masterData:{[kind]:[{code:'existing',name:'原名称',enabled:false,department:'所属产业部',extra:'keep'}]}};
const table=configurationTables(source,kind)[0];
table.headers.shift();table.rows=kind==='groups'?[['原名称','启用','所属产业部'],['新增名称','启用','']]:[['原名称','启用'],['新增名称','启用']];
const result=importConfigurationTables(source,[table],kind,'MERGE');const rows=result.definition.masterData[kind];
assert.equal(rows[0].code,'existing');assert.equal(rows[0].extra,'keep');assert.equal(rows[0].department,'所属产业部');assert.equal(rows[0].enabled,true);
assert.match(rows[1].code,/^REF-/);assert.notEqual(rows[0].code,rows[1].code);
assert.equal(result.changes[0].added,1);assert.equal(result.changes[0].updated,1);
assert.deepEqual(importConfigurationTables(result.definition,[table],kind,'MERGE').definition,result.definition);
}
});
test('hidden identity lets an exported row be renamed, and new rows need only a name',async()=>{
const book=await createConfigurationWorkbook(base,'groups');const sheet=book.getWorksheet('业务分组名录');
sheet.getCell('B2').value='改名分组';sheet.addRow(['','新分组','启用','A']);
const tables=await readConfigurationWorkbook(await book.xlsx.writeBuffer());
const result=importConfigurationTables(base,tables,'groups','MERGE').definition.masterData.groups;
assert.equal(result.length,2);assert.equal(result[0].code,'002');assert.equal(result[0].name,'改名分组');assert.equal(result[0].unknown,'keep');
assert.match(result[1].code,/^REF-/);assert.equal(result[1].department,'A');
assert.deepEqual(configurationPreviewTable(tables[0]).rows.map(row=>row[0]),['改名分组','新分组']);
});
test('catalogs accept only the required name column without losing state or group ownership',()=>{
const source={masterData:{groups:[{code:'kept',name:'G',enabled:false,department:'A',extra:'keep'}]}};
const table={name:'业务分组名录',headers:['名称'],rows:[['G'],['新组']]};
const result=importConfigurationTables(source,[table],'groups','MERGE').definition;
assert.deepEqual(result.masterData.groups[0],source.masterData.groups[0]);assert.match(result.masterData.groups[1].code,/^REF-/);
assert.equal(result.masterData.groups[1].enabled,true);
const reordered={name:'业务分组名录',headers:['名称','编码'],rows:[['G','kept']]};
assert.deepEqual(configurationPreviewTable(reordered),{name:'业务分组名录',headers:['名称'],rows:[['G']]});
assert.deepEqual(importConfigurationTables(source,[reordered],'groups','MERGE').definition,source);
});
test('edits upsert by source key, and replace removes only records in supplied tables',()=>{
const tables=configurationTables(base,'categoryLookup');tables[0].rows[0][2]='新分组';
const imported=importConfigurationTables(base,tables,'categoryLookup','MERGE');
assert.equal(imported.definition.categoryLookup['00001|欧洲'],'新分组');assert.equal(imported.definition.categoryLookup['00002|空'],null);
assert.equal(imported.changes[0].updated,1);assert.equal(base.categoryLookup['00001|欧洲'],'G');
tables[0].rows=tables[0].rows.slice(0,1);const replaced=importConfigurationTables(base,tables,'all','REPLACE');
assert.equal(Object.keys(replaced.definition.categoryLookup).length,1);assert.deepEqual(replaced.definition.projectGroupLookup,base.projectGroupLookup);assert.equal(replaced.changes[0].removed,1);
});
test('multiple conditions are grouped by rule id; changing raw and computed fields cannot conflate them',async()=>{
const source={matchRules:[{...rule,conditions:[{field:'产业部',operator:'EQUALS',value:'原始A'},{field:'@department',operator:'EQUALS',value:'计算A'}]}]};
const book=await createConfigurationWorkbook(source,'matchRules');book.getWorksheet('条件匹配').getCell('F2').value='新分组';book.getWorksheet('条件匹配').getCell('F3').value='新分组';
const imported=importConfigurationTables(source,await readConfigurationWorkbook(await book.xlsx.writeBuffer()),'matchRules','MERGE').definition;
assert.deepEqual(imported.matchRules[0].conditions,source.matchRules[0].conditions);assert.equal(imported.matchRules[0].value,'新分组');
const rows=configurationTables(base,'matchRules');rows[0].rows[1][5]='不同分组';assert.throws(()=>importConfigurationTables(base,rows,'matchRules','MERGE'),/不一致/);
});
test('legacy defaults remain inherited on unchanged export/import; clearing the sheet explicitly disables them',async()=>{
const source={categoryLookup:{a:'G'}};const book=await createConfigurationWorkbook(source,'matchRules',[rule]);
const tables=await readConfigurationWorkbook(await book.xlsx.writeBuffer());assert.deepEqual(importConfigurationTables(source,tables,'matchRules','MERGE',[rule]).definition,source);
tables[0].rows=[];assert.deepEqual(importConfigurationTables(source,tables,'matchRules','REPLACE',[rule]).definition.matchRules,[]);
});
test('invalid headers, duplicate identities, hierarchy cycles and formulas are rejected before any change',async()=>{
let tables=configurationTables(base,'departments');tables[0].headers[0]='错误';assert.throws(()=>importConfigurationTables(base,tables,'departments','MERGE'),/表头/);
tables=configurationTables(base,'departments');tables[0].rows.push([...tables[0].rows[0]]);assert.throws(()=>importConfigurationTables(base,tables,'departments','MERGE'),/重复/);
tables=configurationTables(base,'all').filter(t=>t.name==='组织层级');tables[0].rows[0][1]='A';assert.throws(()=>importConfigurationTables(base,tables,'all','MERGE'),/循环/);
const book=await createConfigurationWorkbook(base,'departments');book.getWorksheet('产业部名录').getCell('A2').value={formula:'1+1',result:2};
await assert.rejects(async()=>readConfigurationWorkbook(await book.xlsx.writeBuffer()),/Excel公式/);
});
test('query fields and calculation definitions round-trip, preserve formulas as text, and accept edits',async()=>{
const rules={schemaVersion:'rule-v1',sourceFields:{black:{qualified:'合格重量'},bright:{qualified:'亮片重量'}},rowRules:[{stage:'black',target:'total',formula:'qualified+scrap',nullPolicy:'PROPAGATE',zeroDenominatorPolicy:'ONE'}],metricRules:{RATE:{formula:'qualified/total',zeroDenominatorPolicy:'ONE'}},unknown:'keep'};
const workbook=await createConfigurationWorkbook(rules,'rules');let tables=await readConfigurationWorkbook(await workbook.xlsx.writeBuffer());assert.deepEqual(importConfigurationTables(rules,tables,'rules','REPLACE').definition,rules);
tables.find(t=>t.name==='指标计算公式').rows[0][1]='qualified/(qualified+scrap)';const imported=importConfigurationTables(rules,tables,'rules','MERGE').definition;assert.equal(imported.metricRules.RATE.formula,'qualified/(qualified+scrap)');assert.equal(imported.metricRules.RATE.nullPolicy,undefined);
const fields={filterFields:[{key:'0001',label:'客户',blackColumn:'客户代码',brightColumn:'客户代码',enabled:false,allowEmpty:true,sortOrder:1}]};const book=await createConfigurationWorkbook(fields,'filterFields');tables=await readConfigurationWorkbook(await book.xlsx.writeBuffer());assert.deepEqual(importConfigurationTables(fields,tables,'filterFields','REPLACE').definition,fields);
});
test('legacy twelve-column workbooks preserve the existing unclassified-only restriction',()=>{
const tables=configurationTables(base,'matchRules');tables[0].headers.pop();tables[0].rows.forEach(row=>row.pop());
for(const mode of ['MERGE','REPLACE'])assert.equal(importConfigurationTables(base,tables,'matchRules',mode).definition.matchRules[0].onlyUnclassified,true);
});
test('project unclassified-only rules retain their scope and customer codes through Excel',async()=>{
const projectRule={...rule,target:'projectGroup',conditions:[{field:'客户代码',operator:'EQUALS',value:'00001'},{field:'@group',operator:'EQUALS',value:'G'}]};const source={matchRules:[projectRule]};
const workbook=await createConfigurationWorkbook(source,'matchRules');const tables=await readConfigurationWorkbook(await workbook.xlsx.writeBuffer());
assert.deepEqual(importConfigurationTables(source,tables,'matchRules','REPLACE').definition,source);
});