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,7 +1,14 @@
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import { GetLeagueStandingsUseCase } from './GetLeagueStandingsUseCase';
import { IStandingRepository } from '../../domain/repositories/IStandingRepository';
import { IDriverRepository } from '../../domain/repositories/IDriverRepository';
import { describe, it, expect, beforeEach, vi, type Mock } from 'vitest';
import type { UseCaseOutputPort } from '@core/shared/application';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import {
GetLeagueStandingsUseCase,
type GetLeagueStandingsInput,
type GetLeagueStandingsResult,
type GetLeagueStandingsErrorCode,
} from './GetLeagueStandingsUseCase';
import type { IStandingRepository } from '../../domain/repositories/IStandingRepository';
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
import { Standing } from '../../domain/entities/Standing';
import { Driver } from '../../domain/entities/Driver';
@@ -13,6 +20,7 @@ describe('GetLeagueStandingsUseCase', () => {
let driverRepository: {
findById: Mock;
};
let output: UseCaseOutputPort<GetLeagueStandingsResult> & { present: Mock };
beforeEach(() => {
standingRepository = {
@@ -21,13 +29,18 @@ describe('GetLeagueStandingsUseCase', () => {
driverRepository = {
findById: vi.fn(),
};
output = {
present: vi.fn(),
};
useCase = new GetLeagueStandingsUseCase(
standingRepository as unknown as IStandingRepository,
driverRepository as unknown as IDriverRepository,
output,
);
});
it('should return standings with drivers mapped', async () => {
it('should present standings with drivers mapped and return ok result', async () => {
const leagueId = 'league-1';
const standings = [
Standing.create({
@@ -65,37 +78,43 @@ describe('GetLeagueStandingsUseCase', () => {
return Promise.resolve(null);
});
const result = await useCase.execute({ leagueId });
const result = await useCase.execute({ leagueId } satisfies GetLeagueStandingsInput);
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toEqual({
standings: [
{
driverId: 'driver-1',
driver: { id: 'driver-1', name: 'Driver One' },
points: 100,
rank: 1,
},
{
driverId: 'driver-2',
driver: { id: 'driver-2', name: 'Driver Two' },
points: 80,
rank: 2,
},
],
expect(result.unwrap()).toBeUndefined();
expect(output.present).toHaveBeenCalledTimes(1);
const presented = output.present.mock.calls[0]![0] as GetLeagueStandingsResult;
expect(presented.standings).toHaveLength(2);
expect(presented.standings[0]).toEqual({
driverId: 'driver-1',
driver: driver1,
points: 100,
rank: 1,
});
expect(presented.standings[1]).toEqual({
driverId: 'driver-2',
driver: driver2,
points: 80,
rank: 2,
});
});
it('should return error when repository fails', async () => {
it('should return repository error and not call output when repository fails', async () => {
const leagueId = 'league-1';
standingRepository.findByLeagueId.mockRejectedValue(new Error('DB error'));
const result = await useCase.execute({ leagueId });
const result = await useCase.execute({ leagueId } satisfies GetLeagueStandingsInput);
expect(result.isErr()).toBe(true);
expect(result.unwrapErr()).toEqual({
code: 'REPOSITORY_ERROR',
message: 'Failed to fetch league standings',
});
const error = result.unwrapErr() as ApplicationErrorCode<
GetLeagueStandingsErrorCode,
{ message: string }
>;
expect(error.code).toBe('REPOSITORY_ERROR');
expect(error.details.message).toBe('DB error');
expect(output.present).not.toHaveBeenCalled();
});
});