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,7 +7,6 @@ import {
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
import type { ILeagueScoringConfigRepository } from '../../domain/repositories/ILeagueScoringConfigRepository';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
describe('CreateLeagueWithSeasonAndScoringUseCase', () => {
let useCase: CreateLeagueWithSeasonAndScoringUseCase;
@@ -27,7 +26,7 @@ describe('CreateLeagueWithSeasonAndScoringUseCase', () => {
warn: Mock;
error: Mock;
};
let output: { present: Mock } & UseCaseOutputPort<CreateLeagueWithSeasonAndScoringResult>;
let output: { present: Mock } ;
beforeEach(() => {
leagueRepository = {
@@ -47,14 +46,11 @@ describe('CreateLeagueWithSeasonAndScoringUseCase', () => {
error: vi.fn(),
};
output = { present: vi.fn() } as unknown as typeof output;
useCase = new CreateLeagueWithSeasonAndScoringUseCase(
leagueRepository as unknown as ILeagueRepository,
useCase = new CreateLeagueWithSeasonAndScoringUseCase(leagueRepository as unknown as ILeagueRepository,
seasonRepository as unknown as ISeasonRepository,
leagueScoringConfigRepository as unknown as ILeagueScoringConfigRepository,
getLeagueScoringPresetById,
logger as unknown as Logger,
output,
);
logger as unknown as Logger);
});
it('should create league, season, and scoring successfully', async () => {
@@ -88,9 +84,7 @@ describe('CreateLeagueWithSeasonAndScoringUseCase', () => {
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeUndefined();
expect(output.present).toHaveBeenCalledTimes(1);
const presented = (output.present as Mock).mock.calls[0]?.[0] as unknown as CreateLeagueWithSeasonAndScoringResult;
expect(presented?.league.id.toString()).toBeDefined();
const presented = (expect(presented?.league.id.toString()).toBeDefined();
expect(presented?.season.id).toBeDefined();
expect(presented?.scoringConfig.seasonId.toString()).toBe(presented?.season.id);
expect(leagueRepository.create).toHaveBeenCalledTimes(1);
@@ -119,8 +113,7 @@ describe('CreateLeagueWithSeasonAndScoringUseCase', () => {
if ('details' in err && err.details && typeof err.details === 'object' && 'message' in err.details) {
expect(err.details.message).toBe('League name is required');
}
expect(output.present).not.toHaveBeenCalled();
});
});
it('should return error when ownerId is empty', async () => {
const command = {
@@ -143,8 +136,7 @@ describe('CreateLeagueWithSeasonAndScoringUseCase', () => {
if ('details' in err && err.details && typeof err.details === 'object' && 'message' in err.details) {
expect(err.details.message).toBe('League ownerId is required');
}
expect(output.present).not.toHaveBeenCalled();
});
});
it('should return error when gameId is empty', async () => {
const command = {
@@ -167,8 +159,7 @@ describe('CreateLeagueWithSeasonAndScoringUseCase', () => {
if ('details' in err && err.details && typeof err.details === 'object' && 'message' in err.details) {
expect(err.details.message).toBe('gameId is required');
}
expect(output.present).not.toHaveBeenCalled();
});
});
it('should return error when visibility is missing', async () => {
const command: Partial<CreateLeagueWithSeasonAndScoringCommand> = {
@@ -189,8 +180,7 @@ describe('CreateLeagueWithSeasonAndScoringUseCase', () => {
if ('details' in err && err.details && typeof err.details === 'object' && 'message' in err.details) {
expect(err.details.message).toBe('visibility is required');
}
expect(output.present).not.toHaveBeenCalled();
});
});
it('should return error when maxDrivers is invalid', async () => {
const command = {
@@ -214,8 +204,7 @@ describe('CreateLeagueWithSeasonAndScoringUseCase', () => {
if ('details' in err && err.details && typeof err.details === 'object' && 'message' in err.details) {
expect(err.details.message).toBe('maxDrivers must be greater than 0 when provided');
}
expect(output.present).not.toHaveBeenCalled();
});
});
it('should return error when ranked league has insufficient drivers', async () => {
const command = {
@@ -239,8 +228,7 @@ describe('CreateLeagueWithSeasonAndScoringUseCase', () => {
if ('details' in err && err.details && typeof err.details === 'object' && 'message' in err.details) {
expect(err.details.message).toContain('Ranked leagues require at least 10 drivers');
}
expect(output.present).not.toHaveBeenCalled();
});
});
it('should return error when scoring preset is unknown', async () => {
const command = {
@@ -266,8 +254,7 @@ describe('CreateLeagueWithSeasonAndScoringUseCase', () => {
if ('details' in err && err.details && typeof err.details === 'object' && 'message' in err.details) {
expect(err.details.message).toBe('Unknown scoring preset: unknown-preset');
}
expect(output.present).not.toHaveBeenCalled();
});
});
it('should return error when repository throws', async () => {
const command = {
@@ -298,6 +285,5 @@ describe('CreateLeagueWithSeasonAndScoringUseCase', () => {
if ('details' in err && err.details && typeof err.details === 'object' && 'message' in err.details) {
expect(err.details.message).toBe('DB error');
}
expect(output.present).not.toHaveBeenCalled();
});
});
});