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

29 lines
4.2 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'),restricted=require('../../src/modules/order-rules/restricted-test.cjs');
function fixture(t){const dir=fs.mkdtempSync(path.join(os.tmpdir(),'stability-expiry-'));let now=new Date('2026-10-26T03:00:00Z'),store=new RuleStore(dir,{clock:()=>now});store.put('settings',{...store.get('settings','main'),enabled:true,processingEnabled:true});t.after(()=>{store.close();fs.rmSync(dir,{recursive:true,force:true});});return {get store(){return store;},setNow:value=>{now=new Date(value);},restart:()=>{store.close();store=new RuleStore(dir,{clock:()=>now});}};}
const period={id:'main',enabled:true,startedAt:'2026-10-26T03:00:00.000Z',endsAt:'2026-10-31T03:00:00.000Z'};
test('five-day period stops processing and sending exactly at expiry and remains stopped after restart or settings toggle',t=>{
const f=fixture(t);f.store.put('stability-test',period);f.setNow('2026-10-31T02:59:59.999Z');assert.equal(f.store.settings().enabled,true);assert.equal(f.store.settings().processingEnabled,true);
f.setNow(period.endsAt);assert.equal(f.store.settings().enabled,false);assert.equal(f.store.settings().processingEnabled,false);assert.equal(f.store.get('settings','main').enabled,true);
f.restart();assert.equal(f.store.settings().enabled,false);f.store.put('settings',{...f.store.get('settings','main'),enabled:true,processingEnabled:true});assert.equal(f.store.settings().enabled,false);
assert.equal(Object.hasOwn(f.store.settings(),'testEndsAt'),false);
});
test('expired restricted run stays present but disabled and blocks event rule and batch paths',t=>{
const f=fixture(t),run={id:'restricted',enabled:true,version:1,rules:[],adapterHashes:{},since:period.startedAt};f.store.put('restricted-test',{id:'main',value:run});f.store.put('stability-test',period);f.setNow(period.endsAt);
assert.deepEqual(restricted.state(f.store),{...run,enabled:false});assert.equal(f.store.get('restricted-test','main').value.enabled,true);
assert.throws(()=>restricted.assertEvent(f.store,{source:'todo',receivedAt:period.startedAt}),{status:409});assert.throws(()=>restricted.assertRule(f.store,{}),{status:409});assert.throws(()=>restricted.assertBatch(f.store,{ruleSnapshot:{}},{}),{status:409});
assert.deepEqual(f.store.restrictedTest().period,{startedAt:period.startedAt,endsAt:period.endsAt,expired:true});
});
for(const malformed of ['',null,'invalid','2026-10-25T03:00:00.000Z'])test(`invalid enabled period fails closed (${malformed})`,t=>{const f=fixture(t);f.store.put('stability-test',{...period,endsAt:malformed});assert.equal(f.store.settings().enabled,false);assert.equal(f.store.settings().processingEnabled,false);});
test('missing or disabled period preserves existing behavior',t=>{const f=fixture(t);assert.equal(f.store.settings().enabled,true);f.store.put('stability-test',{...period,enabled:false});f.setNow('2027-01-01T00:00:00Z');assert.equal(f.store.settings().enabled,true);});
test('expiry during SMTP verification prevents even manual send before message submission',async t=>{
const f=fixture(t);f.store.put('stability-test',period);let verified=0,sent=0,begun=0;const finished=[];
f.store.capabilities={liveTodo:true,pdfExport:true,automaticArchive:true};f.store.beginSending=()=>{begun++;};f.store.finishSending=(id,status)=>finished.push(status);
const mail={store:{channelSecret:()=>({id:'mail',version:1,email:'isolated@example.test',smtp:{}}),guard:()=>{}},smtpFactory:()=>({verify:async()=>{verified++;f.setNow(period.endsAt);},sendMail:async()=>{sent++;return {accepted:['2639266652@qq.com']};},close:()=>{}})};
const {OrderRuleDispatcher}=require('../../src/modules/order-rules/dispatcher.cjs');
const dispatcher=new OrderRuleDispatcher({store:f.store,mail,clock:()=>new Date(f.store.now()),providersReady:()=>true});
await dispatcher.dispatch({id:'isolated',status:'prepared',channelId:'mail',to:['2639266652@qq.com'],cc:[],attachments:[],ruleSnapshot:{},orderIds:[]},{enabled:true,channelId:'mail'},{manual:true});
assert.equal(verified,1);assert.equal(sent,0);assert.equal(begun,0);assert.deepEqual(finished,['failed']);
});