import { CardPortalService } from '../../../src/modules/card/service/portal'; import { CardInfoEntity } from '../../../src/modules/card/entity/info'; import { CardBentoEntity } from '../../../src/modules/card/entity/bento'; describe('CardPortalService: default 标识平滑降级与微官网访问容错 (TDD Red -> Green)', () => { let cardPortalService: CardPortalService; const mockCardData: CardInfoEntity[] = [ { id: 1, slug: 'alex-morgan', name: 'Alex Morgan', title: 'VP of Global Business', company: 'OmniCard AI Global Inc.', status: 'published', createTime: new Date(), updateTime: new Date(), } as CardInfoEntity, ]; beforeEach(() => { cardPortalService = new CardPortalService(); (CardPortalService as any).localCache?.clear?.(); const mockCardRepo = { findOne: jest.fn(async (options: any) => { const where = options?.where; // 如果是按默认/第一张已发布排序查询 if (options?.order?.id === 'ASC' && where?.status === 'published') { return mockCardData.find((c) => c.status === 'published') || null; } if (Array.isArray(where)) { return mockCardData.find((c) => c.id === where[0]?.id || c.slug === where[1]?.slug) || null; } if (where?.slug) { return mockCardData.find((c) => c.slug === where.slug) || null; } if (where?.id) { return mockCardData.find((c) => c.id === where.id) || null; } return null; }), }; const mockBentoRepo = { find: jest.fn(async () => []), }; cardPortalService.cardInfoEntity = mockCardRepo as any; cardPortalService.cardBentoEntity = mockBentoRepo as any; }); it('1. 当请求 slug 为 default 且无 default 名片时,应自动平滑降级获取首张已发布名片', async () => { const res = await cardPortalService.getPortalDetail('default'); expect(res).toBeDefined(); expect(res.name).toBe('Alex Morgan'); expect(res.slug).toBe('alex-morgan'); expect(res.status).toBe('published'); }); it('2. 当请求明确的 slug 时,应正常返回指定名片', async () => { const res = await cardPortalService.getPortalDetail('alex-morgan'); expect(res).toBeDefined(); expect(res.name).toBe('Alex Morgan'); expect(res.slug).toBe('alex-morgan'); }); });