Files
gridpilot.gg/core/racing/application/use-cases/PublishLeagueSeasonScheduleUseCase.test.ts
2026-01-08 16:30:15 +01:00

132 lines
4.9 KiB
TypeScript

import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import { Season } from '../../domain/entities/season/Season';
import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
import {
PublishLeagueSeasonScheduleUseCase,
type PublishLeagueSeasonScheduleErrorCode,
} from './PublishLeagueSeasonScheduleUseCase';
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('PublishLeagueSeasonScheduleUseCase', () => {
let seasonRepository: { findById: Mock; update: Mock };
let logger: Logger;
beforeEach(() => {
seasonRepository = { findById: vi.fn(), update: 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);
const result = await useCase.execute({ leagueId: 'league-1', seasonId: 'season-1' });
expect(result.isOk()).toBe(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);
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();
});
it('returns SEASON_NOT_FOUND when season does not exist', async () => {
seasonRepository.findById.mockResolvedValue(null);
const useCase = new PublishLeagueSeasonScheduleUseCase(seasonRepository as unknown as ISeasonRepository,
logger);
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();
});
it('returns REPOSITORY_ERROR when repository throws during find', async () => {
const repositoryError = new Error('DB connection failed');
seasonRepository.findById.mockRejectedValue(repositoryError);
const useCase = new PublishLeagueSeasonScheduleUseCase(seasonRepository as unknown as ISeasonRepository,
logger);
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('REPOSITORY_ERROR');
expect(error.details.message).toBe('DB connection failed');
});
it('returns REPOSITORY_ERROR when repository throws during update', async () => {
const season = createSeasonWithinWindow();
const repositoryError = new Error('DB write failed');
seasonRepository.findById.mockResolvedValue(season);
seasonRepository.update.mockRejectedValue(repositoryError);
const useCase = new PublishLeagueSeasonScheduleUseCase(seasonRepository as unknown as ISeasonRepository,
logger);
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('REPOSITORY_ERROR');
expect(error.details.message).toBe('DB write failed');
});
});