Adding SMS automation
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import {
|
||||
applyMessageTemplate,
|
||||
buildSendAts,
|
||||
cancelTriggersFor,
|
||||
clipSms,
|
||||
delayMs,
|
||||
} from '../message-automation.util';
|
||||
|
||||
function assert(cond: unknown, msg: string): asserts cond {
|
||||
if (!cond) throw new Error(msg);
|
||||
}
|
||||
|
||||
function testDelay() {
|
||||
assert(delayMs(0, 'minutes') === 0, 'zero delay');
|
||||
assert(delayMs(2, 'hours') === 2 * 3_600_000, '2 hours');
|
||||
assert(delayMs(2, 'days') === 2 * 86_400_000, '2 days');
|
||||
}
|
||||
|
||||
function testSendAts() {
|
||||
const now = new Date('2026-09-13T12:00:00.000Z');
|
||||
const once = buildSendAts(now, 0, 'minutes', 1, 2, 'days');
|
||||
assert(once.length === 1, 'one send');
|
||||
assert(once[0].getTime() === now.getTime(), 'immediate first');
|
||||
|
||||
const three = buildSendAts(now, 1, 'hours', 3, 2, 'days');
|
||||
assert(three.length === 3, 'three sends');
|
||||
assert(three[0].getTime() === now.getTime() + 3_600_000, 'start + 1h');
|
||||
assert(
|
||||
three[1].getTime() === three[0].getTime() + 2 * 86_400_000,
|
||||
'second + 2 days',
|
||||
);
|
||||
assert(
|
||||
three[2].getTime() === three[0].getTime() + 4 * 86_400_000,
|
||||
'third + 4 days',
|
||||
);
|
||||
}
|
||||
|
||||
function testCancelMap() {
|
||||
assert(
|
||||
cancelTriggersFor('invoice_paid').includes('invoice_issued'),
|
||||
'paid cancels issued',
|
||||
);
|
||||
assert(cancelTriggersFor('person_created').length === 0, 'no cancel on create');
|
||||
}
|
||||
|
||||
function testTemplate() {
|
||||
assert(
|
||||
applyMessageTemplate('سلام {title}', 'فاکتور ۱۲') === 'سلام فاکتور ۱۲',
|
||||
'title var',
|
||||
);
|
||||
assert(clipSms('الف'.repeat(200)).length === 160, 'sms clip');
|
||||
}
|
||||
|
||||
testDelay();
|
||||
testSendAts();
|
||||
testCancelMap();
|
||||
testTemplate();
|
||||
console.log('message-automation checks passed');
|
||||
Reference in New Issue
Block a user