Files

33 lines
6.5 KiB
JavaScript

'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 formPath='/YZSoft/Forms/XForm/Test/OrderV4.aspx',origin='https://bpm.skyworthdigital.com';
function fixture(t){
const dir=fs.mkdtempSync(path.join(os.tmpdir(),'version-compatible-')),store=new RuleStore(dir,{clock:()=>new Date('2026-10-26T02:00:00Z'),capabilities:{liveTodo:true,pdfExport:true,automaticArchive:true}});t.after(()=>{store.close();fs.rmSync(dir,{recursive:true,force:true});});
const base={connectionId:'c',source:'history',configurationOnly:true,portalSource:'history-v4',instanceId:'11',taskId:'10',processName:'Compatibility test',processVersion:'1.4',nodeName:'Notice',receivedAt:store.now(),serialNum:'S',title:'订单号:123456',fields:{'Order.Number':'123456','Order.Quantity':'2'}};
const snap=s=>({processVersion:s.processVersion,formPath,sourceUrl:origin+formPath+'?tid='+s.instanceId+'&pid='+s.taskId,fields:Object.entries(s.fields).map(([path,value])=>({path,value}))});
const captured=store.recordCapture({sample:base,snapshot:snap(base),taskId:'baseline'});
const adapter={id:'verified-test',processName:base.processName,processVersion:'1.4',verified:true,reference:false,sampleId:captured.id,evidenceHash:captured.evidenceHash,sources:{orderNo:{type:'form',path:'Order.Number'},quantity:{type:'form',path:'Order.Quantity'},brief:{type:'title'}}};store.put('field-adapters',adapter);
const rule={...store.get('rules','bs-change').draft,id:'compatibility-test',categoryId:'compatibility-test',name:'Compatibility test',scopes:[{processName:base.processName,nodeName:'Notice'}],adapterIds:[adapter.id],filenameDate:'receivedAt',status:'enabled',to:['only@example.com'],cc:[]};store.put('rules',{id:rule.id,draft:rule,published:rule});
const sample={...base,source:'todo',configurationOnly:false,portalSource:'todo-v4',taskId:'20',instanceId:'21',processVersion:'1.5',formPath,formVersion:'V4',detailUrl:origin+'/YZSoft/Forms/Process.aspx?tid=21&pid=20',detailEvidence:{readOnly:true,tid:'21',pid:'20',formVersion:'V4',listProcessVersion:'1.5'}};
const capture=()=>store.recordCapture({sample,snapshot:{...snap(sample),html:'<html><body>captured form</body></html>'},taskId:'current'});
return {store,sample,adapter,rule,capture,captured};
}
test('persisted full readonly capture permits ephemeral compatibility without changing registry or rule',t=>{
const f=fixture(t),before=JSON.stringify([f.store.adapters(),f.store.all('rules')]);f.capture();const order=f.store.ingest(f.sample,'test');assert.equal(order.status,'identified',order.error);assert.equal(order.event.processVersion,'1.5');assert.equal(order.event.adapterId,f.adapter.id);assert.equal(order.trace.adapterCompatibility.baselineVersion,'1.4');assert.deepEqual(order.ruleSnapshot.to,['only@example.com']);assert.equal(JSON.stringify([f.store.adapters(),f.store.all('rules')]),before);assert.equal(f.store.previewUntrusted(f.sample).valid,false);
assert.equal(f.store.recheck(order.id,{note:'verified evidence again'},'test').status,'identified');
});
test('restricted frozen rules and adapter hashes survive compatibility and still reject recipient widening',t=>{
const f=fixture(t),crypto=require('node:crypto'),canonical=v=>Array.isArray(v)?v.map(canonical):v&&typeof v==='object'?Object.fromEntries(Object.keys(v).sort().map(k=>[k,canonical(v[k])])):v,hash=v=>crypto.createHash('sha256').update(JSON.stringify(canonical(v))).digest('hex');
const local=fs.mkdtempSync(path.join(os.tmpdir(),'compatible-restricted-'));t.after(()=>fs.rmSync(local,{recursive:true,force:true}));const root='\\\\localhost\\'+local[0]+'$'+local.slice(2);
const rule={...f.rule,testRunId:'restricted',testArchiveRoot:root,archiveDirectory:root,attachPdf:false,to:['2639266652@qq.com'],cc:[]},run={id:'restricted',version:1,enabled:true,since:f.store.now(),recipient:'2639266652@qq.com',archiveRoot:root,rules:[rule],adapterHashes:{[rule.id]:hash(rule.adapterIds.map(id=>f.store.adapters().find(a=>a.id===id)))}};
f.store.put('restricted-test',{id:'main',value:run});f.capture();const before=JSON.stringify(f.store.get('restricted-test','main')),order=f.store.ingest(f.sample,'test');assert.equal(order.status,'identified',order.error);assert.equal(JSON.stringify(f.store.get('restricted-test','main')),before);assert.deepEqual(order.ruleSnapshot.to,['2639266652@qq.com']);assert.deepEqual(order.ruleSnapshot.cc,[]);require('../../src/modules/order-rules/restricted-test.cjs').assertRule(f.store,order.ruleSnapshot);assert.throws(()=>require('../../src/modules/order-rules/restricted-test.cjs').assertRule(f.store,{...order.ruleSnapshot,to:['someone@example.com']}),/受限测试/);
});
test('persisted capture cannot be replaced with forged fields or mapping-only snapshot',t=>{
const f=fixture(t);const capture=f.capture();assert.equal(f.store.preview({...f.sample,fields:{...f.sample.fields,'Order.Number':'999999'}}).valid,false);capture.snapshot.purpose='field-mapping';f.store.put('capture-samples',capture);assert.equal(f.store.preview(f.sample).valid,false);
});
test('compatibility preview never stamps rule publication evidence',t=>{
const f=fixture(t),capture=f.capture(),before=JSON.stringify(f.store.all('rules'));assert.equal(f.store.capturedPreview(capture.id).valid,true);assert.equal(JSON.stringify(f.store.all('rules')),before);
});
for(const [name,change] of Object.entries({missingCapture:f=>{},wrongPath:f=>{f.sample.formPath='/YZSoft/Forms/XForm/Test/OtherV4.aspx';f.capture();},changedType:f=>{f.sample.fields['Order.Quantity']=2;f.capture();},missingField:f=>{delete f.sample.fields['Order.Quantity'];f.capture();},objectField:f=>{f.sample.fields['Order.Quantity']={value:'2'};f.capture();},wrongIdentity:f=>{f.sample.detailEvidence.pid='99';f.capture();},multipleCandidates:f=>{f.store.put('field-adapters',{...f.adapter,id:'second',processVersion:'1.3'});const b=f.store.get('capture-samples',f.captured.id);const s={...b.sample,processVersion:'1.3'};const c=f.store.recordCapture({sample:s,snapshot:{...b.snapshot,processVersion:'1.3'},taskId:'other'});f.store.put('field-adapters',{...f.adapter,id:'second',processVersion:'1.3',sampleId:c.id,evidenceHash:c.evidenceHash});f.rule.adapterIds.push('second');f.store.put('rules',{id:f.rule.id,draft:f.rule,published:f.rule});f.capture();}}))test('compatibility blocks '+name,t=>{const f=fixture(t);change(f);const order=f.store.ingest(f.sample,'test');assert.equal(order.status,'blocked');assert.match(order.error,/版本兼容/);});