refactor racing use cases

This commit is contained in:
2025-12-21 00:43:42 +01:00
parent e9d6f90bb2
commit c12656d671
308 changed files with 14401 additions and 7419 deletions

View File

@@ -1,11 +1,16 @@
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import { GetAllLeaguesWithCapacityAndScoringUseCase } from './GetAllLeaguesWithCapacityAndScoringUseCase';
import {
GetAllLeaguesWithCapacityAndScoringUseCase,
type GetAllLeaguesWithCapacityAndScoringInput,
type GetAllLeaguesWithCapacityAndScoringResult,
type LeagueCapacityAndScoringSummary,
} from './GetAllLeaguesWithCapacityAndScoringUseCase';
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
import type { ILeagueScoringConfigRepository } from '../../domain/repositories/ILeagueScoringConfigRepository';
import type { IGameRepository } from '../../domain/repositories/IGameRepository';
import type { LeagueScoringPresetProvider } from '../ports/LeagueScoringPresetProvider';
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
describe('GetAllLeaguesWithCapacityAndScoringUseCase', () => {
let mockLeagueRepo: { findAll: Mock };
@@ -13,7 +18,7 @@ describe('GetAllLeaguesWithCapacityAndScoringUseCase', () => {
let mockSeasonRepo: { findByLeagueId: Mock };
let mockScoringConfigRepo: { findBySeasonId: Mock };
let mockGameRepo: { findById: Mock };
let mockPresetProvider: { getPresetById: Mock };
let output: UseCaseOutputPort<GetAllLeaguesWithCapacityAndScoringResult> & { present: Mock };
beforeEach(() => {
mockLeagueRepo = { findAll: vi.fn() };
@@ -21,7 +26,7 @@ describe('GetAllLeaguesWithCapacityAndScoringUseCase', () => {
mockSeasonRepo = { findByLeagueId: vi.fn() };
mockScoringConfigRepo = { findBySeasonId: vi.fn() };
mockGameRepo = { findById: vi.fn() };
mockPresetProvider = { getPresetById: vi.fn() };
output = { present: vi.fn() } as unknown as typeof output;
});
it('should return enriched leagues with capacity and scoring', async () => {
@@ -31,10 +36,11 @@ describe('GetAllLeaguesWithCapacityAndScoringUseCase', () => {
mockSeasonRepo as unknown as ISeasonRepository,
mockScoringConfigRepo as unknown as ILeagueScoringConfigRepository,
mockGameRepo as unknown as IGameRepository,
mockPresetProvider as unknown as LeagueScoringPresetProvider,
{ getPresetById: vi.fn().mockReturnValue({ id: 'preset1', name: 'Default' }) },
output,
);
const league = { id: 'league1', name: 'Test League' };
const league = { id: 'league1', name: 'Test League', settings: { maxDrivers: 30 } };
const members = [
{ status: 'active', role: 'member' },
{ status: 'active', role: 'owner' },
@@ -42,29 +48,33 @@ describe('GetAllLeaguesWithCapacityAndScoringUseCase', () => {
const season = { id: 'season1', status: 'active', gameId: 'game1' };
const scoringConfig = { scoringPresetId: 'preset1' };
const game = { id: 'game1', name: 'iRacing' };
const preset = { id: 'preset1', name: 'Default' };
mockLeagueRepo.findAll.mockResolvedValue([league]);
mockMembershipRepo.getLeagueMembers.mockResolvedValue(members);
mockSeasonRepo.findByLeagueId.mockResolvedValue([season]);
mockScoringConfigRepo.findBySeasonId.mockResolvedValue(scoringConfig);
mockGameRepo.findById.mockResolvedValue(game);
mockPresetProvider.getPresetById.mockReturnValue(preset);
const result = await useCase.execute();
const result = await useCase.execute({} as GetAllLeaguesWithCapacityAndScoringInput);
expect(result.isOk()).toBe(true);
expect(result.value).toEqual({
leagues: [
{
league,
usedDriverSlots: 2,
season,
scoringConfig,
game,
preset,
},
],
});
expect(result.unwrap()).toBeUndefined();
expect(output.present).toHaveBeenCalledTimes(1);
const presented =
output.present.mock.calls[0][0] as GetAllLeaguesWithCapacityAndScoringResult;
expect(presented.leagues).toHaveLength(1);
const [summary] = presented.leagues as LeagueCapacityAndScoringSummary[];
expect(summary.league).toEqual(league);
expect(summary.currentDrivers).toBe(2);
expect(summary.maxDrivers).toBe(30);
expect(summary.season).toEqual(season);
expect(summary.scoringConfig).toEqual(scoringConfig);
expect(summary.game).toEqual(game);
expect(summary.preset).toEqual({ id: 'preset1', name: 'Default' });
});
});