Files
gridpilot.gg/core/racing/application/use-cases/GetAllLeaguesWithCapacityUseCase.test.ts
2025-12-23 14:43:49 +01:00

82 lines
3.0 KiB
TypeScript

import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
import { beforeEach, describe, expect, it, Mock, vi } from 'vitest';
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
import {
GetAllLeaguesWithCapacityUseCase,
type GetAllLeaguesWithCapacityInput,
type GetAllLeaguesWithCapacityResult,
} from './GetAllLeaguesWithCapacityUseCase';
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,
);
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' },
{ status: 'inactive', role: 'member' },
];
const members2 = [
{ status: 'active', role: 'admin' },
];
mockLeagueRepo.findAll.mockResolvedValue([league1, league2]);
mockMembershipRepo.getLeagueMembers
.mockResolvedValueOnce(members1)
.mockResolvedValueOnce(members2);
const result = await useCase.execute({} as GetAllLeaguesWithCapacityInput);
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeUndefined();
const resultValue = result.unwrap();
expect(resultValue).toBeDefined();
expect(resultValue?.leagues).toHaveLength(2);
const [first, second] = resultValue?.leagues ?? [];
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,
);
mockLeagueRepo.findAll.mockResolvedValue([]);
const result = await useCase.execute({} as GetAllLeaguesWithCapacityInput);
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeUndefined();
const resultValue = result.unwrap();
expect(resultValue).toBeDefined();
expect(resultValue?.leagues).toEqual([]);
});
});