feat: 完成全系统30个功能点开发与E2E全链路验收 (8轮调度32个子代理闭环)
- 模块一: 出海案例作品集、博客动态发布、人名发音语音条、RTL阿语多语言切换 - 模块二: AI顾问人设配置、行业术语库润色、RAG切片沙盒调试、流式会话与意向留资卡片 - 模块三: 全球访客雷达大屏、多维加权意向算分、外部CRM对接推送、团队公海池流转 - 模块四: 移动端NFC安全挂失、NFC动态路由规则配置、动态防伪验签、展会制卡Zip批量导出 - 模块五: 组织架构与名片分配、移动端个人中心设置、离职交接与历史线索迁移、安全审计日志 - 模块六: Stripe国际信用卡订阅Webhook、独立二级域名与SSL检测、坐席配额预警、多币种发票 - 模块七: 纸质名片OCR解析入库、国际商务日程RFC 5545 ICS日历、微官网离线SWR预加载、展会实时排行榜大屏 - 模块八: 全球出海SEO OpenGraph元标签注入与社交爬虫探测、30点端到端全链路业务流集成测试套件
这个提交包含在:
202
backend/test/modules/ai/chat.test.ts
普通文件
202
backend/test/modules/ai/chat.test.ts
普通文件
@@ -0,0 +1,202 @@
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import { CONTROLLER_KEY, getClassMetadata } from '@midwayjs/core';
|
||||
import { BaseController, CoolCommException, CurdOption } from '@cool-midway/core';
|
||||
import { AiChatService } from '../../../src/modules/ai/service/chat';
|
||||
import {
|
||||
OpenAiChatController,
|
||||
openAiChatCurdOption,
|
||||
} from '../../../src/modules/ai/controller/open/chat';
|
||||
import { AiChatRequest } from '../../../src/comm/types/omni';
|
||||
|
||||
/**
|
||||
* CoolController 元数据(脚本路径解析为异步)
|
||||
*/
|
||||
interface CoolControllerMeta {
|
||||
prefix?: string;
|
||||
curdOption?: CurdOption;
|
||||
}
|
||||
|
||||
/**
|
||||
* 等待 CoolController 将 CRUD 配置写入类元数据
|
||||
*/
|
||||
async function waitCoolMeta(
|
||||
target: new (...args: never[]) => unknown
|
||||
): Promise<CoolControllerMeta> {
|
||||
const started = Date.now();
|
||||
while (Date.now() - started < 2000) {
|
||||
const meta = getClassMetadata(CONTROLLER_KEY, target) as
|
||||
| CoolControllerMeta
|
||||
| undefined;
|
||||
if (meta?.curdOption) {
|
||||
return meta;
|
||||
}
|
||||
await new Promise<void>(resolve => setImmediate(resolve));
|
||||
}
|
||||
throw new Error(`${target.name} 的 CoolController 元数据未就绪`);
|
||||
}
|
||||
|
||||
function serviceApiMethods(apis: CurdOption['serviceApis'] = []): string[] {
|
||||
return apis.map(api => (typeof api === 'string' ? api : api.method));
|
||||
}
|
||||
|
||||
function userAsk(
|
||||
content: string,
|
||||
extra?: Partial<AiChatRequest>
|
||||
): AiChatRequest {
|
||||
return {
|
||||
cardId: extra?.cardId ?? 101,
|
||||
locale: extra?.locale,
|
||||
messages: extra?.messages ?? [{ role: 'user', content }],
|
||||
};
|
||||
}
|
||||
|
||||
describe('T36: 名片 AI 顾问对话 (modules/ai chat)', () => {
|
||||
describe('1. AiChatService.ask 入参校验', () => {
|
||||
let service: AiChatService;
|
||||
|
||||
beforeEach(() => {
|
||||
service = new AiChatService();
|
||||
});
|
||||
|
||||
it('缺 cardId 应抛 CoolCommException', async () => {
|
||||
await expect(
|
||||
service.ask({
|
||||
cardId: '' as unknown as number,
|
||||
messages: [{ role: 'user', content: 'hello' }],
|
||||
})
|
||||
).rejects.toThrow(CoolCommException);
|
||||
|
||||
await expect(
|
||||
service.ask({
|
||||
messages: [{ role: 'user', content: 'hello' }],
|
||||
} as AiChatRequest)
|
||||
).rejects.toThrow(CoolCommException);
|
||||
});
|
||||
|
||||
it('空 messages 应抛 CoolCommException', async () => {
|
||||
await expect(
|
||||
service.ask({ cardId: 101, messages: [] })
|
||||
).rejects.toThrow(CoolCommException);
|
||||
|
||||
await expect(
|
||||
service.ask({ cardId: 101, messages: undefined as unknown as [] })
|
||||
).rejects.toThrow(CoolCommException);
|
||||
});
|
||||
|
||||
it('最后一条非 user 或内容为空应抛 CoolCommException', async () => {
|
||||
await expect(
|
||||
service.ask({
|
||||
cardId: 101,
|
||||
messages: [{ role: 'assistant', content: 'hi' }],
|
||||
})
|
||||
).rejects.toThrow(CoolCommException);
|
||||
|
||||
await expect(
|
||||
service.ask({
|
||||
cardId: 101,
|
||||
messages: [{ role: 'user', content: ' ' }],
|
||||
})
|
||||
).rejects.toThrow(CoolCommException);
|
||||
});
|
||||
});
|
||||
|
||||
describe('2. RuleBased 回复与留资意图', () => {
|
||||
let service: AiChatService;
|
||||
|
||||
beforeEach(() => {
|
||||
service = new AiChatService();
|
||||
});
|
||||
|
||||
it('价格问题 shouldCollectLead=true,并引导留资', async () => {
|
||||
const result = await service.ask(userAsk('What is the price for a partnership?'));
|
||||
expect(result.shouldCollectLead).toBe(true);
|
||||
expect(result.reply).toBeTruthy();
|
||||
expect(result.locale).toBe('en-US');
|
||||
});
|
||||
|
||||
it('合作 / 联系问题 shouldCollectLead=true', async () => {
|
||||
const coop = await service.ask(userAsk('想谈合作'));
|
||||
expect(coop.shouldCollectLead).toBe(true);
|
||||
|
||||
const contact = await service.ask(
|
||||
userAsk('How can I contact you?', { locale: 'en-GB' })
|
||||
);
|
||||
expect(contact.shouldCollectLead).toBe(true);
|
||||
expect(contact.locale).toBe('en-GB');
|
||||
});
|
||||
|
||||
it('普通问候 shouldCollectLead=false 且 reply 非空', async () => {
|
||||
const result = await service.ask(userAsk('hello'));
|
||||
expect(result.shouldCollectLead).toBe(false);
|
||||
expect(result.reply).toBeTruthy();
|
||||
expect(result.reply.trim().length).toBeGreaterThan(0);
|
||||
expect(result.locale).toBe('en-US');
|
||||
});
|
||||
|
||||
it('locale 缺省为 en-US,英文回复不含中文', async () => {
|
||||
const result = await service.ask(userAsk('hi there'));
|
||||
expect(result.locale).toBe('en-US');
|
||||
expect(/[\u4e00-\u9fff]/.test(result.reply)).toBe(false);
|
||||
});
|
||||
|
||||
it('zh-CN 回复含中文', async () => {
|
||||
const result = await service.ask(userAsk('你好', { locale: 'zh-CN' }));
|
||||
expect(result.locale).toBe('zh-CN');
|
||||
expect(result.shouldCollectLead).toBe(false);
|
||||
expect(/[\u4e00-\u9fff]/.test(result.reply)).toBe(true);
|
||||
});
|
||||
|
||||
it('兼容 serviceApis 单对象入参', async () => {
|
||||
const body: AiChatRequest = {
|
||||
cardId: 88,
|
||||
locale: 'zh-CN',
|
||||
messages: [
|
||||
{ role: 'system', content: 'concierge' },
|
||||
{ role: 'user', content: '价格多少' },
|
||||
],
|
||||
};
|
||||
const result = await service.ask(body);
|
||||
expect(result.shouldCollectLead).toBe(true);
|
||||
expect(/[\u4e00-\u9fff]/.test(result.reply)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('3. OpenAiChatController /open/ai/chat', () => {
|
||||
const controllerFile = path.join(
|
||||
__dirname,
|
||||
'../../../src/modules/ai/controller/open/chat.ts'
|
||||
);
|
||||
|
||||
let meta: CoolControllerMeta;
|
||||
let option: CurdOption;
|
||||
|
||||
beforeAll(async () => {
|
||||
meta = await waitCoolMeta(OpenAiChatController);
|
||||
option = meta.curdOption as CurdOption;
|
||||
});
|
||||
|
||||
it('文件路径应符合 /open/ai/chat 自动路由约定', () => {
|
||||
expect(fs.existsSync(controllerFile)).toBe(true);
|
||||
expect(controllerFile.replace(/\\/g, '/')).toMatch(
|
||||
/modules\/ai\/controller\/open\/chat\.ts$/
|
||||
);
|
||||
});
|
||||
|
||||
it('路由前缀应为 /open/ai/chat', () => {
|
||||
expect(meta.prefix).toBe('/open/ai/chat');
|
||||
});
|
||||
|
||||
it('控制器应继承 BaseController,绑定 AiChatService,不开 CRUD', () => {
|
||||
expect(OpenAiChatController.prototype).toBeInstanceOf(BaseController);
|
||||
expect(option.service).toBe(AiChatService);
|
||||
expect(openAiChatCurdOption.service).toBe(AiChatService);
|
||||
expect(option.api || []).toEqual([]);
|
||||
});
|
||||
|
||||
it('serviceApis 应包含 ask', () => {
|
||||
const methods = serviceApiMethods(option.serviceApis);
|
||||
expect(methods).toContain('ask');
|
||||
});
|
||||
});
|
||||
});
|
||||
在新工单中引用
屏蔽一个用户