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

30 lines
2.2 KiB
JavaScript

'use strict';
const {test}=require('node:test');const assert=require('node:assert/strict');
const {windowForCutoff,upcomingWindow,inWindow}=require('../../src/modules/order-rules/window.cjs');
const schedule={times:['11:00','17:00'],timezone:'Asia/Shanghai',weekends:false};
test('normal weekday first interval continuously covers prior evening',()=>{
const morning=windowForCutoff('2026-10-20T03:00:00Z',schedule),afternoon=windowForCutoff('2026-10-20T09:00:00Z',schedule);
assert.equal(morning.start,'2026-10-19T09:00:00.000Z');assert.equal(morning.end,'2026-10-20T03:00:00.000Z');
assert.equal(afternoon.start,'2026-10-20T03:00:00.000Z');assert.equal(afternoon.end,'2026-10-20T09:00:00.000Z');
assert.equal(inWindow('2026-10-19T08:00:00Z',morning),false);
assert.equal(inWindow('2026-10-20T03:00:00Z',morning),false);assert.equal(inWindow('2026-10-20T03:00:00Z',afternoon),true);
});
test('Monday first mail covers last workday final cutoff through Monday morning, never older history',()=>{
const window=windowForCutoff('2026-10-19T03:00:00Z',schedule);
assert.equal(window.start,'2026-10-16T09:00:00.000Z');assert.equal(window.end,'2026-10-19T03:00:00.000Z');
assert.equal(inWindow('2026-10-16T08:59:59Z',window),false);assert.equal(inWindow('2026-10-17T10:00:00Z',window),true);
});
test('business calendar determines previous actual working day across a rest period',()=>{
const window=windowForCutoff('2026-10-19T03:00:00Z',schedule,[{date:'2026-10-16',kind:'restday',send:false,scan:false}]);
assert.equal(window.start,'2026-10-15T09:00:00.000Z');
assert.throws(()=>windowForCutoff('2026-10-18T03:00:00Z',schedule),e=>e.status===400);
assert.throws(()=>windowForCutoff('2026-10-20T04:00:00Z',schedule),e=>e.status===400);
});
test('future preview cannot freeze a mail before its cutoff; missed slots do not reopen old windows',()=>{
const next=upcomingWindow(new Date('2026-10-20T02:00:00Z'),schedule);
assert.equal(next.end,'2026-10-20T03:00:00.000Z');assert.equal(next.available,false);
assert.equal(upcomingWindow(new Date('2026-10-20T03:00:30Z'),schedule).available,true);
const missed=upcomingWindow(new Date('2026-10-20T03:02:00Z'),schedule);
assert.equal(missed.start,'2026-10-20T03:00:00.000Z');assert.equal(missed.available,false);
});