refactor racing use cases

This commit is contained in:
2025-12-21 00:43:42 +01:00
parent e9d6f90bb2
commit c12656d671
308 changed files with 14401 additions and 7419 deletions

View File

@@ -1,10 +1,13 @@
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import { CreateLeagueWithSeasonAndScoringUseCase } from './CreateLeagueWithSeasonAndScoringUseCase';
import type { CreateLeagueWithSeasonAndScoringCommand } from '../dto/CreateLeagueWithSeasonAndScoringCommand';
import {
CreateLeagueWithSeasonAndScoringUseCase,
type CreateLeagueWithSeasonAndScoringCommand,
type CreateLeagueWithSeasonAndScoringResult,
} from './CreateLeagueWithSeasonAndScoringUseCase';
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
import type { ILeagueScoringConfigRepository } from '../../domain/repositories/ILeagueScoringConfigRepository';
import type { Logger } from '@core/shared/application';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
describe('CreateLeagueWithSeasonAndScoringUseCase', () => {
let useCase: CreateLeagueWithSeasonAndScoringUseCase;
@@ -24,6 +27,7 @@ describe('CreateLeagueWithSeasonAndScoringUseCase', () => {
warn: Mock;
error: Mock;
};
let output: { present: Mock } & UseCaseOutputPort<CreateLeagueWithSeasonAndScoringResult>;
beforeEach(() => {
leagueRepository = {
@@ -42,12 +46,14 @@ describe('CreateLeagueWithSeasonAndScoringUseCase', () => {
warn: vi.fn(),
error: vi.fn(),
};
output = { present: vi.fn() } as unknown as typeof output;
useCase = new CreateLeagueWithSeasonAndScoringUseCase(
leagueRepository as unknown as ILeagueRepository,
seasonRepository as unknown as ISeasonRepository,
leagueScoringConfigRepository as unknown as ILeagueScoringConfigRepository,
getLeagueScoringPresetById,
logger as unknown as Logger,
output,
);
});
@@ -80,11 +86,13 @@ describe('CreateLeagueWithSeasonAndScoringUseCase', () => {
const result = await useCase.execute(command);
expect(result.isOk()).toBe(true);
const data = result.unwrap();
expect(data.leagueId).toBeDefined();
expect(data.seasonId).toBeDefined();
expect(data.scoringPresetId).toBe('club-default');
expect(data.scoringPresetName).toBe('Club Default');
expect(result.unwrap()).toBeUndefined();
expect(output.present).toHaveBeenCalledTimes(1);
const presented = output.present.mock.calls[0][0] as CreateLeagueWithSeasonAndScoringResult;
expect(presented.league.id.toString()).toBeDefined();
expect(presented.season.id).toBeDefined();
expect(presented.scoringConfig.seasonId.toString()).toBe(presented.season.id);
expect(leagueRepository.create).toHaveBeenCalledTimes(1);
expect(seasonRepository.create).toHaveBeenCalledTimes(1);
expect(leagueScoringConfigRepository.save).toHaveBeenCalledTimes(1);
@@ -106,7 +114,9 @@ describe('CreateLeagueWithSeasonAndScoringUseCase', () => {
const result = await useCase.execute(command);
expect(result.isErr()).toBe(true);
expect(result.unwrapErr().details.message).toBe('League name is required');
expect(result.unwrapErr().code).toBe('VALIDATION_ERROR');
expect(result.unwrapErr().details?.message).toBe('League name is required');
expect(output.present).not.toHaveBeenCalled();
});
it('should return error when ownerId is empty', async () => {
@@ -125,7 +135,9 @@ describe('CreateLeagueWithSeasonAndScoringUseCase', () => {
const result = await useCase.execute(command as CreateLeagueWithSeasonAndScoringCommand);
expect(result.isErr()).toBe(true);
expect(result.unwrapErr().details.message).toBe('League ownerId is required');
expect(result.unwrapErr().code).toBe('VALIDATION_ERROR');
expect(result.unwrapErr().details?.message).toBe('League ownerId is required');
expect(output.present).not.toHaveBeenCalled();
});
it('should return error when gameId is empty', async () => {
@@ -144,7 +156,9 @@ describe('CreateLeagueWithSeasonAndScoringUseCase', () => {
const result = await useCase.execute(command);
expect(result.isErr()).toBe(true);
expect(result.unwrapErr().details.message).toBe('gameId is required');
expect(result.unwrapErr().code).toBe('VALIDATION_ERROR');
expect(result.unwrapErr().details?.message).toBe('gameId is required');
expect(output.present).not.toHaveBeenCalled();
});
it('should return error when visibility is missing', async () => {
@@ -161,7 +175,9 @@ describe('CreateLeagueWithSeasonAndScoringUseCase', () => {
const result = await useCase.execute(command as CreateLeagueWithSeasonAndScoringCommand);
expect(result.isErr()).toBe(true);
expect(result.unwrapErr().details.message).toBe('visibility is required');
expect(result.unwrapErr().code).toBe('VALIDATION_ERROR');
expect(result.unwrapErr().details?.message).toBe('visibility is required');
expect(output.present).not.toHaveBeenCalled();
});
it('should return error when maxDrivers is invalid', async () => {
@@ -181,7 +197,9 @@ describe('CreateLeagueWithSeasonAndScoringUseCase', () => {
const result = await useCase.execute(command);
expect(result.isErr()).toBe(true);
expect(result.unwrapErr().details.message).toBe('maxDrivers must be greater than 0 when provided');
expect(result.unwrapErr().code).toBe('VALIDATION_ERROR');
expect(result.unwrapErr().details?.message).toBe('maxDrivers must be greater than 0 when provided');
expect(output.present).not.toHaveBeenCalled();
});
it('should return error when ranked league has insufficient drivers', async () => {
@@ -201,7 +219,9 @@ describe('CreateLeagueWithSeasonAndScoringUseCase', () => {
const result = await useCase.execute(command);
expect(result.isErr()).toBe(true);
expect(result.unwrapErr().details.message).toContain('Ranked leagues require at least 10 drivers');
expect(result.unwrapErr().code).toBe('VALIDATION_ERROR');
expect(result.unwrapErr().details?.message).toContain('Ranked leagues require at least 10 drivers');
expect(output.present).not.toHaveBeenCalled();
});
it('should return error when scoring preset is unknown', async () => {
@@ -223,7 +243,9 @@ describe('CreateLeagueWithSeasonAndScoringUseCase', () => {
const result = await useCase.execute(command);
expect(result.isErr()).toBe(true);
expect(result.unwrapErr().details.message).toBe('Unknown scoring preset: unknown-preset');
expect(result.unwrapErr().code).toBe('UNKNOWN_PRESET');
expect(result.unwrapErr().details?.message).toBe('Unknown scoring preset: unknown-preset');
expect(output.present).not.toHaveBeenCalled();
});
it('should return error when repository throws', async () => {
@@ -250,6 +272,8 @@ describe('CreateLeagueWithSeasonAndScoringUseCase', () => {
const result = await useCase.execute(command);
expect(result.isErr()).toBe(true);
expect(result.unwrapErr().details.message).toBe('DB error');
expect(result.unwrapErr().code).toBe('REPOSITORY_ERROR');
expect(result.unwrapErr().details?.message).toBe('DB error');
expect(output.present).not.toHaveBeenCalled();
});
});