Files
lolympus-tournament-support/apps/api/src/modules/dakao/handlers/match-join.handler.spec.ts
T

390 lines
12 KiB
TypeScript

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<MatchService>;
dakaoUserService?: Partial<DakaoUserService>;
}): MatchJoinHandler {
const matchService = {
join: jest.fn(),
list: jest.fn(),
...overrides?.matchService,
} as unknown as MatchService;
const dakaoUserService = {
resolveRiotAccount: jest.fn(),
findUserByKakaoTalkId: jest.fn(),
...overrides?.dakaoUserService,
} as unknown as DakaoUserService;
return new MatchJoinHandler(matchService, dakaoUserService);
}
const baseParams = {
communityId: objectId('111111111111111111111111'),
matchAndRiotId: '69b63bc96da18dfdb218ee8c 프리리#KR1',
senderUserId: 'kakao-user-1',
senderNickname: '프리리',
positions: ['MID', 'TOP'] as const,
};
describe('MatchJoinHandler', () => {
it('rejects missing match reference when multiple matches are recruiting', async () => {
const resolveRiotAccount = jest.fn();
const handler = makeHandler({
matchService: {
list: jest.fn().mockResolvedValue([
{ _id: objectId('333333333333333333333333'), title: '저녁 내전' },
{ _id: objectId('444444444444444444444444'), title: '심야 내전' },
]),
} as Partial<MatchService>,
dakaoUserService: {
resolveRiotAccount,
} as Partial<DakaoUserService>,
});
const segments = await handler.handle({
...baseParams,
matchAndRiotId: '프리리#KR1',
positions: [...baseParams.positions],
});
expect(resolveRiotAccount).not.toHaveBeenCalled();
expect(segments).toEqual([
expect.objectContaining({ kind: 'mention', userId: 'kakao-user-1' }),
expect.objectContaining({
kind: 'text',
text: expect.stringContaining('코드 또는 제목'),
}),
]);
});
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<MatchService>,
dakaoUserService: {
resolveRiotAccount,
} as Partial<DakaoUserService>,
});
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('uses the only recruiting match when match reference is omitted', async () => {
const matchId = objectId('333333333333333333333333');
const join = jest.fn().mockResolvedValue({
title: '저녁 내전',
participants: [{}],
maxParticipants: 10,
});
const handler = makeHandler({
matchService: {
list: jest
.fn()
.mockResolvedValue([{ _id: matchId, title: '저녁 내전' }]),
join,
} as Partial<MatchService>,
dakaoUserService: {
resolveRiotAccount: jest.fn().mockResolvedValue({
user: { _id: objectId('222222222222222222222222') },
riotAccount: { puuid: 'puuid-1', gameName: '프리리', tagLine: 'KR1' },
}),
} as Partial<DakaoUserService>,
});
const segments = await handler.handle({
...baseParams,
matchAndRiotId: '프리리#KR1',
positions: [...baseParams.positions],
});
expect(join).toHaveBeenCalledWith(
expect.objectContaining({
matchId,
}),
);
expect(segments[1]).toMatchObject({
kind: 'text',
text: expect.stringContaining('신청 완료 (1/10)'),
});
});
it('resolves a unique recruiting match by title prefix', async () => {
const matchId = objectId('333333333333333333333333');
const join = jest.fn().mockResolvedValue({
title: '저녁 내전',
participants: [{}],
maxParticipants: 10,
});
const handler = makeHandler({
matchService: {
list: jest.fn().mockResolvedValue([
{ _id: matchId, title: '저녁 내전' },
{ _id: objectId('444444444444444444444444'), title: '심야 내전' },
]),
join,
} as Partial<MatchService>,
dakaoUserService: {
resolveRiotAccount: jest.fn().mockResolvedValue({
user: { _id: objectId('222222222222222222222222') },
riotAccount: { puuid: 'puuid-1', gameName: '프리리', tagLine: 'KR1' },
}),
} as Partial<DakaoUserService>,
});
await handler.handle({
...baseParams,
matchAndRiotId: '저녁 프리리#KR1',
positions: [...baseParams.positions],
});
expect(join).toHaveBeenCalledWith(
expect.objectContaining({
matchId,
}),
);
});
it('resolves a recruiting match by short code', async () => {
const matchId = objectId('333333333333333333333333');
const join = jest.fn().mockResolvedValue({
title: '저녁 내전',
participants: [{}],
maxParticipants: 10,
});
const handler = makeHandler({
matchService: {
list: jest.fn().mockResolvedValue([
{ _id: matchId, title: '저녁 내전', shortCode: 'AB12' },
{
_id: objectId('444444444444444444444444'),
title: '심야 내전',
shortCode: 'CD34',
},
]),
join,
} as Partial<MatchService>,
dakaoUserService: {
resolveRiotAccount: jest.fn().mockResolvedValue({
user: { _id: objectId('222222222222222222222222') },
riotAccount: { puuid: 'puuid-1', gameName: '프리리', tagLine: 'KR1' },
}),
} as Partial<DakaoUserService>,
});
await handler.handle({
...baseParams,
matchAndRiotId: 'AB12 프리리#KR1',
positions: [...baseParams.positions],
});
expect(join).toHaveBeenCalledWith(
expect.objectContaining({
matchId,
}),
);
});
it('uses registered account and preferred positions for abbreviated join', async () => {
const matchId = objectId('333333333333333333333333');
const userId = objectId('222222222222222222222222');
const join = jest.fn().mockResolvedValue({
title: '저녁 내전',
participants: [{}],
maxParticipants: 10,
});
const resolveRiotAccount = jest.fn();
const handler = makeHandler({
matchService: {
list: jest.fn().mockResolvedValue([
{ _id: matchId, title: '저녁 내전', shortCode: 'AB12' },
]),
join,
} as Partial<MatchService>,
dakaoUserService: {
resolveRiotAccount,
findUserByKakaoTalkId: jest.fn().mockResolvedValue({
_id: userId,
riotAccounts: [
{
puuid: 'puuid-1',
gameName: '프리리',
tagLine: 'KR1',
isPrimary: true,
},
],
preferredPositions: [
{ position: 'MID', priority: 1 },
{ position: 'JGL', priority: 2 },
],
}),
} as Partial<DakaoUserService>,
});
const segments = await handler.handle({
...baseParams,
matchAndRiotId: 'AB12',
positions: undefined,
});
expect(resolveRiotAccount).not.toHaveBeenCalled();
expect(join).toHaveBeenCalledWith({
matchId,
userId,
preferredPositions: [
{ position: 'MID', priority: 1 },
{ position: 'JGL', priority: 2 },
],
riotPuuid: 'puuid-1',
});
expect(segments[1]).toMatchObject({
kind: 'text',
text: expect.stringContaining('계정: 프리리#KR1'),
});
});
it('asks abbreviated join users to register missing preferred positions', async () => {
const matchId = objectId('333333333333333333333333');
const join = jest.fn();
const handler = makeHandler({
matchService: {
list: jest.fn().mockResolvedValue([
{ _id: matchId, title: '저녁 내전', shortCode: 'AB12' },
]),
join,
} as Partial<MatchService>,
dakaoUserService: {
findUserByKakaoTalkId: jest.fn().mockResolvedValue({
_id: objectId('222222222222222222222222'),
riotAccounts: [
{
puuid: 'puuid-1',
gameName: '프리리',
tagLine: 'KR1',
isPrimary: true,
},
],
preferredPositions: [],
}),
} as Partial<DakaoUserService>,
});
const segments = await handler.handle({
...baseParams,
matchAndRiotId: 'AB12',
positions: undefined,
});
expect(join).not.toHaveBeenCalled();
expect(segments[1]).toMatchObject({
kind: 'text',
text: expect.stringContaining('선호포지션등록'),
});
});
it('rejects ambiguous title prefixes', async () => {
const resolveRiotAccount = jest.fn();
const handler = makeHandler({
matchService: {
list: jest.fn().mockResolvedValue([
{ _id: objectId('333333333333333333333333'), title: '저녁 내전 A' },
{ _id: objectId('444444444444444444444444'), title: '저녁 내전 B' },
]),
} as Partial<MatchService>,
dakaoUserService: {
resolveRiotAccount,
} as Partial<DakaoUserService>,
});
const segments = await handler.handle({
...baseParams,
matchAndRiotId: '저녁 프리리#KR1',
positions: [...baseParams.positions],
});
expect(resolveRiotAccount).not.toHaveBeenCalled();
expect(segments[1]).toMatchObject({
kind: 'text',
text: expect.stringContaining('여러 개와 겹칩니다'),
});
});
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<DakaoUserService>,
});
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<MatchService>,
dakaoUserService: {
resolveRiotAccount: jest.fn().mockResolvedValue({
user: { _id: objectId('222222222222222222222222') },
riotAccount: { puuid: 'p', gameName: 'g', tagLine: 't' },
}),
} as Partial<DakaoUserService>,
});
const segments = await handler.handle({
...baseParams,
positions: [...baseParams.positions],
});
expect(segments[1]).toMatchObject({
kind: 'text',
text: expect.stringContaining('정원이 가득'),
});
});
});