import test from 'node:test'; import assert from 'node:assert/strict'; import { ruleLogicIssues, planRuleMove, neighboringRuleIndex, displayRowFormula, isDefaultAssignment } from '../src/ruleEditing.ts'; const schema={sourceFields:{black:{platingWeight:'送电镀重量',shipmentWeight:'黑片出货重量',responsibilityScrap:'责废总重量',materialScrap:'料废重量',externalScrap:'外废重量',inspectionNote:'检验备注'},bright:{goodWeight:'合格重量',scrapWeight:'检包入废品库重量',applicability:'是否电镀来料/二等品库来料'}},rowTargets:{black:['black_good_weight','black_responsibility_scrap','black_material_scrap','black_external_scrap','black_scrap_weight','black_total_weight'],bright:['bright_good_weight','bright_scrap_weight']},componentLabels:{black_good_weight:'黑片合格重量',black_scrap_weight:'黑片报废重量',black_total_weight:'黑片总重量'},conditionOperators:['EQ','CONTAINS','GT','IS_BLANK'],metricCodes:['blackRate','brightRate','compositeRate'],metricLabels:{blackRate:'黑片合格率'}}; const base={schemaVersion:'unchanged',sourceFields:{},rowRules:[ {stage:'black',target:'black_good_weight',formula:'platingWeight + shipmentWeight'}, {stage:'black',target:'black_responsibility_scrap',formula:'responsibilityScrap'}, {stage:'black',target:'black_material_scrap',formula:'materialScrap'}, {stage:'black',target:'black_external_scrap',formula:'externalScrap'}, {stage:'black',target:'black_external_scrap',formula:'0',when:{field:'inspectionNote',operator:'CONTAINS',value:'过账'}}, {stage:'black',target:'black_scrap_weight',formula:'SUM(black_responsibility_scrap, black_material_scrap, black_external_scrap)'}, {stage:'black',target:'black_total_weight',formula:'black_good_weight + black_scrap_weight'}, {stage:'bright',target:'bright_good_weight',formula:'0'}, {stage:'bright',target:'bright_good_weight',formula:'goodWeight',when:{field:'applicability',operator:'EQ',value:'是'}}, {stage:'bright',target:'bright_scrap_weight',formula:'0'}, {stage:'bright',target:'bright_scrap_weight',formula:'scrapWeight',when:{field:'applicability',operator:'EQ',value:'是'}}, ],metricRules:{blackRate:{formula:'blackGood / blackTotal'},brightRate:{formula:'brightGood / brightTotal'},compositeRate:{formula:'blackRate * brightRate'}}}; test('baseline formulas and ordered conditional assignments remain valid without any mutation',()=>{const text=JSON.stringify(base);assert.deepEqual(ruleLogicIssues(base,schema),[]);assert.equal(JSON.stringify(base),text);}); test('a move swaps exactly two same-stage rows, retaining every other row and metadata',()=>{ const text=JSON.stringify(base),result=planRuleMove(base,0,1,schema);assert.ok(result.definition);assert.equal(result.definition.rowRules[0].target,'black_responsibility_scrap');assert.equal(result.definition.rowRules[1].target,'black_good_weight'); assert.deepEqual(result.definition.rowRules.slice(2),base.rowRules.slice(2));assert.equal(JSON.stringify(base),text);assert.deepEqual(planRuleMove(result.definition,1,-1,schema).definition,base); }); test('moves do not cross stage boundaries',()=>{assert.equal(neighboringRuleIndex(base.rowRules,6,1),undefined);assert.equal(neighboringRuleIndex(base.rowRules,7,-1),undefined);assert.equal(planRuleMove(base,7,-1,schema).definition,undefined);}); test('moving a conditional assignment above its default is rejected',()=>{const result=planRuleMove(base,8,-1,schema);assert.equal(result.definition,undefined);assert.match(result.reason,/默认值应放在条件覆盖之前/);}); test('moving totals ahead of their component assignments is rejected',()=>{const result=planRuleMove(base,5,-1,schema);assert.equal(result.definition,undefined);assert.match(result.reason,/后面还会修改/);}); test('unknown variables, text arithmetic and forward metric references are caught',()=>{ for(const formula of ['typoWeight','inspectionNote + 1']){const def=structuredClone(base);def.rowRules[0].formula=formula;assert.ok(ruleLogicIssues(def,schema).some(issue=>issue.key.startsWith('variable:')));} const def=structuredClone(base);def.metricRules.blackRate.formula='compositeRate';assert.ok(ruleLogicIssues(def,schema).some(issue=>issue.key.startsWith('metric-variable:'))); }); test('duplicate conditions with different formulas are rejected regardless of JSON property order',()=>{const def=structuredClone(base);def.rowRules.push({stage:'bright',target:'bright_scrap_weight',formula:'1',when:{value:'是',operator:'EQ',field:'applicability'}});assert.ok(ruleLogicIssues(def,schema).some(issue=>issue.key.startsWith('duplicate:')));}); test('legacy partial overrides keep access to baseline components',()=>{const def={rowRules:[{stage:'black',target:'black_total_weight',formula:'black_good_weight + black_scrap_weight'}]};assert.deepEqual(ruleLogicIssues(def,schema),[]);}); test('source aliases are rendered in Chinese while the original formula is retained',()=>{const row=base.rowRules[10];assert.equal(displayRowFormula(row,schema),'检包入废品库重量');assert.equal(row.formula,'scrapWeight');assert.equal(isDefaultAssignment(base.rowRules[9],9,base.rowRules),true);assert.equal(isDefaultAssignment(row,10,base.rowRules),false);});