refactor racing use cases
This commit is contained in:
@@ -1,18 +1,26 @@
|
||||
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
|
||||
import { RecalculateChampionshipStandingsUseCase } from './RecalculateChampionshipStandingsUseCase';
|
||||
import { describe, it, expect, beforeEach, vi, type Mock } from 'vitest';
|
||||
import {
|
||||
RecalculateChampionshipStandingsUseCase,
|
||||
type RecalculateChampionshipStandingsInput,
|
||||
type RecalculateChampionshipStandingsResult,
|
||||
type RecalculateChampionshipStandingsErrorCode,
|
||||
} from './RecalculateChampionshipStandingsUseCase';
|
||||
import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
|
||||
import type { ILeagueScoringConfigRepository } from '../../domain/repositories/ILeagueScoringConfigRepository';
|
||||
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
||||
import type { IResultRepository } from '../../domain/repositories/IResultRepository';
|
||||
import type { IPenaltyRepository } from '../../domain/repositories/IPenaltyRepository';
|
||||
import type { IChampionshipStandingRepository } from '../../domain/repositories/IChampionshipStandingRepository';
|
||||
import type { Result } from '../../domain/entities/Result';
|
||||
import type { Penalty } from '../../domain/entities/Penalty';
|
||||
import { EventScoringService } from '../../domain/services/EventScoringService';
|
||||
import { ChampionshipAggregator } from '../../domain/services/ChampionshipAggregator';
|
||||
import type { UseCaseOutputPort, Logger } from '@core/shared/application';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
|
||||
describe('RecalculateChampionshipStandingsUseCase', () => {
|
||||
let useCase: RecalculateChampionshipStandingsUseCase;
|
||||
let leagueRepository: { findById: Mock };
|
||||
let seasonRepository: { findById: Mock };
|
||||
let leagueScoringConfigRepository: { findBySeasonId: Mock };
|
||||
let raceRepository: { findByLeagueId: Mock };
|
||||
@@ -21,8 +29,13 @@ describe('RecalculateChampionshipStandingsUseCase', () => {
|
||||
let championshipStandingRepository: { saveAll: Mock };
|
||||
let eventScoringService: { scoreSession: Mock };
|
||||
let championshipAggregator: { aggregate: Mock };
|
||||
let logger: Logger;
|
||||
let output: UseCaseOutputPort<RecalculateChampionshipStandingsResult> & {
|
||||
present: ReturnType<typeof vi.fn>;
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
leagueRepository = { findById: vi.fn() };
|
||||
seasonRepository = { findById: vi.fn() };
|
||||
leagueScoringConfigRepository = { findBySeasonId: vi.fn() };
|
||||
raceRepository = { findByLeagueId: vi.fn() };
|
||||
@@ -31,7 +44,16 @@ describe('RecalculateChampionshipStandingsUseCase', () => {
|
||||
championshipStandingRepository = { saveAll: vi.fn() };
|
||||
eventScoringService = { scoreSession: vi.fn() };
|
||||
championshipAggregator = { aggregate: vi.fn() };
|
||||
logger = {
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
};
|
||||
output = { present: vi.fn() } as unknown as typeof output;
|
||||
|
||||
useCase = new RecalculateChampionshipStandingsUseCase(
|
||||
leagueRepository as unknown as ISeasonRepository,
|
||||
seasonRepository as unknown as ISeasonRepository,
|
||||
leagueScoringConfigRepository as unknown as ILeagueScoringConfigRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
@@ -40,56 +62,96 @@ describe('RecalculateChampionshipStandingsUseCase', () => {
|
||||
championshipStandingRepository as unknown as IChampionshipStandingRepository,
|
||||
eventScoringService as unknown as EventScoringService,
|
||||
championshipAggregator as unknown as ChampionshipAggregator,
|
||||
logger,
|
||||
output,
|
||||
);
|
||||
});
|
||||
|
||||
it('should return season not found error', async () => {
|
||||
it('returns league not found error', async () => {
|
||||
leagueRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const input: RecalculateChampionshipStandingsInput = {
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
RecalculateChampionshipStandingsErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('LEAGUE_NOT_FOUND');
|
||||
expect(error.details.message).toContain('league-1');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns season not found error when season does not exist', async () => {
|
||||
leagueRepository.findById.mockResolvedValue({ id: 'league-1' });
|
||||
seasonRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const result = await useCase.execute({ seasonId: 'season-1', championshipId: 'champ-1' });
|
||||
const input: RecalculateChampionshipStandingsInput = {
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr()).toEqual({
|
||||
code: 'SEASON_NOT_FOUND',
|
||||
details: { message: 'Season not found: season-1' },
|
||||
});
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
RecalculateChampionshipStandingsErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('SEASON_NOT_FOUND');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should return league scoring config not found error', async () => {
|
||||
seasonRepository.findById.mockResolvedValue({ id: 'season-1', leagueId: 'league-1' });
|
||||
leagueScoringConfigRepository.findBySeasonId.mockResolvedValue(null);
|
||||
it('returns season not found error when season belongs to different league', async () => {
|
||||
leagueRepository.findById.mockResolvedValue({ id: 'league-1' });
|
||||
seasonRepository.findById.mockResolvedValue({ id: 'season-1', leagueId: 'other-league' });
|
||||
|
||||
const result = await useCase.execute({ seasonId: 'season-1', championshipId: 'champ-1' });
|
||||
const input: RecalculateChampionshipStandingsInput = {
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr()).toEqual({
|
||||
code: 'LEAGUE_SCORING_CONFIG_NOT_FOUND',
|
||||
details: { message: 'League scoring config not found for season: season-1' },
|
||||
});
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
RecalculateChampionshipStandingsErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('SEASON_NOT_FOUND');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should return championship config not found error', async () => {
|
||||
seasonRepository.findById.mockResolvedValue({ id: 'season-1', leagueId: 'league-1' });
|
||||
leagueScoringConfigRepository.findBySeasonId.mockResolvedValue({ championships: [] });
|
||||
|
||||
const result = await useCase.execute({ seasonId: 'season-1', championshipId: 'champ-1' });
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr()).toEqual({
|
||||
code: 'CHAMPIONSHIP_CONFIG_NOT_FOUND',
|
||||
details: { message: 'Championship config not found: champ-1' },
|
||||
});
|
||||
});
|
||||
|
||||
it('should recalculate standings successfully', async () => {
|
||||
it('recalculates standings successfully and presents result', async () => {
|
||||
const league = { id: 'league-1' };
|
||||
const season = { id: 'season-1', leagueId: 'league-1' };
|
||||
const championship = { id: 'champ-1', name: 'Champ 1', sessionTypes: ['main'], pointsTableBySessionType: {}, dropScorePolicy: {} };
|
||||
const championship = {
|
||||
id: 'champ-1',
|
||||
name: 'Champ 1',
|
||||
sessionTypes: ['main'],
|
||||
pointsTableBySessionType: {},
|
||||
dropScorePolicy: {},
|
||||
};
|
||||
const leagueScoringConfig = { championships: [championship] };
|
||||
const races = [{ id: 'race-1', sessionType: 'race' }];
|
||||
const results: Result[] = [];
|
||||
const races = [{ id: 'race-1', sessionType: 'race' as const }];
|
||||
const results: unknown[] = [];
|
||||
const penalties: Penalty[] = [];
|
||||
const standings = [{ participant: 'driver-1', position: 1, totalPoints: 25, resultsCounted: 1, resultsDropped: 0 }];
|
||||
const standings = [
|
||||
{
|
||||
participant: { id: 'driver-1' },
|
||||
position: { toNumber: () => 1 },
|
||||
totalPoints: { toNumber: () => 25 },
|
||||
resultsCounted: { toNumber: () => 1 },
|
||||
resultsDropped: { toNumber: () => 0 },
|
||||
},
|
||||
];
|
||||
|
||||
leagueRepository.findById.mockResolvedValue(league);
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
leagueScoringConfigRepository.findBySeasonId.mockResolvedValue(leagueScoringConfig);
|
||||
raceRepository.findByLeagueId.mockResolvedValue(races);
|
||||
@@ -99,13 +161,50 @@ describe('RecalculateChampionshipStandingsUseCase', () => {
|
||||
championshipAggregator.aggregate.mockReturnValue(standings);
|
||||
championshipStandingRepository.saveAll.mockResolvedValue(undefined);
|
||||
|
||||
const result = await useCase.execute({ seasonId: 'season-1', championshipId: 'champ-1' });
|
||||
const input: RecalculateChampionshipStandingsInput = {
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
const dto = result.unwrap();
|
||||
expect(dto.seasonId).toBe('season-1');
|
||||
expect(dto.championshipId).toBe('champ-1');
|
||||
expect(dto.championshipName).toBe('Champ 1');
|
||||
expect(dto.rows).toEqual([{ participant: 'driver-1', position: 1, totalPoints: 25, resultsCounted: 1, resultsDropped: 0 }]);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
|
||||
const presented = output.present.mock.calls[0][0] as RecalculateChampionshipStandingsResult;
|
||||
expect(presented.leagueId).toBe('league-1');
|
||||
expect(presented.seasonId).toBe('season-1');
|
||||
expect(presented.entries).toHaveLength(1);
|
||||
expect(presented.entries[0]).toEqual({
|
||||
driverId: 'driver-1',
|
||||
teamId: null,
|
||||
position: 1,
|
||||
points: 25,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('wraps repository failures in REPOSITORY_ERROR and does not call output', async () => {
|
||||
leagueRepository.findById.mockResolvedValue({ id: 'league-1' });
|
||||
seasonRepository.findById.mockResolvedValue({ id: 'season-1', leagueId: 'league-1' });
|
||||
leagueScoringConfigRepository.findBySeasonId.mockImplementation(() => {
|
||||
throw new Error('boom');
|
||||
});
|
||||
|
||||
const input: RecalculateChampionshipStandingsInput = {
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
RecalculateChampionshipStandingsErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('REPOSITORY_ERROR');
|
||||
expect(error.details.message).toContain('boom');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user