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,200 +1,223 @@
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import {
GetLeagueScoringConfigUseCase,
type GetLeagueScoringConfigResult,
type GetLeagueScoringConfigInput,
type GetLeagueScoringConfigErrorCode,
} from './GetLeagueScoringConfigUseCase';
import { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
import { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
import { ILeagueScoringConfigRepository } from '../../domain/repositories/ILeagueScoringConfigRepository';
import { IGameRepository } from '../../domain/repositories/IGameRepository';
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import { Result } from '@core/shared/application/Result';
import { GetLeagueScoringConfigUseCase } from './GetLeagueScoringConfigUseCase';
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
import type { ILeagueScoringConfigRepository } from '../../domain/repositories/ILeagueScoringConfigRepository';
import type { IGameRepository } from '../../domain/repositories/IGameRepository';
import type { League } from '../../domain/entities/League';
import type { Season } from '../../domain/entities/season/Season';
import type { LeagueScoringConfig } from '../../domain/entities/LeagueScoringConfig';
import type { Game } from '../../domain/entities/Game';
import type { LeagueScoringPreset } from '../../domain/types/LeagueScoringPreset';
describe('GetLeagueScoringConfigUseCase', () => {
let useCase: GetLeagueScoringConfigUseCase;
let leagueRepository: { findById: Mock };
let seasonRepository: { findByLeagueId: Mock };
let leagueScoringConfigRepository: { findBySeasonId: Mock };
let gameRepository: { findById: Mock };
let presetProvider: { getPresetById: Mock };
let output: UseCaseOutputPort<GetLeagueScoringConfigResult> & { present: Mock };
let mockLeagueRepository: jest.Mocked<ILeagueRepository>;
let mockSeasonRepository: jest.Mocked<ISeasonRepository>;
let mockLeagueScoringConfigRepository: jest.Mocked<ILeagueScoringConfigRepository>;
let mockGameRepository: jest.Mocked<IGameRepository>;
let mockPresetProvider: { getPresetById: jest.Mock };
beforeEach(() => {
leagueRepository = { findById: vi.fn() };
seasonRepository = { findByLeagueId: vi.fn() };
leagueScoringConfigRepository = { findBySeasonId: vi.fn() };
gameRepository = { findById: vi.fn() };
presetProvider = { getPresetById: vi.fn() };
output = { present: vi.fn() } as unknown as UseCaseOutputPort<GetLeagueScoringConfigResult> & {
present: Mock;
mockLeagueRepository = {
findById: jest.fn(),
exists: jest.fn(),
save: jest.fn(),
findAll: jest.fn(),
} as any;
mockSeasonRepository = {
findByLeagueId: jest.fn(),
save: jest.fn(),
findById: jest.fn(),
} as any;
mockLeagueScoringConfigRepository = {
findBySeasonId: jest.fn(),
save: jest.fn(),
} as any;
mockGameRepository = {
findById: jest.fn(),
save: jest.fn(),
findAll: jest.fn(),
} as any;
mockPresetProvider = {
getPresetById: jest.fn(),
};
useCase = new GetLeagueScoringConfigUseCase(
leagueRepository as unknown as ILeagueRepository,
seasonRepository as unknown as ISeasonRepository,
leagueScoringConfigRepository as unknown as ILeagueScoringConfigRepository,
gameRepository as unknown as IGameRepository,
presetProvider,
output,
mockLeagueRepository,
mockSeasonRepository,
mockLeagueScoringConfigRepository,
mockGameRepository,
mockPresetProvider,
);
});
it('should return scoring config for active season', async () => {
const input: GetLeagueScoringConfigInput = { leagueId: 'league-1' };
const league = { id: input.leagueId };
const season = { id: 'season-1', status: 'active', gameId: 'game-1' };
const scoringConfig = { scoringPresetId: 'preset-1', championships: [] };
const game = { id: 'game-1', name: 'Game 1' };
const preset = { id: 'preset-1', name: 'Preset 1' };
it('should return scoring config with league, season, game, and preset', async () => {
const mockLeague = { id: 'league-1', name: 'Test League' } as League;
const mockSeason = {
id: 'season-1',
gameId: 'game-1',
status: { toString: () => 'active' }
} as unknown as Season;
const mockScoringConfig = {
id: 'config-1',
gameId: 'game-1',
scoringPresetId: 'preset-1'
} as unknown as LeagueScoringConfig;
const mockGame = { id: 'game-1', name: 'Test Game' } as Game;
const mockPreset = { id: 'preset-1', name: 'Test Preset' } as LeagueScoringPreset;
leagueRepository.findById.mockResolvedValue(league);
seasonRepository.findByLeagueId.mockResolvedValue([season]);
leagueScoringConfigRepository.findBySeasonId.mockResolvedValue(scoringConfig);
gameRepository.findById.mockResolvedValue(game);
presetProvider.getPresetById.mockReturnValue(preset);
mockLeagueRepository.findById.mockResolvedValue(mockLeague);
mockSeasonRepository.findByLeagueId.mockResolvedValue([mockSeason]);
mockLeagueScoringConfigRepository.findBySeasonId.mockResolvedValue(mockScoringConfig);
mockGameRepository.findById.mockResolvedValue(mockGame);
mockPresetProvider.getPresetById.mockReturnValue(mockPreset);
const result = await useCase.execute(input);
const result = await useCase.execute({ leagueId: 'league-1' });
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeUndefined();
expect(output.present).toHaveBeenCalledTimes(1);
const presented =
output.present.mock.calls[0]?.[0] as GetLeagueScoringConfigResult;
expect(presented?.league).toEqual(league);
expect(presented?.season).toEqual(season);
expect(presented?.scoringConfig).toEqual(scoringConfig);
expect(presented?.game).toEqual(game);
expect(presented?.preset).toEqual(preset);
const value = result.value as any;
expect(value.league).toBe(mockLeague);
expect(value.season).toBe(mockSeason);
expect(value.scoringConfig).toBe(mockScoringConfig);
expect(value.game).toBe(mockGame);
expect(value.preset).toBe(mockPreset);
});
it('should return scoring config for first season if no active', async () => {
const input: GetLeagueScoringConfigInput = { leagueId: 'league-1' };
const league = { id: input.leagueId };
const season = { id: 'season-1', status: 'inactive', gameId: 'game-1' };
const scoringConfig = { scoringPresetId: undefined, championships: [] };
const game = { id: 'game-1', name: 'Game 1' };
it('should return LEAGUE_NOT_FOUND when league does not exist', async () => {
mockLeagueRepository.findById.mockResolvedValue(null);
leagueRepository.findById.mockResolvedValue(league);
seasonRepository.findByLeagueId.mockResolvedValue([season]);
leagueScoringConfigRepository.findBySeasonId.mockResolvedValue(scoringConfig);
gameRepository.findById.mockResolvedValue(game);
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 GetLeagueScoringConfigResult;
expect(presented?.league).toEqual(league);
expect(presented?.season).toEqual(season);
expect(presented?.scoringConfig).toEqual(scoringConfig);
expect(presented?.game).toEqual(game);
expect(presented?.preset).toBeUndefined();
});
it('should return error if league not found', async () => {
leagueRepository.findById.mockResolvedValue(null);
const result = await useCase.execute({ leagueId: 'league-1' });
const result = await useCase.execute({ leagueId: 'non-existent' });
expect(result.isErr()).toBe(true);
const err = result.unwrapErr() as ApplicationErrorCode<
GetLeagueScoringConfigErrorCode,
{ message: string }
>;
expect(err.code).toBe('LEAGUE_NOT_FOUND');
expect(err.details.message).toBe('League not found');
expect(output.present).not.toHaveBeenCalled();
});
it('should return error if no seasons', async () => {
leagueRepository.findById.mockResolvedValue({ id: 'league-1' });
seasonRepository.findByLeagueId.mockResolvedValue([]);
const result = await useCase.execute({ leagueId: 'league-1' });
expect(result.isErr()).toBe(true);
const err = result.unwrapErr() as ApplicationErrorCode<
GetLeagueScoringConfigErrorCode,
{ message: string }
>;
expect(err.code).toBe('NO_SEASONS');
expect(err.details.message).toBe('No seasons found for league');
expect(output.present).not.toHaveBeenCalled();
});
it('should return error if no seasons (null)', async () => {
leagueRepository.findById.mockResolvedValue({ id: 'league-1' });
seasonRepository.findByLeagueId.mockResolvedValue(null);
const result = await useCase.execute({ leagueId: 'league-1' });
expect(result.isErr()).toBe(true);
const err = result.unwrapErr() as ApplicationErrorCode<
GetLeagueScoringConfigErrorCode,
{ message: string }
>;
expect(err.code).toBe('NO_SEASONS');
expect(err.details.message).toBe('No seasons found for league');
expect(output.present).not.toHaveBeenCalled();
});
it('should return error if no scoring config', async () => {
leagueRepository.findById.mockResolvedValue({ id: 'league-1' });
seasonRepository.findByLeagueId.mockResolvedValue([
{ id: 'season-1', status: 'active', gameId: 'game-1' },
]);
leagueScoringConfigRepository.findBySeasonId.mockResolvedValue(null);
const result = await useCase.execute({ leagueId: 'league-1' });
expect(result.isErr()).toBe(true);
const err = result.unwrapErr() as ApplicationErrorCode<
GetLeagueScoringConfigErrorCode,
{ message: string }
>;
expect(err.code).toBe('NO_SCORING_CONFIG');
expect(err.details.message).toBe('Scoring configuration not found');
expect(output.present).not.toHaveBeenCalled();
});
it('should return error if game not found', async () => {
leagueRepository.findById.mockResolvedValue({ id: 'league-1' });
seasonRepository.findByLeagueId.mockResolvedValue([
{ id: 'season-1', status: 'active', gameId: 'game-1' },
]);
leagueScoringConfigRepository.findBySeasonId.mockResolvedValue({
scoringPresetId: undefined,
championships: [],
expect(result.value).toEqual({
code: 'LEAGUE_NOT_FOUND',
details: { message: 'League not found' },
});
gameRepository.findById.mockResolvedValue(null);
const result = await useCase.execute({ leagueId: 'league-1' });
expect(result.isErr()).toBe(true);
const err = result.unwrapErr() as ApplicationErrorCode<
GetLeagueScoringConfigErrorCode,
{ message: string }
>;
expect(err.code).toBe('GAME_NOT_FOUND');
expect(err.details.message).toBe('Game not found for season');
expect(output.present).not.toHaveBeenCalled();
});
it('should wrap repository errors', async () => {
leagueRepository.findById.mockRejectedValue(new Error('db down'));
it('should return NO_SEASONS when no seasons exist for league', async () => {
const mockLeague = { id: 'league-1', name: 'Test League' } as League;
mockLeagueRepository.findById.mockResolvedValue(mockLeague);
mockSeasonRepository.findByLeagueId.mockResolvedValue([]);
const result = await useCase.execute({ leagueId: 'league-1' });
expect(result.isErr()).toBe(true);
const err = result.unwrapErr() as ApplicationErrorCode<
GetLeagueScoringConfigErrorCode,
{ message: string }
>;
expect(err.code).toBe('REPOSITORY_ERROR');
expect(err.details.message).toBe('db down');
expect(output.present).not.toHaveBeenCalled();
expect(result.value).toEqual({
code: 'NO_SEASONS',
details: { message: 'No seasons found for league' },
});
});
it('should return NO_ACTIVE_SEASON when no active season exists', async () => {
const mockLeague = { id: 'league-1', name: 'Test League' } as League;
const mockSeason = {
id: 'season-1',
gameId: 'game-1',
status: { toString: () => 'inactive' }
} as unknown as Season;
mockLeagueRepository.findById.mockResolvedValue(mockLeague);
mockSeasonRepository.findByLeagueId.mockResolvedValue([mockSeason]);
const result = await useCase.execute({ leagueId: 'league-1' });
expect(result.isErr()).toBe(true);
expect(result.value).toEqual({
code: 'NO_ACTIVE_SEASON',
details: { message: 'No active season found for league' },
});
});
it('should return NO_SCORING_CONFIG when scoring config not found', async () => {
const mockLeague = { id: 'league-1', name: 'Test League' } as League;
const mockSeason = {
id: 'season-1',
gameId: 'game-1',
status: { toString: () => 'active' }
} as unknown as Season;
mockLeagueRepository.findById.mockResolvedValue(mockLeague);
mockSeasonRepository.findByLeagueId.mockResolvedValue([mockSeason]);
mockLeagueScoringConfigRepository.findBySeasonId.mockResolvedValue(null);
const result = await useCase.execute({ leagueId: 'league-1' });
expect(result.isErr()).toBe(true);
expect(result.value).toEqual({
code: 'NO_SCORING_CONFIG',
details: { message: 'Scoring configuration not found' },
});
});
it('should return GAME_NOT_FOUND when game does not exist', async () => {
const mockLeague = { id: 'league-1', name: 'Test League' } as League;
const mockSeason = {
id: 'season-1',
gameId: 'game-1',
status: { toString: () => 'active' }
} as unknown as Season;
const mockScoringConfig = {
id: 'config-1',
gameId: 'game-1',
scoringPresetId: 'preset-1'
} as unknown as LeagueScoringConfig;
mockLeagueRepository.findById.mockResolvedValue(mockLeague);
mockSeasonRepository.findByLeagueId.mockResolvedValue([mockSeason]);
mockLeagueScoringConfigRepository.findBySeasonId.mockResolvedValue(mockScoringConfig);
mockGameRepository.findById.mockResolvedValue(null);
const result = await useCase.execute({ leagueId: 'league-1' });
expect(result.isErr()).toBe(true);
expect(result.value).toEqual({
code: 'GAME_NOT_FOUND',
details: { message: 'Game not found for season' },
});
});
it('should handle preset without presetId', async () => {
const mockLeague = { id: 'league-1', name: 'Test League' } as League;
const mockSeason = {
id: 'season-1',
gameId: 'game-1',
status: { toString: () => 'active' }
} as unknown as Season;
const mockScoringConfig = {
id: 'config-1',
gameId: 'game-1',
scoringPresetId: null
} as unknown as LeagueScoringConfig;
const mockGame = { id: 'game-1', name: 'Test Game' } as Game;
mockLeagueRepository.findById.mockResolvedValue(mockLeague);
mockSeasonRepository.findByLeagueId.mockResolvedValue([mockSeason]);
mockLeagueScoringConfigRepository.findBySeasonId.mockResolvedValue(mockScoringConfig);
mockGameRepository.findById.mockResolvedValue(mockGame);
const result = await useCase.execute({ leagueId: 'league-1' });
expect(result.isOk()).toBe(true);
const value = result.value as any;
expect(value.preset).toBeUndefined();
});
it('should return REPOSITORY_ERROR on exception', async () => {
mockLeagueRepository.findById.mockRejectedValue(new Error('Database error'));
const result = await useCase.execute({ leagueId: 'league-1' });
expect(result.isErr()).toBe(true);
expect(result.value).toEqual({
code: 'REPOSITORY_ERROR',
details: { message: 'Database error' },
});
});
});