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,51 +1,51 @@
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import { GetTotalDriversUseCase } from './GetTotalDriversUseCase';
import {
GetTotalDriversUseCase,
GetTotalDriversInput,
GetTotalDriversResult,
GetTotalDriversErrorCode,
} from './GetTotalDriversUseCase';
import { IDriverRepository } from '../../domain/repositories/IDriverRepository';
import { Driver } from '../../domain/entities/Driver';
import type { Logger } from '@core/shared/application';
import type { UseCaseOutputPort } from '@core/shared/application';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
describe('GetTotalDriversUseCase', () => {
let useCase: GetTotalDriversUseCase;
let driverRepository: {
findAll: Mock;
};
let logger: {
debug: Mock;
info: Mock;
warn: Mock;
error: Mock;
};
let output: UseCaseOutputPort<GetTotalDriversResult> & { present: Mock };
beforeEach(() => {
driverRepository = {
findAll: vi.fn(),
};
logger = {
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
};
output = {
present: vi.fn(),
} as unknown as UseCaseOutputPort<GetTotalDriversResult> & { present: Mock };
useCase = new GetTotalDriversUseCase(
driverRepository as unknown as IDriverRepository,
logger as unknown as Logger,
output,
);
});
it('should return total number of drivers', async () => {
const drivers = [
Driver.create({ id: '1', iracingId: '123', name: 'Driver 1', country: 'US' }),
Driver.create({ id: '2', iracingId: '456', name: 'Driver 2', country: 'UK' }),
];
const drivers = [{ id: '1' }, { id: '2' }];
driverRepository.findAll.mockResolvedValue(drivers);
const result = await useCase.execute();
const input: GetTotalDriversInput = {};
const result = await useCase.execute(input);
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toEqual({
totalDrivers: 2,
});
expect(result.unwrap()).toBeUndefined();
expect(output.present).toHaveBeenCalledTimes(1);
expect(output.present).toHaveBeenCalledWith<[{ totalDrivers: number }]>(
expect.objectContaining({ totalDrivers: 2 }),
);
});
it('should return error on repository failure', async () => {
@@ -53,12 +53,19 @@ describe('GetTotalDriversUseCase', () => {
driverRepository.findAll.mockRejectedValue(error);
const result = await useCase.execute();
const input: GetTotalDriversInput = {};
const result = await useCase.execute(input);
expect(result.isErr()).toBe(true);
expect(result.unwrapErr()).toEqual({
code: 'REPOSITORY_ERROR',
details: { message: 'Failed to retrieve total drivers' },
});
const unwrappedError = result.unwrapErr() as ApplicationErrorCode<
GetTotalDriversErrorCode,
{ message: string }
>;
expect(unwrappedError.code).toBe('REPOSITORY_ERROR');
expect(unwrappedError.details.message).toBe(error.message);
expect(output.present).not.toHaveBeenCalled();
});
});