wip
This commit is contained in:
@@ -0,0 +1,426 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest';
|
||||
|
||||
import { RecalculateChampionshipStandingsUseCase } from '@gridpilot/racing/application/use-cases/RecalculateChampionshipStandingsUseCase';
|
||||
import type { ISeasonRepository } from '@gridpilot/racing/domain/repositories/ISeasonRepository';
|
||||
import type { ILeagueScoringConfigRepository } from '@gridpilot/racing/domain/repositories/ILeagueScoringConfigRepository';
|
||||
import type { IRaceRepository } from '@gridpilot/racing/domain/repositories/IRaceRepository';
|
||||
import type { IResultRepository } from '@gridpilot/racing/domain/repositories/IResultRepository';
|
||||
import type { IPenaltyRepository } from '@gridpilot/racing/domain/repositories/IPenaltyRepository';
|
||||
import type { IChampionshipStandingRepository } from '@gridpilot/racing/domain/repositories/IChampionshipStandingRepository';
|
||||
|
||||
import type { Season } from '@gridpilot/racing/domain/entities/Season';
|
||||
import type { LeagueScoringConfig } from '@gridpilot/racing/domain/entities/LeagueScoringConfig';
|
||||
import type { Race } from '@gridpilot/racing/domain/entities/Race';
|
||||
import type { Result } from '@gridpilot/racing/domain/entities/Result';
|
||||
import type { Penalty } from '@gridpilot/racing/domain/entities/Penalty';
|
||||
import type { ChampionshipStanding } from '@gridpilot/racing/domain/entities/ChampionshipStanding';
|
||||
import type { ChampionshipConfig } from '@gridpilot/racing/domain/value-objects/ChampionshipConfig';
|
||||
import { EventScoringService } from '@gridpilot/racing/domain/services/EventScoringService';
|
||||
import { DropScoreApplier } from '@gridpilot/racing/domain/services/DropScoreApplier';
|
||||
import { ChampionshipAggregator } from '@gridpilot/racing/domain/services/ChampionshipAggregator';
|
||||
import { PointsTable } from '@gridpilot/racing/domain/value-objects/PointsTable';
|
||||
import type { SessionType } from '@gridpilot/racing/domain/value-objects/SessionType';
|
||||
import type { BonusRule } from '@gridpilot/racing/domain/value-objects/BonusRule';
|
||||
import type { DropScorePolicy } from '@gridpilot/racing/domain/value-objects/DropScorePolicy';
|
||||
|
||||
class InMemorySeasonRepository implements ISeasonRepository {
|
||||
private seasons: Season[] = [];
|
||||
|
||||
async findById(id: string): Promise<Season | null> {
|
||||
return this.seasons.find((s) => s.id === id) || null;
|
||||
}
|
||||
|
||||
async findByLeagueId(leagueId: string): Promise<Season[]> {
|
||||
return this.seasons.filter((s) => s.leagueId === leagueId);
|
||||
}
|
||||
|
||||
seedSeason(season: Season): void {
|
||||
this.seasons.push(season);
|
||||
}
|
||||
}
|
||||
|
||||
class InMemoryLeagueScoringConfigRepository implements ILeagueScoringConfigRepository {
|
||||
private configs: LeagueScoringConfig[] = [];
|
||||
|
||||
async findBySeasonId(seasonId: string): Promise<LeagueScoringConfig | null> {
|
||||
return this.configs.find((c) => c.seasonId === seasonId) || null;
|
||||
}
|
||||
|
||||
seedConfig(config: LeagueScoringConfig): void {
|
||||
this.configs.push(config);
|
||||
}
|
||||
}
|
||||
|
||||
class InMemoryRaceRepository implements IRaceRepository {
|
||||
private races: Race[] = [];
|
||||
|
||||
async findById(id: string): Promise<Race | null> {
|
||||
return this.races.find((r) => r.id === id) || null;
|
||||
}
|
||||
|
||||
async findAll(): Promise<Race[]> {
|
||||
return [...this.races];
|
||||
}
|
||||
|
||||
async findByLeagueId(leagueId: string): Promise<Race[]> {
|
||||
return this.races.filter((r) => r.leagueId === leagueId);
|
||||
}
|
||||
|
||||
async findUpcomingByLeagueId(): Promise<Race[]> {
|
||||
return [];
|
||||
}
|
||||
|
||||
async findCompletedByLeagueId(): Promise<Race[]> {
|
||||
return [];
|
||||
}
|
||||
|
||||
async findByStatus(): Promise<Race[]> {
|
||||
return [];
|
||||
}
|
||||
|
||||
async findByDateRange(): Promise<Race[]> {
|
||||
return [];
|
||||
}
|
||||
|
||||
async create(race: Race): Promise<Race> {
|
||||
this.races.push(race);
|
||||
return race;
|
||||
}
|
||||
|
||||
async update(race: Race): Promise<Race> {
|
||||
const index = this.races.findIndex((r) => r.id === race.id);
|
||||
if (index >= 0) {
|
||||
this.races[index] = race;
|
||||
} else {
|
||||
this.races.push(race);
|
||||
}
|
||||
return race;
|
||||
}
|
||||
|
||||
async delete(id: string): Promise<void> {
|
||||
this.races = this.races.filter((r) => r.id !== id);
|
||||
}
|
||||
|
||||
async exists(id: string): Promise<boolean> {
|
||||
return this.races.some((r) => r.id === id);
|
||||
}
|
||||
|
||||
seedRace(race: Race): void {
|
||||
this.races.push(race);
|
||||
}
|
||||
}
|
||||
|
||||
class InMemoryResultRepository implements IResultRepository {
|
||||
private results: Result[] = [];
|
||||
|
||||
async findByRaceId(raceId: string): Promise<Result[]> {
|
||||
return this.results.filter((r) => r.raceId === raceId);
|
||||
}
|
||||
|
||||
seedResult(result: Result): void {
|
||||
this.results.push(result);
|
||||
}
|
||||
}
|
||||
|
||||
class InMemoryPenaltyRepository implements IPenaltyRepository {
|
||||
private penalties: Penalty[] = [];
|
||||
|
||||
async findByLeagueId(leagueId: string): Promise<Penalty[]> {
|
||||
return this.penalties.filter((p) => p.leagueId === leagueId);
|
||||
}
|
||||
|
||||
async findByLeagueIdAndDriverId(
|
||||
leagueId: string,
|
||||
driverId: string,
|
||||
): Promise<Penalty[]> {
|
||||
return this.penalties.filter(
|
||||
(p) => p.leagueId === leagueId && p.driverId === driverId,
|
||||
);
|
||||
}
|
||||
|
||||
async findAll(): Promise<Penalty[]> {
|
||||
return [...this.penalties];
|
||||
}
|
||||
|
||||
seedPenalty(penalty: Penalty): void {
|
||||
this.penalties.push(penalty);
|
||||
}
|
||||
}
|
||||
|
||||
class InMemoryChampionshipStandingRepository implements IChampionshipStandingRepository {
|
||||
private standings: ChampionshipStanding[] = [];
|
||||
|
||||
async findBySeasonAndChampionship(
|
||||
seasonId: string,
|
||||
championshipId: string,
|
||||
): Promise<ChampionshipStanding[]> {
|
||||
return this.standings.filter(
|
||||
(s) => s.seasonId === seasonId && s.championshipId === championshipId,
|
||||
);
|
||||
}
|
||||
|
||||
async saveAll(standings: ChampionshipStanding[]): Promise<void> {
|
||||
this.standings = standings;
|
||||
}
|
||||
|
||||
getAll(): ChampionshipStanding[] {
|
||||
return [...this.standings];
|
||||
}
|
||||
}
|
||||
|
||||
function makePointsTable(points: number[]): PointsTable {
|
||||
const byPos: Record<number, number> = {};
|
||||
points.forEach((p, idx) => {
|
||||
byPos[idx + 1] = p;
|
||||
});
|
||||
return new PointsTable(byPos);
|
||||
}
|
||||
|
||||
function makeChampionshipConfig(): ChampionshipConfig {
|
||||
const mainPoints = makePointsTable([25, 18, 15, 12, 10, 8, 6, 4, 2, 1]);
|
||||
const sprintPoints = makePointsTable([8, 7, 6, 5, 4, 3, 2, 1]);
|
||||
|
||||
const fastestLapBonus: BonusRule = {
|
||||
id: 'fastest-lap-main',
|
||||
type: 'fastestLap',
|
||||
points: 1,
|
||||
requiresFinishInTopN: 10,
|
||||
};
|
||||
|
||||
const sessionTypes: SessionType[] = ['sprint', 'main'];
|
||||
const pointsTableBySessionType: Record<SessionType, PointsTable> = {
|
||||
sprint: sprintPoints,
|
||||
main: mainPoints,
|
||||
practice: new PointsTable({}),
|
||||
qualifying: new PointsTable({}),
|
||||
q1: new PointsTable({}),
|
||||
q2: new PointsTable({}),
|
||||
q3: new PointsTable({}),
|
||||
timeTrial: new PointsTable({}),
|
||||
};
|
||||
|
||||
const bonusRulesBySessionType: Record<SessionType, BonusRule[]> = {
|
||||
sprint: [],
|
||||
main: [fastestLapBonus],
|
||||
practice: [],
|
||||
qualifying: [],
|
||||
q1: [],
|
||||
q2: [],
|
||||
q3: [],
|
||||
timeTrial: [],
|
||||
};
|
||||
|
||||
const dropScorePolicy: DropScorePolicy = {
|
||||
strategy: 'bestNResults',
|
||||
count: 6,
|
||||
};
|
||||
|
||||
return {
|
||||
id: 'driver-champ',
|
||||
name: 'Driver Championship',
|
||||
type: 'driver',
|
||||
sessionTypes,
|
||||
pointsTableBySessionType,
|
||||
bonusRulesBySessionType,
|
||||
dropScorePolicy,
|
||||
};
|
||||
}
|
||||
|
||||
describe('RecalculateChampionshipStandingsUseCase', () => {
|
||||
const leagueId = 'league-1';
|
||||
const seasonId = 'season-1';
|
||||
const championshipId = 'driver-champ';
|
||||
|
||||
let seasonRepository: InMemorySeasonRepository;
|
||||
let leagueScoringConfigRepository: InMemoryLeagueScoringConfigRepository;
|
||||
let raceRepository: InMemoryRaceRepository;
|
||||
let resultRepository: InMemoryResultRepository;
|
||||
let penaltyRepository: InMemoryPenaltyRepository;
|
||||
let championshipStandingRepository: InMemoryChampionshipStandingRepository;
|
||||
|
||||
let useCase: RecalculateChampionshipStandingsUseCase;
|
||||
|
||||
beforeEach(() => {
|
||||
seasonRepository = new InMemorySeasonRepository();
|
||||
leagueScoringConfigRepository = new InMemoryLeagueScoringConfigRepository();
|
||||
raceRepository = new InMemoryRaceRepository();
|
||||
resultRepository = new InMemoryResultRepository();
|
||||
penaltyRepository = new InMemoryPenaltyRepository();
|
||||
championshipStandingRepository = new InMemoryChampionshipStandingRepository();
|
||||
|
||||
const eventScoringService = new EventScoringService();
|
||||
const dropScoreApplier = new DropScoreApplier();
|
||||
const championshipAggregator = new ChampionshipAggregator(dropScoreApplier);
|
||||
|
||||
useCase = new RecalculateChampionshipStandingsUseCase(
|
||||
seasonRepository,
|
||||
leagueScoringConfigRepository,
|
||||
raceRepository,
|
||||
resultRepository,
|
||||
penaltyRepository,
|
||||
championshipStandingRepository,
|
||||
eventScoringService,
|
||||
championshipAggregator,
|
||||
);
|
||||
|
||||
const season: Season = {
|
||||
id: seasonId,
|
||||
leagueId,
|
||||
gameId: 'iracing',
|
||||
name: 'Demo Season',
|
||||
status: 'active',
|
||||
year: 2025,
|
||||
order: 1,
|
||||
startDate: new Date('2025-01-01'),
|
||||
endDate: new Date('2025-12-31'),
|
||||
};
|
||||
|
||||
seasonRepository.seedSeason(season);
|
||||
|
||||
const championship = makeChampionshipConfig();
|
||||
|
||||
const leagueScoringConfig: LeagueScoringConfig = {
|
||||
id: 'lsc-1',
|
||||
seasonId,
|
||||
championships: [championship],
|
||||
};
|
||||
|
||||
leagueScoringConfigRepository.seedConfig(leagueScoringConfig);
|
||||
|
||||
const races: Race[] = [
|
||||
{
|
||||
id: 'race-1-sprint',
|
||||
leagueId,
|
||||
scheduledAt: new Date('2025-02-01'),
|
||||
track: 'Track 1',
|
||||
car: 'Car A',
|
||||
sessionType: 'race',
|
||||
status: 'completed',
|
||||
},
|
||||
{
|
||||
id: 'race-1-main',
|
||||
leagueId,
|
||||
scheduledAt: new Date('2025-02-01'),
|
||||
track: 'Track 1',
|
||||
car: 'Car A',
|
||||
sessionType: 'race',
|
||||
status: 'completed',
|
||||
},
|
||||
{
|
||||
id: 'race-2-sprint',
|
||||
leagueId,
|
||||
scheduledAt: new Date('2025-03-01'),
|
||||
track: 'Track 2',
|
||||
car: 'Car A',
|
||||
sessionType: 'race',
|
||||
status: 'completed',
|
||||
},
|
||||
{
|
||||
id: 'race-2-main',
|
||||
leagueId,
|
||||
scheduledAt: new Date('2025-03-01'),
|
||||
track: 'Track 2',
|
||||
car: 'Car A',
|
||||
sessionType: 'race',
|
||||
status: 'completed',
|
||||
},
|
||||
{
|
||||
id: 'race-3-sprint',
|
||||
leagueId,
|
||||
scheduledAt: new Date('2025-04-01'),
|
||||
track: 'Track 3',
|
||||
car: 'Car A',
|
||||
sessionType: 'race',
|
||||
status: 'completed',
|
||||
},
|
||||
{
|
||||
id: 'race-3-main',
|
||||
leagueId,
|
||||
scheduledAt: new Date('2025-04-01'),
|
||||
track: 'Track 3',
|
||||
car: 'Car A',
|
||||
sessionType: 'race',
|
||||
status: 'completed',
|
||||
},
|
||||
];
|
||||
|
||||
races.forEach((race) => raceRepository.seedRace(race));
|
||||
|
||||
const drivers = ['driver-1', 'driver-2', 'driver-3'];
|
||||
|
||||
const resultsData: Array<{
|
||||
raceId: string;
|
||||
finishingOrder: string[];
|
||||
fastestLapDriverId: string;
|
||||
}> = [
|
||||
{
|
||||
raceId: 'race-1-sprint',
|
||||
finishingOrder: ['driver-1', 'driver-2', 'driver-3'],
|
||||
fastestLapDriverId: 'driver-2',
|
||||
},
|
||||
{
|
||||
raceId: 'race-1-main',
|
||||
finishingOrder: ['driver-2', 'driver-1', 'driver-3'],
|
||||
fastestLapDriverId: 'driver-1',
|
||||
},
|
||||
{
|
||||
raceId: 'race-2-sprint',
|
||||
finishingOrder: ['driver-1', 'driver-3', 'driver-2'],
|
||||
fastestLapDriverId: 'driver-1',
|
||||
},
|
||||
{
|
||||
raceId: 'race-2-main',
|
||||
finishingOrder: ['driver-1', 'driver-2', 'driver-3'],
|
||||
fastestLapDriverId: 'driver-1',
|
||||
},
|
||||
{
|
||||
raceId: 'race-3-sprint',
|
||||
finishingOrder: ['driver-2', 'driver-1', 'driver-3'],
|
||||
fastestLapDriverId: 'driver-2',
|
||||
},
|
||||
{
|
||||
raceId: 'race-3-main',
|
||||
finishingOrder: ['driver-3', 'driver-1', 'driver-2'],
|
||||
fastestLapDriverId: 'driver-3',
|
||||
},
|
||||
];
|
||||
|
||||
let resultIdCounter = 1;
|
||||
for (const raceData of resultsData) {
|
||||
raceData.finishingOrder.forEach((driverId, index) => {
|
||||
const result: Result = {
|
||||
id: `result-${resultIdCounter++}`,
|
||||
raceId: raceData.raceId,
|
||||
driverId,
|
||||
position: index + 1,
|
||||
fastestLap: driverId === raceData.fastestLapDriverId ? 90000 : 91000 + index * 100,
|
||||
incidents: 0,
|
||||
startPosition: index + 1,
|
||||
};
|
||||
resultRepository.seedResult(result);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
it('recalculates standings for a driver championship with sprint and main races', async () => {
|
||||
const dto = await useCase.execute({
|
||||
seasonId,
|
||||
championshipId,
|
||||
});
|
||||
|
||||
expect(dto.seasonId).toBe(seasonId);
|
||||
expect(dto.championshipId).toBe(championshipId);
|
||||
expect(dto.championshipName).toBe('Driver Championship');
|
||||
expect(dto.rows.length).toBeGreaterThan(0);
|
||||
|
||||
const rows = dto.rows;
|
||||
const sorted = [...rows].sort((a, b) => b.totalPoints - a.totalPoints);
|
||||
expect(rows.map((r) => r.participant.id)).toEqual(
|
||||
sorted.map((r) => r.participant.id),
|
||||
);
|
||||
|
||||
const leader = rows[0];
|
||||
expect(leader.resultsCounted).toBeLessThanOrEqual(6);
|
||||
expect(leader.resultsDropped).toBeGreaterThanOrEqual(0);
|
||||
});
|
||||
});
|
||||
88
tests/unit/domain/services/DropScoreApplier.test.ts
Normal file
88
tests/unit/domain/services/DropScoreApplier.test.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
|
||||
import { DropScoreApplier } from '@gridpilot/racing/domain/services/DropScoreApplier';
|
||||
import type { EventPointsEntry } from '@gridpilot/racing/domain/services/DropScoreApplier';
|
||||
import type { DropScorePolicy } from '@gridpilot/racing/domain/value-objects/DropScorePolicy';
|
||||
|
||||
describe('DropScoreApplier', () => {
|
||||
it('with strategy none counts all events and drops none', () => {
|
||||
const applier = new DropScoreApplier();
|
||||
|
||||
const policy: DropScorePolicy = {
|
||||
strategy: 'none',
|
||||
};
|
||||
|
||||
const events: EventPointsEntry[] = [
|
||||
{ eventId: 'event-1', points: 25 },
|
||||
{ eventId: 'event-2', points: 18 },
|
||||
{ eventId: 'event-3', points: 15 },
|
||||
];
|
||||
|
||||
const result = applier.apply(policy, events);
|
||||
|
||||
expect(result.counted).toHaveLength(3);
|
||||
expect(result.dropped).toHaveLength(0);
|
||||
expect(result.totalPoints).toBe(25 + 18 + 15);
|
||||
});
|
||||
|
||||
it('with bestNResults keeps the highest scoring events and drops the rest', () => {
|
||||
const applier = new DropScoreApplier();
|
||||
|
||||
const policy: DropScorePolicy = {
|
||||
strategy: 'bestNResults',
|
||||
count: 6,
|
||||
};
|
||||
|
||||
const events: EventPointsEntry[] = [
|
||||
{ eventId: 'event-1', points: 25 },
|
||||
{ eventId: 'event-2', points: 18 },
|
||||
{ eventId: 'event-3', points: 15 },
|
||||
{ eventId: 'event-4', points: 12 },
|
||||
{ eventId: 'event-5', points: 10 },
|
||||
{ eventId: 'event-6', points: 8 },
|
||||
{ eventId: 'event-7', points: 6 },
|
||||
{ eventId: 'event-8', points: 4 },
|
||||
];
|
||||
|
||||
const result = applier.apply(policy, events);
|
||||
|
||||
expect(result.counted).toHaveLength(6);
|
||||
expect(result.dropped).toHaveLength(2);
|
||||
|
||||
const countedIds = result.counted.map((e) => e.eventId);
|
||||
expect(countedIds).toEqual([
|
||||
'event-1',
|
||||
'event-2',
|
||||
'event-3',
|
||||
'event-4',
|
||||
'event-5',
|
||||
'event-6',
|
||||
]);
|
||||
|
||||
const droppedIds = result.dropped.map((e) => e.eventId);
|
||||
expect(droppedIds).toEqual(['event-7', 'event-8']);
|
||||
|
||||
expect(result.totalPoints).toBe(25 + 18 + 15 + 12 + 10 + 8);
|
||||
});
|
||||
|
||||
it('bestNResults with count greater than available events counts all of them', () => {
|
||||
const applier = new DropScoreApplier();
|
||||
|
||||
const policy: DropScorePolicy = {
|
||||
strategy: 'bestNResults',
|
||||
count: 10,
|
||||
};
|
||||
|
||||
const events: EventPointsEntry[] = [
|
||||
{ eventId: 'event-1', points: 25 },
|
||||
{ eventId: 'event-2', points: 18 },
|
||||
{ eventId: 'event-3', points: 15 },
|
||||
];
|
||||
|
||||
const result = applier.apply(policy, events);
|
||||
|
||||
expect(result.counted).toHaveLength(3);
|
||||
expect(result.dropped).toHaveLength(0);
|
||||
expect(result.totalPoints).toBe(25 + 18 + 15);
|
||||
});
|
||||
});
|
||||
266
tests/unit/domain/services/EventScoringService.test.ts
Normal file
266
tests/unit/domain/services/EventScoringService.test.ts
Normal file
@@ -0,0 +1,266 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
|
||||
import { EventScoringService } from '@gridpilot/racing/domain/services/EventScoringService';
|
||||
import type { ParticipantRef } from '@gridpilot/racing/domain/value-objects/ParticipantRef';
|
||||
import type { SessionType } from '@gridpilot/racing/domain/value-objects/SessionType';
|
||||
import { PointsTable } from '@gridpilot/racing/domain/value-objects/PointsTable';
|
||||
import type { BonusRule } from '@gridpilot/racing/domain/value-objects/BonusRule';
|
||||
import type { ChampionshipConfig } from '@gridpilot/racing/domain/value-objects/ChampionshipConfig';
|
||||
import type { Result } from '@gridpilot/racing/domain/entities/Result';
|
||||
import type { Penalty } from '@gridpilot/racing/domain/entities/Penalty';
|
||||
import type { ChampionshipType } from '@gridpilot/racing/domain/value-objects/ChampionshipType';
|
||||
|
||||
function makeDriverRef(id: string): ParticipantRef {
|
||||
return {
|
||||
type: 'driver' as ChampionshipType,
|
||||
id,
|
||||
};
|
||||
}
|
||||
|
||||
function makePointsTable(points: number[]): PointsTable {
|
||||
const pointsByPosition: Record<number, number> = {};
|
||||
points.forEach((value, index) => {
|
||||
pointsByPosition[index + 1] = value;
|
||||
});
|
||||
return new PointsTable(pointsByPosition);
|
||||
}
|
||||
|
||||
function makeChampionshipConfig(params: {
|
||||
id: string;
|
||||
name: string;
|
||||
sessionTypes: SessionType[];
|
||||
mainPoints: number[];
|
||||
sprintPoints?: number[];
|
||||
mainBonusRules?: BonusRule[];
|
||||
}): ChampionshipConfig {
|
||||
const { id, name, sessionTypes, mainPoints, sprintPoints, mainBonusRules } = params;
|
||||
|
||||
const pointsTableBySessionType: Record<SessionType, PointsTable> = {} as Record<SessionType, PointsTable>;
|
||||
|
||||
sessionTypes.forEach((sessionType) => {
|
||||
if (sessionType === 'main') {
|
||||
pointsTableBySessionType[sessionType] = makePointsTable(mainPoints);
|
||||
} else if (sessionType === 'sprint' && sprintPoints) {
|
||||
pointsTableBySessionType[sessionType] = makePointsTable(sprintPoints);
|
||||
} else {
|
||||
pointsTableBySessionType[sessionType] = new PointsTable({});
|
||||
}
|
||||
});
|
||||
|
||||
const bonusRulesBySessionType: Record<SessionType, BonusRule[]> = {} as Record<SessionType, BonusRule[]>;
|
||||
sessionTypes.forEach((sessionType) => {
|
||||
if (sessionType === 'main' && mainBonusRules) {
|
||||
bonusRulesBySessionType[sessionType] = mainBonusRules;
|
||||
} else {
|
||||
bonusRulesBySessionType[sessionType] = [];
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
id,
|
||||
name,
|
||||
type: 'driver',
|
||||
sessionTypes,
|
||||
pointsTableBySessionType,
|
||||
bonusRulesBySessionType,
|
||||
dropScorePolicy: {
|
||||
strategy: 'none',
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe('EventScoringService', () => {
|
||||
const seasonId = 'season-1';
|
||||
|
||||
it('assigns base points based on finishing positions for a main race', () => {
|
||||
const service = new EventScoringService();
|
||||
|
||||
const championship = makeChampionshipConfig({
|
||||
id: 'champ-1',
|
||||
name: 'Driver Championship',
|
||||
sessionTypes: ['main'],
|
||||
mainPoints: [25, 18, 15, 12, 10],
|
||||
});
|
||||
|
||||
const results: Result[] = [
|
||||
{
|
||||
id: 'result-1',
|
||||
raceId: 'race-1',
|
||||
driverId: 'driver-1',
|
||||
position: 1,
|
||||
fastestLap: 90000,
|
||||
incidents: 0,
|
||||
startPosition: 1,
|
||||
},
|
||||
{
|
||||
id: 'result-2',
|
||||
raceId: 'race-1',
|
||||
driverId: 'driver-2',
|
||||
position: 2,
|
||||
fastestLap: 90500,
|
||||
incidents: 0,
|
||||
startPosition: 2,
|
||||
},
|
||||
{
|
||||
id: 'result-3',
|
||||
raceId: 'race-1',
|
||||
driverId: 'driver-3',
|
||||
position: 3,
|
||||
fastestLap: 91000,
|
||||
incidents: 0,
|
||||
startPosition: 3,
|
||||
},
|
||||
{
|
||||
id: 'result-4',
|
||||
raceId: 'race-1',
|
||||
driverId: 'driver-4',
|
||||
position: 4,
|
||||
fastestLap: 91500,
|
||||
incidents: 0,
|
||||
startPosition: 4,
|
||||
},
|
||||
{
|
||||
id: 'result-5',
|
||||
raceId: 'race-1',
|
||||
driverId: 'driver-5',
|
||||
position: 5,
|
||||
fastestLap: 92000,
|
||||
incidents: 0,
|
||||
startPosition: 5,
|
||||
},
|
||||
];
|
||||
|
||||
const penalties: Penalty[] = [];
|
||||
|
||||
const points = service.scoreSession({
|
||||
seasonId,
|
||||
championship,
|
||||
sessionType: 'main',
|
||||
results,
|
||||
penalties,
|
||||
});
|
||||
|
||||
const byParticipant = new Map(points.map((p) => [p.participant.id, p]));
|
||||
|
||||
expect(byParticipant.get('driver-1')?.basePoints).toBe(25);
|
||||
expect(byParticipant.get('driver-2')?.basePoints).toBe(18);
|
||||
expect(byParticipant.get('driver-3')?.basePoints).toBe(15);
|
||||
expect(byParticipant.get('driver-4')?.basePoints).toBe(12);
|
||||
expect(byParticipant.get('driver-5')?.basePoints).toBe(10);
|
||||
|
||||
for (const entry of byParticipant.values()) {
|
||||
expect(entry.bonusPoints).toBe(0);
|
||||
expect(entry.penaltyPoints).toBe(0);
|
||||
expect(entry.totalPoints).toBe(entry.basePoints);
|
||||
}
|
||||
});
|
||||
|
||||
it('applies fastest lap bonus only when inside top 10', () => {
|
||||
const service = new EventScoringService();
|
||||
|
||||
const fastestLapBonus: BonusRule = {
|
||||
id: 'bonus-fastest-lap',
|
||||
type: 'fastestLap',
|
||||
points: 1,
|
||||
requiresFinishInTopN: 10,
|
||||
};
|
||||
|
||||
const championship = makeChampionshipConfig({
|
||||
id: 'champ-1',
|
||||
name: 'Driver Championship',
|
||||
sessionTypes: ['main'],
|
||||
mainPoints: [25, 18, 15, 12, 10, 8, 6, 4, 2, 1],
|
||||
mainBonusRules: [fastestLapBonus],
|
||||
});
|
||||
|
||||
const baseResultTemplate = {
|
||||
raceId: 'race-1',
|
||||
incidents: 0,
|
||||
} as const;
|
||||
|
||||
const resultsP11Fastest: Result[] = [
|
||||
{
|
||||
id: 'result-1',
|
||||
...baseResultTemplate,
|
||||
driverId: 'driver-1',
|
||||
position: 1,
|
||||
startPosition: 1,
|
||||
fastestLap: 91000,
|
||||
},
|
||||
{
|
||||
id: 'result-2',
|
||||
...baseResultTemplate,
|
||||
driverId: 'driver-2',
|
||||
position: 2,
|
||||
startPosition: 2,
|
||||
fastestLap: 90500,
|
||||
},
|
||||
{
|
||||
id: 'result-3',
|
||||
...baseResultTemplate,
|
||||
driverId: 'driver-3',
|
||||
position: 11,
|
||||
startPosition: 15,
|
||||
fastestLap: 90000,
|
||||
},
|
||||
];
|
||||
|
||||
const penalties: Penalty[] = [];
|
||||
|
||||
const pointsNoBonus = service.scoreSession({
|
||||
seasonId,
|
||||
championship,
|
||||
sessionType: 'main',
|
||||
results: resultsP11Fastest,
|
||||
penalties,
|
||||
});
|
||||
|
||||
const mapNoBonus = new Map(pointsNoBonus.map((p) => [p.participant.id, p]));
|
||||
|
||||
expect(mapNoBonus.get('driver-3')?.bonusPoints).toBe(0);
|
||||
|
||||
const resultsP8Fastest: Result[] = [
|
||||
{
|
||||
id: 'result-1',
|
||||
...baseResultTemplate,
|
||||
driverId: 'driver-1',
|
||||
position: 1,
|
||||
startPosition: 1,
|
||||
fastestLap: 91000,
|
||||
},
|
||||
{
|
||||
id: 'result-2',
|
||||
...baseResultTemplate,
|
||||
driverId: 'driver-2',
|
||||
position: 2,
|
||||
startPosition: 2,
|
||||
fastestLap: 90500,
|
||||
},
|
||||
{
|
||||
id: 'result-3',
|
||||
...baseResultTemplate,
|
||||
driverId: 'driver-3',
|
||||
position: 8,
|
||||
startPosition: 15,
|
||||
fastestLap: 90000,
|
||||
},
|
||||
];
|
||||
|
||||
const pointsWithBonus = service.scoreSession({
|
||||
seasonId,
|
||||
championship,
|
||||
sessionType: 'main',
|
||||
results: resultsP8Fastest,
|
||||
penalties,
|
||||
});
|
||||
|
||||
const mapWithBonus = new Map(pointsWithBonus.map((p) => [p.participant.id, p]));
|
||||
|
||||
expect(mapWithBonus.get('driver-3')?.bonusPoints).toBe(1);
|
||||
expect(mapWithBonus.get('driver-3')?.totalPoints).toBe(
|
||||
(mapWithBonus.get('driver-3')?.basePoints || 0) +
|
||||
(mapWithBonus.get('driver-3')?.bonusPoints || 0) -
|
||||
(mapWithBonus.get('driver-3')?.penaltyPoints || 0),
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user