Files
lolympus-tournament-support/apps/api/src/modules/tournament-result/competition-result.adapter.spec.ts
T

245 lines
6.6 KiB
TypeScript

import mongoose from 'mongoose';
import type { RiotMatchDto } from '../riot/riot.types';
import type { MatchPuuidLookup } from './team-mapping.util';
import {
compareRiotGameChronology,
mapRiotGameToCompetitionSlot,
resolveReadyCompetitionSlot,
type CompetitionResultSnapshot,
} from './competition-result.adapter';
const competitionId = new mongoose.Types.ObjectId().toString();
const seriesId = new mongoose.Types.ObjectId().toString();
const gameSlotId = new mongoose.Types.ObjectId().toString();
const teamAId = new mongoose.Types.ObjectId().toString();
const teamBId = new mongoose.Types.ObjectId().toString();
function fixture(): {
competition: CompetitionResultSnapshot;
detail: RiotMatchDto;
lookup: MatchPuuidLookup;
} {
const users = Array.from({ length: 10 }, (_, index) => ({
userId: new mongoose.Types.ObjectId(),
puuid: `puuid-${index + 1}`,
}));
const lookup: MatchPuuidLookup = {
puuidByUserId: new Map(
users.map(({ userId, puuid }) => [userId.toString(), puuid]),
),
userIdByPuuid: new Map(users.map(({ userId, puuid }) => [puuid, userId])),
};
const competition: CompetitionResultSnapshot = {
competitionId,
revision: 4,
phase: 'running',
teams: [
{
teamId: teamAId,
members: users.slice(0, 5).map(({ userId }) => ({
userId: userId.toString(),
})),
},
{
teamId: teamBId,
members: users.slice(5).map(({ userId }) => ({
userId: userId.toString(),
})),
},
],
projection: {
phase: 'running',
series: [
{
seriesId,
status: 'ready',
entries: [
{ slot: 'A', teamId: teamAId, gameWins: 0 },
{ slot: 'B', teamId: teamBId, gameWins: 0 },
],
},
],
gameSlots: [
{
id: gameSlotId,
seriesId,
ordinal: 1,
displayNumber: 17,
status: 'ready',
},
],
games: [],
},
};
const participants = [
...users.slice(5).map(({ puuid }, index) => ({
puuid,
participantId: index + 1,
teamId: 100,
championId: index + 1,
kills: 10,
deaths: 2,
assists: 8,
win: true,
})),
...users.slice(0, 5).map(({ puuid }, index) => ({
puuid,
participantId: index + 6,
teamId: 200,
championId: index + 6,
kills: 2,
deaths: 10,
assists: 3,
win: false,
})),
];
const detail = {
metadata: {
dataVersion: '2',
matchId: 'KR_123456789',
participants: participants.map(({ puuid }) => puuid),
},
info: {
gameCreation: Date.parse('2026-07-12T10:00:00.000Z'),
gameEndTimestamp: Date.parse('2026-07-12T10:30:00.000Z'),
gameDuration: 1800,
participants,
teams: [
{ teamId: 100, win: true },
{ teamId: 200, win: false },
],
},
} as unknown as RiotMatchDto;
return { competition, detail, lookup };
}
describe('Competition result Riot mapping', () => {
it('maps Riot sides to stable team ids using the frozen roster snapshot', () => {
const { competition, detail, lookup } = fixture();
const accepted = mapRiotGameToCompetitionSlot({
competition,
target: competition.projection.gameSlots[0],
detail,
lookup,
});
expect(accepted).toMatchObject({
seriesId,
gameSlotId,
ordinal: 1,
displayNumber: 17,
mapping: {
mappingVersion: 1,
blueTeamId: teamBId,
redTeamId: teamAId,
winnerTeamId: teamBId,
algorithmVersion: 'roster-overlap-v1',
confidence: 1,
diagnostics: {
trustedOverlap: 10,
normalOverlap: 0,
reversedOverlap: 10,
},
},
evidence: {
type: 'riot',
riotMatchId: 'KR_123456789',
normalizedSnapshot: {
startedAt: '2026-07-12T10:00:00.000Z',
durationSeconds: 1800,
},
},
});
expect(accepted?.evidence.payloadHash).toMatch(/^[a-f0-9]{64}$/);
expect(accepted?.normalizedParticipants).toHaveLength(10);
expect(typeof accepted?.normalizedParticipants[0]?.userId).toBe('string');
expect(accepted?.normalizedParticipants[0]).toMatchObject({
teamId: teamBId,
side: 'blue',
summonerName: '',
tagLine: '',
});
});
it('retains unknown Riot participants as normalized evidence with a null user id', () => {
const { competition, detail, lookup } = fixture();
detail.info.participants[0].puuid = 'unknown-substitute';
const accepted = mapRiotGameToCompetitionSlot({
competition,
target: competition.projection.gameSlots[0],
detail,
lookup,
});
expect(accepted?.mapping.confidence).toBe(0.9);
expect(accepted?.normalizedParticipants).toHaveLength(10);
expect(accepted?.normalizedParticipants[0]).toMatchObject({
userId: null,
teamId: teamBId,
side: 'blue',
});
expect(accepted?.mapping.participantAssignments).toHaveLength(9);
});
it('resolves a manual result from ready stable GameSlots, not display codes', () => {
const { competition, detail, lookup } = fixture();
const accepted = resolveReadyCompetitionSlot({
competition,
detail,
lookup,
});
expect(accepted).toMatchObject({ seriesId, gameSlotId, displayNumber: 17 });
});
it('rejects an untrusted roster overlap instead of guessing a team', () => {
const { competition, detail, lookup } = fixture();
detail.info.participants.forEach((participant, index) => {
participant.puuid = `unknown-${index}`;
});
expect(
mapRiotGameToCompetitionSlot({
competition,
target: competition.projection.gameSlots[0],
detail,
lookup,
}),
).toBeNull();
});
it('rejects inconsistent Riot evidence with more than one winning team', () => {
const { competition, detail, lookup } = fixture();
detail.info.teams.forEach((team) => {
team.win = true;
});
expect(
mapRiotGameToCompetitionSlot({
competition,
target: competition.projection.gameSlots[0],
detail,
lookup,
}),
).toBeNull();
});
it('orders collected Riot games by occurrence before first-to projection', () => {
const { detail } = fixture();
const later = {
...detail,
metadata: { ...detail.metadata, matchId: 'KR_LATER' },
info: { ...detail.info, gameCreation: detail.info.gameCreation + 60_000 },
};
expect(
[later, detail]
.sort(compareRiotGameChronology)
.map((game) => game.metadata.matchId),
).toEqual(['KR_123456789', 'KR_LATER']);
});
});