Files
memind/notification-dispatcher.test.mjs

127 lines
3.1 KiB
JavaScript

import assert from 'node:assert/strict';
import test from 'node:test';
import { createNotificationDispatcher } from './notification-dispatcher.mjs';
test('notification dispatcher forwards recharge success text unchanged', async () => {
const sent = [];
const dedupeKeys = [];
const logs = [];
const dispatcher = createNotificationDispatcher({
async sendWechatTextToUser(userId, text) {
sent.push({ userId, text });
},
logger: {
info(message, payload) {
logs.push({ message, payload });
},
},
});
const sentOk = await dispatcher.sendRechargeSuccess({
userId: 'user-1',
title: '充值成功',
body: '你已成功充值 ¥25.00,余额已更新。',
dedupeKey: (dedupeKeys.push('recharge:order-1'), 'recharge:order-1'),
});
assert.equal(sentOk, true);
assert.deepEqual(dedupeKeys, ['recharge:order-1']);
assert.deepEqual(sent, [
{ userId: 'user-1', text: '充值成功\n你已成功充值 ¥25.00,余额已更新。' },
]);
assert.deepEqual(logs, [
{
message: 'Notification dispatch:',
payload: {
type: 'recharge_success',
userId: 'user-1',
dedupeKey: 'recharge:order-1',
sent: true,
},
},
]);
});
test('notification dispatcher forwards schedule notification text unchanged', async () => {
const sent = [];
const logs = [];
const dispatcher = createNotificationDispatcher({
async sendWechatTextToUser(userId, text) {
sent.push({ userId, text });
},
logger: {
info(message, payload) {
logs.push({ message, payload });
},
},
});
const sentOk = await dispatcher.sendScheduleNotification({
userId: 'user-2',
text: '【待办提醒】开会\n提醒时间:09:00',
});
assert.equal(sentOk, true);
assert.deepEqual(sent, [
{ userId: 'user-2', text: '【待办提醒】开会\n提醒时间:09:00' },
]);
assert.deepEqual(logs, [
{
message: 'Notification dispatch:',
payload: {
type: 'schedule_notification',
userId: 'user-2',
dedupeKey: null,
sent: true,
},
},
]);
});
test('notification dispatcher returns false when wechat sender is unavailable', async () => {
const logs = [];
const dispatcher = createNotificationDispatcher({
logger: {
info(message, payload) {
logs.push({ message, payload });
},
},
});
assert.equal(
await dispatcher.sendRechargeSuccess({
userId: 'user-3',
title: '充值成功',
body: '余额已更新。',
}),
false,
);
assert.equal(
await dispatcher.sendScheduleNotification({
userId: 'user-3',
text: '【待办提醒】开会',
}),
false,
);
assert.deepEqual(logs, [
{
message: 'Notification dispatch:',
payload: {
type: 'recharge_success',
userId: 'user-3',
dedupeKey: null,
sent: false,
},
},
{
message: 'Notification dispatch:',
payload: {
type: 'schedule_notification',
userId: 'user-3',
dedupeKey: null,
sent: false,
},
},
]);
});