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,25 +1,34 @@
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import { GetAllLeaguesWithCapacityUseCase } from './GetAllLeaguesWithCapacityUseCase';
import {
GetAllLeaguesWithCapacityUseCase,
type GetAllLeaguesWithCapacityInput,
type GetAllLeaguesWithCapacityResult,
type LeagueCapacitySummary,
} from './GetAllLeaguesWithCapacityUseCase';
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
describe('GetAllLeaguesWithCapacityUseCase', () => {
let mockLeagueRepo: { findAll: Mock };
let mockMembershipRepo: { getLeagueMembers: Mock };
let output: UseCaseOutputPort<GetAllLeaguesWithCapacityResult> & { present: Mock };
beforeEach(() => {
mockLeagueRepo = { findAll: vi.fn() };
mockMembershipRepo = { getLeagueMembers: vi.fn() };
output = { present: vi.fn() } as unknown as typeof output;
});
it('should return leagues with capacity information', async () => {
const useCase = new GetAllLeaguesWithCapacityUseCase(
mockLeagueRepo as unknown as ILeagueRepository,
mockMembershipRepo as unknown as ILeagueMembershipRepository,
output,
);
const league1 = { id: 'league1', name: 'Test League 1' };
const league2 = { id: 'league2', name: 'Test League 2' };
const league1 = { id: 'league1', name: 'Test League 1', settings: { maxDrivers: 10 } };
const league2 = { id: 'league2', name: 'Test League 2', settings: { maxDrivers: 20 } };
const members1 = [
{ status: 'active', role: 'member' },
{ status: 'active', role: 'owner' },
@@ -34,33 +43,43 @@ describe('GetAllLeaguesWithCapacityUseCase', () => {
.mockResolvedValueOnce(members1)
.mockResolvedValueOnce(members2);
const result = await useCase.execute();
const result = await useCase.execute({} as GetAllLeaguesWithCapacityInput);
expect(result.isOk()).toBe(true);
expect(result.value).toEqual({
leagues: [league1, league2],
memberCounts: new Map([
['league1', 2],
['league2', 1],
]),
});
expect(result.unwrap()).toBeUndefined();
expect(output.present).toHaveBeenCalledTimes(1);
const presented = output.present.mock.calls[0][0] as GetAllLeaguesWithCapacityResult;
expect(presented.leagues).toHaveLength(2);
const [first, second] = presented.leagues as LeagueCapacitySummary[];
expect(first.league).toEqual(league1);
expect(first.currentDrivers).toBe(2);
expect(first.maxDrivers).toBe(10);
expect(second.league).toEqual(league2);
expect(second.currentDrivers).toBe(1);
expect(second.maxDrivers).toBe(20);
});
it('should return empty result when no leagues', async () => {
const useCase = new GetAllLeaguesWithCapacityUseCase(
mockLeagueRepo as unknown as ILeagueRepository,
mockMembershipRepo as unknown as ILeagueMembershipRepository,
output,
);
mockLeagueRepo.findAll.mockResolvedValue([]);
mockMembershipRepo.getLeagueMembers.mockResolvedValue([]);
const result = await useCase.execute();
const result = await useCase.execute({} as GetAllLeaguesWithCapacityInput);
expect(result.isOk()).toBe(true);
expect(result.value).toEqual({
leagues: [],
memberCounts: new Map(),
});
expect(result.unwrap()).toBeUndefined();
expect(output.present).toHaveBeenCalledTimes(1);
const presented = output.present.mock.calls[0][0] as GetAllLeaguesWithCapacityResult;
expect(presented.leagues).toEqual([]);
});
});