code quality
Some checks failed
CI / lint-typecheck (pull_request) Failing after 12s
CI / tests (pull_request) Has been skipped
CI / contract-tests (pull_request) Has been skipped
CI / e2e-tests (pull_request) Has been skipped
CI / comment-pr (pull_request) Has been skipped
CI / commit-types (pull_request) Has been skipped

This commit is contained in:
2026-01-26 02:27:37 +01:00
parent bf2c0fdb0c
commit afef777961
23 changed files with 565 additions and 134 deletions

View File

@@ -1,6 +1,6 @@
/**
* Unit tests for CalculateRatingUseCase
*
*
* Tests business logic and orchestration using mocked ports.
*/
@@ -9,6 +9,11 @@ import { CalculateRatingUseCase } from './CalculateRatingUseCase';
import { Driver } from '../../../racing/domain/entities/Driver';
import { Race } from '../../../racing/domain/entities/Race';
import { Result } from '../../../racing/domain/entities/result/Result';
import { DriverRepository } from '../../../racing/domain/repositories/DriverRepository';
import { RaceRepository } from '../../../racing/domain/repositories/RaceRepository';
import { ResultRepository } from '../../../racing/domain/repositories/ResultRepository';
import { RatingRepository } from '../../ports/RatingRepository';
import { EventPublisher } from '../../../shared/ports/EventPublisher';
// Mock repositories and publisher
const mockDriverRepository = {
@@ -37,11 +42,11 @@ describe('CalculateRatingUseCase', () => {
beforeEach(() => {
vi.clearAllMocks();
useCase = new CalculateRatingUseCase({
driverRepository: mockDriverRepository as any,
raceRepository: mockRaceRepository as any,
resultRepository: mockResultRepository as any,
ratingRepository: mockRatingRepository as any,
eventPublisher: mockEventPublisher as any,
driverRepository: mockDriverRepository as unknown as DriverRepository,
raceRepository: mockRaceRepository as unknown as RaceRepository,
resultRepository: mockResultRepository as unknown as ResultRepository,
ratingRepository: mockRatingRepository as unknown as RatingRepository,
eventPublisher: mockEventPublisher as unknown as EventPublisher,
});
});

View File

@@ -14,6 +14,7 @@ import { RatingComponents } from '../../domain/RatingComponents';
import { RatingCalculatedEvent } from '../../domain/events/RatingCalculatedEvent';
import { DriverId } from '../../../racing/domain/entities/DriverId';
import { RaceId } from '../../../racing/domain/entities/RaceId';
import { Result as RaceResult } from '../../../racing/domain/entities/result/Result';
export interface CalculateRatingUseCasePorts {
driverRepository: DriverRepository;
@@ -84,12 +85,12 @@ export class CalculateRatingUseCase {
}
}
private calculateComponents(driverResult: any, allResults: any[]): RatingComponents {
const position = typeof driverResult.position === 'object' ? (typeof driverResult.position.toNumber === 'function' ? driverResult.position.toNumber() : driverResult.position.value) : driverResult.position;
private calculateComponents(driverResult: RaceResult, allResults: RaceResult[]): RatingComponents {
const position = driverResult.position.toNumber();
const totalDrivers = allResults.length;
const incidents = typeof driverResult.incidents === 'object' ? (typeof driverResult.incidents.toNumber === 'function' ? driverResult.incidents.toNumber() : driverResult.incidents.value) : driverResult.incidents;
const lapsCompleted = typeof driverResult.lapsCompleted === 'object' ? (typeof driverResult.lapsCompleted.toNumber === 'function' ? driverResult.lapsCompleted.toNumber() : driverResult.lapsCompleted.value) : (driverResult.lapsCompleted !== undefined ? driverResult.lapsCompleted : (driverResult.totalTime === 0 && (typeof position === 'object' ? position.value : position) > 0 ? 5 : (driverResult.points === 0 && (typeof position === 'object' ? position.value : position) > 0 ? 5 : 20)));
const startPosition = typeof driverResult.startPosition === 'object' ? driverResult.startPosition.toNumber() : driverResult.startPosition;
const incidents = driverResult.incidents.toNumber();
const startPosition = driverResult.startPosition.toNumber();
const points = driverResult.points;
// Results Strength: Based on position relative to field size
const resultsStrength = this.calculateResultsStrength(position, totalDrivers);
@@ -104,10 +105,12 @@ export class CalculateRatingUseCase {
const racecraft = this.calculateRacecraft(position, startPosition);
// Reliability: Based on laps completed and DNF/DNS
const reliability = this.calculateReliability(lapsCompleted, position, driverResult.points);
// For the Result entity, we need to determine reliability based on position and points
// If position is 0 (DNS) or points are 0 (DNF), reliability is low
const reliability = this.calculateReliabilityFromResult(position, points);
// Team Contribution: Based on points scored
const teamContribution = this.calculateTeamContribution(driverResult.points);
const teamContribution = this.calculateTeamContribution(points);
return {
resultsStrength,
@@ -119,6 +122,21 @@ export class CalculateRatingUseCase {
};
}
private calculateReliabilityFromResult(position: number, points: number): number {
// DNS (Did Not Start) - position 0
if (position === 0) {
return 1;
}
// DNF (Did Not Finish) - no points but finished (position > 0)
if (points === 0 && position > 0) {
return 20;
}
// Finished with points - high reliability
return 100;
}
private calculateResultsStrength(position: number, totalDrivers: number): number {
if (position <= 0) return 1; // DNF/DNS (ensure > 0)
const drivers = totalDrivers || 1;

View File

@@ -8,6 +8,8 @@ import { Driver } from '../../../racing/domain/entities/Driver';
import { Race } from '../../../racing/domain/entities/Race';
import { Result } from '../../../racing/domain/entities/result/Result';
import { Rating } from '../../domain/Rating';
import { DriverId } from '../../../racing/domain/entities/DriverId';
import { RaceId } from '../../../racing/domain/entities/RaceId';
const mockRatingRepository = {
findByDriverAndRace: vi.fn(),
@@ -92,8 +94,8 @@ describe('CalculateTeamContributionUseCase', () => {
const points = 12.5; // 50% contribution
const existingRating = Rating.create({
driverId: 'driver-1' as any, // Simplified for test
raceId: 'race-1' as any,
driverId: DriverId.create('driver-1'),
raceId: RaceId.create('race-1'),
rating: 1500,
components: {
resultsStrength: 80,
@@ -106,10 +108,10 @@ describe('CalculateTeamContributionUseCase', () => {
timestamp: new Date('2023-01-01')
});
mockDriverRepository.findById.mockResolvedValue({ id: driverId } as any);
mockRaceRepository.findById.mockResolvedValue({ id: raceId } as any);
mockDriverRepository.findById.mockResolvedValue({ id: driverId });
mockRaceRepository.findById.mockResolvedValue({ id: raceId });
mockResultRepository.findByRaceId.mockResolvedValue([
{ driverId: { toString: () => driverId }, points } as any
{ driverId: { toString: () => driverId }, points }
]);
mockRatingRepository.findByDriverAndRace.mockResolvedValue(existingRating);

View File

@@ -5,6 +5,8 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { GetRatingLeaderboardUseCase } from './GetRatingLeaderboardUseCase';
import { Rating } from '../../domain/Rating';
import { DriverId } from '../../../racing/domain/entities/DriverId';
import { RaceId } from '../../../racing/domain/entities/RaceId';
const mockRatingRepository = {
findByDriver: vi.fn(),
@@ -37,37 +39,65 @@ describe('GetRatingLeaderboardUseCase', () => {
const ratingsD1 = [
Rating.create({
driverId: 'd1' as any,
raceId: 'r1' as any,
driverId: DriverId.create('d1'),
raceId: RaceId.create('r1'),
rating: 1000,
components: {} as any,
components: {
resultsStrength: 0,
consistency: 0,
cleanDriving: 0,
racecraft: 0,
reliability: 0,
teamContribution: 0,
},
timestamp: new Date('2023-01-01')
}),
Rating.create({
driverId: 'd1' as any,
raceId: 'r2' as any,
driverId: DriverId.create('d1'),
raceId: RaceId.create('r2'),
rating: 1200, // Latest for D1
components: {} as any,
components: {
resultsStrength: 0,
consistency: 0,
cleanDriving: 0,
racecraft: 0,
reliability: 0,
teamContribution: 0,
},
timestamp: new Date('2023-01-02')
})
];
const ratingsD2 = [
Rating.create({
driverId: 'd2' as any,
raceId: 'r1' as any,
driverId: DriverId.create('d2'),
raceId: RaceId.create('r1'),
rating: 1500, // Latest for D2
components: {} as any,
components: {
resultsStrength: 0,
consistency: 0,
cleanDriving: 0,
racecraft: 0,
reliability: 0,
teamContribution: 0,
},
timestamp: new Date('2023-01-01')
})
];
const ratingsD3 = [
Rating.create({
driverId: 'd3' as any,
raceId: 'r1' as any,
driverId: DriverId.create('d3'),
raceId: RaceId.create('r1'),
rating: 800, // Latest for D3
components: {} as any,
components: {
resultsStrength: 0,
consistency: 0,
cleanDriving: 0,
racecraft: 0,
reliability: 0,
teamContribution: 0,
},
timestamp: new Date('2023-01-01')
})
];

View File

@@ -4,6 +4,7 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { SaveRatingUseCase } from './SaveRatingUseCase';
import { RatingRepository } from '../../ports/RatingRepository';
const mockRatingRepository = {
save: vi.fn(),
@@ -15,7 +16,7 @@ describe('SaveRatingUseCase', () => {
beforeEach(() => {
vi.clearAllMocks();
useCase = new SaveRatingUseCase({
ratingRepository: mockRatingRepository as any,
ratingRepository: mockRatingRepository as unknown as RatingRepository,
});
});