Files
memind/notification-dispatcher.test.mjs
T
john d820247aa1
Memind CI / Test, build, and release guards (push) Successful in 3m32s
fix(schedule): retry reminders when WeChat delivery is deferred
Treat deferred/skipped customer-service sends as failures so reminder worker
does not mark reminders sent, and add a 103 read-only health check script.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-16 21:30:53 +08:00

161 lines
4.1 KiB
JavaScript

import assert from 'node:assert/strict';
import test from 'node:test';
import {
createNotificationDispatcher,
deliverWechatScheduleNotification,
WECHAT_SCHEDULE_NOTIFICATION_NOT_SENT,
} 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,
verifiedHtmlCount: 0,
},
},
]);
});
test('notification dispatcher returns false when wechat sender defers delivery', async () => {
const dispatcher = createNotificationDispatcher({
async sendWechatTextToUser() {
return { sent: false, deferred: true, errcode: 45015 };
},
logger: { info() {} },
});
assert.equal(
await dispatcher.sendScheduleNotification({
userId: 'user-4',
text: '定时任务完成',
verifiedHtmlUrls: ['https://m.tkmind.cn/MindSpace/user-4/public/news.html'],
}),
false,
);
});
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,
verifiedHtmlCount: 0,
},
},
]);
});
test('deliverWechatScheduleNotification throws when sender defers', async () => {
await assert.rejects(
() => deliverWechatScheduleNotification(
async () => ({ sent: false, deferred: true, errcode: 45015 }),
{ userId: 'user-5', text: '提醒' },
),
(err) => err.code === WECHAT_SCHEDULE_NOTIFICATION_NOT_SENT,
);
});