core tests

This commit is contained in:
2026-01-24 12:18:31 +01:00
parent 3bef15f3bd
commit 5da14b1b21
15 changed files with 809 additions and 312 deletions

View File

@@ -1,54 +1,35 @@
import { describe, it, expect } from 'vitest';
import { AverageStrengthOfFieldCalculator } from './StrengthOfFieldCalculator';
import { AverageStrengthOfFieldCalculator, DriverRating } from './StrengthOfFieldCalculator';
describe('AverageStrengthOfFieldCalculator', () => {
const calculator = new AverageStrengthOfFieldCalculator();
it('should calculate average SOF and round it', () => {
const ratings = [
{ driverId: 'd1', rating: 1500 },
{ driverId: 'd2', rating: 2000 },
{ driverId: 'd3', rating: 1750 },
];
const sof = calculator.calculate(ratings);
expect(sof).toBe(1750);
});
it('should handle rounding correctly', () => {
const ratings = [
{ driverId: 'd1', rating: 1000 },
{ driverId: 'd2', rating: 1001 },
];
const sof = calculator.calculate(ratings);
expect(sof).toBe(1001); // (1000 + 1001) / 2 = 1000.5 -> 1001
});
it('should return null for empty ratings', () => {
it('should return null for empty list', () => {
expect(calculator.calculate([])).toBeNull();
});
it('should filter out non-positive ratings', () => {
const ratings = [
{ driverId: 'd1', rating: 1500 },
{ driverId: 'd2', rating: 0 },
{ driverId: 'd3', rating: -100 },
it('should return null if no valid ratings (>0)', () => {
const ratings: DriverRating[] = [
{ driverId: '1', rating: 0 },
{ driverId: '2', rating: -100 },
];
const sof = calculator.calculate(ratings);
expect(sof).toBe(1500);
});
it('should return null if all ratings are non-positive', () => {
const ratings = [
{ driverId: 'd1', rating: 0 },
{ driverId: 'd2', rating: -500 },
];
expect(calculator.calculate(ratings)).toBeNull();
});
it('should calculate average of valid ratings', () => {
const ratings: DriverRating[] = [
{ driverId: '1', rating: 1000 },
{ driverId: '2', rating: 2000 },
{ driverId: '3', rating: 0 }, // Should be ignored
];
expect(calculator.calculate(ratings)).toBe(1500);
});
it('should round the result', () => {
const ratings: DriverRating[] = [
{ driverId: '1', rating: 1000 },
{ driverId: '2', rating: 1001 },
];
expect(calculator.calculate(ratings)).toBe(1001); // (1000+1001)/2 = 1000.5 -> 1001
});
});