94 lines
3.5 KiB
TypeScript
94 lines
3.5 KiB
TypeScript
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
|
|
import {
|
|
GetRacePenaltiesUseCase,
|
|
type GetRacePenaltiesInput,
|
|
type GetRacePenaltiesResult,
|
|
type GetRacePenaltiesErrorCode,
|
|
} from './GetRacePenaltiesUseCase';
|
|
import type { IPenaltyRepository } from '../../domain/repositories/IPenaltyRepository';
|
|
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
|
|
import type { UseCaseOutputPort } from '@core/shared/application';
|
|
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
|
|
|
describe('GetRacePenaltiesUseCase', () => {
|
|
let useCase: GetRacePenaltiesUseCase;
|
|
let penaltyRepository: { findByRaceId: Mock };
|
|
let driverRepository: { findById: Mock };
|
|
let output: UseCaseOutputPort<GetRacePenaltiesResult> & { present: Mock };
|
|
|
|
beforeEach(() => {
|
|
penaltyRepository = { findByRaceId: vi.fn() };
|
|
driverRepository = { findById: vi.fn() };
|
|
output = { present: vi.fn() } as UseCaseOutputPort<GetRacePenaltiesResult> & { present: Mock };
|
|
useCase = new GetRacePenaltiesUseCase(
|
|
penaltyRepository as unknown as IPenaltyRepository,
|
|
driverRepository as unknown as IDriverRepository,
|
|
output,
|
|
);
|
|
});
|
|
|
|
it('should return penalties with drivers', async () => {
|
|
const input: GetRacePenaltiesInput = { raceId: 'race-1' };
|
|
const penalties = [
|
|
{
|
|
id: 'penalty-1',
|
|
raceId: input.raceId,
|
|
driverId: 'driver-1',
|
|
issuedBy: 'driver-2',
|
|
type: 'time' as const,
|
|
value: 10,
|
|
reason: 'Reason 1',
|
|
status: 'applied' as const,
|
|
issuedAt: new Date(),
|
|
},
|
|
];
|
|
const drivers = [
|
|
{ id: 'driver-1', name: 'Driver 1' },
|
|
{ id: 'driver-2', name: 'Driver 2' },
|
|
];
|
|
|
|
penaltyRepository.findByRaceId.mockResolvedValue(penalties);
|
|
driverRepository.findById.mockImplementation((id) => Promise.resolve(drivers.find((d) => d.id === id)));
|
|
|
|
const result = await useCase.execute(input);
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
expect(output.present).toHaveBeenCalledTimes(1);
|
|
const presented = output.present.mock.calls[0]![0] as GetRacePenaltiesResult;
|
|
expect(presented.penalties).toEqual(penalties);
|
|
expect(presented.drivers).toEqual(drivers);
|
|
});
|
|
|
|
it('should return empty when no penalties', async () => {
|
|
penaltyRepository.findByRaceId.mockResolvedValue([]);
|
|
driverRepository.findById.mockResolvedValue(null);
|
|
|
|
const input: GetRacePenaltiesInput = { raceId: 'race-1' };
|
|
const result = await useCase.execute(input);
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
expect(output.present).toHaveBeenCalledTimes(1);
|
|
const presented = output.present.mock.calls[0]![0] as GetRacePenaltiesResult;
|
|
expect(presented.penalties).toEqual([]);
|
|
expect(presented.drivers).toEqual([]);
|
|
});
|
|
|
|
it('should return repository error when repository throws', async () => {
|
|
const input: GetRacePenaltiesInput = { raceId: 'race-1' };
|
|
const repositoryError = new Error('Repository failure');
|
|
penaltyRepository.findByRaceId.mockRejectedValue(repositoryError);
|
|
|
|
const result = await useCase.execute(input);
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
const err = result.unwrapErr() as ApplicationErrorCode<
|
|
GetRacePenaltiesErrorCode,
|
|
{ message: string }
|
|
>;
|
|
expect(err.code).toBe('REPOSITORY_ERROR');
|
|
expect(err.details.message).toBe('Repository failure');
|
|
expect(output.present).not.toHaveBeenCalled();
|
|
});
|
|
}); |