import { describe, it, expect, beforeEach, vi, Mock } from 'vitest'; import { GetLeagueScoringConfigUseCase } 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 { LeagueScoringPresetProvider } from '../ports/LeagueScoringPresetProvider'; 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; listPresets: Mock; createScoringConfigFromPreset: Mock }; beforeEach(() => { leagueRepository = { findById: vi.fn() }; seasonRepository = { findByLeagueId: vi.fn() }; leagueScoringConfigRepository = { findBySeasonId: vi.fn() }; gameRepository = { findById: vi.fn() }; presetProvider = { getPresetById: vi.fn(), listPresets: vi.fn(), createScoringConfigFromPreset: vi.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 as LeagueScoringPresetProvider, ); }); it('should return scoring config for active season', async () => { const leagueId = 'league-1'; const league = { id: 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' }; leagueRepository.findById.mockResolvedValue(league); seasonRepository.findByLeagueId.mockResolvedValue([season]); leagueScoringConfigRepository.findBySeasonId.mockResolvedValue(scoringConfig); gameRepository.findById.mockResolvedValue(game); presetProvider.getPresetById.mockReturnValue(preset); const result = await useCase.execute({ leagueId }); expect(result.isOk()).toBe(true); expect(result.unwrap()).toEqual({ leagueId, seasonId: 'season-1', gameId: 'game-1', gameName: 'Game 1', scoringPresetId: 'preset-1', preset, championships: [], }); }); it('should return scoring config for first season if no active', async () => { const leagueId = 'league-1'; const league = { id: leagueId }; const season = { id: 'season-1', status: 'inactive', gameId: 'game-1' }; const scoringConfig = { scoringPresetId: undefined, championships: [] }; const game = { id: 'game-1', name: 'Game 1' }; leagueRepository.findById.mockResolvedValue(league); seasonRepository.findByLeagueId.mockResolvedValue([season]); leagueScoringConfigRepository.findBySeasonId.mockResolvedValue(scoringConfig); gameRepository.findById.mockResolvedValue(game); const result = await useCase.execute({ leagueId }); expect(result.isOk()).toBe(true); expect(result.unwrap()).toEqual({ leagueId, seasonId: 'season-1', gameId: 'game-1', gameName: 'Game 1', championships: [], }); }); it('should return error if league not found', async () => { leagueRepository.findById.mockResolvedValue(null); const result = await useCase.execute({ leagueId: 'league-1' }); expect(result.isErr()).toBe(true); expect(result.error).toEqual({ code: 'LEAGUE_NOT_FOUND' }); }); 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); expect(result.error).toEqual({ code: 'NO_SEASONS' }); }); 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); expect(result.error).toEqual({ code: 'NO_SEASONS' }); }); 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); expect(result.error).toEqual({ code: 'NO_SCORING_CONFIG' }); }); 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: [] }); gameRepository.findById.mockResolvedValue(null); const result = await useCase.execute({ leagueId: 'league-1' }); expect(result.isErr()).toBe(true); expect(result.error).toEqual({ code: 'GAME_NOT_FOUND' }); }); });