2026-10-08 20:11:07 +03:30
|
|
|
|
import { describe, it } from 'node:test';
|
|
|
|
|
|
import assert from 'node:assert/strict';
|
|
|
|
|
|
import {
|
|
|
|
|
|
MAX_MESSAGE_REPEAT,
|
|
|
|
|
|
addDelay,
|
|
|
|
|
|
applyMessageTemplate,
|
|
|
|
|
|
buildSendAts,
|
|
|
|
|
|
cancelTriggersFor,
|
|
|
|
|
|
clipSms,
|
2026-10-31 16:41:41 +03:30
|
|
|
|
customerPhone,
|
2026-10-08 20:11:07 +03:30
|
|
|
|
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');
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-10-31 16:41:41 +03:30
|
|
|
|
it('متن پیامک بریده نمیشود', () => {
|
2026-10-08 20:11:07 +03:30
|
|
|
|
const long = 'ا'.repeat(200);
|
2026-10-31 16:41:41 +03:30
|
|
|
|
assert.equal(clipSms(long), long);
|
2026-10-08 20:11:07 +03:30
|
|
|
|
assert.equal(clipSms(' کوتاه '), 'کوتاه');
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-10-31 16:41:41 +03:30
|
|
|
|
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), '');
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-10-08 20:11:07 +03:30
|
|
|
|
it('لغو پیامهای قبلی', () => {
|
|
|
|
|
|
assert.deepEqual(cancelTriggersFor('invoice_paid'), ['invoice_issued']);
|
|
|
|
|
|
assert.deepEqual(cancelTriggersFor('deal_won'), ['deal_created', 'deal_stage_changed']);
|
|
|
|
|
|
assert.deepEqual(cancelTriggersFor('person_created'), []);
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|