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

@@ -5,7 +5,6 @@ import {
type GetLeagueScheduleResult,
type GetLeagueScheduleErrorCode,
} from './GetLeagueScheduleUseCase';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { League } from '../../domain/entities/League';
import { Race } from '../../domain/entities/Race';
@@ -25,8 +24,6 @@ describe('GetLeagueScheduleUseCase', () => {
findByLeagueId: Mock;
};
let logger: Logger;
let output: UseCaseOutputPort<GetLeagueScheduleResult> & { present: Mock };
beforeEach(() => {
leagueRepository = {
findById: vi.fn(),
@@ -44,17 +41,10 @@ describe('GetLeagueScheduleUseCase', () => {
warn: vi.fn(),
error: vi.fn(),
} as unknown as Logger;
output = {
present: vi.fn(),
} as unknown as UseCaseOutputPort<GetLeagueScheduleResult> & { present: Mock };
useCase = new GetLeagueScheduleUseCase(
leagueRepository as unknown as ILeagueRepository,
useCase = new GetLeagueScheduleUseCase(leagueRepository as unknown as ILeagueRepository,
seasonRepository as any,
raceRepository as unknown as IRaceRepository,
logger,
output,
);
logger);
});
it('should present league schedule when races exist', async () => {
@@ -78,16 +68,12 @@ describe('GetLeagueScheduleUseCase', () => {
const result = await useCase.execute(input);
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeUndefined();
expect(output.present).toHaveBeenCalledTimes(1);
const presented = output.present.mock.calls[0]?.[0] as GetLeagueScheduleResult;
expect(presented.league).toBe(league);
expect(presented.seasonId).toBe('season-1');
expect(presented.published).toBe(false);
expect(presented.races).toHaveLength(1);
expect(presented.races[0]?.race).toBe(race);
const resultValue = result.unwrap();
expect(resultValue.league).toBe(league);
expect(resultValue.seasonId).toBe('season-1');
expect(resultValue.published).toBe(false);
expect(resultValue.races).toHaveLength(1);
expect(resultValue.races[0]?.race).toBe(race);
});
it('should scope schedule by seasonId (no season bleed)', async () => {
@@ -125,20 +111,18 @@ describe('GetLeagueScheduleUseCase', () => {
// Season 1 covers January
const resultSeason1 = await useCase.execute({ leagueId, seasonId: 'season-jan' } as any);
expect(resultSeason1.isOk()).toBe(true);
const presented1 = output.present.mock.calls.at(-1)?.[0] as any;
expect(presented1.seasonId).toBe('season-jan');
expect(presented1.published).toBe(false);
expect((presented1.races as GetLeagueScheduleResult['races']).map(r => r.race.id)).toEqual(['race-jan']);
const resultValue1 = resultSeason1.unwrap();
expect(resultValue1.seasonId).toBe('season-jan');
expect(resultValue1.published).toBe(false);
expect(resultValue1.races.map(r => r.race.id)).toEqual(['race-jan']);
// Season 2 covers February
const resultSeason2 = await useCase.execute({ leagueId, seasonId: 'season-feb' } as any);
expect(resultSeason2.isOk()).toBe(true);
const presented2 = output.present.mock.calls.at(-1)?.[0] as any;
expect(presented2.seasonId).toBe('season-feb');
expect(presented2.published).toBe(false);
expect((presented2.races as GetLeagueScheduleResult['races']).map(r => r.race.id)).toEqual(['race-feb']);
const resultValue2 = resultSeason2.unwrap();
expect(resultValue2.seasonId).toBe('season-feb');
expect(resultValue2.published).toBe(false);
expect(resultValue2.races.map(r => r.race.id)).toEqual(['race-feb']);
});
it('should present empty schedule when no races exist', async () => {
@@ -155,15 +139,10 @@ describe('GetLeagueScheduleUseCase', () => {
const result = await useCase.execute(input);
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeUndefined();
expect(output.present).toHaveBeenCalledTimes(1);
const presented =
output.present.mock.calls[0]?.[0] as GetLeagueScheduleResult;
expect(presented.league).toBe(league);
expect(presented.published).toBe(false);
expect(presented.races).toHaveLength(0);
const resultValue = result.unwrap();
expect(resultValue.league).toBe(league);
expect(resultValue.published).toBe(false);
expect(resultValue.races).toHaveLength(0);
});
it('should return LEAGUE_NOT_FOUND error when league does not exist', async () => {
@@ -182,8 +161,7 @@ describe('GetLeagueScheduleUseCase', () => {
expect(err.code).toBe('LEAGUE_NOT_FOUND');
expect(err.details.message).toBe('League not found');
expect(output.present).not.toHaveBeenCalled();
});
});
it('should return REPOSITORY_ERROR when repository throws', async () => {
const leagueId = 'league-1';
@@ -207,6 +185,5 @@ describe('GetLeagueScheduleUseCase', () => {
expect(err.code).toBe('REPOSITORY_ERROR');
expect(err.details.message).toBe('DB down');
expect(output.present).not.toHaveBeenCalled();
});
});
});