import test from 'node:test'; import assert from 'node:assert/strict'; import { displayFormula, stageRuleRows, ruleChangeSummary, matchingTarget, policyDescription } from '../src/rulePresentation.ts'; const original={sourceFields:{black:{platingWeight:'送电镀重量'},bright:{goodWeight:'合格重量'}},rowRules:Array.from({length:22},(_,i)=>({stage:i<13?'black':'bright',target:i%3===0?'black_good_weight':i%3===1?'black_scrap_weight':'black_reuse_good',formula:String(i),when:{field:'inspectionNote',operator:'CONTAINS',value:'过账'}})),metricRules:{blackRate:{formula:'blackGood / blackTotal'}}}; test('stage tables keep every execution index and full rule unchanged',()=>{ const before=JSON.stringify(original);const all=[...stageRuleRows(original,'black'),...stageRuleRows(original,'bright')].sort((a,b)=>a.index-b.index); assert.equal(all.length,22);assert.deepEqual(all.map(({index,...row})=>row),original.rowRules);assert.equal(JSON.stringify(original),before);assert.deepEqual(ruleChangeSummary(original,structuredClone(original)),[]); }); test('business formula translation explains percentages without mutating source rules',()=>{ assert.equal(displayFormula('blackRate * brightRate'),'黑片合格率 × 亮片合格率');assert.match(policyDescription({zeroDenominatorPolicy:'ONE',nullResultPolicy:'NULL'}),/分母为0:按100%处理/);assert.match(policyDescription({zeroDenominatorPolicy:'ONE'},false),/分母为0:取1/); const next=structuredClone(original);next.metricRules.blackRate.formula='(blackGood / blackTotal)';assert.deepEqual(ruleChangeSummary(original,next),['合格率公式 1 项']);assert.notEqual(original.metricRules.blackRate.formula,next.metricRules.blackRate.formula); }); test('target display preserves existing specificity and group tie-break precedence',()=>{ const overall={dimensions:{},value:.955},department={dimensions:{department:'A'},value:.96},group={dimensions:{group:'G'},value:.97},specific={dimensions:{department:'A',group:'G'},value:.98};const list=[overall,department,group,specific];const before=JSON.stringify(list); assert.equal(matchingTarget(list,{department:'A',group:'G'}),specific);assert.equal(matchingTarget(list.slice(0,3),{department:'A',group:'G'}),group);assert.equal(matchingTarget(list,{department:'A',group:'OTHER'}),department);assert.equal(matchingTarget(list,{department:'B'}),overall);assert.equal(JSON.stringify(list),before); }); test('stage table reflects the actual row order without business grouping',()=>{ const definition={rowRules:[{stage:'black',target:'black_responsibility_scrap',formula:'1'},{stage:'black',target:'black_good_weight',formula:'2'},{stage:'black',target:'black_material_scrap',formula:'3'}]}; assert.deepEqual(stageRuleRows(definition,'black').map(row=>row.target),['black_responsibility_scrap','black_good_weight','black_material_scrap']); });