Files
gridpilot.gg/core/racing/application/use-cases/GetLeagueScoringConfigUseCase.test.ts
2026-01-08 15:34:51 +01:00

223 lines
8.0 KiB
TypeScript

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 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(() => {
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(
mockLeagueRepository,
mockSeasonRepository,
mockLeagueScoringConfigRepository,
mockGameRepository,
mockPresetProvider,
);
});
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;
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({ leagueId: 'league-1' });
expect(result.isOk()).toBe(true);
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 LEAGUE_NOT_FOUND when league does not exist', async () => {
mockLeagueRepository.findById.mockResolvedValue(null);
const result = await useCase.execute({ leagueId: 'non-existent' });
expect(result.isErr()).toBe(true);
expect(result.value).toEqual({
code: 'LEAGUE_NOT_FOUND',
details: { message: 'League not found' },
});
});
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);
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' },
});
});
});