修复:分类规则仅处理未分类并保留已有归属

This commit is contained in:
2026-10-31 19:18:46 +08:00
parent aa7c320dc6
commit cbe7fc17bb
20 changed files with 320 additions and 36 deletions
@@ -13,7 +13,7 @@ test('blank-key source creates a narrowly scoped rule and preserves inherited de
{field:'PDT代码',operator:'IS_BLANK',value:''},
{field:'销售片区',operator:'EQUALS',value:'销售区A'},
]);
assert.equal(result.matchRules[0].stage,'black');assert.deepEqual(result.matchRules[1],defaults[0]);
assert.equal(result.matchRules[0].onlyUnclassified,true);assert.equal(result.matchRules[0].stage,'black');assert.deepEqual(result.matchRules[1],defaults[0]);
assert.deepEqual(base,{projectGroupLookup:{'001|G':'项目组A'},extra:['keep']});
const changed=editClassification(result,selection,'业务组A',defaults);
assert.equal(changed.matchRules.length,2);assert.equal(changed.matchRules[0].value,'业务组A');
@@ -43,11 +43,11 @@ test('editing a disabled project relationship enables only that relationship and
assert.throws(()=>editClassification(base,selection,'不存在的项目组',[]),/未登记/);
});
test('removable conditions apply to all matching rows without an implicit unclassified predicate',()=>{
test('removable conditions retain an explicit unclassified-only scope',()=>{
const options={stage:'all',conditions:[{field:'@department',operator:'EQUALS',value:'产业部A'},{field:'销售片区',operator:'EQUALS',value:'销售区A'}]};
const result=editClassification({matchRules:[]},selection,'G',[],options);
assert.deepEqual(result.matchRules[0].conditions,options.conditions);assert.equal(result.matchRules[0].stage,'all');
assert.equal(result.matchRules[0].conditions.some(c=>c.field==='@group'),false);
assert.equal(result.matchRules[0].onlyUnclassified,true);
assert.equal(classificationRuleId(source,options),classificationRuleId(source,{...options,conditions:[...options.conditions].reverse()}));
const specific=editClassification({matchRules:[]},selection,'G',[],{stage:'all'});
assert.ok(specific.matchRules[0].priority<result.matchRules[0].priority);
@@ -2,7 +2,7 @@ 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 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}]};
@@ -46,3 +46,8 @@ test('query fields and calculation definitions round-trip, preserve formulas as
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);
});