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 }) => { 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 }) => { 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( /不能与自己互换/ ); }); });