Files
CRM_Montzar/apps/api/src/message-automations/__tests__/message-automation.check.ts
T

79 lines
2.2 KiB
TypeScript
Raw Normal View History

2026-09-13 19:11:47 +03:30
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');
2026-09-13 21:22:41 +03:30
assert(delayMs(1, 'weeks') === 7 * 86_400_000, '1 week');
2026-09-13 19:11:47 +03:30
}
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(
2026-09-14 16:49:43 +03:30
applyMessageTemplate('سلام {title}', { title: 'فاکتور ۱۲' }) === 'سلام فاکتور ۱۲',
2026-09-13 19:11:47 +03:30
'title var',
);
2026-09-14 16:49:43 +03:30
assert(
applyMessageTemplate('سلام {نام} {شماره}', {
name: 'علی',
phone: '0912',
}) === 'سلام علی 0912',
'name phone vars',
);
assert(
applyMessageTemplate('تبریک {از طرف}', { from: 'هنراندیشه' }) ===
'تبریک هنراندیشه',
'from org var',
);
assert(
applyMessageTemplate('مانده {مانده ریالی} / {مانده فلزی}', {
rialBalance: '۱۰۰۰ بس',
metalBalance: 'طلا ۲',
}) === 'مانده ۱۰۰۰ بس / طلا ۲',
'balance vars',
);
assert(clipSms('ا'.repeat(200)).length === 200, 'sms is not clipped');
2026-09-13 19:11:47 +03:30
}
testDelay();
testSendAts();
testCancelMap();
testTemplate();
console.log('message-automation checks passed');