import mongoose from 'mongoose'; import { ConnectionService } from '../global/database/connection.service'; import { RiotAccount, UserDocument } from '../global/database/schema'; import { RiotAccountEnrichmentService } from '../riot/riot-account-enrichment.service'; import { RiotAccountService } from '../riot/riot-account.service'; import { DakaoUserService } from './dakao-user.service'; const objectId = (hex: string) => new mongoose.Types.ObjectId(hex); function makeQuery(value: T) { return { exec: jest.fn().mockResolvedValue(value), }; } function makeService(params: { user: Partial; community?: Record | null; riotAccountService?: Partial; enrichmentService?: Partial; }) { const findOneAndUpdate = jest .fn() .mockReturnValue(makeQuery(params.user as UserDocument)); const findById = jest.fn().mockReturnValue({ select: jest.fn().mockReturnValue({ lean: jest.fn().mockReturnValue(makeQuery(params.community ?? {})), }), }); const connectionService = { getModel: jest.fn().mockImplementation(async (name: string) => { if (name === 'user') return { findOneAndUpdate }; if (name === 'community') return { findById }; throw new Error(`Unexpected model: ${name}`); }), } as unknown as ConnectionService; const riotAccountService = { getByRiotId: jest.fn(), ...params.riotAccountService, } as unknown as RiotAccountService; const enrichmentService = { enrichRiotAccount: jest.fn(), ...params.enrichmentService, } as unknown as RiotAccountEnrichmentService; return { service: new DakaoUserService( connectionService, riotAccountService, enrichmentService, ), connectionService, findOneAndUpdate, findById, riotAccountService, enrichmentService, }; } describe('DakaoUserService', () => { it('registers an additional Riot account when the user already has one', async () => { const save = jest.fn().mockResolvedValue(undefined); const existingAccount = { puuid: 'puuid-1', gameName: '기존소환사', tagLine: 'KR1', isPrimary: true, } as RiotAccount; const { service, connectionService, riotAccountService, enrichmentService, } = makeService({ user: { _id: objectId('222222222222222222222222'), riotAccounts: [existingAccount], save, } as Partial, riotAccountService: { getByRiotId: jest.fn().mockResolvedValue({ puuid: 'puuid-2', gameName: '새소환사', tagLine: 'KR2', }), }, enrichmentService: { enrichRiotAccount: jest.fn().mockResolvedValue({ account: null }), }, }); const result = await service.registerRiotAccount({ communityId: objectId('111111111111111111111111'), kakaoTalkId: 'kakao-user-1', kakaoTalkNickname: '프리리', gameName: '새소환사', tagLine: 'KR2', }); expect(result.riotAccount).toMatchObject({ puuid: 'puuid-2', gameName: '새소환사', tagLine: 'KR2', isPrimary: false, }); expect((result.user.riotAccounts as RiotAccount[])[0]).toMatchObject({ puuid: 'puuid-1', isPrimary: true, }); expect(result.user.riotAccounts).toHaveLength(2); expect(connectionService.getModel).toHaveBeenCalledWith('user'); expect(connectionService.getModel).toHaveBeenCalledWith('community'); expect(riotAccountService.getByRiotId).toHaveBeenCalledWith( '새소환사', 'KR2', { region: 'asia', }, ); expect(enrichmentService.enrichRiotAccount).toHaveBeenCalledWith( expect.objectContaining({ userId: expect.any(mongoose.Types.ObjectId), puuid: 'puuid-2', options: { force: true, platform: 'kr', region: 'asia' }, }), ); expect(save).toHaveBeenCalled(); }); it('reuses an already attached Riot account by puuid', async () => { const save = jest.fn().mockResolvedValue(undefined); const existingAccount = { puuid: 'puuid-1', gameName: '기존소환사', tagLine: 'KR1', isPrimary: true, } as RiotAccount; const { service, enrichmentService } = makeService({ user: { _id: objectId('222222222222222222222222'), riotAccounts: [existingAccount], save, } as Partial, riotAccountService: { getByRiotId: jest.fn().mockResolvedValue({ puuid: 'puuid-1', gameName: '바뀐소환사', tagLine: 'KR1', }), }, }); const result = await service.registerRiotAccount({ communityId: objectId('111111111111111111111111'), kakaoTalkId: 'kakao-user-1', kakaoTalkNickname: '프리리', gameName: '바뀐소환사', tagLine: 'KR1', }); expect(result.user.riotAccounts).toHaveLength(1); expect(result.riotAccount).toMatchObject({ puuid: 'puuid-1', gameName: '바뀐소환사', tagLine: 'KR1', isPrimary: true, }); expect(enrichmentService.enrichRiotAccount).not.toHaveBeenCalled(); expect(save).toHaveBeenCalled(); }); });