119 lines
4.3 KiB
JavaScript
119 lines
4.3 KiB
JavaScript
'use strict';
|
|
|
|
const { describe, it } = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const {
|
|
SMS_TEMPLATE_DEFS,
|
|
normalizeStoredEntry,
|
|
resolveVariablesList,
|
|
parseIncomingVariables,
|
|
mergeTemplateEntry
|
|
} = require('./smsTemplates');
|
|
|
|
const invoiceDef = SMS_TEMPLATE_DEFS.find((def) => def.key === 'invoiceCreated');
|
|
|
|
const threeInvoiceVars = [
|
|
{ slot: 'amount', name: 'PAYMENT_PRICE' },
|
|
{ slot: 'fullName', name: 'FULLNAME' },
|
|
{ slot: 'course', name: 'COURSE' }
|
|
];
|
|
|
|
describe('SMS template variables', () => {
|
|
it('keeps all invoiceCreated variables when stored as an array', () => {
|
|
const stored = normalizeStoredEntry({
|
|
templateId: '111',
|
|
variables: threeInvoiceVars
|
|
}, invoiceDef);
|
|
const resolved = resolveVariablesList(invoiceDef, stored.variables);
|
|
|
|
assert.equal(resolved.length, 3);
|
|
assert.deepEqual(resolved.map((item) => item.slot), ['amount', 'fullName', 'course']);
|
|
assert.deepEqual(resolved.map((item) => item.name), ['PAYMENT_PRICE', 'FULLNAME', 'COURSE']);
|
|
});
|
|
|
|
it('keeps all invoiceCreated variables when an array was stored as a numeric object', () => {
|
|
const stored = normalizeStoredEntry({
|
|
templateId: '111',
|
|
variables: {
|
|
0: { slot: 'amount', name: 'PAYMENT_PRICE' },
|
|
1: { slot: 'fullName', name: 'FULLNAME' },
|
|
2: { slot: 'course', name: 'COURSE' }
|
|
}
|
|
}, invoiceDef);
|
|
const resolved = resolveVariablesList(invoiceDef, stored.variables);
|
|
|
|
assert.equal(resolved.length, 3);
|
|
assert.deepEqual(resolved.map((item) => item.slot), ['amount', 'fullName', 'course']);
|
|
});
|
|
|
|
it('replaces a legacy single-slot map with every incoming invoiceCreated variable', () => {
|
|
const incoming = parseIncomingVariables(threeInvoiceVars, invoiceDef);
|
|
const merged = mergeTemplateEntry(
|
|
invoiceDef,
|
|
{ templateId: '111', variables: { amount: 'PAYMENT_PRICE' } },
|
|
{ templateId: '111', variables: incoming }
|
|
);
|
|
|
|
assert.equal(merged.variables.length, 3);
|
|
assert.deepEqual(merged.variables.map((item) => item.slot), ['amount', 'fullName', 'course']);
|
|
assert.deepEqual(merged.variables.map((item) => item.name), ['PAYMENT_PRICE', 'FULLNAME', 'COURSE']);
|
|
});
|
|
|
|
it('exposes class start date, class days, and course time slots', () => {
|
|
const expected = ['classStartDate', 'classDays', 'courseTime'];
|
|
for (const key of ['classRegistered', 'classReminder', 'invoiceCreated']) {
|
|
const def = SMS_TEMPLATE_DEFS.find((item) => item.key === key);
|
|
const slots = (def?.slots || []).map((slot) => slot.key);
|
|
for (const slot of expected) {
|
|
assert.ok(slots.includes(slot), `${key} is missing slot ${slot}`);
|
|
}
|
|
}
|
|
});
|
|
|
|
it('keeps username and password slots on the accountCreated template', () => {
|
|
const def = SMS_TEMPLATE_DEFS.find((item) => item.key === 'accountCreated');
|
|
const slots = (def?.slots || []).map((slot) => slot.key);
|
|
assert.ok(slots.includes('username'));
|
|
assert.ok(slots.includes('password'));
|
|
});
|
|
|
|
it('exposes reason slot on the sessionCancelled template', () => {
|
|
const def = SMS_TEMPLATE_DEFS.find((item) => item.key === 'sessionCancelled');
|
|
const slots = (def?.slots || []).map((slot) => slot.key);
|
|
assert.ok(slots.includes('reason'));
|
|
});
|
|
|
|
it('defines the sessionHolding template with all expected slots', () => {
|
|
const def = SMS_TEMPLATE_DEFS.find((item) => item.key === 'sessionHolding');
|
|
assert.ok(def, 'sessionHolding template definition missing');
|
|
const slots = (def.slots || []).map((slot) => slot.key);
|
|
const expected = ['fullName', 'topic', 'className', 'sessionDate', 'classTime'];
|
|
for (const slot of expected) {
|
|
assert.ok(slots.includes(slot), `sessionHolding is missing slot ${slot}`);
|
|
}
|
|
});
|
|
|
|
it('normalizes and merges enabled property for SMS templates', () => {
|
|
const stored = normalizeStoredEntry({
|
|
enabled: false,
|
|
templateId: '123',
|
|
variables: threeInvoiceVars
|
|
}, invoiceDef);
|
|
assert.equal(stored.enabled, false);
|
|
|
|
const mergedToTrue = mergeTemplateEntry(
|
|
invoiceDef,
|
|
stored,
|
|
{ enabled: true }
|
|
);
|
|
assert.equal(mergedToTrue.enabled, true);
|
|
|
|
const mergedToFalse = mergeTemplateEntry(
|
|
invoiceDef,
|
|
mergedToTrue,
|
|
{ enabled: false }
|
|
);
|
|
assert.equal(mergedToFalse.enabled, false);
|
|
});
|
|
});
|