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

@@ -7,9 +7,9 @@ import {
} from './SubmitProtestDefenseUseCase';
import type { IProtestRepository } from '../../domain/repositories/IProtestRepository';
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import { Result } from '@core/shared/application/Result';
import type { Logger } from '@core/shared/application';
interface MockProtest {
id: string;
@@ -22,7 +22,6 @@ describe('SubmitProtestDefenseUseCase', () => {
let leagueRepository: ILeagueRepository & { findById: ReturnType<typeof vi.fn> };
let protestRepository: IProtestRepository & { findById: ReturnType<typeof vi.fn>; update: ReturnType<typeof vi.fn> };
let logger: Logger & { error: ReturnType<typeof vi.fn> };
let output: UseCaseOutputPort<SubmitProtestDefenseResult> & { present: ReturnType<typeof vi.fn> };
let useCase: SubmitProtestDefenseUseCase;
const createInput = (overrides: Partial<SubmitProtestDefenseInput> = {}): SubmitProtestDefenseInput => ({
@@ -35,7 +34,7 @@ describe('SubmitProtestDefenseUseCase', () => {
});
const unwrapError = (
result: Result<void, ApplicationErrorCode<SubmitProtestDefenseErrorCode, { message: string }>>,
result: Result<SubmitProtestDefenseResult, ApplicationErrorCode<SubmitProtestDefenseErrorCode, { message: string }>>,
): ApplicationErrorCode<SubmitProtestDefenseErrorCode, { message: string }> => {
expect(result.isErr()).toBe(true);
return result.unwrapErr();
@@ -75,16 +74,9 @@ describe('SubmitProtestDefenseUseCase', () => {
error: vi.fn(),
} as unknown as Logger & { error: ReturnType<typeof vi.fn> };
output = {
present: vi.fn(),
} as unknown as UseCaseOutputPort<SubmitProtestDefenseResult> & { present: ReturnType<typeof vi.fn> };
useCase = new SubmitProtestDefenseUseCase(
leagueRepository as unknown as ILeagueRepository,
useCase = new SubmitProtestDefenseUseCase(leagueRepository as unknown as ILeagueRepository,
protestRepository as unknown as IProtestRepository,
logger as unknown as Logger,
output,
);
logger as unknown as Logger);
});
it('submits defense successfully', async () => {
@@ -104,19 +96,16 @@ describe('SubmitProtestDefenseUseCase', () => {
const result = await useCase.execute(input);
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeUndefined();
const value = result.unwrap();
expect(value.leagueId).toBe('league-1');
expect(value.protestId).toBe('protest-1');
expect(value.driverId).toBe('driver-1');
expect(value.status).toBe('defense_submitted');
expect(leagueRepository.findById).toHaveBeenCalledWith('league-1');
expect(protestRepository.findById).toHaveBeenCalledWith('protest-1');
expect(mockProtest.canSubmitDefense).toHaveBeenCalled();
expect(mockProtest.submitDefense).toHaveBeenCalledWith('My defense', 'http://video.com');
expect(protestRepository.update).toHaveBeenCalled();
expect(output.present).toHaveBeenCalledTimes(1);
expect(output.present).toHaveBeenCalledWith({
leagueId: 'league-1',
protestId: 'protest-1',
driverId: 'driver-1',
status: 'defense_submitted',
});
});
it('returns error when league not found', async () => {
@@ -129,7 +118,6 @@ describe('SubmitProtestDefenseUseCase', () => {
const error = unwrapError(result);
expect(error.code).toBe('LEAGUE_NOT_FOUND');
expect(error.details?.message).toBe('League not found');
expect(output.present).not.toHaveBeenCalled();
});
it('returns error when protest not found', async () => {
@@ -143,7 +131,6 @@ describe('SubmitProtestDefenseUseCase', () => {
const error = unwrapError(result);
expect(error.code).toBe('PROTEST_NOT_FOUND');
expect(error.details?.message).toBe('Protest not found');
expect(output.present).not.toHaveBeenCalled();
});
it('returns error when driver is not allowed', async () => {
@@ -162,7 +149,6 @@ describe('SubmitProtestDefenseUseCase', () => {
const error = unwrapError(result);
expect(error.code).toBe('DRIVER_NOT_ALLOWED');
expect(error.details?.message).toBe('Driver is not allowed to submit a defense for this protest');
expect(output.present).not.toHaveBeenCalled();
});
it('returns error when defense cannot be submitted due to invalid state', async () => {
@@ -183,7 +169,6 @@ describe('SubmitProtestDefenseUseCase', () => {
const error = unwrapError(result);
expect(error.code).toBe('INVALID_PROTEST_STATE');
expect(error.details?.message).toBe('Defense cannot be submitted for this protest');
expect(output.present).not.toHaveBeenCalled();
expect(mockProtest.submitDefense).not.toHaveBeenCalled();
});
@@ -206,7 +191,6 @@ describe('SubmitProtestDefenseUseCase', () => {
const error = unwrapError(result);
expect(error.code).toBe('REPOSITORY_ERROR');
expect(error.details?.message).toBe('DB failure');
expect(output.present).not.toHaveBeenCalled();
expect(logger.error).toHaveBeenCalled();
});
});
});