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,6 +1,5 @@
import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import { Race } from '../../domain/entities/Race';
@@ -58,7 +57,6 @@ function createSeasonWithinWindow(overrides?: Partial<{ leagueId: string }>): Se
describe('CreateLeagueSeasonScheduleRaceUseCase', () => {
let seasonRepository: { findById: Mock };
let raceRepository: { create: Mock };
let output: UseCaseOutputPort<CreateLeagueSeasonScheduleRaceResult> & { present: Mock };
let logger: Logger;
beforeEach(() => {
@@ -73,11 +71,9 @@ describe('CreateLeagueSeasonScheduleRaceUseCase', () => {
seasonRepository.findById.mockResolvedValue(season);
raceRepository.create.mockImplementation(async (race: Race) => race);
const useCase = new CreateLeagueSeasonScheduleRaceUseCase(
seasonRepository as unknown as ISeasonRepository,
const useCase = new CreateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
raceRepository as unknown as IRaceRepository,
logger,
output,
{ generateRaceId: () => 'race-123' },
);
@@ -91,9 +87,6 @@ describe('CreateLeagueSeasonScheduleRaceUseCase', () => {
});
expect(result.isOk()).toBe(true);
expect(output.present).toHaveBeenCalledTimes(1);
expect(output.present).toHaveBeenCalledWith({ raceId: 'race-123' });
expect(raceRepository.create).toHaveBeenCalledTimes(1);
const createdRace = raceRepository.create.mock.calls[0]?.[0] as Race;
expect(createdRace.id).toBe('race-123');
@@ -107,11 +100,9 @@ describe('CreateLeagueSeasonScheduleRaceUseCase', () => {
const season = createSeasonWithinWindow({ leagueId: 'other-league' });
seasonRepository.findById.mockResolvedValue(season);
const useCase = new CreateLeagueSeasonScheduleRaceUseCase(
seasonRepository as unknown as ISeasonRepository,
const useCase = new CreateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
raceRepository as unknown as IRaceRepository,
logger,
output,
{ generateRaceId: () => 'race-123' },
);
@@ -129,7 +120,6 @@ describe('CreateLeagueSeasonScheduleRaceUseCase', () => {
{ message: string }
>;
expect(error.code).toBe('SEASON_NOT_FOUND');
expect(output.present).not.toHaveBeenCalled();
expect(raceRepository.create).not.toHaveBeenCalled();
});
@@ -137,11 +127,9 @@ describe('CreateLeagueSeasonScheduleRaceUseCase', () => {
const season = createSeasonWithinWindow();
seasonRepository.findById.mockResolvedValue(season);
const useCase = new CreateLeagueSeasonScheduleRaceUseCase(
seasonRepository as unknown as ISeasonRepository,
const useCase = new CreateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
raceRepository as unknown as IRaceRepository,
logger,
output,
{ generateRaceId: () => 'race-123' },
);
@@ -159,7 +147,6 @@ describe('CreateLeagueSeasonScheduleRaceUseCase', () => {
{ message: string }
>;
expect(error.code).toBe('RACE_OUTSIDE_SEASON_WINDOW');
expect(output.present).not.toHaveBeenCalled();
expect(raceRepository.create).not.toHaveBeenCalled();
});
});
@@ -167,7 +154,6 @@ describe('CreateLeagueSeasonScheduleRaceUseCase', () => {
describe('UpdateLeagueSeasonScheduleRaceUseCase', () => {
let seasonRepository: { findById: Mock };
let raceRepository: { findById: Mock; update: Mock };
let output: UseCaseOutputPort<UpdateLeagueSeasonScheduleRaceResult> & { present: Mock };
let logger: Logger;
beforeEach(() => {
@@ -191,12 +177,9 @@ describe('UpdateLeagueSeasonScheduleRaceUseCase', () => {
raceRepository.findById.mockResolvedValue(existing);
raceRepository.update.mockImplementation(async (race: Race) => race);
const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(
seasonRepository as unknown as ISeasonRepository,
const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
raceRepository as unknown as IRaceRepository,
logger,
output,
);
logger);
const newScheduledAt = new Date('2025-01-20T20:00:00Z');
const result = await useCase.execute({
@@ -209,9 +192,6 @@ describe('UpdateLeagueSeasonScheduleRaceUseCase', () => {
});
expect(result.isOk()).toBe(true);
expect(output.present).toHaveBeenCalledTimes(1);
expect(output.present).toHaveBeenCalledWith({ success: true });
expect(raceRepository.update).toHaveBeenCalledTimes(1);
const updated = raceRepository.update.mock.calls[0]?.[0] as Race;
expect(updated.id).toBe('race-1');
@@ -225,12 +205,9 @@ describe('UpdateLeagueSeasonScheduleRaceUseCase', () => {
const season = createSeasonWithinWindow({ leagueId: 'other-league' });
seasonRepository.findById.mockResolvedValue(season);
const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(
seasonRepository as unknown as ISeasonRepository,
const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
raceRepository as unknown as IRaceRepository,
logger,
output,
);
logger);
const result = await useCase.execute({
leagueId: 'league-1',
@@ -247,8 +224,7 @@ describe('UpdateLeagueSeasonScheduleRaceUseCase', () => {
expect(error.code).toBe('SEASON_NOT_FOUND');
expect(raceRepository.findById).not.toHaveBeenCalled();
expect(raceRepository.update).not.toHaveBeenCalled();
expect(output.present).not.toHaveBeenCalled();
});
});
it('returns RACE_OUTSIDE_SEASON_WINDOW when updated scheduledAt is outside window and does not update', async () => {
const season = createSeasonWithinWindow();
@@ -263,12 +239,9 @@ describe('UpdateLeagueSeasonScheduleRaceUseCase', () => {
});
raceRepository.findById.mockResolvedValue(existing);
const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(
seasonRepository as unknown as ISeasonRepository,
const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
raceRepository as unknown as IRaceRepository,
logger,
output,
);
logger);
const result = await useCase.execute({
leagueId: 'league-1',
@@ -284,20 +257,16 @@ describe('UpdateLeagueSeasonScheduleRaceUseCase', () => {
>;
expect(error.code).toBe('RACE_OUTSIDE_SEASON_WINDOW');
expect(raceRepository.update).not.toHaveBeenCalled();
expect(output.present).not.toHaveBeenCalled();
});
});
it('returns RACE_NOT_FOUND when race does not exist for league and does not update', async () => {
const season = createSeasonWithinWindow();
seasonRepository.findById.mockResolvedValue(season);
raceRepository.findById.mockResolvedValue(null);
const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(
seasonRepository as unknown as ISeasonRepository,
const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
raceRepository as unknown as IRaceRepository,
logger,
output,
);
logger);
const result = await useCase.execute({
leagueId: 'league-1',
@@ -313,14 +282,12 @@ describe('UpdateLeagueSeasonScheduleRaceUseCase', () => {
>;
expect(error.code).toBe('RACE_NOT_FOUND');
expect(raceRepository.update).not.toHaveBeenCalled();
expect(output.present).not.toHaveBeenCalled();
});
});
});
describe('DeleteLeagueSeasonScheduleRaceUseCase', () => {
let seasonRepository: { findById: Mock };
let raceRepository: { findById: Mock; delete: Mock };
let output: UseCaseOutputPort<DeleteLeagueSeasonScheduleRaceResult> & { present: Mock };
let logger: Logger;
beforeEach(() => {
@@ -344,12 +311,9 @@ describe('DeleteLeagueSeasonScheduleRaceUseCase', () => {
raceRepository.findById.mockResolvedValue(existing);
raceRepository.delete.mockResolvedValue(undefined);
const useCase = new DeleteLeagueSeasonScheduleRaceUseCase(
seasonRepository as unknown as ISeasonRepository,
const useCase = new DeleteLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
raceRepository as unknown as IRaceRepository,
logger,
output,
);
logger);
const result = await useCase.execute({
leagueId: 'league-1',
@@ -358,8 +322,6 @@ describe('DeleteLeagueSeasonScheduleRaceUseCase', () => {
});
expect(result.isOk()).toBe(true);
expect(output.present).toHaveBeenCalledTimes(1);
expect(output.present).toHaveBeenCalledWith({ success: true });
expect(raceRepository.delete).toHaveBeenCalledTimes(1);
expect(raceRepository.delete).toHaveBeenCalledWith('race-1');
});
@@ -368,12 +330,9 @@ describe('DeleteLeagueSeasonScheduleRaceUseCase', () => {
const season = createSeasonWithinWindow({ leagueId: 'other-league' });
seasonRepository.findById.mockResolvedValue(season);
const useCase = new DeleteLeagueSeasonScheduleRaceUseCase(
seasonRepository as unknown as ISeasonRepository,
const useCase = new DeleteLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
raceRepository as unknown as IRaceRepository,
logger,
output,
);
logger);
const result = await useCase.execute({
leagueId: 'league-1',
@@ -389,20 +348,16 @@ describe('DeleteLeagueSeasonScheduleRaceUseCase', () => {
expect(error.code).toBe('SEASON_NOT_FOUND');
expect(raceRepository.findById).not.toHaveBeenCalled();
expect(raceRepository.delete).not.toHaveBeenCalled();
expect(output.present).not.toHaveBeenCalled();
});
});
it('returns RACE_NOT_FOUND when race does not exist for league and does not delete', async () => {
const season = createSeasonWithinWindow();
seasonRepository.findById.mockResolvedValue(season);
raceRepository.findById.mockResolvedValue(null);
const useCase = new DeleteLeagueSeasonScheduleRaceUseCase(
seasonRepository as unknown as ISeasonRepository,
const useCase = new DeleteLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
raceRepository as unknown as IRaceRepository,
logger,
output,
);
logger);
const result = await useCase.execute({
leagueId: 'league-1',
@@ -417,13 +372,11 @@ describe('DeleteLeagueSeasonScheduleRaceUseCase', () => {
>;
expect(error.code).toBe('RACE_NOT_FOUND');
expect(raceRepository.delete).not.toHaveBeenCalled();
expect(output.present).not.toHaveBeenCalled();
});
});
});
describe('PublishLeagueSeasonScheduleUseCase', () => {
let seasonRepository: { findById: Mock; update: Mock };
let output: UseCaseOutputPort<PublishLeagueSeasonScheduleResult> & { present: Mock };
let logger: Logger;
beforeEach(() => {
@@ -437,22 +390,12 @@ describe('PublishLeagueSeasonScheduleUseCase', () => {
seasonRepository.findById.mockResolvedValue(season);
seasonRepository.update.mockResolvedValue(undefined);
const useCase = new PublishLeagueSeasonScheduleUseCase(
seasonRepository as unknown as ISeasonRepository,
logger,
output,
);
const useCase = new PublishLeagueSeasonScheduleUseCase(seasonRepository as unknown as ISeasonRepository,
logger);
const result = await useCase.execute({ leagueId: 'league-1', seasonId: 'season-1' });
expect(result.isOk()).toBe(true);
expect(output.present).toHaveBeenCalledTimes(1);
expect(output.present).toHaveBeenCalledWith({
success: true,
seasonId: 'season-1',
published: true,
});
expect(seasonRepository.update).toHaveBeenCalledTimes(1);
const updatedSeason = seasonRepository.update.mock.calls[0]?.[0] as Season;
expect(updatedSeason.id).toBe('season-1');
@@ -464,11 +407,8 @@ describe('PublishLeagueSeasonScheduleUseCase', () => {
const season = createSeasonWithinWindow({ leagueId: 'other-league' });
seasonRepository.findById.mockResolvedValue(season);
const useCase = new PublishLeagueSeasonScheduleUseCase(
seasonRepository as unknown as ISeasonRepository,
logger,
output,
);
const useCase = new PublishLeagueSeasonScheduleUseCase(seasonRepository as unknown as ISeasonRepository,
logger);
const result = await useCase.execute({ leagueId: 'league-1', seasonId: 'season-1' });
@@ -479,13 +419,11 @@ describe('PublishLeagueSeasonScheduleUseCase', () => {
>;
expect(error.code).toBe('SEASON_NOT_FOUND');
expect(seasonRepository.update).not.toHaveBeenCalled();
expect(output.present).not.toHaveBeenCalled();
});
});
});
describe('UnpublishLeagueSeasonScheduleUseCase', () => {
let seasonRepository: { findById: Mock; update: Mock };
let output: UseCaseOutputPort<UnpublishLeagueSeasonScheduleResult> & { present: Mock };
let logger: Logger;
beforeEach(() => {
@@ -499,22 +437,12 @@ describe('UnpublishLeagueSeasonScheduleUseCase', () => {
seasonRepository.findById.mockResolvedValue(season);
seasonRepository.update.mockResolvedValue(undefined);
const useCase = new UnpublishLeagueSeasonScheduleUseCase(
seasonRepository as unknown as ISeasonRepository,
logger,
output,
);
const useCase = new UnpublishLeagueSeasonScheduleUseCase(seasonRepository as unknown as ISeasonRepository,
logger);
const result = await useCase.execute({ leagueId: 'league-1', seasonId: 'season-1' });
expect(result.isOk()).toBe(true);
expect(output.present).toHaveBeenCalledTimes(1);
expect(output.present).toHaveBeenCalledWith({
success: true,
seasonId: 'season-1',
published: false,
});
expect(seasonRepository.update).toHaveBeenCalledTimes(1);
const updatedSeason = seasonRepository.update.mock.calls[0]?.[0] as Season;
expect(updatedSeason.id).toBe('season-1');
@@ -526,11 +454,8 @@ describe('UnpublishLeagueSeasonScheduleUseCase', () => {
const season = createSeasonWithinWindow({ leagueId: 'other-league' });
seasonRepository.findById.mockResolvedValue(season);
const useCase = new UnpublishLeagueSeasonScheduleUseCase(
seasonRepository as unknown as ISeasonRepository,
logger,
output,
);
const useCase = new UnpublishLeagueSeasonScheduleUseCase(seasonRepository as unknown as ISeasonRepository,
logger);
const result = await useCase.execute({ leagueId: 'league-1', seasonId: 'season-1' });
@@ -541,6 +466,5 @@ describe('UnpublishLeagueSeasonScheduleUseCase', () => {
>;
expect(error.code).toBe('SEASON_NOT_FOUND');
expect(seasonRepository.update).not.toHaveBeenCalled();
expect(output.present).not.toHaveBeenCalled();
});
});
});