refactor racing use cases
This commit is contained in:
@@ -1,10 +1,17 @@
|
||||
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
|
||||
import { GetRaceWithSOFUseCase } from './GetRaceWithSOFUseCase';
|
||||
import {
|
||||
GetRaceWithSOFUseCase,
|
||||
type GetRaceWithSOFInput,
|
||||
type GetRaceWithSOFResult,
|
||||
type GetRaceWithSOFErrorCode,
|
||||
} from './GetRaceWithSOFUseCase';
|
||||
import { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
||||
import { IRaceRegistrationRepository } from '../../domain/repositories/IRaceRegistrationRepository';
|
||||
import { IResultRepository } from '../../domain/repositories/IResultRepository';
|
||||
import { Race } from '../../domain/entities/Race';
|
||||
import { SessionType } from '../../domain/value-objects/SessionType';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
describe('GetRaceWithSOFUseCase', () => {
|
||||
let useCase: GetRaceWithSOFUseCase;
|
||||
@@ -18,6 +25,7 @@ describe('GetRaceWithSOFUseCase', () => {
|
||||
findByRaceId: Mock;
|
||||
};
|
||||
let getDriverRating: Mock;
|
||||
let output: UseCaseOutputPort<GetRaceWithSOFResult> & { present: Mock };
|
||||
|
||||
beforeEach(() => {
|
||||
raceRepository = {
|
||||
@@ -30,21 +38,31 @@ describe('GetRaceWithSOFUseCase', () => {
|
||||
findByRaceId: vi.fn(),
|
||||
};
|
||||
getDriverRating = vi.fn();
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
};
|
||||
useCase = new GetRaceWithSOFUseCase(
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
registrationRepository as unknown as IRaceRegistrationRepository,
|
||||
resultRepository as unknown as IResultRepository,
|
||||
getDriverRating,
|
||||
output,
|
||||
);
|
||||
});
|
||||
|
||||
it('should return error when race not found', async () => {
|
||||
raceRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const result = await useCase.execute({ raceId: 'race-1' });
|
||||
const result = await useCase.execute({ raceId: 'race-1' } as GetRaceWithSOFInput);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr()).toEqual({ code: 'RACE_NOT_FOUND' });
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
GetRaceWithSOFErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(err.code).toBe('RACE_NOT_FOUND');
|
||||
expect(err.details?.message).toBe('Race with id race-1 not found');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should return race with stored SOF when available', async () => {
|
||||
@@ -62,20 +80,31 @@ describe('GetRaceWithSOFUseCase', () => {
|
||||
});
|
||||
|
||||
raceRepository.findById.mockResolvedValue(race);
|
||||
registrationRepository.getRegisteredDrivers.mockResolvedValue(['driver-1', 'driver-2', 'driver-3', 'driver-4', 'driver-5', 'driver-6', 'driver-7', 'driver-8', 'driver-9', 'driver-10']);
|
||||
registrationRepository.getRegisteredDrivers.mockResolvedValue([
|
||||
'driver-1',
|
||||
'driver-2',
|
||||
'driver-3',
|
||||
'driver-4',
|
||||
'driver-5',
|
||||
'driver-6',
|
||||
'driver-7',
|
||||
'driver-8',
|
||||
'driver-9',
|
||||
'driver-10',
|
||||
]);
|
||||
|
||||
const result = await useCase.execute({ raceId: 'race-1' });
|
||||
const result = await useCase.execute({ raceId: 'race-1' } as GetRaceWithSOFInput);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
const dto = result.unwrap();
|
||||
expect(dto.raceId).toBe('race-1');
|
||||
expect(dto.leagueId).toBe('league-1');
|
||||
expect(dto.strengthOfField).toBe(1500);
|
||||
expect(dto.registeredCount).toBe(10);
|
||||
expect(dto.maxParticipants).toBe(20);
|
||||
expect(dto.participantCount).toBe(10);
|
||||
expect(dto.sessionType).toBe('main');
|
||||
expect(dto.status).toBe('scheduled');
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const [[presented]] = output.present.mock.calls as [[GetRaceWithSOFResult]];
|
||||
expect(presented.race.id).toBe('race-1');
|
||||
expect(presented.race.leagueId).toBe('league-1');
|
||||
expect(presented.strengthOfField).toBe(1500);
|
||||
expect(presented.registeredCount).toBe(10);
|
||||
expect(presented.maxParticipants).toBe(20);
|
||||
expect(presented.participantCount).toBe(10);
|
||||
});
|
||||
|
||||
it('should calculate SOF for upcoming race using registrations', async () => {
|
||||
@@ -92,17 +121,23 @@ describe('GetRaceWithSOFUseCase', () => {
|
||||
raceRepository.findById.mockResolvedValue(race);
|
||||
registrationRepository.getRegisteredDrivers.mockResolvedValue(['driver-1', 'driver-2']);
|
||||
getDriverRating.mockImplementation((input) => {
|
||||
if (input.driverId === 'driver-1') return Promise.resolve({ rating: 1400, ratingChange: null });
|
||||
if (input.driverId === 'driver-2') return Promise.resolve({ rating: 1600, ratingChange: null });
|
||||
if (input.driverId === 'driver-1') {
|
||||
return Promise.resolve({ rating: 1400, ratingChange: null });
|
||||
}
|
||||
if (input.driverId === 'driver-2') {
|
||||
return Promise.resolve({ rating: 1600, ratingChange: null });
|
||||
}
|
||||
return Promise.resolve({ rating: null, ratingChange: null });
|
||||
});
|
||||
|
||||
const result = await useCase.execute({ raceId: 'race-1' });
|
||||
const result = await useCase.execute({ raceId: 'race-1' } as GetRaceWithSOFInput);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
const dto = result.unwrap();
|
||||
expect(dto.strengthOfField).toBe(1500); // average
|
||||
expect(dto.participantCount).toBe(2);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const [[presented]] = output.present.mock.calls as [[GetRaceWithSOFResult]];
|
||||
expect(presented.strengthOfField).toBe(1500); // average
|
||||
expect(presented.participantCount).toBe(2);
|
||||
expect(registrationRepository.getRegisteredDrivers).toHaveBeenCalledWith('race-1');
|
||||
expect(resultRepository.findByRaceId).not.toHaveBeenCalled();
|
||||
});
|
||||
@@ -124,17 +159,23 @@ describe('GetRaceWithSOFUseCase', () => {
|
||||
{ driverId: 'driver-2' },
|
||||
]);
|
||||
getDriverRating.mockImplementation((input) => {
|
||||
if (input.driverId === 'driver-1') return Promise.resolve({ rating: 1400, ratingChange: null });
|
||||
if (input.driverId === 'driver-2') return Promise.resolve({ rating: 1600, ratingChange: null });
|
||||
if (input.driverId === 'driver-1') {
|
||||
return Promise.resolve({ rating: 1400, ratingChange: null });
|
||||
}
|
||||
if (input.driverId === 'driver-2') {
|
||||
return Promise.resolve({ rating: 1600, ratingChange: null });
|
||||
}
|
||||
return Promise.resolve({ rating: null, ratingChange: null });
|
||||
});
|
||||
|
||||
const result = await useCase.execute({ raceId: 'race-1' });
|
||||
const result = await useCase.execute({ raceId: 'race-1' } as GetRaceWithSOFInput);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
const dto = result.unwrap();
|
||||
expect(dto.strengthOfField).toBe(1500);
|
||||
expect(dto.participantCount).toBe(2);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const [[presented]] = output.present.mock.calls as [[GetRaceWithSOFResult]];
|
||||
expect(presented.strengthOfField).toBe(1500);
|
||||
expect(presented.participantCount).toBe(2);
|
||||
expect(resultRepository.findByRaceId).toHaveBeenCalledWith('race-1');
|
||||
expect(registrationRepository.getRegisteredDrivers).not.toHaveBeenCalled();
|
||||
});
|
||||
@@ -153,17 +194,21 @@ describe('GetRaceWithSOFUseCase', () => {
|
||||
raceRepository.findById.mockResolvedValue(race);
|
||||
registrationRepository.getRegisteredDrivers.mockResolvedValue(['driver-1', 'driver-2']);
|
||||
getDriverRating.mockImplementation((input) => {
|
||||
if (input.driverId === 'driver-1') return Promise.resolve({ rating: 1400, ratingChange: null });
|
||||
if (input.driverId === 'driver-1') {
|
||||
return Promise.resolve({ rating: 1400, ratingChange: null });
|
||||
}
|
||||
// driver-2 missing
|
||||
return Promise.resolve({ rating: null, ratingChange: null });
|
||||
});
|
||||
|
||||
const result = await useCase.execute({ raceId: 'race-1' });
|
||||
const result = await useCase.execute({ raceId: 'race-1' } as GetRaceWithSOFInput);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
const dto = result.unwrap();
|
||||
expect(dto.strengthOfField).toBe(1400); // only one rating
|
||||
expect(dto.participantCount).toBe(2);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const [[presented]] = output.present.mock.calls as [[GetRaceWithSOFResult]];
|
||||
expect(presented.strengthOfField).toBe(1400); // only one rating
|
||||
expect(presented.participantCount).toBe(2);
|
||||
});
|
||||
|
||||
it('should return null SOF when no participants', async () => {
|
||||
@@ -180,11 +225,28 @@ describe('GetRaceWithSOFUseCase', () => {
|
||||
raceRepository.findById.mockResolvedValue(race);
|
||||
registrationRepository.getRegisteredDrivers.mockResolvedValue([]);
|
||||
|
||||
const result = await useCase.execute({ raceId: 'race-1' });
|
||||
const result = await useCase.execute({ raceId: 'race-1' } as GetRaceWithSOFInput);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
const dto = result.unwrap();
|
||||
expect(dto.strengthOfField).toBe(null);
|
||||
expect(dto.participantCount).toBe(0);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const [[presented]] = output.present.mock.calls as [[GetRaceWithSOFResult]];
|
||||
expect(presented.strengthOfField).toBe(null);
|
||||
expect(presented.participantCount).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
it('should wrap repository errors in REPOSITORY_ERROR and not call output', async () => {
|
||||
raceRepository.findById.mockRejectedValue(new Error('boom'));
|
||||
|
||||
const result = await useCase.execute({ raceId: 'race-1' } as GetRaceWithSOFInput);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
GetRaceWithSOFErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(err.code).toBe('REPOSITORY_ERROR');
|
||||
expect(err.details?.message).toBe('boom');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user