Files
CRM_Montzar/apps/api/test/message-automation.test.ts
T

75 lines
2.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import {
MAX_MESSAGE_REPEAT,
addDelay,
applyMessageTemplate,
buildSendAts,
cancelTriggersFor,
clipSms,
customerPhone,
delayMs,
} from '../src/message-automations/message-automation.util';
describe('اتوماسیون پیام', () => {
it('تأخیرها', () => {
assert.equal(delayMs(5, 'minutes'), 300_000);
assert.equal(delayMs(2, 'hours'), 7_200_000);
assert.equal(delayMs(-3, 'days'), 0);
assert.equal(delayMs(1, 'weeks'), 7 * 86_400_000);
const d = addDelay(new Date(2026, 0, 31), 1, 'months');
assert.equal(d.getMonth(), 2); // ۳۱ ژانویه + ۱ ماه در جاوااسکریپت = ۳ مارس
});
it('زمان‌های ارسال تکراری', () => {
const now = new Date('2026-10-08T00:00:00Z');
const times = buildSendAts(now, 1, 'hours', 3, 1, 'days');
assert.deepEqual(
times.map((t) => t.toISOString()),
['2026-10-08T01:00:00.000Z', '2026-10-09T01:00:00.000Z', '2026-10-10T01:00:00.000Z'],
);
assert.equal(buildSendAts(now, 0, 'minutes', 999, 1, 'days').length, MAX_MESSAGE_REPEAT);
assert.equal(buildSendAts(now, 0, 'minutes', 0, 1, 'days').length, 1);
});
it('جایگذاری متغیرهای پیام', () => {
assert.equal(
applyMessageTemplate('سلام {نام} از {شرکت} - {unknown}', { name: 'علی', company: 'منتکت' }),
'سلام علی از منتکت - {unknown}',
);
assert.equal(applyMessageTemplate('{ name }', { name: 'x' }), 'x');
assert.equal(
applyMessageTemplate('از طرف {از طرف}', { from: 'هنراندیشه' }),
'از طرف هنراندیشه',
);
});
it('متن پیامک بریده نمی‌شود', () => {
const long = 'ا'.repeat(200);
assert.equal(clipSms(long), long);
assert.equal(clipSms(' کوتاه '), 'کوتاه');
});
it('شماره سرنخ جای شماره مشتری می‌نشیند', () => {
assert.equal(
customerPhone(null, { mobile: '09120000000' }),
'09120000000',
);
assert.equal(
customerPhone({ mobile: '', phone: null }, { mobile: '09121111111', phone: '021' }),
'09121111111',
);
assert.equal(
customerPhone({ mobile: '09123333333' }, { mobile: '09124444444' }),
'09123333333',
);
assert.equal(customerPhone(null, null), '');
});
it('لغو پیام‌های قبلی', () => {
assert.deepEqual(cancelTriggersFor('invoice_paid'), ['invoice_issued']);
assert.deepEqual(cancelTriggersFor('deal_won'), ['deal_created', 'deal_stage_changed']);
assert.deepEqual(cancelTriggersFor('person_created'), []);
});
});