refactor use cases

This commit is contained in:
2026-01-08 15:34:51 +01:00
parent d984ab24a8
commit 52e9a2f6a7
362 changed files with 5192 additions and 8409 deletions

View File

@@ -1,4 +1,3 @@
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import { beforeEach, describe, expect, it, Mock, vi } from 'vitest';
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
@@ -16,8 +15,6 @@ describe('FileProtestUseCase', () => {
let mockLeagueMembershipRepo: {
getLeagueMembers: Mock;
};
let output: UseCaseOutputPort<FileProtestResult> & { present: Mock };
beforeEach(() => {
mockProtestRepo = {
create: vi.fn(),
@@ -28,18 +25,12 @@ describe('FileProtestUseCase', () => {
mockLeagueMembershipRepo = {
getLeagueMembers: vi.fn(),
};
output = {
present: vi.fn(),
} as unknown as UseCaseOutputPort<FileProtestResult> & { present: Mock };
});
});
it('should return error when race does not exist', async () => {
const useCase = new FileProtestUseCase(
mockProtestRepo as unknown as IProtestRepository,
const useCase = new FileProtestUseCase(mockProtestRepo as unknown as IProtestRepository,
mockRaceRepo as unknown as IRaceRepository,
mockLeagueMembershipRepo as unknown as ILeagueMembershipRepository,
output,
);
mockLeagueMembershipRepo as unknown as ILeagueMembershipRepository);
mockRaceRepo.findById.mockResolvedValue(null);
@@ -54,16 +45,12 @@ describe('FileProtestUseCase', () => {
const err = result.unwrapErr() as ApplicationErrorCode<FileProtestErrorCode, { message: string }>;
expect(err.code).toBe('RACE_NOT_FOUND');
expect(err.details?.message).toBe('Race not found');
expect(output.present).not.toHaveBeenCalled();
});
});
it('should return error when protesting against self', async () => {
const useCase = new FileProtestUseCase(
mockProtestRepo as unknown as IProtestRepository,
const useCase = new FileProtestUseCase(mockProtestRepo as unknown as IProtestRepository,
mockRaceRepo as unknown as IRaceRepository,
mockLeagueMembershipRepo as unknown as ILeagueMembershipRepository,
output,
);
mockLeagueMembershipRepo as unknown as ILeagueMembershipRepository);
mockRaceRepo.findById.mockResolvedValue({ id: 'race1', leagueId: 'league1' });
@@ -78,16 +65,12 @@ describe('FileProtestUseCase', () => {
const err = result.unwrapErr() as ApplicationErrorCode<FileProtestErrorCode, { message: string }>;
expect(err.code).toBe('SELF_PROTEST');
expect(err.details?.message).toBe('Cannot file a protest against yourself');
expect(output.present).not.toHaveBeenCalled();
});
});
it('should return error when protesting driver is not an active member', async () => {
const useCase = new FileProtestUseCase(
mockProtestRepo as unknown as IProtestRepository,
const useCase = new FileProtestUseCase(mockProtestRepo as unknown as IProtestRepository,
mockRaceRepo as unknown as IRaceRepository,
mockLeagueMembershipRepo as unknown as ILeagueMembershipRepository,
output,
);
mockLeagueMembershipRepo as unknown as ILeagueMembershipRepository);
mockRaceRepo.findById.mockResolvedValue({ id: 'race1', leagueId: 'league1' });
mockLeagueMembershipRepo.getLeagueMembers.mockResolvedValue([
@@ -105,16 +88,12 @@ describe('FileProtestUseCase', () => {
const err = result.unwrapErr() as ApplicationErrorCode<FileProtestErrorCode, { message: string }>;
expect(err.code).toBe('NOT_MEMBER');
expect(err.details?.message).toBe('Protesting driver is not an active member of this league');
expect(output.present).not.toHaveBeenCalled();
});
});
it('should create protest and return protestId on success', async () => {
const useCase = new FileProtestUseCase(
mockProtestRepo as unknown as IProtestRepository,
const useCase = new FileProtestUseCase(mockProtestRepo as unknown as IProtestRepository,
mockRaceRepo as unknown as IRaceRepository,
mockLeagueMembershipRepo as unknown as ILeagueMembershipRepository,
output,
);
mockLeagueMembershipRepo as unknown as ILeagueMembershipRepository);
mockRaceRepo.findById.mockResolvedValue({ id: 'race1', leagueId: 'league1' });
mockLeagueMembershipRepo.getLeagueMembers.mockResolvedValue([
@@ -159,9 +138,7 @@ describe('FileProtestUseCase', () => {
expect(created.incident.description.toString()).toBe('Collision');
expect(created.incident.timeInRace).toBeUndefined();
expect(output.present).toHaveBeenCalledTimes(1);
const presented = (output.present as unknown as Mock).mock.calls[0]?.[0] as FileProtestResult;
expect(presented.protest.raceId.toString()).toBe('race1');
const presented = (expect(presented.protest.raceId.toString()).toBe('race1');
expect(presented.protest.protestingDriverId.toString()).toBe('driver1');
expect(presented.protest.accusedDriverId.toString()).toBe('driver2');
expect(presented.protest.incident.lap.toNumber()).toBe(5);