Files
order-notification-automation/tests/order-rules/recognition-proposal.test.cjs
T

58 lines
9.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use strict';
const test=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs'),os=require('node:os'),path=require('node:path');
const {RuleStore}=require('../../src/modules/order-rules/store.cjs');
const {recommendFields}=require('../../src/modules/order-rules/field-recommendation.cjs');
const admin={username:'admin',role:'admin'};
function fixture(t){
const dir=fs.mkdtempSync(path.join(os.tmpdir(),'recognition-')),store=new RuleStore(dir,{clock:()=>new Date('2026-10-20T02:00:00Z')});
t.after(()=>{store.close();fs.rmSync(dir,{recursive:true,force:true});});
const sample={source:'todo',connectionId:'c1',instanceId:'i1',taskId:'t1',revision:1,processName:'备损订单更改评审',processVersion:'1.4',nodeName:'订单专员(订单通知书)',receivedAt:'2026-10-20T01:00:00Z',serialNum:'S-1',title:'订单号:148396机芯号:X客户简称:甲总数量:0订单号:HA148396客户简称:乙总数量:5',fields:{order:'148396',qty:0,amount:500,date:'2026-10-20'}};
const capture=(overrides={},labels={})=>{const s={...sample,...overrides};return store.recordCapture({taskId:s.taskId,sample:s,snapshot:{processVersion:s.processVersion,fields:Object.entries(s.fields).map(([path,value])=>({path,value,label:labels[path]||({order:'订单号',qty:'订单数量',amount:'总金额',date:'申请日期'})[path]||path}))}});};
return {store,sample,capture,captured:capture()};
}
const kinds=['rules','field-mapping-drafts','field-adapters','orders','batches','execution-tasks','field-mapping-requests','audit'];
const snapshot=store=>Object.fromEntries(kinds.map(k=>[k,store.all(k)]));
test('unique explicit labels recommend real sources, preserving zero and rejecting component quantities',()=>{
const r=recommendFields([{path:'form.order',label:'订单号',value:'148396'},{path:'form.qty',label:'订单数量',value:0},{path:'form.amount',label:'总金额',value:500},{path:'part.count',label:'数量',value:2}]);
assert.equal(r.sources.orderNo.path,'form.order');assert.equal(r.sources.quantity.path,'form.qty');assert.equal(r.sources.totalAmount.path,'form.amount');assert.equal(r.sources.customer,undefined);assert.deepEqual(r.sources.brief,{type:'title'});
const a=recommendFields([{path:'one',label:'订单号',value:'148396'},{path:'two',label:'订单号',value:'HA148396'}]);assert.equal(a.sources.orderNo,undefined);assert.equal(a.candidates.orderNo.length,2);
assert.equal(recommendFields([{label:'订单号',value:'123'}]).sources.orderNo,undefined);
});
test('selected orders recognize their own values and proposals never persist configuration or publication evidence',t=>{
const f=fixture(t),before=snapshot(f.store),p=f.store.proposeMapping({sampleId:f.captured.id,ruleId:'bs-change',ruleVersion:1});
assert.equal(p.mapping.basis,'recommended');assert.equal(p.recognition.valid,true);assert.equal(p.recognition.event.quantity,0);assert.match(p.recognition.event.brief,/HA148396/);assert.equal(p.recognition.publishEvidence,false);assert.deepEqual(p.scopeCandidate,{processName:f.sample.processName,nodeName:f.sample.nodeName});assert.equal(p.classification.status,'matched');assert.deepEqual(snapshot(f.store),before);
const draft=f.store.saveMapping({processName:f.sample.processName,processVersion:'1.4',sampleId:f.captured.id,sources:{orderNo:{type:'form',path:'order'},brief:{type:'title'}},version:0,idempotencyKey:'d'},admin);
const second=f.capture({taskId:'two',title:'订单号:200机芯号:Y',fields:{order:'200',qty:8}}),b=snapshot(f.store),p2=f.store.proposeMapping({sampleId:second.id});assert.equal(p2.mapping.basis,'draft');assert.equal(p2.mapping.id,draft.id);assert.equal(p2.recognition.event.orderNo,'200');assert.equal(p2.mapping.sources.quantity,undefined);assert.equal(p2.recognition.event.quantity,null);assert.deepEqual(snapshot(f.store),b);
const next=f.capture({taskId:'new',processVersion:'1.5'});assert.equal(f.store.proposeMapping({sampleId:next.id}).mapping.basis,'recommended');
});
test('ambiguity and missing order fields produce complete blocked results; forged sources are rejected',t=>{
const f=fixture(t),amb=f.capture({taskId:'amb',fields:{a:'148396',b:'HA148396'}},{a:'订单号',b:'订单号'}),p=f.store.proposeMapping({sampleId:amb.id});
assert.equal(p.recognition.valid,false);assert.equal(p.fields.find(f=>f.key==='orderNo').state,'ambiguous');assert.equal(p.fields.find(f=>f.key==='orderNo').candidates.length,2);assert.ok(p.blockedReasons.length);
assert.throws(()=>f.store.proposeMapping({sampleId:amb.id,sources:{orderNo:{type:'form',path:'invented'}}}),{status:400});
const unknown=f.capture({taskId:'unknown',processVersion:''});assert.equal(f.store.proposeMapping({sampleId:unknown.id}).recognition.valid,false);
});
test('history never proposes a node; unconfirmed scope is candidate and other categories block matching',t=>{
const f=fixture(t),h=f.capture({taskId:'history',source:'history',configurationOnly:true,timestampType:'process-start',nodeName:'历史查看(未提供办理节点)'}),hp=f.store.proposeMapping({sampleId:h.id,ruleId:'bs-change',ruleVersion:1});assert.equal(hp.scopeCandidate,null);assert.equal(hp.classification.status,'history');assert.equal(hp.effects,null);
const n=f.capture({taskId:'other-node',nodeName:'主管审核'}),p=f.store.proposeMapping({sampleId:n.id,ruleId:'bs-change',ruleVersion:1});assert.equal(p.classification.status,'candidate');assert.equal(p.effects,null);
const rule=f.store.get('rules','bs-change').draft,other=f.store.get('rules','bs-notice');other.draft.scopes=[{processName:f.sample.processName,nodeName:'主管审核'}];f.store.put('rules',other);
const conflict=f.store.proposeMapping({sampleId:n.id,ruleId:rule.id,ruleVersion:rule.version,ruleDraft:{...rule,scopes:[...require('../../src/modules/order-rules/scopes.cjs').scopesFor(rule),{processName:f.sample.processName,nodeName:'主管审核'}]}});assert.equal(conflict.classification.status,'conflict');assert.ok(conflict.conflicts.some(c=>c.ruleId==='bs-notice'));assert.equal(conflict.effects,null);
});
test('unsaved rule dates, subject and directory affect preview without changing stored rules',t=>{
const f=fixture(t),rule=f.store.get('rules','bs-change').draft,before=snapshot(f.store),draft={...rule,filenameDate:'appDate',subjectPrefix:'待保存主题',archiveDirectory:'\\\\server\\share\\pending',to:['test@example.test']};
const p=f.store.proposeMapping({sampleId:f.captured.id,ruleId:rule.id,ruleVersion:rule.version,ruleDraft:draft});assert.equal(p.recognition.valid,true);assert.match(p.effects.path,/pending/);assert.match(p.effects.mail.subject,/待保存主题/);assert.equal(p.effects.simulation,true);assert.equal(p.effects.archiveVerified,false);assert.deepEqual(snapshot(f.store),before);
const bad=f.capture({taskId:'bad-date',fields:{order:'148396',date:'2026-02-30'}});const b=f.store.proposeMapping({sampleId:bad.id,ruleId:rule.id,ruleVersion:rule.version,ruleDraft:draft});assert.equal(b.recognition.valid,false);assert.match(b.blockedReasons.join(' '),/日期/);assert.equal(b.effects,null);
assert.throws(()=>f.store.proposeMapping({sampleId:f.captured.id,ruleId:rule.id,ruleVersion:0}),{status:409});
});
test('duplicate active mappings remain blocked even when a saved draft exists',t=>{const f=fixture(t);f.store.saveMapping({processName:f.sample.processName,processVersion:'1.4',sampleId:f.captured.id,sources:{orderNo:{type:'form',path:'order'}},version:0,idempotencyKey:'duplicate-draft'},admin);for(const id of ['active-one','active-two'])f.store.put('field-adapters',{id,processName:f.sample.processName,processVersion:'1.4',verified:true,version:1,sources:{orderNo:{type:'form',path:'order'}}});const p=f.store.proposeMapping({sampleId:f.captured.id});assert.equal(p.recognition.valid,false);assert.match(p.blockedReasons.join(';'),/多个生效映射/);});
test('all fifteen categories reuse methods and recognize the next order independently, including HA and attachment distinctions',t=>{
const f=fixture(t);const ids=['overseas-notice','overseas-change','new-retail-notice','new-retail-change','domestic-notice','carrier-notice','hardware-notice','retail-notice','domestic-change','domestic-rework','processing-notice','bs-notice','bs-change','trial-notice','trial-change'];
for(const [index,id]of ids.entries()){const rule=f.store.get('rules',id).draft,name=rule.processNames[0],node=rule.nodeNames[0],no='ORDER'+index,title='订单号:'+no+'机芯号:X总数量:0订单号:HA'+no+'机芯号:Y总数量:5',captured=f.capture({taskId:'all-'+id,processName:name,processVersion:'test-'+id,nodeName:node,title,fields:{order:no,qty:0}}),p=f.store.proposeMapping({sampleId:captured.id,ruleId:id,ruleVersion:rule.version});assert.equal(p.classification.status,'matched',id);assert.equal(p.recognition.valid,true,id);assert.equal(p.recognition.event.orderNo,no);assert.equal(p.recognition.event.quantity,0);assert.equal(p.recognition.event.titleOrders.length,2);assert.match(p.recognition.event.brief,new RegExp('HA'+no));
const draft=f.store.saveMapping({processName:name,processVersion:'test-'+id,sampleId:captured.id,sources:p.mapping.sources,version:0,idempotencyKey:'save-'+id},admin),check=f.store.checkMapping({id:draft.id,version:draft.version,sampleId:captured.id,idempotencyKey:'check-'+id},admin);assert.equal(check.valid,true,id);f.store.activateMapping({id:draft.id,version:draft.version,checkId:check.id,idempotencyKey:'activate-'+id},admin);
const next=f.capture({taskId:'second-'+id,processName:name,processVersion:'test-'+id,nodeName:node,title:'订单号:NEXT'+index+'机芯号:Z',fields:{order:'NEXT'+index,qty:7}}),result=f.store.proposeMapping({sampleId:next.id});assert.equal(result.recognition.event.orderNo,'NEXT'+index);assert.equal(result.recognition.event.quantity,7);assert.equal(f.store.get('field-mapping-drafts',draft.id).version,1);
assert.equal(rule.attachPdf,false);
}
assert.equal(f.store.settings().enabled,false);assert.deepEqual(f.store.all('orders'),[]);assert.deepEqual(f.store.all('batches'),[]);
});