import { BadRequestException } from '@nestjs/common'; import mongoose from 'mongoose'; import { MatchService } from '../../match/match.service'; import { DakaoUserError, DakaoUserService } from '../dakao-user.service'; import { MatchJoinHandler } from './match-join.handler'; const objectId = (hex: string) => new mongoose.Types.ObjectId(hex); function makeHandler(overrides?: { matchService?: Partial; dakaoUserService?: Partial; }): MatchJoinHandler { const matchService = { join: jest.fn(), ...overrides?.matchService, } as unknown as MatchService; const dakaoUserService = { resolveRiotAccount: jest.fn(), ...overrides?.dakaoUserService, } as unknown as DakaoUserService; return new MatchJoinHandler(matchService, dakaoUserService); } const baseParams = { communityId: objectId('111111111111111111111111'), matchId: '69b63bc96da18dfdb218ee8c', senderUserId: 'kakao-user-1', senderNickname: '프리리', gameName: '프리리', tagLine: 'KR1', positions: ['MID', 'TOP'] as const, }; describe('MatchJoinHandler', () => { it('rejects invalid match id without touching services', async () => { const handler = makeHandler(); const segments = await handler.handle({ ...baseParams, matchId: 'not-an-id', positions: [...baseParams.positions], }); expect(segments).toEqual([ expect.objectContaining({ kind: 'mention', userId: 'kakao-user-1' }), expect.objectContaining({ kind: 'text', text: expect.stringContaining('유효하지 않은 내전 ID'), }), ]); }); it('replies with success and uses input position priority', async () => { const join = jest.fn().mockResolvedValue({ title: '저녁 내전', participants: [{}, {}, {}], maxParticipants: 10, }); const resolveRiotAccount = jest.fn().mockResolvedValue({ user: { _id: objectId('222222222222222222222222') }, riotAccount: { puuid: 'puuid-1', gameName: '프리리', tagLine: 'KR1' }, }); const handler = makeHandler({ matchService: { join } as Partial, dakaoUserService: { resolveRiotAccount, } as Partial, }); const segments = await handler.handle({ ...baseParams, positions: [...baseParams.positions], }); expect(resolveRiotAccount).toHaveBeenCalledWith({ communityId: baseParams.communityId, kakaoTalkId: 'kakao-user-1', kakaoTalkNickname: '프리리', gameName: '프리리', tagLine: 'KR1', }); expect(join).toHaveBeenCalledWith({ matchId: expect.any(mongoose.Types.ObjectId), userId: expect.any(mongoose.Types.ObjectId), preferredPositions: [ { position: 'MID', priority: 1 }, { position: 'TOP', priority: 2 }, ], riotPuuid: 'puuid-1', }); expect(segments[1]).toMatchObject({ kind: 'text', text: expect.stringContaining('신청 완료 (3/10)'), }); }); it('translates riot_account_not_found errors', async () => { const handler = makeHandler({ dakaoUserService: { resolveRiotAccount: jest .fn() .mockRejectedValue( new DakaoUserError('riot_account_not_found', '프리리#KR1'), ), } as Partial, }); const segments = await handler.handle({ ...baseParams, positions: [...baseParams.positions], }); expect(segments[1]).toMatchObject({ kind: 'text', text: expect.stringContaining('라이엇 계정을 찾을 수 없습니다'), }); }); it('translates match BadRequest into korean', async () => { const handler = makeHandler({ matchService: { join: jest .fn() .mockRejectedValue(new BadRequestException('Match is full.')), } as Partial, dakaoUserService: { resolveRiotAccount: jest.fn().mockResolvedValue({ user: { _id: objectId('222222222222222222222222') }, riotAccount: { puuid: 'p', gameName: 'g', tagLine: 't' }, }), } as Partial, }); const segments = await handler.handle({ ...baseParams, positions: [...baseParams.positions], }); expect(segments[1]).toMatchObject({ kind: 'text', text: expect.stringContaining('정원이 가득'), }); }); });