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,60 +1,46 @@
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import { ImportRaceResultsApiUseCase } from './ImportRaceResultsApiUseCase';
import { IRaceRepository } from '../../domain/repositories/IRaceRepository';
import { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
import { IResultRepository } from '../../domain/repositories/IResultRepository';
import { IDriverRepository } from '../../domain/repositories/IDriverRepository';
import { IStandingRepository } from '../../domain/repositories/IStandingRepository';
import { describe, it, expect, beforeEach, vi, type Mock } from 'vitest';
import {
ImportRaceResultsApiUseCase,
type ImportRaceResultsApiInput,
type ImportRaceResultsApiResult,
type ImportRaceResultsApiErrorCode,
} from './ImportRaceResultsApiUseCase';
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
import type { IResultRepository } from '../../domain/repositories/IResultRepository';
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
import type { IStandingRepository } from '../../domain/repositories/IStandingRepository';
import type { Logger } from '@core/shared/application';
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import { Result } from '@core/shared/application/Result';
describe('ImportRaceResultsApiUseCase', () => {
let useCase: ImportRaceResultsApiUseCase;
let raceRepository: {
findById: Mock;
};
let leagueRepository: {
findById: Mock;
};
let resultRepository: {
existsByRaceId: Mock;
createMany: Mock;
};
let driverRepository: {
findByIRacingId: Mock;
};
let standingRepository: {
recalculate: Mock;
};
let logger: {
debug: Mock;
info: Mock;
warn: Mock;
error: Mock;
};
let raceRepository: { findById: Mock };
let leagueRepository: { findById: Mock };
let resultRepository: { existsByRaceId: Mock; createMany: Mock };
let driverRepository: { findByIRacingId: Mock };
let standingRepository: { recalculate: Mock };
let logger: { debug: Mock; info: Mock; warn: Mock; error: Mock };
let output: UseCaseOutputPort<ImportRaceResultsApiResult> & { present: Mock };
beforeEach(() => {
raceRepository = {
findById: vi.fn(),
};
leagueRepository = {
findById: vi.fn(),
};
resultRepository = {
existsByRaceId: vi.fn(),
createMany: vi.fn(),
};
driverRepository = {
findByIRacingId: vi.fn(),
};
standingRepository = {
recalculate: vi.fn(),
};
raceRepository = { findById: vi.fn() };
leagueRepository = { findById: vi.fn() };
resultRepository = { existsByRaceId: vi.fn(), createMany: vi.fn() };
driverRepository = { findByIRacingId: vi.fn() };
standingRepository = { recalculate: vi.fn() };
logger = {
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
};
output = {
present: vi.fn(),
} as unknown as UseCaseOutputPort<ImportRaceResultsApiResult> & { present: Mock };
useCase = new ImportRaceResultsApiUseCase(
raceRepository as unknown as IRaceRepository,
leagueRepository as unknown as ILeagueRepository,
@@ -62,85 +48,129 @@ describe('ImportRaceResultsApiUseCase', () => {
driverRepository as unknown as IDriverRepository,
standingRepository as unknown as IStandingRepository,
logger as unknown as Logger,
output,
);
});
it('should return parse error for invalid JSON', async () => {
const params = { raceId: 'race-1', resultsFileContent: 'invalid json' };
const input: ImportRaceResultsApiInput = { raceId: 'race-1', resultsFileContent: 'invalid json' };
const result = await useCase.execute(params);
const result: Result<
void,
ApplicationErrorCode<ImportRaceResultsApiErrorCode, { message: string }>
> = await useCase.execute(input);
expect(result.isErr()).toBe(true);
expect(result.unwrapErr()).toEqual({
code: 'PARSE_ERROR',
details: { message: 'Invalid JSON in results file content' },
});
const err = result.unwrapErr() as ApplicationErrorCode<
ImportRaceResultsApiErrorCode,
{ message: string }
>;
expect(err.code).toBe('PARSE_ERROR');
expect(err.details?.message).toBe('Invalid JSON in results file content');
expect(output.present).not.toHaveBeenCalled();
});
it('should return race not found error', async () => {
const params = { raceId: 'race-1', resultsFileContent: '[]' };
const input: ImportRaceResultsApiInput = { raceId: 'race-1', resultsFileContent: '[]' };
raceRepository.findById.mockResolvedValue(null);
const result = await useCase.execute(params);
const result: Result<
void,
ApplicationErrorCode<ImportRaceResultsApiErrorCode, { message: string }>
> = await useCase.execute(input);
expect(result.isErr()).toBe(true);
expect(result.unwrapErr()).toEqual({
code: 'RACE_NOT_FOUND',
details: { message: 'Race race-1 not found' },
});
const err = result.unwrapErr() as ApplicationErrorCode<
ImportRaceResultsApiErrorCode,
{ message: string }
>;
expect(err.code).toBe('RACE_NOT_FOUND');
expect(err.details?.message).toBe('Race race-1 not found');
expect(output.present).not.toHaveBeenCalled();
});
it('should return league not found error', async () => {
const params = { raceId: 'race-1', resultsFileContent: '[]' };
const input: ImportRaceResultsApiInput = { raceId: 'race-1', resultsFileContent: '[]' };
raceRepository.findById.mockResolvedValue({ id: 'race-1', leagueId: 'league-1' });
leagueRepository.findById.mockResolvedValue(null);
const result = await useCase.execute(params);
const result: Result<
void,
ApplicationErrorCode<ImportRaceResultsApiErrorCode, { message: string }>
> = await useCase.execute(input);
expect(result.isErr()).toBe(true);
expect(result.unwrapErr()).toEqual({
code: 'LEAGUE_NOT_FOUND',
details: { message: 'League league-1 not found' },
});
const err = result.unwrapErr() as ApplicationErrorCode<
ImportRaceResultsApiErrorCode,
{ message: string }
>;
expect(err.code).toBe('LEAGUE_NOT_FOUND');
expect(err.details?.message).toBe('League league-1 not found');
expect(output.present).not.toHaveBeenCalled();
});
it('should return results exist error', async () => {
const params = { raceId: 'race-1', resultsFileContent: '[]' };
const input: ImportRaceResultsApiInput = { raceId: 'race-1', resultsFileContent: '[]' };
raceRepository.findById.mockResolvedValue({ id: 'race-1', leagueId: 'league-1' });
leagueRepository.findById.mockResolvedValue({ id: 'league-1' });
resultRepository.existsByRaceId.mockResolvedValue(true);
const result = await useCase.execute(params);
const result: Result<
void,
ApplicationErrorCode<ImportRaceResultsApiErrorCode, { message: string }>
> = await useCase.execute(input);
expect(result.isErr()).toBe(true);
expect(result.unwrapErr()).toEqual({
code: 'RESULTS_EXIST',
details: { message: 'Results already exist for this race' },
});
const err = result.unwrapErr() as ApplicationErrorCode<
ImportRaceResultsApiErrorCode,
{ message: string }
>;
expect(err.code).toBe('RESULTS_EXIST');
expect(err.details?.message).toBe('Results already exist for this race');
expect(output.present).not.toHaveBeenCalled();
});
it('should return driver not found error', async () => {
const params = { raceId: 'race-1', resultsFileContent: '[{"id":"result-1","raceId":"race-1","driverId":"123","position":1,"fastestLap":100,"incidents":0,"startPosition":1}]' };
const input: ImportRaceResultsApiInput = {
raceId: 'race-1',
resultsFileContent:
'[{"id":"result-1","raceId":"race-1","driverId":"123","position":1,"fastestLap":100,"incidents":0,"startPosition":1}]',
};
raceRepository.findById.mockResolvedValue({ id: 'race-1', leagueId: 'league-1' });
leagueRepository.findById.mockResolvedValue({ id: 'league-1' });
resultRepository.existsByRaceId.mockResolvedValue(false);
driverRepository.findByIRacingId.mockResolvedValue(null);
const result = await useCase.execute(params);
const result: Result<
void,
ApplicationErrorCode<ImportRaceResultsApiErrorCode, { message: string }>
> = await useCase.execute(input);
expect(result.isErr()).toBe(true);
expect(result.unwrapErr()).toEqual({
code: 'DRIVER_NOT_FOUND',
details: { message: 'Driver with iRacing ID 123 not found' },
});
const err = result.unwrapErr() as ApplicationErrorCode<
ImportRaceResultsApiErrorCode,
{ message: string }
>;
expect(err.code).toBe('DRIVER_NOT_FOUND');
expect(err.details?.message).toBe('Driver with iRacing ID 123 not found');
expect(output.present).not.toHaveBeenCalled();
});
it('should import results successfully', async () => {
const params = { raceId: 'race-1', resultsFileContent: '[{"id":"result-1","raceId":"race-1","driverId":"123","position":1,"fastestLap":100,"incidents":0,"startPosition":1}]' };
const input: ImportRaceResultsApiInput = {
raceId: 'race-1',
resultsFileContent:
'[{"id":"result-1","raceId":"race-1","driverId":"123","position":1,"fastestLap":100,"incidents":0,"startPosition":1}]',
};
raceRepository.findById.mockResolvedValue({ id: 'race-1', leagueId: 'league-1' });
leagueRepository.findById.mockResolvedValue({ id: 'league-1' });
@@ -149,16 +179,25 @@ describe('ImportRaceResultsApiUseCase', () => {
resultRepository.createMany.mockResolvedValue(undefined);
standingRepository.recalculate.mockResolvedValue(undefined);
const result = await useCase.execute(params);
const result: Result<
void,
ApplicationErrorCode<ImportRaceResultsApiErrorCode, { message: string }>
> = await useCase.execute(input);
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toEqual({
success: true,
raceId: 'race-1',
driversProcessed: 1,
resultsRecorded: 1,
errors: [],
});
expect(result.unwrap()).toBeUndefined();
expect(output.present).toHaveBeenCalledTimes(1);
const presented =
output.present.mock.calls[0][0] as ImportRaceResultsApiResult;
expect(presented.success).toBe(true);
expect(presented.raceId).toBe('race-1');
expect(presented.leagueId).toBe('league-1');
expect(presented.driversProcessed).toBe(1);
expect(presented.resultsRecorded).toBe(1);
expect(presented.errors).toEqual([]);
expect(resultRepository.createMany).toHaveBeenCalledWith([
expect.objectContaining({
id: 'result-1',