import { describe, it, expect, beforeEach } from 'vitest'; import { LeaguesTestContext } from '../LeaguesTestContext'; import { LeagueCreateCommand } from '../../../../core/leagues/application/ports/LeagueCreateCommand'; import { InMemoryLeagueRepository } from '../../../../adapters/leagues/persistence/inmemory/InMemoryLeagueRepository'; import { CreateLeagueUseCase } from '../../../../core/leagues/application/use-cases/CreateLeagueUseCase'; describe('League Creation - Error Handling', () => { let context: LeaguesTestContext; beforeEach(() => { context = new LeaguesTestContext(); context.clear(); }); it('should throw error when driver ID is invalid', async () => { const command: LeagueCreateCommand = { name: 'Test League', visibility: 'public', ownerId: '', approvalRequired: false, lateJoinAllowed: false, bonusPointsEnabled: false, penaltiesEnabled: false, protestsEnabled: false, appealsEnabled: false, }; await expect(context.createLeagueUseCase.execute(command)).rejects.toThrow('Owner ID is required'); expect(context.eventPublisher.getLeagueCreatedEventCount()).toBe(0); }); it('should throw error when league name is empty', async () => { const command: LeagueCreateCommand = { name: '', visibility: 'public', ownerId: 'driver-123', approvalRequired: false, lateJoinAllowed: false, bonusPointsEnabled: false, penaltiesEnabled: false, protestsEnabled: false, appealsEnabled: false, }; await expect(context.createLeagueUseCase.execute(command)).rejects.toThrow(); expect(context.eventPublisher.getLeagueCreatedEventCount()).toBe(0); }); it('should throw error when repository throws error', async () => { const errorRepo = new InMemoryLeagueRepository(); errorRepo.create = async () => { throw new Error('Database error'); }; const errorUseCase = new CreateLeagueUseCase(errorRepo, context.eventPublisher); const command: LeagueCreateCommand = { name: 'Test League', visibility: 'public', ownerId: 'driver-123', approvalRequired: false, lateJoinAllowed: false, bonusPointsEnabled: false, penaltiesEnabled: false, protestsEnabled: false, appealsEnabled: false, }; await expect(errorUseCase.execute(command)).rejects.toThrow('Database error'); expect(context.eventPublisher.getLeagueCreatedEventCount()).toBe(0); }); });