Files
ameiba_cn_new/cms/server/test/wecom-notifier.test.js
T

59 lines
2.0 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { formatContactLeadMessage, sendContactLeadToWeCom } from '../src/wecom-notifier.js';
test('咨询消息包含完整字段并清理换行', () => {
const message = formatContactLeadMessage({
name: '张三\n测试',
company: '',
phone: '13800138000',
need: '希望\r\n了解企业进化方案',
createdAt: '2026-09-30T07:30:00.000Z'
});
assert.match(message, /【官网新咨询】/);
assert.match(message, /姓名:张三 测试/);
assert.match(message, /企业:未填写/);
assert.match(message, /联系电话:13800138000/);
assert.match(message, /需求描述:希望 了解企业进化方案/);
assert.match(message, /提交时间:2026\/9\/30 15:30:00/);
});
test('未配置 Webhook 时安全跳过', async () => {
const result = await sendContactLeadToWeCom({}, { webhookUrl: '' });
assert.deepEqual(result, { skipped: true });
});
test('按企业微信文本消息格式发送', async () => {
let request;
const result = await sendContactLeadToWeCom(
{ name: '李四', phone: '13900139000' },
{
webhookUrl: 'https://example.test/webhook',
fetchImpl: async (url, options) => {
request = { url, options };
return { ok: true, json: async () => ({ errcode: 0, errmsg: 'ok' }) };
}
}
);
assert.deepEqual(result, { sent: true });
assert.equal(request.url, 'https://example.test/webhook');
const body = JSON.parse(request.options.body);
assert.equal(body.msgtype, 'text');
assert.match(body.text.content, /姓名:李四/);
});
test('企业微信返回错误时抛出异常', async () => {
await assert.rejects(
sendContactLeadToWeCom(
{ name: '王五', phone: '13700137000' },
{
webhookUrl: 'https://example.test/webhook',
fetchImpl: async () => ({ ok: true, json: async () => ({ errcode: 93000, errmsg: 'invalid webhook' }) })
}
),
/invalid webhook/
);
});