224 lines
8.1 KiB
TypeScript
224 lines
8.1 KiB
TypeScript
import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
|
|
import type { Game } from '../../domain/entities/Game';
|
|
import type { League } from '../../domain/entities/League';
|
|
import type { LeagueScoringConfig } from '../../domain/entities/LeagueScoringConfig';
|
|
import type { Season } from '../../domain/entities/season/Season';
|
|
import type { GameRepository } from '../../domain/repositories/GameRepository';
|
|
import type { LeagueRepository } from '../../domain/repositories/LeagueRepository';
|
|
import type { LeagueScoringConfigRepository } from '../../domain/repositories/LeagueScoringConfigRepository';
|
|
import type { SeasonRepository } from '../../domain/repositories/SeasonRepository';
|
|
import type { LeagueScoringPreset } from '../../domain/types/LeagueScoringPreset';
|
|
import { GetLeagueScoringConfigUseCase } from './GetLeagueScoringConfigUseCase';
|
|
|
|
describe('GetLeagueScoringConfigUseCase', () => {
|
|
let useCase: GetLeagueScoringConfigUseCase;
|
|
let mockLeagueRepository: LeagueRepository;
|
|
let mockSeasonRepository: SeasonRepository;
|
|
let mockLeagueScoringConfigRepository: LeagueScoringConfigRepository;
|
|
let mockGameRepository: GameRepository;
|
|
let mockPresetProvider: { getPresetById: Mock };
|
|
|
|
beforeEach(() => {
|
|
mockLeagueRepository = {
|
|
findById: vi.fn(),
|
|
exists: vi.fn(),
|
|
save: vi.fn(),
|
|
findAll: vi.fn(),
|
|
} as unknown as LeagueRepository;
|
|
|
|
mockSeasonRepository = {
|
|
findByLeagueId: vi.fn(),
|
|
save: vi.fn(),
|
|
findById: vi.fn(),
|
|
} as unknown as SeasonRepository;
|
|
|
|
mockLeagueScoringConfigRepository = {
|
|
findBySeasonId: vi.fn(),
|
|
save: vi.fn(),
|
|
} as unknown as LeagueScoringConfigRepository;
|
|
|
|
mockGameRepository = {
|
|
findById: vi.fn(),
|
|
save: vi.fn(),
|
|
findAll: vi.fn(),
|
|
} as unknown as GameRepository;
|
|
|
|
mockPresetProvider = {
|
|
getPresetById: vi.fn(),
|
|
};
|
|
|
|
useCase = new GetLeagueScoringConfigUseCase(
|
|
mockLeagueRepository,
|
|
mockSeasonRepository,
|
|
mockLeagueScoringConfigRepository,
|
|
mockGameRepository,
|
|
mockPresetProvider as unknown as { getPresetById: (id: string) => LeagueScoringPreset | undefined },
|
|
);
|
|
});
|
|
|
|
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.unwrap();
|
|
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.unwrapErr()).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.unwrapErr()).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.unwrapErr()).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.unwrapErr()).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.unwrapErr()).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.unwrap();
|
|
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.unwrapErr()).toEqual({
|
|
code: 'REPOSITORY_ERROR',
|
|
details: { message: 'Database error' },
|
|
});
|
|
});
|
|
});
|