import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode'; import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest'; import { League } from '../../domain/entities/League'; import { Season } from '../../domain/entities/season/Season'; import type { LeagueRepository } from '../../domain/repositories/LeagueRepository'; import type { SeasonRepository } from '../../domain/repositories/SeasonRepository'; import { GetLeagueSeasonsUseCase, type GetLeagueSeasonsErrorCode, type GetLeagueSeasonsInput, } from './GetLeagueSeasonsUseCase'; describe('GetLeagueSeasonsUseCase', () => { let useCase: GetLeagueSeasonsUseCase; let seasonRepository: { findByLeagueId: Mock; }; let leagueRepository: { findById: Mock; exists: Mock; }; beforeEach(() => { seasonRepository = { findByLeagueId: vi.fn(), }; leagueRepository = { findById: vi.fn(), exists: vi.fn(), }; useCase = new GetLeagueSeasonsUseCase( leagueRepository as unknown as LeagueRepository, seasonRepository as unknown as SeasonRepository ); }); it('should return seasons with correct isParallelActive flags on success', async () => { const leagueId = 'league-1'; const league = League.create({ id: leagueId, name: 'Test League', description: 'A test league', ownerId: 'owner-1', }); const seasons = [ Season.create({ id: 'season-1', leagueId, gameId: 'game-1', name: 'Season 1', status: 'active', startDate: new Date('2023-01-01'), endDate: new Date('2023-12-31'), }), Season.create({ id: 'season-2', leagueId, gameId: 'game-1', name: 'Season 2', status: 'planned', }), ]; leagueRepository.exists.mockResolvedValue(true); leagueRepository.findById.mockResolvedValue(league); seasonRepository.findByLeagueId.mockResolvedValue(seasons); const result = await useCase.execute({ leagueId } satisfies GetLeagueSeasonsInput); expect(result.isOk()).toBe(true); const presented = result.unwrap(); expect(presented.seasons).toHaveLength(2); expect(presented.seasons[0]?.season).toBe(seasons[0]); expect(presented.seasons[0]?.isPrimary).toBe(true); expect(presented.seasons[0]?.isParallelActive).toBe(false); expect(presented.seasons[1]?.season).toBe(seasons[1]); expect(presented.seasons[1]?.isPrimary).toBe(false); expect(presented.seasons[1]?.isParallelActive).toBe(false); }); it('should set isParallelActive true for active seasons when multiple active', async () => { const leagueId = 'league-1'; const league = League.create({ id: leagueId, name: 'Test League', description: 'A test league', ownerId: 'owner-1', }); const seasons = [ Season.create({ id: 'season-1', leagueId, gameId: 'game-1', name: 'Season 1', status: 'active', }), Season.create({ id: 'season-2', leagueId, gameId: 'game-1', name: 'Season 2', status: 'active', }), ]; leagueRepository.exists.mockResolvedValue(true); leagueRepository.findById.mockResolvedValue(league); seasonRepository.findByLeagueId.mockResolvedValue(seasons); const result = await useCase.execute({ leagueId } satisfies GetLeagueSeasonsInput); expect(result.isOk()).toBe(true); const presented = result.unwrap(); expect(presented.seasons).toHaveLength(2); expect(presented.seasons[0]?.isParallelActive).toBe(true); expect(presented.seasons[1]?.isParallelActive).toBe(true); }); it('should return LEAGUE_NOT_FOUND error when league does not exist', async () => { const leagueId = 'missing-league'; leagueRepository.exists.mockResolvedValue(false); const result = await useCase.execute({ leagueId } satisfies GetLeagueSeasonsInput); expect(result.isErr()).toBe(true); const err = result.unwrapErr() as ApplicationErrorCode< GetLeagueSeasonsErrorCode, { message: string } >; expect(err.code).toBe('LEAGUE_NOT_FOUND'); expect(err.details.message).toBe('League not found'); }); it('should return REPOSITORY_ERROR when repository throws', async () => { const leagueId = 'league-1'; const errorMessage = 'DB error'; leagueRepository.exists.mockRejectedValue(new Error(errorMessage)); const result = await useCase.execute({ leagueId } satisfies GetLeagueSeasonsInput); expect(result.isErr()).toBe(true); const err = result.unwrapErr() as ApplicationErrorCode< GetLeagueSeasonsErrorCode, { message: string } >; expect(err.code).toBe('REPOSITORY_ERROR'); expect(err.details.message).toBe(errorMessage); }); });