wip
This commit is contained in:
278
packages/racing/domain/services/ScheduleCalculator.test.ts
Normal file
278
packages/racing/domain/services/ScheduleCalculator.test.ts
Normal file
@@ -0,0 +1,278 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { calculateRaceDates, getNextWeekday, type ScheduleConfig } from './ScheduleCalculator';
|
||||
import type { Weekday } from '../value-objects/Weekday';
|
||||
|
||||
describe('ScheduleCalculator', () => {
|
||||
describe('calculateRaceDates', () => {
|
||||
describe('with empty or invalid input', () => {
|
||||
it('should return empty array when weekdays is empty', () => {
|
||||
// Given
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: [],
|
||||
frequency: 'weekly',
|
||||
rounds: 8,
|
||||
startDate: new Date('2024-01-01'),
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
expect(result.raceDates).toEqual([]);
|
||||
expect(result.seasonDurationWeeks).toBe(0);
|
||||
});
|
||||
|
||||
it('should return empty array when rounds is 0', () => {
|
||||
// Given
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: ['Sat'] as Weekday[],
|
||||
frequency: 'weekly',
|
||||
rounds: 0,
|
||||
startDate: new Date('2024-01-01'),
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
expect(result.raceDates).toEqual([]);
|
||||
});
|
||||
|
||||
it('should return empty array when rounds is negative', () => {
|
||||
// Given
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: ['Sat'] as Weekday[],
|
||||
frequency: 'weekly',
|
||||
rounds: -5,
|
||||
startDate: new Date('2024-01-01'),
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
expect(result.raceDates).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('weekly scheduling', () => {
|
||||
it('should schedule 8 races on Saturdays starting from a Saturday', () => {
|
||||
// Given - January 6, 2024 is a Saturday
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: ['Sat'] as Weekday[],
|
||||
frequency: 'weekly',
|
||||
rounds: 8,
|
||||
startDate: new Date('2024-01-06'),
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
expect(result.raceDates.length).toBe(8);
|
||||
// All dates should be Saturdays
|
||||
result.raceDates.forEach(date => {
|
||||
expect(date.getDay()).toBe(6); // Saturday
|
||||
});
|
||||
// First race should be Jan 6
|
||||
expect(result.raceDates[0].toISOString().split('T')[0]).toBe('2024-01-06');
|
||||
// Last race should be 7 weeks later (Feb 24)
|
||||
expect(result.raceDates[7].toISOString().split('T')[0]).toBe('2024-02-24');
|
||||
});
|
||||
|
||||
it('should schedule races on multiple weekdays', () => {
|
||||
// Given
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: ['Wed', 'Sat'] as Weekday[],
|
||||
frequency: 'weekly',
|
||||
rounds: 8,
|
||||
startDate: new Date('2024-01-01'), // Monday
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
expect(result.raceDates.length).toBe(8);
|
||||
// Should alternate between Wednesday and Saturday
|
||||
result.raceDates.forEach(date => {
|
||||
const day = date.getDay();
|
||||
expect([3, 6]).toContain(day); // Wed=3, Sat=6
|
||||
});
|
||||
});
|
||||
|
||||
it('should schedule 8 races on Sundays', () => {
|
||||
// Given - January 7, 2024 is a Sunday
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: ['Sun'] as Weekday[],
|
||||
frequency: 'weekly',
|
||||
rounds: 8,
|
||||
startDate: new Date('2024-01-01'),
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
expect(result.raceDates.length).toBe(8);
|
||||
result.raceDates.forEach(date => {
|
||||
expect(date.getDay()).toBe(0); // Sunday
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('bi-weekly scheduling', () => {
|
||||
it('should schedule races every 2 weeks on Saturdays', () => {
|
||||
// Given - January 6, 2024 is a Saturday
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: ['Sat'] as Weekday[],
|
||||
frequency: 'everyNWeeks',
|
||||
rounds: 4,
|
||||
startDate: new Date('2024-01-06'),
|
||||
intervalWeeks: 2,
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
expect(result.raceDates.length).toBe(4);
|
||||
// First race Jan 6
|
||||
expect(result.raceDates[0].toISOString().split('T')[0]).toBe('2024-01-06');
|
||||
// Second race 2 weeks later (Jan 20)
|
||||
expect(result.raceDates[1].toISOString().split('T')[0]).toBe('2024-01-20');
|
||||
// Third race 2 weeks later (Feb 3)
|
||||
expect(result.raceDates[2].toISOString().split('T')[0]).toBe('2024-02-03');
|
||||
// Fourth race 2 weeks later (Feb 17)
|
||||
expect(result.raceDates[3].toISOString().split('T')[0]).toBe('2024-02-17');
|
||||
});
|
||||
});
|
||||
|
||||
describe('with start and end dates', () => {
|
||||
it('should evenly distribute races across the date range', () => {
|
||||
// Given - 3 month season
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: ['Sat'] as Weekday[],
|
||||
frequency: 'weekly',
|
||||
rounds: 8,
|
||||
startDate: new Date('2024-01-06'),
|
||||
endDate: new Date('2024-03-30'),
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
expect(result.raceDates.length).toBe(8);
|
||||
// First race should be at or near start
|
||||
expect(result.raceDates[0].toISOString().split('T')[0]).toBe('2024-01-06');
|
||||
// Races should be spread across the range, not consecutive weeks
|
||||
});
|
||||
|
||||
it('should use all available days if fewer than rounds requested', () => {
|
||||
// Given - short period with only 3 Saturdays
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: ['Sat'] as Weekday[],
|
||||
frequency: 'weekly',
|
||||
rounds: 10,
|
||||
startDate: new Date('2024-01-06'),
|
||||
endDate: new Date('2024-01-21'),
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
// Only 3 Saturdays in this range: Jan 6, 13, 20
|
||||
expect(result.raceDates.length).toBe(3);
|
||||
});
|
||||
});
|
||||
|
||||
describe('season duration calculation', () => {
|
||||
it('should calculate correct season duration in weeks', () => {
|
||||
// Given
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: ['Sat'] as Weekday[],
|
||||
frequency: 'weekly',
|
||||
rounds: 8,
|
||||
startDate: new Date('2024-01-06'),
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
// 8 races, 1 week apart = 7 weeks duration
|
||||
expect(result.seasonDurationWeeks).toBe(7);
|
||||
});
|
||||
|
||||
it('should return 0 duration for single race', () => {
|
||||
// Given
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: ['Sat'] as Weekday[],
|
||||
frequency: 'weekly',
|
||||
rounds: 1,
|
||||
startDate: new Date('2024-01-06'),
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
expect(result.raceDates.length).toBe(1);
|
||||
expect(result.seasonDurationWeeks).toBe(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('getNextWeekday', () => {
|
||||
it('should return next Saturday from a Monday', () => {
|
||||
// Given - January 1, 2024 is a Monday
|
||||
const fromDate = new Date('2024-01-01');
|
||||
|
||||
// When
|
||||
const result = getNextWeekday(fromDate, 'Sat');
|
||||
|
||||
// Then
|
||||
expect(result.toISOString().split('T')[0]).toBe('2024-01-06');
|
||||
expect(result.getDay()).toBe(6);
|
||||
});
|
||||
|
||||
it('should return next occurrence when already on that weekday', () => {
|
||||
// Given - January 6, 2024 is a Saturday
|
||||
const fromDate = new Date('2024-01-06');
|
||||
|
||||
// When
|
||||
const result = getNextWeekday(fromDate, 'Sat');
|
||||
|
||||
// Then
|
||||
// Should return NEXT Saturday (7 days later), not same day
|
||||
expect(result.toISOString().split('T')[0]).toBe('2024-01-13');
|
||||
});
|
||||
|
||||
it('should return next Sunday from a Friday', () => {
|
||||
// Given - January 5, 2024 is a Friday
|
||||
const fromDate = new Date('2024-01-05');
|
||||
|
||||
// When
|
||||
const result = getNextWeekday(fromDate, 'Sun');
|
||||
|
||||
// Then
|
||||
expect(result.toISOString().split('T')[0]).toBe('2024-01-07');
|
||||
expect(result.getDay()).toBe(0);
|
||||
});
|
||||
|
||||
it('should return next Wednesday from a Thursday', () => {
|
||||
// Given - January 4, 2024 is a Thursday
|
||||
const fromDate = new Date('2024-01-04');
|
||||
|
||||
// When
|
||||
const result = getNextWeekday(fromDate, 'Wed');
|
||||
|
||||
// Then
|
||||
// Next Wednesday is 6 days later
|
||||
expect(result.toISOString().split('T')[0]).toBe('2024-01-10');
|
||||
expect(result.getDay()).toBe(3);
|
||||
});
|
||||
});
|
||||
});
|
||||
147
packages/racing/domain/services/ScheduleCalculator.ts
Normal file
147
packages/racing/domain/services/ScheduleCalculator.ts
Normal file
@@ -0,0 +1,147 @@
|
||||
import type { Weekday } from '../value-objects/Weekday';
|
||||
|
||||
export type RecurrenceStrategy = 'weekly' | 'everyNWeeks' | 'monthlyNthWeekday';
|
||||
|
||||
export interface ScheduleConfig {
|
||||
weekdays: Weekday[];
|
||||
frequency: RecurrenceStrategy;
|
||||
rounds: number;
|
||||
startDate: Date;
|
||||
endDate?: Date;
|
||||
intervalWeeks?: number;
|
||||
}
|
||||
|
||||
export interface ScheduleResult {
|
||||
raceDates: Date[];
|
||||
seasonDurationWeeks: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* JavaScript getDay(): 0=Sunday, 1=Monday, 2=Tuesday, etc.
|
||||
*/
|
||||
const DAY_MAP: Record<Weekday, number> = {
|
||||
'Sun': 0, 'Mon': 1, 'Tue': 2, 'Wed': 3, 'Thu': 4, 'Fri': 5, 'Sat': 6
|
||||
};
|
||||
|
||||
/**
|
||||
* Calculate race dates based on schedule configuration.
|
||||
*
|
||||
* If both startDate and endDate are provided, races are evenly distributed
|
||||
* across the selected weekdays within that range.
|
||||
*
|
||||
* If only startDate is provided, races are scheduled according to the
|
||||
* recurrence strategy (weekly or bi-weekly).
|
||||
*/
|
||||
export function calculateRaceDates(config: ScheduleConfig): ScheduleResult {
|
||||
const { weekdays, frequency, rounds, startDate, endDate, intervalWeeks } = config;
|
||||
const dates: Date[] = [];
|
||||
|
||||
if (weekdays.length === 0 || rounds <= 0) {
|
||||
return { raceDates: [], seasonDurationWeeks: 0 };
|
||||
}
|
||||
|
||||
// Convert weekday names to day numbers for faster lookup
|
||||
const selectedDayNumbers = new Set(weekdays.map(wd => DAY_MAP[wd]));
|
||||
|
||||
// If we have both start and end dates, evenly distribute races
|
||||
if (endDate && endDate > startDate) {
|
||||
const allPossibleDays: Date[] = [];
|
||||
const currentDate = new Date(startDate);
|
||||
currentDate.setHours(12, 0, 0, 0);
|
||||
|
||||
const endDateTime = new Date(endDate);
|
||||
endDateTime.setHours(12, 0, 0, 0);
|
||||
|
||||
while (currentDate <= endDateTime) {
|
||||
const dayOfWeek = currentDate.getDay();
|
||||
if (selectedDayNumbers.has(dayOfWeek)) {
|
||||
allPossibleDays.push(new Date(currentDate));
|
||||
}
|
||||
currentDate.setDate(currentDate.getDate() + 1);
|
||||
}
|
||||
|
||||
// Evenly distribute the rounds across available days
|
||||
const totalPossible = allPossibleDays.length;
|
||||
if (totalPossible >= rounds) {
|
||||
const spacing = totalPossible / rounds;
|
||||
for (let i = 0; i < rounds; i++) {
|
||||
const index = Math.min(Math.floor(i * spacing), totalPossible - 1);
|
||||
dates.push(allPossibleDays[index]);
|
||||
}
|
||||
} else {
|
||||
// Not enough days - use all available
|
||||
dates.push(...allPossibleDays);
|
||||
}
|
||||
|
||||
const seasonDurationWeeks = dates.length > 1
|
||||
? Math.ceil((dates[dates.length - 1].getTime() - dates[0].getTime()) / (7 * 24 * 60 * 60 * 1000))
|
||||
: 0;
|
||||
|
||||
return { raceDates: dates, seasonDurationWeeks };
|
||||
}
|
||||
|
||||
// Schedule based on frequency (no end date)
|
||||
const currentDate = new Date(startDate);
|
||||
currentDate.setHours(12, 0, 0, 0);
|
||||
let roundsScheduled = 0;
|
||||
|
||||
// Generate race dates for up to 2 years to ensure we can schedule all rounds
|
||||
const maxDays = 365 * 2;
|
||||
let daysChecked = 0;
|
||||
const seasonStart = new Date(startDate);
|
||||
seasonStart.setHours(12, 0, 0, 0);
|
||||
|
||||
while (roundsScheduled < rounds && daysChecked < maxDays) {
|
||||
const dayOfWeek = currentDate.getDay();
|
||||
const isSelectedDay = selectedDayNumbers.has(dayOfWeek);
|
||||
|
||||
// Calculate which week this is from the start
|
||||
const daysSinceStart = Math.floor((currentDate.getTime() - seasonStart.getTime()) / (24 * 60 * 60 * 1000));
|
||||
const currentWeek = Math.floor(daysSinceStart / 7);
|
||||
|
||||
if (isSelectedDay) {
|
||||
let shouldRace = false;
|
||||
|
||||
if (frequency === 'weekly') {
|
||||
// Weekly: race every week on selected days
|
||||
shouldRace = true;
|
||||
} else if (frequency === 'everyNWeeks') {
|
||||
// Every N weeks: race only on matching week intervals
|
||||
const interval = intervalWeeks ?? 2;
|
||||
shouldRace = currentWeek % interval === 0;
|
||||
} else {
|
||||
// Default to weekly if frequency not set
|
||||
shouldRace = true;
|
||||
}
|
||||
|
||||
if (shouldRace) {
|
||||
dates.push(new Date(currentDate));
|
||||
roundsScheduled++;
|
||||
}
|
||||
}
|
||||
|
||||
currentDate.setDate(currentDate.getDate() + 1);
|
||||
daysChecked++;
|
||||
}
|
||||
|
||||
const seasonDurationWeeks = dates.length > 1
|
||||
? Math.ceil((dates[dates.length - 1].getTime() - dates[0].getTime()) / (7 * 24 * 60 * 60 * 1000))
|
||||
: 0;
|
||||
|
||||
return { raceDates: dates, seasonDurationWeeks };
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the next occurrence of a specific weekday from a given date.
|
||||
*/
|
||||
export function getNextWeekday(fromDate: Date, weekday: Weekday): Date {
|
||||
const targetDay = DAY_MAP[weekday];
|
||||
const result = new Date(fromDate);
|
||||
result.setHours(12, 0, 0, 0);
|
||||
|
||||
const currentDay = result.getDay();
|
||||
const daysUntilTarget = (targetDay - currentDay + 7) % 7 || 7;
|
||||
|
||||
result.setDate(result.getDate() + daysUntilTarget);
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user