Files
gridpilot.gg/core/racing/application/use-cases/LeagueSeasonScheduleMutationsUseCases.test.ts
2025-12-28 12:04:12 +01:00

546 lines
19 KiB
TypeScript

import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import { Race } from '../../domain/entities/Race';
import { Season } from '../../domain/entities/season/Season';
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
import {
CreateLeagueSeasonScheduleRaceUseCase,
type CreateLeagueSeasonScheduleRaceErrorCode,
type CreateLeagueSeasonScheduleRaceResult,
} from './CreateLeagueSeasonScheduleRaceUseCase';
import {
UpdateLeagueSeasonScheduleRaceUseCase,
type UpdateLeagueSeasonScheduleRaceErrorCode,
type UpdateLeagueSeasonScheduleRaceResult,
} from './UpdateLeagueSeasonScheduleRaceUseCase';
import {
DeleteLeagueSeasonScheduleRaceUseCase,
type DeleteLeagueSeasonScheduleRaceErrorCode,
type DeleteLeagueSeasonScheduleRaceResult,
} from './DeleteLeagueSeasonScheduleRaceUseCase';
import {
PublishLeagueSeasonScheduleUseCase,
type PublishLeagueSeasonScheduleErrorCode,
type PublishLeagueSeasonScheduleResult,
} from './PublishLeagueSeasonScheduleUseCase';
import {
UnpublishLeagueSeasonScheduleUseCase,
type UnpublishLeagueSeasonScheduleErrorCode,
type UnpublishLeagueSeasonScheduleResult,
} from './UnpublishLeagueSeasonScheduleUseCase';
function createLogger(): Logger {
return {
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
} as unknown as Logger;
}
function createSeasonWithinWindow(overrides?: Partial<{ leagueId: string }>): Season {
return Season.create({
id: 'season-1',
leagueId: overrides?.leagueId ?? 'league-1',
gameId: 'iracing',
name: 'Schedule Season',
status: 'planned',
startDate: new Date('2025-01-01T00:00:00Z'),
endDate: new Date('2025-01-31T00:00:00Z'),
});
}
describe('CreateLeagueSeasonScheduleRaceUseCase', () => {
let seasonRepository: { findById: Mock };
let raceRepository: { create: Mock };
let output: UseCaseOutputPort<CreateLeagueSeasonScheduleRaceResult> & { present: Mock };
let logger: Logger;
beforeEach(() => {
seasonRepository = { findById: vi.fn() };
raceRepository = { create: vi.fn() };
output = { present: vi.fn() };
logger = createLogger();
});
it('creates a race when season belongs to league and scheduledAt is within season window', async () => {
const season = createSeasonWithinWindow();
seasonRepository.findById.mockResolvedValue(season);
raceRepository.create.mockImplementation(async (race: Race) => race);
const useCase = new CreateLeagueSeasonScheduleRaceUseCase(
seasonRepository as unknown as ISeasonRepository,
raceRepository as unknown as IRaceRepository,
logger,
output,
{ generateRaceId: () => 'race-123' },
);
const scheduledAt = new Date('2025-01-10T20:00:00Z');
const result = await useCase.execute({
leagueId: 'league-1',
seasonId: 'season-1',
track: 'Road Atlanta',
car: 'MX-5',
scheduledAt,
});
expect(result.isOk()).toBe(true);
expect(output.present).toHaveBeenCalledTimes(1);
expect(output.present).toHaveBeenCalledWith({ raceId: 'race-123' });
expect(raceRepository.create).toHaveBeenCalledTimes(1);
const createdRace = raceRepository.create.mock.calls[0]?.[0] as Race;
expect(createdRace.id).toBe('race-123');
expect(createdRace.leagueId).toBe('league-1');
expect(createdRace.track).toBe('Road Atlanta');
expect(createdRace.car).toBe('MX-5');
expect(createdRace.scheduledAt.getTime()).toBe(scheduledAt.getTime());
});
it('returns SEASON_NOT_FOUND when season does not belong to league and does not create', async () => {
const season = createSeasonWithinWindow({ leagueId: 'other-league' });
seasonRepository.findById.mockResolvedValue(season);
const useCase = new CreateLeagueSeasonScheduleRaceUseCase(
seasonRepository as unknown as ISeasonRepository,
raceRepository as unknown as IRaceRepository,
logger,
output,
{ generateRaceId: () => 'race-123' },
);
const result = await useCase.execute({
leagueId: 'league-1',
seasonId: 'season-1',
track: 'Road Atlanta',
car: 'MX-5',
scheduledAt: new Date('2025-01-10T20:00:00Z'),
});
expect(result.isErr()).toBe(true);
const error = result.unwrapErr() as ApplicationErrorCode<
CreateLeagueSeasonScheduleRaceErrorCode,
{ message: string }
>;
expect(error.code).toBe('SEASON_NOT_FOUND');
expect(output.present).not.toHaveBeenCalled();
expect(raceRepository.create).not.toHaveBeenCalled();
});
it('returns RACE_OUTSIDE_SEASON_WINDOW when scheduledAt is before season start and does not create', async () => {
const season = createSeasonWithinWindow();
seasonRepository.findById.mockResolvedValue(season);
const useCase = new CreateLeagueSeasonScheduleRaceUseCase(
seasonRepository as unknown as ISeasonRepository,
raceRepository as unknown as IRaceRepository,
logger,
output,
{ generateRaceId: () => 'race-123' },
);
const result = await useCase.execute({
leagueId: 'league-1',
seasonId: 'season-1',
track: 'Road Atlanta',
car: 'MX-5',
scheduledAt: new Date('2024-12-31T23:59:59Z'),
});
expect(result.isErr()).toBe(true);
const error = result.unwrapErr() as ApplicationErrorCode<
CreateLeagueSeasonScheduleRaceErrorCode,
{ message: string }
>;
expect(error.code).toBe('RACE_OUTSIDE_SEASON_WINDOW');
expect(output.present).not.toHaveBeenCalled();
expect(raceRepository.create).not.toHaveBeenCalled();
});
});
describe('UpdateLeagueSeasonScheduleRaceUseCase', () => {
let seasonRepository: { findById: Mock };
let raceRepository: { findById: Mock; update: Mock };
let output: UseCaseOutputPort<UpdateLeagueSeasonScheduleRaceResult> & { present: Mock };
let logger: Logger;
beforeEach(() => {
seasonRepository = { findById: vi.fn() };
raceRepository = { findById: vi.fn(), update: vi.fn() };
output = { present: vi.fn() };
logger = createLogger();
});
it('updates race when season belongs to league and updated scheduledAt stays within window', async () => {
const season = createSeasonWithinWindow();
seasonRepository.findById.mockResolvedValue(season);
const existing = Race.create({
id: 'race-1',
leagueId: 'league-1',
track: 'Old Track',
car: 'Old Car',
scheduledAt: new Date('2025-01-05T20:00:00Z'),
});
raceRepository.findById.mockResolvedValue(existing);
raceRepository.update.mockImplementation(async (race: Race) => race);
const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(
seasonRepository as unknown as ISeasonRepository,
raceRepository as unknown as IRaceRepository,
logger,
output,
);
const newScheduledAt = new Date('2025-01-20T20:00:00Z');
const result = await useCase.execute({
leagueId: 'league-1',
seasonId: 'season-1',
raceId: 'race-1',
track: 'New Track',
car: 'New Car',
scheduledAt: newScheduledAt,
});
expect(result.isOk()).toBe(true);
expect(output.present).toHaveBeenCalledTimes(1);
expect(output.present).toHaveBeenCalledWith({ success: true });
expect(raceRepository.update).toHaveBeenCalledTimes(1);
const updated = raceRepository.update.mock.calls[0]?.[0] as Race;
expect(updated.id).toBe('race-1');
expect(updated.leagueId).toBe('league-1');
expect(updated.track).toBe('New Track');
expect(updated.car).toBe('New Car');
expect(updated.scheduledAt.getTime()).toBe(newScheduledAt.getTime());
});
it('returns SEASON_NOT_FOUND when season does not belong to league and does not read/update race', async () => {
const season = createSeasonWithinWindow({ leagueId: 'other-league' });
seasonRepository.findById.mockResolvedValue(season);
const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(
seasonRepository as unknown as ISeasonRepository,
raceRepository as unknown as IRaceRepository,
logger,
output,
);
const result = await useCase.execute({
leagueId: 'league-1',
seasonId: 'season-1',
raceId: 'race-1',
track: 'New Track',
});
expect(result.isErr()).toBe(true);
const error = result.unwrapErr() as ApplicationErrorCode<
UpdateLeagueSeasonScheduleRaceErrorCode,
{ message: string }
>;
expect(error.code).toBe('SEASON_NOT_FOUND');
expect(raceRepository.findById).not.toHaveBeenCalled();
expect(raceRepository.update).not.toHaveBeenCalled();
expect(output.present).not.toHaveBeenCalled();
});
it('returns RACE_OUTSIDE_SEASON_WINDOW when updated scheduledAt is outside window and does not update', async () => {
const season = createSeasonWithinWindow();
seasonRepository.findById.mockResolvedValue(season);
const existing = Race.create({
id: 'race-1',
leagueId: 'league-1',
track: 'Old Track',
car: 'Old Car',
scheduledAt: new Date('2025-01-05T20:00:00Z'),
});
raceRepository.findById.mockResolvedValue(existing);
const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(
seasonRepository as unknown as ISeasonRepository,
raceRepository as unknown as IRaceRepository,
logger,
output,
);
const result = await useCase.execute({
leagueId: 'league-1',
seasonId: 'season-1',
raceId: 'race-1',
scheduledAt: new Date('2025-02-01T00:00:01Z'),
});
expect(result.isErr()).toBe(true);
const error = result.unwrapErr() as ApplicationErrorCode<
UpdateLeagueSeasonScheduleRaceErrorCode,
{ message: string }
>;
expect(error.code).toBe('RACE_OUTSIDE_SEASON_WINDOW');
expect(raceRepository.update).not.toHaveBeenCalled();
expect(output.present).not.toHaveBeenCalled();
});
it('returns RACE_NOT_FOUND when race does not exist for league and does not update', async () => {
const season = createSeasonWithinWindow();
seasonRepository.findById.mockResolvedValue(season);
raceRepository.findById.mockResolvedValue(null);
const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(
seasonRepository as unknown as ISeasonRepository,
raceRepository as unknown as IRaceRepository,
logger,
output,
);
const result = await useCase.execute({
leagueId: 'league-1',
seasonId: 'season-1',
raceId: 'race-404',
track: 'New Track',
});
expect(result.isErr()).toBe(true);
const error = result.unwrapErr() as ApplicationErrorCode<
UpdateLeagueSeasonScheduleRaceErrorCode,
{ message: string }
>;
expect(error.code).toBe('RACE_NOT_FOUND');
expect(raceRepository.update).not.toHaveBeenCalled();
expect(output.present).not.toHaveBeenCalled();
});
});
describe('DeleteLeagueSeasonScheduleRaceUseCase', () => {
let seasonRepository: { findById: Mock };
let raceRepository: { findById: Mock; delete: Mock };
let output: UseCaseOutputPort<DeleteLeagueSeasonScheduleRaceResult> & { present: Mock };
let logger: Logger;
beforeEach(() => {
seasonRepository = { findById: vi.fn() };
raceRepository = { findById: vi.fn(), delete: vi.fn() };
output = { present: vi.fn() };
logger = createLogger();
});
it('deletes race when season belongs to league and race belongs to league', async () => {
const season = createSeasonWithinWindow();
seasonRepository.findById.mockResolvedValue(season);
const existing = Race.create({
id: 'race-1',
leagueId: 'league-1',
track: 'Track',
car: 'Car',
scheduledAt: new Date('2025-01-05T20:00:00Z'),
});
raceRepository.findById.mockResolvedValue(existing);
raceRepository.delete.mockResolvedValue(undefined);
const useCase = new DeleteLeagueSeasonScheduleRaceUseCase(
seasonRepository as unknown as ISeasonRepository,
raceRepository as unknown as IRaceRepository,
logger,
output,
);
const result = await useCase.execute({
leagueId: 'league-1',
seasonId: 'season-1',
raceId: 'race-1',
});
expect(result.isOk()).toBe(true);
expect(output.present).toHaveBeenCalledTimes(1);
expect(output.present).toHaveBeenCalledWith({ success: true });
expect(raceRepository.delete).toHaveBeenCalledTimes(1);
expect(raceRepository.delete).toHaveBeenCalledWith('race-1');
});
it('returns SEASON_NOT_FOUND when season does not belong to league and does not read/delete race', async () => {
const season = createSeasonWithinWindow({ leagueId: 'other-league' });
seasonRepository.findById.mockResolvedValue(season);
const useCase = new DeleteLeagueSeasonScheduleRaceUseCase(
seasonRepository as unknown as ISeasonRepository,
raceRepository as unknown as IRaceRepository,
logger,
output,
);
const result = await useCase.execute({
leagueId: 'league-1',
seasonId: 'season-1',
raceId: 'race-1',
});
expect(result.isErr()).toBe(true);
const error = result.unwrapErr() as ApplicationErrorCode<
DeleteLeagueSeasonScheduleRaceErrorCode,
{ message: string }
>;
expect(error.code).toBe('SEASON_NOT_FOUND');
expect(raceRepository.findById).not.toHaveBeenCalled();
expect(raceRepository.delete).not.toHaveBeenCalled();
expect(output.present).not.toHaveBeenCalled();
});
it('returns RACE_NOT_FOUND when race does not exist for league and does not delete', async () => {
const season = createSeasonWithinWindow();
seasonRepository.findById.mockResolvedValue(season);
raceRepository.findById.mockResolvedValue(null);
const useCase = new DeleteLeagueSeasonScheduleRaceUseCase(
seasonRepository as unknown as ISeasonRepository,
raceRepository as unknown as IRaceRepository,
logger,
output,
);
const result = await useCase.execute({
leagueId: 'league-1',
seasonId: 'season-1',
raceId: 'race-404',
});
expect(result.isErr()).toBe(true);
const error = result.unwrapErr() as ApplicationErrorCode<
DeleteLeagueSeasonScheduleRaceErrorCode,
{ message: string }
>;
expect(error.code).toBe('RACE_NOT_FOUND');
expect(raceRepository.delete).not.toHaveBeenCalled();
expect(output.present).not.toHaveBeenCalled();
});
});
describe('PublishLeagueSeasonScheduleUseCase', () => {
let seasonRepository: { findById: Mock; update: Mock };
let output: UseCaseOutputPort<PublishLeagueSeasonScheduleResult> & { present: Mock };
let logger: Logger;
beforeEach(() => {
seasonRepository = { findById: vi.fn(), update: vi.fn() };
output = { present: vi.fn() };
logger = createLogger();
});
it('publishes schedule deterministically (schedulePublished=true) and persists', async () => {
const season = createSeasonWithinWindow();
seasonRepository.findById.mockResolvedValue(season);
seasonRepository.update.mockResolvedValue(undefined);
const useCase = new PublishLeagueSeasonScheduleUseCase(
seasonRepository as unknown as ISeasonRepository,
logger,
output,
);
const result = await useCase.execute({ leagueId: 'league-1', seasonId: 'season-1' });
expect(result.isOk()).toBe(true);
expect(output.present).toHaveBeenCalledTimes(1);
expect(output.present).toHaveBeenCalledWith({
success: true,
seasonId: 'season-1',
published: true,
});
expect(seasonRepository.update).toHaveBeenCalledTimes(1);
const updatedSeason = seasonRepository.update.mock.calls[0]?.[0] as Season;
expect(updatedSeason.id).toBe('season-1');
expect(updatedSeason.leagueId).toBe('league-1');
expect(updatedSeason.schedulePublished).toBe(true);
});
it('returns SEASON_NOT_FOUND when season does not belong to league and does not update', async () => {
const season = createSeasonWithinWindow({ leagueId: 'other-league' });
seasonRepository.findById.mockResolvedValue(season);
const useCase = new PublishLeagueSeasonScheduleUseCase(
seasonRepository as unknown as ISeasonRepository,
logger,
output,
);
const result = await useCase.execute({ leagueId: 'league-1', seasonId: 'season-1' });
expect(result.isErr()).toBe(true);
const error = result.unwrapErr() as ApplicationErrorCode<
PublishLeagueSeasonScheduleErrorCode,
{ message: string }
>;
expect(error.code).toBe('SEASON_NOT_FOUND');
expect(seasonRepository.update).not.toHaveBeenCalled();
expect(output.present).not.toHaveBeenCalled();
});
});
describe('UnpublishLeagueSeasonScheduleUseCase', () => {
let seasonRepository: { findById: Mock; update: Mock };
let output: UseCaseOutputPort<UnpublishLeagueSeasonScheduleResult> & { present: Mock };
let logger: Logger;
beforeEach(() => {
seasonRepository = { findById: vi.fn(), update: vi.fn() };
output = { present: vi.fn() };
logger = createLogger();
});
it('unpublishes schedule deterministically (schedulePublished=false) and persists', async () => {
const season = createSeasonWithinWindow().withSchedulePublished(true);
seasonRepository.findById.mockResolvedValue(season);
seasonRepository.update.mockResolvedValue(undefined);
const useCase = new UnpublishLeagueSeasonScheduleUseCase(
seasonRepository as unknown as ISeasonRepository,
logger,
output,
);
const result = await useCase.execute({ leagueId: 'league-1', seasonId: 'season-1' });
expect(result.isOk()).toBe(true);
expect(output.present).toHaveBeenCalledTimes(1);
expect(output.present).toHaveBeenCalledWith({
success: true,
seasonId: 'season-1',
published: false,
});
expect(seasonRepository.update).toHaveBeenCalledTimes(1);
const updatedSeason = seasonRepository.update.mock.calls[0]?.[0] as Season;
expect(updatedSeason.id).toBe('season-1');
expect(updatedSeason.leagueId).toBe('league-1');
expect(updatedSeason.schedulePublished).toBe(false);
});
it('returns SEASON_NOT_FOUND when season does not belong to league and does not update', async () => {
const season = createSeasonWithinWindow({ leagueId: 'other-league' });
seasonRepository.findById.mockResolvedValue(season);
const useCase = new UnpublishLeagueSeasonScheduleUseCase(
seasonRepository as unknown as ISeasonRepository,
logger,
output,
);
const result = await useCase.execute({ leagueId: 'league-1', seasonId: 'season-1' });
expect(result.isErr()).toBe(true);
const error = result.unwrapErr() as ApplicationErrorCode<
UnpublishLeagueSeasonScheduleErrorCode,
{ message: string }
>;
expect(error.code).toBe('SEASON_NOT_FOUND');
expect(seasonRepository.update).not.toHaveBeenCalled();
expect(output.present).not.toHaveBeenCalled();
});
});