import test from 'node:test'; import assert from 'node:assert/strict'; import { configurationTables, importConfigurationTables, createConfigurationWorkbook, readConfigurationWorkbook } from '../src/configurationWorkbook.ts'; const rule={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.deepEqual(source,base);assert.equal(workbook.getWorksheet('产业部名录').getCell('A2').numFmt,'@'); }); 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); });