Files
gridpilot.gg/core/racing/domain/services/ScheduleCalculator.test.ts
2026-01-24 12:18:31 +01:00

73 lines
2.5 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { calculateRaceDates, getNextWeekday, ScheduleConfig } from './ScheduleCalculator';
describe('ScheduleCalculator', () => {
describe('calculateRaceDates', () => {
it('should return empty array if no weekdays or rounds', () => {
const config: ScheduleConfig = {
weekdays: [],
frequency: 'weekly',
rounds: 10,
startDate: new Date('2024-01-01'),
};
expect(calculateRaceDates(config).raceDates).toHaveLength(0);
});
it('should schedule weekly races', () => {
const config: ScheduleConfig = {
weekdays: ['Mon'],
frequency: 'weekly',
rounds: 3,
startDate: new Date('2024-01-01'), // Monday
};
const result = calculateRaceDates(config);
expect(result.raceDates).toHaveLength(3);
expect(result.raceDates[0].getDay()).toBe(1);
expect(result.raceDates[1].getDay()).toBe(1);
expect(result.raceDates[2].getDay()).toBe(1);
// Check dates are 7 days apart
const diff = result.raceDates[1].getTime() - result.raceDates[0].getTime();
expect(diff).toBe(7 * 24 * 60 * 60 * 1000);
});
it('should schedule bi-weekly races', () => {
const config: ScheduleConfig = {
weekdays: ['Mon'],
frequency: 'everyNWeeks',
intervalWeeks: 2,
rounds: 2,
startDate: new Date('2024-01-01'),
};
const result = calculateRaceDates(config);
expect(result.raceDates).toHaveLength(2);
const diff = result.raceDates[1].getTime() - result.raceDates[0].getTime();
expect(diff).toBe(14 * 24 * 60 * 60 * 1000);
});
it('should distribute races between start and end date', () => {
const config: ScheduleConfig = {
weekdays: ['Mon', 'Wed', 'Fri'],
frequency: 'weekly',
rounds: 2,
startDate: new Date('2024-01-01'), // Mon
endDate: new Date('2024-01-15'), // Mon
};
const result = calculateRaceDates(config);
expect(result.raceDates).toHaveLength(2);
// Use getTime() to avoid timezone issues in comparison
const expectedDate = new Date('2024-01-01');
expectedDate.setHours(12, 0, 0, 0);
expect(result.raceDates[0].getTime()).toBe(expectedDate.getTime());
});
});
describe('getNextWeekday', () => {
it('should return the next Monday', () => {
const from = new Date('2024-01-01'); // Monday
const next = getNextWeekday(from, 'Mon');
expect(next.getDay()).toBe(1);
expect(next.getDate()).toBe(8);
});
});
});