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

128 lines
10 KiB
JavaScript

'use strict';
const {test}=require('node:test');
const assert=require('node:assert/strict');
const fs=require('node:fs');const os=require('node:os');const path=require('node:path');
const {RuleStore}=require('../../src/modules/order-rules/store.cjs');
function fixture(t,options={}){
options={clock:()=>new Date('2026-10-19T02:00:00Z'),...options};
const dir=fs.mkdtempSync(path.join(os.tmpdir(),'order-rules-'));
let store=new RuleStore(dir,options);
t.after(()=>{store.close();fs.rmSync(dir,{recursive:true,force:true});});
return {dir,get store(){return store;},reopen(){store.close();store=new RuleStore(dir,options);return store;}};
}
function configure(store,id='bs-change'){
const current=store.snapshot().rules.find(r=>r.id===id);
const rule=store.saveRule({...current,to:['orders@example.test'],archiveDirectory:'\\\\server\\orders\\备损',filenameDate:'appDate'},'admin');
const adapter=store.snapshot().adapters.find(a=>a.id===id);
const sample={...adapter.sample,connectionId:'connection-1',instanceId:'instance-1',taskId:'task-1',revision:'1',demo:false};
assert.equal(store.preview(sample).valid,true);
store.publish(id,{version:rule.version},'admin');
return sample;
}
test('draft edits persist and stale saves fail without changing the published rule',t=>{
const f=fixture(t),sample=configure(f.store),before=f.store.snapshot().rules.find(r=>r.id==='bs-change');
const changed=f.store.saveRule({...before,intro:'新版正文'},'admin');
assert.throws(()=>f.store.saveRule(before,'admin'),e=>e.status===409);
const reopened=f.reopen(),rule=reopened.snapshot().rules.find(r=>r.id==='bs-change');
assert.equal(rule.intro,'新版正文');assert.equal(rule.publishedVersion,before.version);assert.equal(rule.hasDraft,true);
const order=reopened.ingest(sample,'admin');assert.notEqual(order.ruleSnapshot.intro,changed.intro);
});
test('preview does not create orders; rule edits do not create duplicate tasks; conflicting revision is rejected',t=>{
const {store}=fixture(t),sample=configure(store);assert.equal(store.snapshot().orders.length,0);
const one=store.ingest(sample,'admin');assert.equal(store.ingest(sample,'admin').id,one.id);
assert.equal(store.snapshot().orders.length,1);
assert.throws(()=>store.ingest({...sample,brief:'更改了原文但没改版本'},'admin'),e=>e.status===409);
assert.throws(()=>store.ingest({...sample,demo:true},'admin'),e=>e.status===400);
});
test('unverified providers prevent automatic execution and HTTP-style status claims cannot fake archiving',t=>{
const {store}=fixture(t),sample=configure(store),order=store.ingest(sample,'operator');
assert.equal(order.status,'blocked');assert.match(order.error,/接入|联调|归档/);
assert.throws(()=>store.saveSettings({...store.settings(),enabled:true,channelId:'x'},'admin'),e=>e.status===409);
assert.throws(()=>store.recordArchive(order.id,{files:[],archivedAt:new Date().toISOString()},'operator'),e=>e.status===400||e.status===409);
assert.equal(store.prepare('2026-10-19T03:00:00Z','admin').length,0);
});
test('all same-category orders before cutoff aggregate once; post-cutoff orders stay pending; restart keeps uncertain sends blocked',t=>{
const {store,reopen}=fixture(t,{capabilities:{liveTodo:true,pdfExport:true,automaticArchive:true}}),sample=configure(store);
for(let i=0;i<205;i++){
const order=store.ingest({...sample,instanceId:'i'+i,taskId:'t'+i,receivedAt:'2026-10-19T01:00:00Z'},'system');
store.recordArchive(order.id,{files:[{name:'order'+i+'.pdf',path:'C:\\archive\\order'+i+'.pdf',sha256:'a'.repeat(64),size:42,kind:'pdf'}],archivedAt:'2026-10-19T01:05:00Z'},'archive-executor');
}
const later=store.ingest({...sample,instanceId:'later',taskId:'later',receivedAt:'2026-10-19T10:00:00Z'},'system');
store.recordArchive(later.id,{files:[{name:'later.pdf',path:'C:\\archive\\later.pdf',sha256:'b'.repeat(64),size:42,kind:'pdf'}],archivedAt:'2026-10-19T10:01:00Z'},'archive-executor');
const batches=store.prepare('2026-10-19T03:00:00Z','admin');assert.equal(batches.length,1);assert.equal(batches[0].orderIds.length,205);
assert.equal(store.prepare('2026-10-19T03:00:00Z','admin').length,0);
assert.equal(store.snapshot().orders.find(o=>o.id===later.id).status,'pending');
store.beginSending(batches[0].id);
const recovered=reopen();assert.equal(recovered.snapshot().batches[0].status,'unknown');
assert.equal(recovered.prepare('2026-10-19T03:00:00Z','admin').length,0);
recovered.resolve(batches[0].id,{resolution:'sent',note:'已查询服务器确认完整接受'},'admin');
assert.equal(recovered.snapshot().orders.filter(o=>o.status==='sent').length,205);
});
test('one incomplete order blocks the entire category, and publication demands a fresh sample trial',t=>{
const {store}=fixture(t,{capabilities:{liveTodo:true,pdfExport:true,automaticArchive:true}}),sample=configure(store);
store.ingest(sample,'system');assert.equal(store.prepare('2026-10-19T03:00:00Z','admin').length,0);
const rule=store.snapshot().rules.find(r=>r.id==='bs-change'),draft=store.saveRule({...rule,intro:'新正文'},'admin');
assert.throws(()=>store.publish(rule.id,{version:draft.version},'admin'),e=>e.status===409);
});
test('semantic identity ignores JSON key order and numeric/string revision formatting',t=>{
const {store}=fixture(t),sample=configure(store);
const first=store.ingest({...sample,revision:0},'system');
const reordered=Object.fromEntries(Object.entries({...sample,revision:'0'}).reverse());
reordered.fields=Object.fromEntries(Object.entries(sample.fields).reverse());
assert.equal(store.ingest(reordered,'system').id,first.id);assert.equal(store.snapshot().orders.length,1);
});
test('demo structure preview cannot authorize publishing a business rule',t=>{
const {store}=fixture(t),rule=store.snapshot().rules.find(r=>r.id==='bs-change');
const saved=store.saveRule({...rule,to:['orders@example.test'],archiveDirectory:'\\\\server\\orders',filenameDate:'appDate'},'admin');
const sample=store.snapshot().adapters.find(a=>a.id==='bs-change').sample;
assert.equal(store.preview(sample).valid,true);
assert.throws(()=>store.publish(saved.id,{version:saved.version},'admin'),e=>e.status===409);
});
test('pausing a rule prevents a prepared batch from entering SMTP sending',t=>{
const {store}=fixture(t,{capabilities:{liveTodo:true,pdfExport:true,automaticArchive:true}}),sample=configure(store);
const order=store.ingest(sample,'system');store.recordArchive(order.id,{files:[{name:'one.pdf',path:'C:\\archive\\one.pdf',sha256:'a'.repeat(64),size:42,kind:'pdf'}],archivedAt:'2026-10-19T01:05:00Z'},'archive-executor');
const batch=store.prepare('2026-10-19T03:00:00Z','admin')[0],rule=store.snapshot().rules.find(r=>r.id==='bs-change');
store.pause(rule.id,{version:rule.version},'admin');assert.throws(()=>store.beginSending(batch.id),e=>e.status===409);
assert.equal(store.snapshot().batches[0].status,'prepared');
});
test('late discovered pre-cutoff orders cannot create a second email for an already frozen category and slot',t=>{
const {store}=fixture(t,{capabilities:{liveTodo:true,pdfExport:true,automaticArchive:true}}),sample=configure(store);
const archive=(sample)=>{const order=store.ingest(sample,'system');store.recordArchive(order.id,{files:[{name:order.id+'.pdf',path:'C:\\archive\\'+order.id+'.pdf',sha256:'a'.repeat(64),size:42,kind:'pdf'}],archivedAt:'2026-10-19T01:05:00Z'},'archive-executor');return order;};
archive(sample);assert.equal(store.prepare('2026-10-19T03:00:00Z','admin').length,1);
archive({...sample,instanceId:'late',taskId:'late'});
assert.equal(store.prepare('2026-10-19T03:00:00Z','admin').length,0);
assert.equal(store.prepare('2026-10-19T09:00:00Z','admin').length,0);
assert.equal(store.snapshot().orders.filter(o=>o.status==='ignored').length,2);
});
test('old history cannot enter this morning mail while current orders remain eligible',t=>{
const {store}=fixture(t,{clock:()=>new Date('2026-10-20T02:00:00Z'),capabilities:{liveTodo:true,pdfExport:true,automaticArchive:true}}),sample=configure(store);
const old=store.ingest({...sample,instanceId:'old',taskId:'old',receivedAt:'2026-10-19T01:00:00Z'},'system');
assert.equal(old.status,'ignored');
assert.throws(()=>store.recordArchive(old.id,{files:[],archivedAt:'2026-10-20T02:00:00Z'},'archive-executor'),e=>e.status===409);
const current=store.ingest({...sample,instanceId:'current',taskId:'current',receivedAt:'2026-10-20T01:00:00Z'},'system');
store.recordArchive(current.id,{files:[{name:'current.pdf',path:'C:\\archive\\current.pdf',sha256:'a'.repeat(64),size:42,kind:'pdf'}],archivedAt:'2026-10-20T01:05:00Z'},'archive-executor');
const batches=store.prepare('2026-10-20T03:00:00Z','admin');assert.equal(batches.length,1);assert.deepEqual(batches[0].orderIds,[current.id]);
assert.equal(batches[0].window.start,'2026-10-19T16:00:00.000Z');
});
test('paused old publication cannot shadow an enabled replacement for the same process and node',t=>{
const {store}=fixture(t),sample=configure(store);
const old=store.snapshot().rules.find(r=>r.id==='bs-change');store.pause(old.id,{version:old.version},'admin');
store.saveRule({...old,processNames:['暂不使用的旧流程']},'admin');
const candidate=store.snapshot().rules.find(r=>r.id==='trial-change');
const replacement=store.saveRule({...candidate,processNames:['备损订单更改评审'],nodeNames:['订单专员(订单通知书)'],adapterIds:['bs-change'],to:['replacement@example.test'],archiveDirectory:'\\\\server\\orders',filenameDate:'appDate'},'admin');
assert.equal(store.preview(sample).ruleId,'trial-change');store.publish(replacement.id,{version:replacement.version},'admin');
const order=store.ingest(sample,'system');assert.equal(order.categoryId,'trial-change');assert.equal(order.trace.valid,true);
});
test('paused ingestion retains classification and explicit recheck can recover against the current rule',t=>{
const {store}=fixture(t,{capabilities:{liveTodo:true,pdfExport:true,automaticArchive:true}}),sample=configure(store);
const rule=store.snapshot().rules.find(r=>r.id==='bs-change');store.pause(rule.id,{version:rule.version},'admin');
const order=store.ingest(sample,'system');assert.equal(order.categoryId,'bs-change');assert.equal(order.status,'blocked');
store.publish(rule.id,{version:rule.version},'admin');
const checked=store.recheck(order.id,{note:'已核实规则恢复,重新识别'},'admin');
assert.equal(checked.status,'identified');assert.equal(checked.ruleSnapshot.version,rule.version);
assert.equal(store.snapshot().orders.length,1);
assert.throws(()=>store.prepare('2026-10-19T12:00:00','admin'),e=>e.status===400);
});