import { describe, it, expect } from 'vitest'; import { InMemorySeasonRepository, } from '@core/racing/infrastructure/repositories/InMemoryScoringRepositories'; import { Season } from '@core/racing/domain/entities/Season'; import type { ILeagueRepository } from '@core/racing/domain/repositories/ILeagueRepository'; import { ManageSeasonLifecycleUseCase, type ManageSeasonLifecycleCommand, } from '@core/racing/application/use-cases/ManageSeasonLifecycleUseCase'; import type { Logger } from '@core/shared/application'; const logger: Logger = { debug: () => {}, info: () => {}, warn: () => {}, error: () => {}, }; function createFakeLeagueRepository(seed: Array<{ id: string }>): ILeagueRepository { return { findById: async (id: string) => seed.find((l) => l.id === id) ?? null, findAll: async () => seed, create: async (league: any) => league, update: async (league: any) => league, } as unknown as ILeagueRepository; } describe('ManageSeasonLifecycleUseCase', () => { function setupLifecycleTest() { const leagueRepo = createFakeLeagueRepository([{ id: 'league-1' }]); const seasonRepo = new InMemorySeasonRepository(logger); const season = Season.create({ id: 'season-1', leagueId: 'league-1', gameId: 'iracing', name: 'Lifecycle Season', status: 'planned', }); seasonRepo.seed(season); const useCase = new ManageSeasonLifecycleUseCase(leagueRepo, seasonRepo); return { leagueRepo, seasonRepo, useCase, season }; } it('applies activate → complete → archive transitions and persists state', async () => { const { useCase, seasonRepo, season } = setupLifecycleTest(); const activateCommand: ManageSeasonLifecycleCommand = { leagueId: 'league-1', seasonId: season.id, transition: 'activate', }; const activated = await useCase.execute(activateCommand); expect(activated.isOk()).toBe(true); expect(activated.value.status).toBe('active'); const completeCommand: ManageSeasonLifecycleCommand = { leagueId: 'league-1', seasonId: season.id, transition: 'complete', }; const completed = await useCase.execute(completeCommand); expect(completed.isOk()).toBe(true); expect(completed.value.status).toBe('completed'); const archiveCommand: ManageSeasonLifecycleCommand = { leagueId: 'league-1', seasonId: season.id, transition: 'archive', }; const archived = await useCase.execute(archiveCommand); expect(archived.isOk()).toBe(true); expect(archived.value.status).toBe('archived'); const persisted = await seasonRepo.findById(season.id); expect(persisted!.status).toBe('archived'); }); it('propagates domain invariant errors for invalid transitions', async () => { const { useCase, seasonRepo, season } = setupLifecycleTest(); const completeCommand: ManageSeasonLifecycleCommand = { leagueId: 'league-1', seasonId: season.id, transition: 'complete', }; const result = await useCase.execute(completeCommand); expect(result.isErr()).toBe(true); expect(result.error.code).toBe('INVALID_TRANSITION'); const persisted = await seasonRepo.findById(season.id); expect(persisted!.status).toBe('planned'); }); });