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

57 lines
2.1 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,
MAX_MESSAGE_SMS,
addDelay,
applyMessageTemplate,
buildSendAts,
cancelTriggersFor,
clipSms,
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');
});
it('کوتاه کردن پیامک', () => {
const long = 'ا'.repeat(200);
const clipped = clipSms(long);
assert.equal(clipped.length, MAX_MESSAGE_SMS);
assert.ok(clipped.endsWith('…'));
assert.equal(clipSms(' کوتاه '), 'کوتاه');
});
it('لغو پیام‌های قبلی', () => {
assert.deepEqual(cancelTriggersFor('invoice_paid'), ['invoice_issued']);
assert.deepEqual(cancelTriggersFor('deal_won'), ['deal_created', 'deal_stage_changed']);
assert.deepEqual(cancelTriggersFor('person_created'), []);
});
});