Files
memind/notification-dispatcher.test.mjs
T
john 1ca06e58e7
Memind CI / Test, build, and release guards (push) Successful in 8m42s
fix(scheduled-task): deliver verified WeChat links only after page is ready
Wait for HTML materialization before releasing delivery contracts, pass
verified MindSpace URLs through proactive WeChat sends, resend on reconcile
when pages land late, and report deferred customer-service delivery as unsent.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-17 22:07:41 +08:00

147 lines
3.7 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,
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,
},
},
]);
});