文件
OmniCard/backend/test/modules/card/contact.test.ts
wikiboss 6c3d5d323a refactor(arch): 落地平台架构演进第一二阶段整改,消除OOM与越权风险并支持高并发缓冲与AI多租户BYOK
- [P0安全底座] 多租户数据闭环:公开留资与雷达足迹自动逆向追溯绑定名片 tenantId 与归属销售 userId
- [P0性能稳定] 消除内存全量加载:公海池分批带条件扫描回收,雷达国家分布改用 SQL 原生 GROUP BY 聚合查询彻底消除 OOM 隐患
- [P0商业变现] 商业化配额门禁切面:在 Cool-Admin modifyBefore 生命周期织入 assertQuotaAvailable 校验,阻断超额名片创建
- [P0环境解耦] 客户端解耦本地硬编码:request.uts 支持 api_base_url 本地覆盖,生产数据库名修正为 crmmpdata
- [P1高并发削峰] 雷达足迹批量缓冲引擎:引入 CrmRadarBufferService 环形缓冲队列与阈值/定时批量 Bulk Insert 落盘
- [P2中枢租户化] AI 智能中枢多租户 BYOK 与 Token 成本风控:支持企业自带 Key 隔离、Token 消耗账本、用量配额门禁与名片人设个性化联动
- [跨端规范] 对齐 Uni-app x 移动微官网原生首屏与底部原生 tabBar 规范
- [测试驱动] 全工程 116 个测试套件,1,175 个单元与集成测试用例全部 100% 绿色通过,管理后台 Vite 打包无错误
2026-09-21 11:14:10 +08:00

216 行
6.7 KiB
TypeScript

