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

@@ -10,7 +10,6 @@ import type { IRaceEventRepository } from '../../domain/repositories/IRaceEventR
import type { IResultRepository } from '../../domain/repositories/IResultRepository';
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
import type { 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/Logger';
@@ -29,7 +28,6 @@ describe('SendFinalResultsUseCase', () => {
let leagueRepository: { findById: Mock };
let membershipRepository: { getMembership: Mock };
let logger: Logger;
let output: UseCaseOutputPort<SendFinalResultsResult> & { present: Mock };
let useCase: SendFinalResultsUseCase;
beforeEach(() => {
@@ -44,17 +42,12 @@ describe('SendFinalResultsUseCase', () => {
warn: vi.fn(),
error: vi.fn(),
};
output = { present: vi.fn() } as unknown as UseCaseOutputPort<SendFinalResultsResult> & { present: Mock };
useCase = new SendFinalResultsUseCase(
notificationService as unknown as NotificationService,
useCase = new SendFinalResultsUseCase(notificationService as unknown as NotificationService,
raceEventRepository as unknown as IRaceEventRepository,
resultRepository as unknown as IResultRepository,
leagueRepository as unknown as ILeagueRepository,
membershipRepository as unknown as ILeagueMembershipRepository,
logger,
output,
);
logger);
});
const createInput = (overrides: Partial<SendFinalResultsInput> = {}): SendFinalResultsInput => ({
@@ -109,9 +102,7 @@ describe('SendFinalResultsUseCase', () => {
expect(resultRepository.findByRaceId).toHaveBeenCalledWith('session-1');
expect(notificationService.sendNotification).toHaveBeenCalledTimes(2);
expect(output.present).toHaveBeenCalledTimes(1);
const presentedRaw = output.present.mock.calls[0]?.[0];
expect(presentedRaw).toBeDefined();
const presentedRaw = expect(presentedRaw).toBeDefined();
const presented = presentedRaw as SendFinalResultsResult;
expect(presented).toEqual({
leagueId: 'league-1',
@@ -128,8 +119,7 @@ describe('SendFinalResultsUseCase', () => {
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 RACE_NOT_FOUND when race event does not exist', async () => {
leagueRepository.findById.mockResolvedValue({ id: 'league-1' });
@@ -140,8 +130,7 @@ describe('SendFinalResultsUseCase', () => {
const error = unwrapError(result);
expect(error.code).toBe('RACE_NOT_FOUND');
expect(error.details?.message).toBe('Race event not found');
expect(output.present).not.toHaveBeenCalled();
});
});
it('returns INSUFFICIENT_PERMISSIONS when user is not steward or higher', async () => {
leagueRepository.findById.mockResolvedValue({ id: 'league-1' });
@@ -153,8 +142,7 @@ describe('SendFinalResultsUseCase', () => {
const error = unwrapError(result);
expect(error.code).toBe('INSUFFICIENT_PERMISSIONS');
expect(error.details?.message).toBe('Insufficient permissions to send final results');
expect(output.present).not.toHaveBeenCalled();
});
});
it('returns RESULTS_NOT_FINAL when race is not closed', async () => {
leagueRepository.findById.mockResolvedValue({ id: 'league-1' });
@@ -172,8 +160,7 @@ describe('SendFinalResultsUseCase', () => {
const error = unwrapError(result);
expect(error.code).toBe('RESULTS_NOT_FINAL');
expect(error.details?.message).toBe('Race results are not in a final state');
expect(output.present).not.toHaveBeenCalled();
});
});
it('returns RESULTS_NOT_FINAL when main race session is missing', async () => {
leagueRepository.findById.mockResolvedValue({ id: 'league-1' });
@@ -191,8 +178,7 @@ describe('SendFinalResultsUseCase', () => {
const error = unwrapError(result);
expect(error.code).toBe('RESULTS_NOT_FINAL');
expect(error.details?.message).toBe('Main race session not found for race event');
expect(output.present).not.toHaveBeenCalled();
});
});
it('wraps repository errors into REPOSITORY_ERROR and does not present output', async () => {
const mockError = new Error('Repository failure');
@@ -203,7 +189,6 @@ describe('SendFinalResultsUseCase', () => {
const error = unwrapError(result);
expect(error.code).toBe('REPOSITORY_ERROR');
expect(error.details?.message).toBe('Repository failure');
expect(output.present).not.toHaveBeenCalled();
expect(logger.error).toHaveBeenCalled();
});
});