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', needs: ['IT数字化部署', '其他个性需求'], otherNeed: '希望\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, /核心诉求:\n• IT数字化部署\n• 其他个性需求/); 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/ ); });