import { describe, it, expect, beforeEach } from 'vitest'; import { LeaguesTestContext } from '../LeaguesTestContext'; import { Driver } from '../../../../core/racing/domain/entities/Driver'; import { Standing } from '../../../../core/racing/domain/entities/Standing'; import { Result } from '../../../../core/racing/domain/entities/result/Result'; import { Race } from '../../../../core/racing/domain/entities/Race'; import { League } from '../../../../core/racing/domain/entities/League'; describe('StandingsCalculation', () => { let context: LeaguesTestContext; beforeEach(() => { context = new LeaguesTestContext(); }); it('should correctly calculate driver statistics from race results', async () => { // Given: A league exists const leagueId = 'league-123'; const driverId = 'driver-1'; await context.racingLeagueRepository.create(League.create({ id: leagueId, name: 'Test League', description: 'Test Description', ownerId: 'owner-1', })); await context.racingDriverRepository.create(Driver.create({ id: driverId, name: 'Driver One', iracingId: 'ir-1', country: 'US', })); // And: A driver has completed races const race1Id = 'race-1'; const race2Id = 'race-2'; await context.raceRepository.create(Race.create({ id: race1Id, leagueId, scheduledAt: new Date(), track: 'Daytona', car: 'GT3', status: 'completed', })); await context.raceRepository.create(Race.create({ id: race2Id, leagueId, scheduledAt: new Date(), track: 'Sebring', car: 'GT3', status: 'completed', })); // And: The driver has results (1 win, 1 podium) await context.resultRepository.create(Result.create({ id: 'res-1', raceId: race1Id, driverId, position: 1, fastestLap: 120000, incidents: 0, startPosition: 1, points: 25, })); await context.resultRepository.create(Result.create({ id: 'res-2', raceId: race2Id, driverId, position: 3, fastestLap: 121000, incidents: 2, startPosition: 5, points: 15, })); // When: Standings are recalculated await context.standingRepository.recalculate(leagueId); // Then: Driver statistics should show correct values const standings = await context.standingRepository.findByLeagueId(leagueId); const driverStanding = standings.find(s => s.driverId.toString() === driverId); expect(driverStanding).toBeDefined(); expect(driverStanding?.wins).toBe(1); expect(driverStanding?.racesCompleted).toBe(2); // Points depend on the points system (default f1-2024: 1st=25, 3rd=15) expect(driverStanding?.points.toNumber()).toBe(40); }); });