文件
OmniCard/backend/test/modules/ai/copilot.test.ts
wikiboss eec259a3a7 feat: 完成全系统30个功能点开发与E2E全链路验收 (8轮调度32个子代理闭环)
- 模块一: 出海案例作品集、博客动态发布、人名发音语音条、RTL阿语多语言切换
- 模块二: AI顾问人设配置、行业术语库润色、RAG切片沙盒调试、流式会话与意向留资卡片
- 模块三: 全球访客雷达大屏、多维加权意向算分、外部CRM对接推送、团队公海池流转
- 模块四: 移动端NFC安全挂失、NFC动态路由规则配置、动态防伪验签、展会制卡Zip批量导出
- 模块五: 组织架构与名片分配、移动端个人中心设置、离职交接与历史线索迁移、安全审计日志
- 模块六: Stripe国际信用卡订阅Webhook、独立二级域名与SSL检测、坐席配额预警、多币种发票
- 模块七: 纸质名片OCR解析入库、国际商务日程RFC 5545 ICS日历、微官网离线SWR预加载、展会实时排行榜大屏
- 模块八: 全球出海SEO OpenGraph元标签注入与社交爬虫探测、30点端到端全链路业务流集成测试套件
2026-09-20 23:47:47 +08:00

192 行
6.2 KiB
TypeScript

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 { AiCopilotService } from '../../../src/modules/ai/service/copilot';
import {
AdminAiCopilotController,
adminAiCopilotCurdOption,
} from '../../../src/modules/ai/controller/admin/copilot';
import { CopilotRequest } from '../../../src/comm/types/omni';
/**
* CoolController 元数据(脚本路径解析为异步)
*/
interface CoolControllerMeta {
prefix?: string;
curdOption?: CurdOption;
}
/**
* generate 入参:契约 + 可选意向分
*/
type CopilotGenerateInput = CopilotRequest & {
intentScore?: number;
};
/**
* 等待 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));
}
describe('T40: 销售跟进话术生成 (modules/ai copilot)', () => {
describe('1. AiCopilotService.generate 入参校验', () => {
let service: AiCopilotService;
beforeEach(() => {
service = new AiCopilotService();
});
it('无名且无 leadId 应抛 CoolCommException', async () => {
await expect(service.generate({} as CopilotRequest)).rejects.toThrow(
CoolCommException
);
await expect(
service.generate({ name: ' ', locale: 'en-US' })
).rejects.toThrow(CoolCommException);
await expect(
service.generate({ leadId: '', name: '' })
).rejects.toThrow(CoolCommException);
});
it('仅有 leadId 无库时可生成,脚本非空', async () => {
const result = await service.generate({ leadId: 12 });
expect(result.script.trim().length).toBeGreaterThan(0);
expect(result.channel).toBe('email');
expect(result.locale).toBe('en-US');
});
});
describe('2. 渠道模板与意向诊断', () => {
let service: AiCopilotService;
beforeEach(() => {
service = new AiCopilotService();
});
it('email / whatsapp 脚本不同', async () => {
const req: CopilotGenerateInput = { name: 'Ada Chen' };
const email = await service.generate({ ...req, channel: 'email' });
const whatsapp = await service.generate({ ...req, channel: 'whatsapp' });
expect(email.channel).toBe('email');
expect(whatsapp.channel).toBe('whatsapp');
expect(email.script).toBeTruthy();
expect(whatsapp.script).toBeTruthy();
expect(email.script).not.toBe(whatsapp.script);
expect(email.script).toContain('Ada Chen');
expect(whatsapp.script).toContain('Ada Chen');
});
it('高意向 diagnosis 不同', async () => {
const low = await service.generate({
name: 'Ada Chen',
intentScore: 10,
} as CopilotGenerateInput);
const high = await service.generate({
name: 'Ada Chen',
intentScore: 90,
} as CopilotGenerateInput);
expect(low.diagnosis).toBeTruthy();
expect(high.diagnosis).toBeTruthy();
expect(high.diagnosis).not.toBe(low.diagnosis);
});
it('缺省 channel=email、locale=en-US,英文不含中文', async () => {
const result = await service.generate({ name: 'Ada Chen' });
expect(result.channel).toBe('email');
expect(result.locale).toBe('en-US');
expect(/[\u4e00-\u9fff]/.test(result.script)).toBe(false);
expect(/[\u4e00-\u9fff]/.test(result.diagnosis)).toBe(false);
});
it('zh-CN 含中文', async () => {
const result = await service.generate({
name: '陈安',
locale: 'zh-CN',
channel: 'email',
});
expect(result.locale).toBe('zh-CN');
expect(/[\u4e00-\u9fff]/.test(result.script)).toBe(true);
expect(/[\u4e00-\u9fff]/.test(result.diagnosis)).toBe(true);
expect(result.script).toContain('陈安');
});
it('兼容 serviceApis 单对象入参且脚本非空', async () => {
const body: CopilotGenerateInput = {
name: 'Ada Chen',
locale: 'zh-CN',
channel: 'whatsapp',
intentScore: 80,
};
const result = await service.generate(body);
expect(result.channel).toBe('whatsapp');
expect(result.locale).toBe('zh-CN');
expect(result.script.trim().length).toBeGreaterThan(0);
expect(/[\u4e00-\u9fff]/.test(result.script)).toBe(true);
});
});
describe('3. AdminAiCopilotController /admin/ai/copilot', () => {
const controllerFile = path.join(
__dirname,
'../../../src/modules/ai/controller/admin/copilot.ts'
);
let meta: CoolControllerMeta;
let option: CurdOption;
beforeAll(async () => {
meta = await waitCoolMeta(AdminAiCopilotController);
option = meta.curdOption as CurdOption;
});
it('文件路径应符合 /admin/ai/copilot 自动路由约定', () => {
expect(fs.existsSync(controllerFile)).toBe(true);
expect(controllerFile.replace(/\\/g, '/')).toMatch(
/modules\/ai\/controller\/admin\/copilot\.ts$/
);
});
it('路由前缀应为 /admin/ai/copilot', () => {
expect(meta.prefix).toBe('/admin/ai/copilot');
});
it('控制器应继承 BaseController,绑定 AiCopilotService,不开 CRUD', () => {
expect(AdminAiCopilotController.prototype).toBeInstanceOf(BaseController);
expect(option.service).toBe(AiCopilotService);
expect(adminAiCopilotCurdOption.service).toBe(AiCopilotService);
expect(option.api || []).toEqual([]);
});
it('serviceApis 应包含 generate', () => {
const methods = serviceApiMethods(option.serviceApis);
expect(methods).toContain('generate');
expect(serviceApiMethods(adminAiCopilotCurdOption.serviceApis)).toContain(
'generate'
);
});
});
});