import { describe, it, expect, beforeAll, beforeEach } from 'vitest'; import { RacesTestContext } from '../RacesTestContext'; import { GetAllRacesUseCase } from '../../../../core/racing/application/use-cases/GetAllRacesUseCase'; import { Race } from '../../../../core/racing/domain/entities/Race'; import { League } from '../../../../core/racing/domain/entities/League'; describe('GetAllRacesUseCase', () => { let context: RacesTestContext; let getAllRacesUseCase: GetAllRacesUseCase; beforeAll(() => { context = RacesTestContext.create(); getAllRacesUseCase = new GetAllRacesUseCase( context.raceRepository, context.leagueRepository, context.logger ); }); beforeEach(async () => { await context.clear(); }); it('should retrieve comprehensive list of all races', async () => { // Given: Multiple races exist const leagueId = 'l1'; const league = League.create({ id: leagueId, name: 'Pro League', description: 'Desc', ownerId: 'o1' }); await context.leagueRepository.create(league); const race1 = Race.create({ id: 'r1', leagueId, scheduledAt: new Date(Date.now() + 86400000), track: 'Spa', car: 'GT3', status: 'scheduled' }); const race2 = Race.create({ id: 'r2', leagueId, scheduledAt: new Date(Date.now() - 86400000), track: 'Monza', car: 'GT3', status: 'completed' }); await context.raceRepository.create(race1); await context.raceRepository.create(race2); // When: GetAllRacesUseCase.execute() is called const result = await getAllRacesUseCase.execute({}); // Then: The result should contain all races and leagues expect(result.isOk()).toBe(true); const data = result.unwrap(); expect(data.races).toHaveLength(2); expect(data.leagues).toHaveLength(1); expect(data.totalCount).toBe(2); }); it('should return empty list when no races exist', async () => { // When: GetAllRacesUseCase.execute() is called const result = await getAllRacesUseCase.execute({}); // Then: The result should be empty expect(result.isOk()).toBe(true); expect(result.unwrap().races).toHaveLength(0); expect(result.unwrap().totalCount).toBe(0); }); it('should retrieve upcoming and recent races (main page logic)', async () => { // Given: Upcoming and completed races exist const leagueId = 'l1'; const league = League.create({ id: leagueId, name: 'Pro League', description: 'Desc', ownerId: 'o1' }); await context.leagueRepository.create(league); const upcomingRace = Race.create({ id: 'r1', leagueId, scheduledAt: new Date(Date.now() + 86400000), track: 'Spa', car: 'GT3', status: 'scheduled' }); const completedRace = Race.create({ id: 'r2', leagueId, scheduledAt: new Date(Date.now() - 86400000), track: 'Monza', car: 'GT3', status: 'completed' }); await context.raceRepository.create(upcomingRace); await context.raceRepository.create(completedRace); // When: GetAllRacesUseCase.execute() is called const result = await getAllRacesUseCase.execute({}); // Then: The result should contain both races expect(result.isOk()).toBe(true); const data = result.unwrap(); expect(data.races).toHaveLength(2); expect(data.races.some(r => r.status.isScheduled())).toBe(true); expect(data.races.some(r => r.status.isCompleted())).toBe(true); }); });