完善咨询表单与企业微信诉求推送

This commit is contained in:
Rei
2026-09-30 18:23:01 +08:00
parent f35ee24510
commit 530f726130
8 changed files with 448 additions and 70 deletions
+52
View File
@@ -0,0 +1,52 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import {
ContactLeadValidationError,
normalizeContactLeadInput
} from '../src/contact-leads.js';
test('规范化有效咨询表单并允许称呼和企业名为空', () => {
const result = normalizeContactLeadInput({
phone: '13800138000',
needs: ['IT数字化部署', 'AI智能体部署']
});
assert.equal(result.name, '');
assert.equal(result.company, '');
assert.equal(result.phone, '13800138000');
assert.equal(result.need, 'IT数字化部署;AI智能体部署');
});
test('拒绝不合法的中国大陆手机号', () => {
for (const phone of ['12800138000', '1380013800', '23800138000', '138001380000']) {
assert.throws(
() => normalizeContactLeadInput({ phone, needs: ['经营体系建设'] }),
ContactLeadValidationError
);
}
});
test('核心诉求至少选择一项且只接受预设选项', () => {
assert.throws(
() => normalizeContactLeadInput({ phone: '13800138000', needs: [] }),
/至少选择一项/
);
assert.throws(
() => normalizeContactLeadInput({ phone: '13800138000', needs: ['未知选项'] }),
/无效选项/
);
});
test('选择其他个性需求时必须填写说明', () => {
assert.throws(
() => normalizeContactLeadInput({ phone: '13800138000', needs: ['其他个性需求'] }),
/填写其他个性需求/
);
const result = normalizeContactLeadInput({
phone: '13800138000',
needs: ['其他个性需求'],
otherNeed: '希望先进行现场访谈'
});
assert.equal(result.need, '其他个性需求:希望先进行现场访谈');
});
+7 -5
View File
@@ -7,15 +7,17 @@ test('咨询消息包含完整字段并清理换行', () => {
name: '张三\n测试',
company: '',
phone: '13800138000',
need: '希望\r\n了解企业进化方案',
needs: ['IT数字化部署', '其他个性需求'],
otherNeed: '希望\r\n了解企业进化方案',
createdAt: '2026-09-30T07:30:00.000Z'
});
assert.match(message, /【官网新咨询】/);
assert.match(message, /姓名:张三 测试/);
assert.match(message, /企业:未填写/);
assert.match(message, /您的称呼:张三 测试/);
assert.match(message, /企业名:未填写/);
assert.match(message, /联系电话:13800138000/);
assert.match(message, /需求描述:希望 了解企业进化方案/);
assert.match(message, /核心诉求:\n• IT数字化部署\n• 其他个性需求/);
assert.match(message, /说明:希望 了解企业进化方案/);
assert.match(message, /提交时间:2026\/9\/30 15:30:00/);
});
@@ -41,7 +43,7 @@ test('按企业微信文本消息格式发送', async () => {
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, /姓名:李四/);
assert.match(body.text.content, /您的称呼:李四/);
});
test('企业微信返回错误时抛出异常', async () => {