import { describe, it, expect, beforeAll, beforeEach } from 'vitest'; import { RatingTestContext } from './RatingTestContext'; import { CalculateRatingUseCase } from '../../../core/rating/application/use-cases/CalculateRatingUseCase'; import { Driver } from '../../../core/racing/domain/entities/Driver'; import { Race } from '../../../core/racing/domain/entities/Race'; import { League } from '../../../core/racing/domain/entities/League'; import { Result as RaceResult } from '../../../core/racing/domain/entities/result/Result'; describe('CalculateRatingUseCase', () => { let context: RatingTestContext; let calculateRatingUseCase: CalculateRatingUseCase; beforeAll(() => { context = RatingTestContext.create(); calculateRatingUseCase = new CalculateRatingUseCase({ driverRepository: context.driverRepository, raceRepository: context.raceRepository, resultRepository: context.resultRepository, ratingRepository: context.ratingRepository, eventPublisher: context.eventPublisher }); }); beforeEach(async () => { await context.clear(); }); describe('UseCase - Success Path', () => { it('should calculate rating after race completion with strong finish', async () => { // Given: A driver with baseline rating const driverId = 'd1'; const driver = Driver.create({ id: driverId, iracingId: '100', name: 'John Doe', country: 'US' }); await context.driverRepository.create(driver); // Given: A completed race with strong field const leagueId = 'l1'; const league = League.create({ id: leagueId, name: 'Pro League', description: 'Desc', ownerId: 'o1' }); await context.leagueRepository.create(league); const raceId = 'r1'; const race = Race.create({ id: raceId, leagueId, scheduledAt: new Date(Date.now() - 86400000), track: 'Spa', car: 'GT3', status: 'completed' }); await context.raceRepository.create(race); // Given: Strong race results (win against strong field) const result = RaceResult.create({ id: 'res1', raceId, driverId, position: 1, lapsCompleted: 20, totalTime: 3600, fastestLap: 105, points: 25, incidents: 0, startPosition: 1 }); await context.resultRepository.create(result); // When: CalculateRatingUseCase.execute() is called const ratingResult = await calculateRatingUseCase.execute({ driverId, raceId }); // Then: The rating should be calculated expect(ratingResult.isOk()).toBe(true); const rating = ratingResult.unwrap(); expect(rating.driverId.toString()).toBe(driverId); expect(rating.rating).toBeGreaterThan(0); expect(rating.components).toBeDefined(); expect(rating.components.resultsStrength).toBeGreaterThan(0); expect(rating.components.consistency).toBeGreaterThan(0); expect(rating.components.cleanDriving).toBeGreaterThan(0); expect(rating.components.racecraft).toBeGreaterThan(0); expect(rating.components.reliability).toBeGreaterThan(0); expect(rating.components.teamContribution).toBeGreaterThan(0); // And: EventPublisher should emit RatingCalculatedEvent expect(context.eventPublisher.events).toContainEqual( expect.objectContaining({ type: 'RatingCalculatedEvent' }) ); }); it('should calculate rating after race completion with poor finish', async () => { // Given: A driver with baseline rating const driverId = 'd1'; const driver = Driver.create({ id: driverId, iracingId: '100', name: 'John Doe', country: 'US' }); await context.driverRepository.create(driver); // Given: A completed race const leagueId = 'l1'; const league = League.create({ id: leagueId, name: 'Pro League', description: 'Desc', ownerId: 'o1' }); await context.leagueRepository.create(league); const raceId = 'r1'; const race = Race.create({ id: raceId, leagueId, scheduledAt: new Date(Date.now() - 86400000), track: 'Spa', car: 'GT3', status: 'completed' }); await context.raceRepository.create(race); // Given: Poor race results (last place with incidents) const result = RaceResult.create({ id: 'res1', raceId, driverId, position: 20, lapsCompleted: 18, totalTime: 4200, fastestLap: 120, points: 0, incidents: 5, startPosition: 15 }); await context.resultRepository.create(result); // When: CalculateRatingUseCase.execute() is called const ratingResult = await calculateRatingUseCase.execute({ driverId, raceId }); // Then: The rating should be calculated expect(ratingResult.isOk()).toBe(true); const rating = ratingResult.unwrap(); expect(rating.driverId.toString()).toBe(driverId); expect(rating.rating).toBeGreaterThan(0); expect(rating.components).toBeDefined(); expect(rating.components.resultsStrength).toBeGreaterThan(0); expect(rating.components.consistency).toBeGreaterThan(0); expect(rating.components.cleanDriving).toBeGreaterThan(0); expect(rating.components.racecraft).toBeGreaterThan(0); expect(rating.components.reliability).toBeGreaterThan(0); expect(rating.components.teamContribution).toBeGreaterThan(0); // And: EventPublisher should emit RatingCalculatedEvent expect(context.eventPublisher.events).toContainEqual( expect.objectContaining({ type: 'RatingCalculatedEvent' }) ); }); it('should calculate rating with consistency over multiple races', async () => { // Given: A driver with baseline rating const driverId = 'd1'; const driver = Driver.create({ id: driverId, iracingId: '100', name: 'John Doe', country: 'US' }); await context.driverRepository.create(driver); // Given: Multiple completed races with consistent results const leagueId = 'l1'; const league = League.create({ id: leagueId, name: 'Pro League', description: 'Desc', ownerId: 'o1' }); await context.leagueRepository.create(league); for (let i = 1; i <= 5; i++) { const raceId = `r${i}`; const race = Race.create({ id: raceId, leagueId, scheduledAt: new Date(Date.now() - (i * 86400000)), track: 'Spa', car: 'GT3', status: 'completed' }); await context.raceRepository.create(race); const result = RaceResult.create({ id: `res${i}`, raceId, driverId, position: 5, lapsCompleted: 20, totalTime: 3600, fastestLap: 105, points: 10, incidents: 1, startPosition: 5 }); await context.resultRepository.create(result); } // When: CalculateRatingUseCase.execute() is called for the last race const ratingResult = await calculateRatingUseCase.execute({ driverId, raceId: 'r5' }); // Then: The rating should reflect consistency expect(ratingResult.isOk()).toBe(true); const rating = ratingResult.unwrap(); expect(rating.driverId.toString()).toBe(driverId); expect(rating.components.consistency).toBeGreaterThan(0); expect(rating.components.consistency).toBeGreaterThan(rating.components.resultsStrength); }); }); describe('UseCase - Edge Cases', () => { it('should handle DNF (Did Not Finish) appropriately', async () => { // Given: A driver with baseline rating const driverId = 'd1'; const driver = Driver.create({ id: driverId, iracingId: '100', name: 'John Doe', country: 'US' }); await context.driverRepository.create(driver); // Given: A completed race with DNF const leagueId = 'l1'; const league = League.create({ id: leagueId, name: 'Pro League', description: 'Desc', ownerId: 'o1' }); await context.leagueRepository.create(league); const raceId = 'r1'; const race = Race.create({ id: raceId, leagueId, scheduledAt: new Date(Date.now() - 86400000), track: 'Spa', car: 'GT3', status: 'completed' }); await context.raceRepository.create(race); // Given: DNF result const result = RaceResult.create({ id: 'res1', raceId, driverId, position: 2, lapsCompleted: 10, totalTime: 0, fastestLap: 0, points: 0, incidents: 3, startPosition: 10 }); await context.resultRepository.create(result); // When: CalculateRatingUseCase.execute() is called const ratingResult = await calculateRatingUseCase.execute({ driverId, raceId }); // Then: The rating should be calculated with DNF impact expect(ratingResult.isOk()).toBe(true); const rating = ratingResult.unwrap(); expect(rating.driverId.toString()).toBe(driverId); expect(rating.components.reliability).toBeGreaterThan(0); expect(rating.components.reliability).toBeLessThan(100); }); it('should handle DNS (Did Not Start) appropriately', async () => { // Given: A driver with baseline rating const driverId = 'd1'; const driver = Driver.create({ id: driverId, iracingId: '100', name: 'John Doe', country: 'US' }); await context.driverRepository.create(driver); // Given: A race with DNS const leagueId = 'l1'; const league = League.create({ id: leagueId, name: 'Pro League', description: 'Desc', ownerId: 'o1' }); await context.leagueRepository.create(league); const raceId = 'r1'; const race = Race.create({ id: raceId, leagueId, scheduledAt: new Date(Date.now() - 86400000), track: 'Spa', car: 'GT3', status: 'completed' }); await context.raceRepository.create(race); // Given: DNS result (no participation) const result = RaceResult.create({ id: 'res1', raceId, driverId, position: 0, lapsCompleted: 0, totalTime: 0, fastestLap: 0, points: 0, incidents: 0, startPosition: 0 }); await context.resultRepository.create(result); // When: CalculateRatingUseCase.execute() is called const ratingResult = await calculateRatingUseCase.execute({ driverId, raceId }); // Then: The rating should be calculated with DNS impact expect(ratingResult.isOk()).toBe(true); const rating = ratingResult.unwrap(); expect(rating.driverId.toString()).toBe(driverId); expect(rating.components.reliability).toBeGreaterThan(0); expect(rating.components.reliability).toBeLessThan(100); }); it('should handle small field sizes appropriately', async () => { // Given: A driver with baseline rating const driverId = 'd1'; const driver = Driver.create({ id: driverId, iracingId: '100', name: 'John Doe', country: 'US' }); await context.driverRepository.create(driver); // Given: A completed race with small field const leagueId = 'l1'; const league = League.create({ id: leagueId, name: 'Pro League', description: 'Desc', ownerId: 'o1' }); await context.leagueRepository.create(league); const raceId = 'r1'; const race = Race.create({ id: raceId, leagueId, scheduledAt: new Date(Date.now() - 86400000), track: 'Spa', car: 'GT3', status: 'completed' }); await context.raceRepository.create(race); // Given: Win in small field (5 drivers) const result = RaceResult.create({ id: 'res1', raceId, driverId, position: 1, lapsCompleted: 20, totalTime: 3600, fastestLap: 105, points: 25, incidents: 0, startPosition: 1 }); await context.resultRepository.create(result); // When: CalculateRatingUseCase.execute() is called const ratingResult = await calculateRatingUseCase.execute({ driverId, raceId }); // Then: The rating should be normalized for small field expect(ratingResult.isOk()).toBe(true); const rating = ratingResult.unwrap(); expect(rating.driverId.toString()).toBe(driverId); expect(rating.rating).toBeGreaterThan(0); }); it('should handle large field sizes appropriately', async () => { // Given: A driver with baseline rating const driverId = 'd1'; const driver = Driver.create({ id: driverId, iracingId: '100', name: 'John Doe', country: 'US' }); await context.driverRepository.create(driver); // Given: A completed race with large field const leagueId = 'l1'; const league = League.create({ id: leagueId, name: 'Pro League', description: 'Desc', ownerId: 'o1' }); await context.leagueRepository.create(league); const raceId = 'r1'; const race = Race.create({ id: raceId, leagueId, scheduledAt: new Date(Date.now() - 86400000), track: 'Spa', car: 'GT3', status: 'completed' }); await context.raceRepository.create(race); // Given: Win in large field (30 drivers) const result = RaceResult.create({ id: 'res1', raceId, driverId, position: 1, lapsCompleted: 20, totalTime: 3600, fastestLap: 105, points: 25, incidents: 0, startPosition: 1 }); await context.resultRepository.create(result); // When: CalculateRatingUseCase.execute() is called const ratingResult = await calculateRatingUseCase.execute({ driverId, raceId }); // Then: The rating should be normalized for large field expect(ratingResult.isOk()).toBe(true); const rating = ratingResult.unwrap(); expect(rating.driverId.toString()).toBe(driverId); expect(rating.rating).toBeGreaterThan(0); }); it('should handle clean races appropriately', async () => { // Given: A driver with baseline rating const driverId = 'd1'; const driver = Driver.create({ id: driverId, iracingId: '100', name: 'John Doe', country: 'US' }); await context.driverRepository.create(driver); // Given: A completed race with zero incidents const leagueId = 'l1'; const league = League.create({ id: leagueId, name: 'Pro League', description: 'Desc', ownerId: 'o1' }); await context.leagueRepository.create(league); const raceId = 'r1'; const race = Race.create({ id: raceId, leagueId, scheduledAt: new Date(Date.now() - 86400000), track: 'Spa', car: 'GT3', status: 'completed' }); await context.raceRepository.create(race); // Given: Clean race (zero incidents) const result = RaceResult.create({ id: 'res1', raceId, driverId, position: 5, lapsCompleted: 20, totalTime: 3600, fastestLap: 105, points: 10, incidents: 0, startPosition: 5 }); await context.resultRepository.create(result); // When: CalculateRatingUseCase.execute() is called const ratingResult = await calculateRatingUseCase.execute({ driverId, raceId }); // Then: The rating should reflect clean driving expect(ratingResult.isOk()).toBe(true); const rating = ratingResult.unwrap(); expect(rating.driverId.toString()).toBe(driverId); expect(rating.components.cleanDriving).toBeGreaterThan(0); expect(rating.components.cleanDriving).toBeGreaterThan(50); }); it('should handle penalty scenarios appropriately', async () => { // Given: A driver with baseline rating const driverId = 'd1'; const driver = Driver.create({ id: driverId, iracingId: '100', name: 'John Doe', country: 'US' }); await context.driverRepository.create(driver); // Given: A completed race with penalty const leagueId = 'l1'; const league = League.create({ id: leagueId, name: 'Pro League', description: 'Desc', ownerId: 'o1' }); await context.leagueRepository.create(league); const raceId = 'r1'; const race = Race.create({ id: raceId, leagueId, scheduledAt: new Date(Date.now() - 86400000), track: 'Spa', car: 'GT3', status: 'completed' }); await context.raceRepository.create(race); // Given: Race with penalty const result = RaceResult.create({ id: 'res1', raceId, driverId, position: 5, lapsCompleted: 20, totalTime: 3600, fastestLap: 105, points: 10, incidents: 2, startPosition: 5 }); await context.resultRepository.create(result); // When: CalculateRatingUseCase.execute() is called const ratingResult = await calculateRatingUseCase.execute({ driverId, raceId }); // Then: The rating should be affected by penalty expect(ratingResult.isOk()).toBe(true); const rating = ratingResult.unwrap(); expect(rating.driverId.toString()).toBe(driverId); expect(rating.components.cleanDriving).toBeGreaterThan(0); expect(rating.components.cleanDriving).toBeLessThan(100); }); }); describe('UseCase - Error Handling', () => { it('should handle missing driver', async () => { // Given: A non-existent driver const driverId = 'd999'; // Given: A completed race const leagueId = 'l1'; const league = League.create({ id: leagueId, name: 'Pro League', description: 'Desc', ownerId: 'o1' }); await context.leagueRepository.create(league); const raceId = 'r1'; const race = Race.create({ id: raceId, leagueId, scheduledAt: new Date(Date.now() - 86400000), track: 'Spa', car: 'GT3', status: 'completed' }); await context.raceRepository.create(race); // When: CalculateRatingUseCase.execute() is called const ratingResult = await calculateRatingUseCase.execute({ driverId, raceId }); // Then: The result should be an error expect(ratingResult.isErr()).toBe(true); }); it('should handle missing race', async () => { // Given: A driver with baseline rating const driverId = 'd1'; const driver = Driver.create({ id: driverId, iracingId: '100', name: 'John Doe', country: 'US' }); await context.driverRepository.create(driver); // Given: A non-existent race const raceId = 'r999'; // When: CalculateRatingUseCase.execute() is called const ratingResult = await calculateRatingUseCase.execute({ driverId, raceId }); // Then: The result should be an error expect(ratingResult.isErr()).toBe(true); }); it('should handle missing race results', async () => { // Given: A driver with baseline rating const driverId = 'd1'; const driver = Driver.create({ id: driverId, iracingId: '100', name: 'John Doe', country: 'US' }); await context.driverRepository.create(driver); // Given: A completed race with no results const leagueId = 'l1'; const league = League.create({ id: leagueId, name: 'Pro League', description: 'Desc', ownerId: 'o1' }); await context.leagueRepository.create(league); const raceId = 'r1'; const race = Race.create({ id: raceId, leagueId, scheduledAt: new Date(Date.now() - 86400000), track: 'Spa', car: 'GT3', status: 'completed' }); await context.raceRepository.create(race); // When: CalculateRatingUseCase.execute() is called const ratingResult = await calculateRatingUseCase.execute({ driverId, raceId }); // Then: The result should be an error expect(ratingResult.isErr()).toBe(true); }); }); describe('UseCase - Data Orchestration', () => { it('should correctly format rating data structure', async () => { // Given: A driver with baseline rating const driverId = 'd1'; const driver = Driver.create({ id: driverId, iracingId: '100', name: 'John Doe', country: 'US' }); await context.driverRepository.create(driver); // Given: A completed race const leagueId = 'l1'; const league = League.create({ id: leagueId, name: 'Pro League', description: 'Desc', ownerId: 'o1' }); await context.leagueRepository.create(league); const raceId = 'r1'; const race = Race.create({ id: raceId, leagueId, scheduledAt: new Date(Date.now() - 86400000), track: 'Spa', car: 'GT3', status: 'completed' }); await context.raceRepository.create(race); const result = RaceResult.create({ id: 'res1', raceId, driverId, position: 1, lapsCompleted: 20, totalTime: 3600, fastestLap: 105, points: 25, incidents: 0, startPosition: 1 }); await context.resultRepository.create(result); // When: CalculateRatingUseCase.execute() is called const ratingResult = await calculateRatingUseCase.execute({ driverId, raceId }); // Then: The rating data should be correctly formatted expect(ratingResult.isOk()).toBe(true); const rating = ratingResult.unwrap(); expect(rating.driverId).toBeDefined(); expect(rating.rating).toBeDefined(); expect(rating.components).toBeDefined(); expect(rating.components.resultsStrength).toBeDefined(); expect(rating.components.consistency).toBeDefined(); expect(rating.components.cleanDriving).toBeDefined(); expect(rating.components.racecraft).toBeDefined(); expect(rating.components.reliability).toBeDefined(); expect(rating.components.teamContribution).toBeDefined(); expect(rating.timestamp).toBeDefined(); }); it('should correctly calculate results strength component', async () => { // Given: A driver with baseline rating const driverId = 'd1'; const driver = Driver.create({ id: driverId, iracingId: '100', name: 'John Doe', country: 'US' }); await context.driverRepository.create(driver); // Given: A completed race with strong finish const leagueId = 'l1'; const league = League.create({ id: leagueId, name: 'Pro League', description: 'Desc', ownerId: 'o1' }); await context.leagueRepository.create(league); const raceId = 'r1'; const race = Race.create({ id: raceId, leagueId, scheduledAt: new Date(Date.now() - 86400000), track: 'Spa', car: 'GT3', status: 'completed' }); await context.raceRepository.create(race); const result = RaceResult.create({ id: 'res1', raceId, driverId, position: 1, lapsCompleted: 20, totalTime: 3600, fastestLap: 105, points: 25, incidents: 0, startPosition: 1 }); await context.resultRepository.create(result); // When: CalculateRatingUseCase.execute() is called const ratingResult = await calculateRatingUseCase.execute({ driverId, raceId }); // Then: The results strength should be calculated expect(ratingResult.isOk()).toBe(true); const rating = ratingResult.unwrap(); expect(rating.components.resultsStrength).toBeGreaterThan(0); expect(rating.components.resultsStrength).toBeGreaterThan(50); }); it('should correctly calculate consistency component', async () => { // Given: A driver with baseline rating const driverId = 'd1'; const driver = Driver.create({ id: driverId, iracingId: '100', name: 'John Doe', country: 'US' }); await context.driverRepository.create(driver); // Given: Multiple completed races with consistent results const leagueId = 'l1'; const league = League.create({ id: leagueId, name: 'Pro League', description: 'Desc', ownerId: 'o1' }); await context.leagueRepository.create(league); for (let i = 1; i <= 5; i++) { const raceId = `r${i}`; const race = Race.create({ id: raceId, leagueId, scheduledAt: new Date(Date.now() - (i * 86400000)), track: 'Spa', car: 'GT3', status: 'completed' }); await context.raceRepository.create(race); const result = RaceResult.create({ id: `res${i}`, raceId, driverId, position: 5, lapsCompleted: 20, totalTime: 3600, fastestLap: 105, points: 10, incidents: 1, startPosition: 5 }); await context.resultRepository.create(result); } // When: CalculateRatingUseCase.execute() is called for the last race const ratingResult = await calculateRatingUseCase.execute({ driverId, raceId: 'r5' }); // Then: The consistency should be calculated expect(ratingResult.isOk()).toBe(true); const rating = ratingResult.unwrap(); expect(rating.components.consistency).toBeGreaterThan(0); expect(rating.components.consistency).toBeGreaterThan(50); }); it('should correctly calculate clean driving component', async () => { // Given: A driver with baseline rating const driverId = 'd1'; const driver = Driver.create({ id: driverId, iracingId: '100', name: 'John Doe', country: 'US' }); await context.driverRepository.create(driver); // Given: A completed race with zero incidents const leagueId = 'l1'; const league = League.create({ id: leagueId, name: 'Pro League', description: 'Desc', ownerId: 'o1' }); await context.leagueRepository.create(league); const raceId = 'r1'; const race = Race.create({ id: raceId, leagueId, scheduledAt: new Date(Date.now() - 86400000), track: 'Spa', car: 'GT3', status: 'completed' }); await context.raceRepository.create(race); const result = RaceResult.create({ id: 'res1', raceId, driverId, position: 5, lapsCompleted: 20, totalTime: 3600, fastestLap: 105, points: 10, incidents: 0, startPosition: 5 }); await context.resultRepository.create(result); // When: CalculateRatingUseCase.execute() is called const ratingResult = await calculateRatingUseCase.execute({ driverId, raceId }); // Then: The clean driving should be calculated expect(ratingResult.isOk()).toBe(true); const rating = ratingResult.unwrap(); expect(rating.components.cleanDriving).toBeGreaterThan(0); expect(rating.components.cleanDriving).toBeGreaterThan(50); }); it('should correctly calculate racecraft component', async () => { // Given: A driver with baseline rating const driverId = 'd1'; const driver = Driver.create({ id: driverId, iracingId: '100', name: 'John Doe', country: 'US' }); await context.driverRepository.create(driver); // Given: A completed race with positions gained const leagueId = 'l1'; const league = League.create({ id: leagueId, name: 'Pro League', description: 'Desc', ownerId: 'o1' }); await context.leagueRepository.create(league); const raceId = 'r1'; const race = Race.create({ id: raceId, leagueId, scheduledAt: new Date(Date.now() - 86400000), track: 'Spa', car: 'GT3', status: 'completed' }); await context.raceRepository.create(race); const result = RaceResult.create({ id: 'res1', raceId, driverId, position: 3, lapsCompleted: 20, totalTime: 3600, fastestLap: 105, points: 15, incidents: 1, startPosition: 10 }); await context.resultRepository.create(result); // When: CalculateRatingUseCase.execute() is called const ratingResult = await calculateRatingUseCase.execute({ driverId, raceId }); // Then: The racecraft should be calculated expect(ratingResult.isOk()).toBe(true); const rating = ratingResult.unwrap(); expect(rating.components.racecraft).toBeGreaterThan(0); expect(rating.components.racecraft).toBeGreaterThan(50); }); it('should correctly calculate reliability component', async () => { // Given: A driver with baseline rating const driverId = 'd1'; const driver = Driver.create({ id: driverId, iracingId: '100', name: 'John Doe', country: 'US' }); await context.driverRepository.create(driver); // Given: Multiple completed races with good attendance const leagueId = 'l1'; const league = League.create({ id: leagueId, name: 'Pro League', description: 'Desc', ownerId: 'o1' }); await context.leagueRepository.create(league); for (let i = 1; i <= 5; i++) { const raceId = `r${i}`; const race = Race.create({ id: raceId, leagueId, scheduledAt: new Date(Date.now() - (i * 86400000)), track: 'Spa', car: 'GT3', status: 'completed' }); await context.raceRepository.create(race); const result = RaceResult.create({ id: `res${i}`, raceId, driverId, position: 5, lapsCompleted: 20, totalTime: 3600, fastestLap: 105, points: 10, incidents: 1, startPosition: 5 }); await context.resultRepository.create(result); } // When: CalculateRatingUseCase.execute() is called for the last race const ratingResult = await calculateRatingUseCase.execute({ driverId, raceId: 'r5' }); // Then: The reliability should be calculated expect(ratingResult.isOk()).toBe(true); const rating = ratingResult.unwrap(); expect(rating.components.reliability).toBeGreaterThan(0); expect(rating.components.reliability).toBeGreaterThan(50); }); it('should correctly calculate team contribution component', async () => { // Given: A driver with baseline rating const driverId = 'd1'; const driver = Driver.create({ id: driverId, iracingId: '100', name: 'John Doe', country: 'US' }); await context.driverRepository.create(driver); // Given: A completed race with points const leagueId = 'l1'; const league = League.create({ id: leagueId, name: 'Pro League', description: 'Desc', ownerId: 'o1' }); await context.leagueRepository.create(league); const raceId = 'r1'; const race = Race.create({ id: raceId, leagueId, scheduledAt: new Date(Date.now() - 86400000), track: 'Spa', car: 'GT3', status: 'completed' }); await context.raceRepository.create(race); const result = RaceResult.create({ id: 'res1', raceId, driverId, position: 1, lapsCompleted: 20, totalTime: 3600, fastestLap: 105, points: 25, incidents: 0, startPosition: 1 }); await context.resultRepository.create(result); // When: CalculateRatingUseCase.execute() is called const ratingResult = await calculateRatingUseCase.execute({ driverId, raceId }); // Then: The team contribution should be calculated expect(ratingResult.isOk()).toBe(true); const rating = ratingResult.unwrap(); expect(rating.components.teamContribution).toBeGreaterThan(0); expect(rating.components.teamContribution).toBeGreaterThan(50); }); }); });