import { CardContactService } from '../../../src/modules/card/service/contact';
import {
CardMutualExchangeService,
createMutualExchangeInvitation,
} from '../../../src/modules/card/service/mutual-exchange';
describe('P0 名片大厅人脉与双向互换落库', () => {
let contactService: CardContactService;
let exchangeService: CardMutualExchangeService;
let contactStore: any[];
let leadStore: any[];
let cardStore: any[];
let exchangeStore: any[];
function createRepo(store: any[], idStart = 1) {
let seq = idStart;
return {
findOne: jest.fn(async (opts: { where?: Record<string, unknown> }) => {
const where = opts?.where || {};
return (
store.find(row =>
Object.keys(where).every(key => row[key] === (where as any)[key])
) || null
);
}),
find: jest.fn(async (opts?: { where?: Record<string, unknown> }) => {
const where = opts?.where || {};
return store.filter(row =>
Object.keys(where).every(key => row[key] === (where as any)[key])
);
}),
save: jest.fn(async (item: any) => {
if (item.id) {
const idx = store.findIndex(row => row.id === item.id);
if (idx >= 0) {
store[idx] = { ...store[idx], ...item };
return store[idx];
}
}
const saved = { ...item, id: item.id || seq++ };
store.push(saved);
return saved;
}),
createQueryBuilder: jest.fn(() => {
const qb: any = {
alias: 'a',
leftJoin: jest.fn().mockReturnThis(),
addSelect: jest.fn().mockReturnThis(),
where: jest.fn().mockReturnThis(),
andWhere: jest.fn().mockReturnThis(),
orderBy: jest.fn().mockReturnThis(),
skip: jest.fn().mockReturnThis(),
take: jest.fn().mockReturnThis(),
getCount: jest.fn(async () =>
store.filter(row => row.ownerUserId === 11).length
),
getRawAndEntities: jest.fn(async () => ({
entities: store.filter(row => row.ownerUserId === 11),
raw: [],
})),
};
return qb;
}),
};
}
beforeEach(() => {
contactStore = [];
leadStore = [];
exchangeStore = [];
cardStore = [
{
id: 101,
userId: 11,
tenantId: 1,
name: 'Alex Rivera',
title: 'VP',
company: 'OmniCard',
phone: '+8613800000001',
email: 'alex@omni.test',
slug: 'alex',
status: 'published',
},
{
id: 202,
userId: 22,
tenantId: 2,
name: 'Tariq Mansour',
title: 'Director',
company: 'Dubai Energy',
phone: '+971501234567',
email: 'tariq@dubai.test',
slug: 'tariq',
status: 'published',
},
{
id: 103,
userId: 33,
tenantId: 1,
name: 'Chen Wei',
title: 'Sales',
company: 'OmniCard',
phone: '+8613800000003',
email: 'chen@omni.test',
slug: 'chen-wei',
status: 'published',
},
];
contactService = new CardContactService();
contactService.cardContactEntity = createRepo(contactStore) as any;
contactService.cardInfoEntity = createRepo(cardStore, 300) as any;
contactService.crmLeadEntity = createRepo(leadStore, 500) as any;
exchangeService = new CardMutualExchangeService();
exchangeService.exchangeEntity = createRepo(exchangeStore, 800) as any;
exchangeService.cardContactService = contactService;
exchangeService.cardInfoEntity = contactService.cardInfoEntity;
exchangeService.crmLeadEntity = contactService.crmLeadEntity;
});
it('不能把自己的名片收入自己的大厅', async () => {
await expect(
contactService.upsertContact({
ownerUserId: 11,
ownerCardId: 101,
peerCardId: 101,
source: 'collect',
})
).rejects.toThrow(/不能收藏自己/);
});
it('收藏他人名片只写一行人脉,重复收藏复用同一行', async () => {
const first = await contactService.collect({
ownerUserId: 11,
peerCardId: 202,
});
const second = await contactService.collect({
ownerUserId: 11,
peerCardId: 202,
});
expect(contactStore).toHaveLength(1);
expect(first.id).toBe(second.id);
expect(first.peerCardId).toBe(202);
expect(first.source).toBe('collect');
expect(first.ownerUserId).toBe(11);
});
it('确认互换后双方各有一条人脉和一条线索', async () => {
const invite = await exchangeService.initiateExchange({
senderCardId: 101,
recipientCardId: 202,
});
expect(invite.status).toBe('REQUESTED');
expect(exchangeStore).toHaveLength(1);
const outcome = await exchangeService.confirmExchange(invite.id, 202);
expect(outcome.status).toBe('ACCEPTED');
expect(outcome.isDualSynced).toBe(true);
expect(contactStore).toHaveLength(2);
expect(leadStore).toHaveLength(2);
const alexContact = contactStore.find(row => row.ownerUserId === 11);
const tariqContact = contactStore.find(row => row.ownerUserId === 22);
expect(alexContact.peerCardId).toBe(202);
expect(tariqContact.peerCardId).toBe(101);
expect(alexContact.source).toBe('exchange');
expect(leadStore.every(row => row.source === 'exchange')).toBe(true);
expect(leadStore.find(row => row.cardId === 101)?.name).toBe('Tariq Mansour');
expect(leadStore.find(row => row.cardId === 202)?.name).toBe('Alex Rivera');
});
it('大厅分页只返回当前用户的人脉', async () => {
await contactService.collect({ ownerUserId: 11, peerCardId: 202 });
await contactService.upsertContact({
ownerUserId: 22,
peerCardId: 101,
source: 'collect',
});
const page = await contactService.pageContacts({
ownerUserId: 11,
page: 1,
size: 20,
});
expect(page.list).toHaveLength(1);
expect(page.list[0].peerCardId).toBe(202);
expect(page.pagination.total).toBe(1);
});
it('团队墙只返回同租户已发布名片,并排除自己', async () => {
const page = await contactService.pageTeamWall({
tenantId: 1,
excludeUserId: 11,
page: 1,
size: 20,
});
expect(page.list).toHaveLength(1);
expect(page.list[0].peerCardId).toBe(103);
expect(page.list[0].peerName).toBe('Chen Wei');
expect(page.list.find(row => row.peerCardId === 101)).toBeUndefined();
expect(page.list.find(row => row.peerCardId === 202)).toBeUndefined();
});
it('纯函数互换邀约仍拒绝自己与自己互换', () => {
expect(() => createMutualExchangeInvitation(101, 101)).toThrow(
/不能与自己互换/
);
});
});