Files
gridpilot.gg/core/racing/application/use-cases/CancelRaceUseCase.test.ts
2025-12-21 00:43:42 +01:00

120 lines
3.8 KiB
TypeScript

import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import { CancelRaceUseCase, type CancelRaceResult } from './CancelRaceUseCase';
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
import type { Logger } from '@core/shared/application';
import { Race } from '../../domain/entities/Race';
import { SessionType } from '../../domain/value-objects/SessionType';
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
describe('CancelRaceUseCase', () => {
let useCase: CancelRaceUseCase;
let raceRepository: {
findById: Mock;
update: Mock;
};
let logger: {
debug: Mock;
warn: Mock;
info: Mock;
error: Mock;
};
let output: UseCaseOutputPort<CancelRaceResult> & { present: Mock };
beforeEach(() => {
raceRepository = {
findById: vi.fn(),
update: vi.fn(),
};
logger = {
debug: vi.fn(),
warn: vi.fn(),
info: vi.fn(),
error: vi.fn(),
};
output = { present: vi.fn() } as unknown as UseCaseOutputPort<CancelRaceResult> & { present: Mock };
useCase = new CancelRaceUseCase(
raceRepository as unknown as IRaceRepository,
logger as unknown as Logger,
output,
);
});
it('should cancel race successfully', async () => {
const raceId = 'race-1';
const race = Race.create({
id: raceId,
leagueId: 'league-1',
scheduledAt: new Date(),
track: 'Track 1',
car: 'Car 1',
sessionType: SessionType.main(),
status: 'scheduled',
});
raceRepository.findById.mockResolvedValue(race);
const result = await useCase.execute({ raceId, cancelledById: 'admin-1' });
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeUndefined();
expect(raceRepository.findById).toHaveBeenCalledWith(raceId);
expect(raceRepository.update).toHaveBeenCalledWith(expect.objectContaining({ id: raceId, status: 'cancelled' }));
expect(output.present).toHaveBeenCalledTimes(1);
expect(output.present).toHaveBeenCalledWith({ race: expect.objectContaining({ id: raceId, status: 'cancelled' }) });
});
it('should return error if race not found', async () => {
const raceId = 'race-1';
raceRepository.findById.mockResolvedValue(null);
const result = await useCase.execute({ raceId, cancelledById: 'admin-1' });
expect(result.isErr()).toBe(true);
expect(result.unwrapErr().code).toBe('RACE_NOT_FOUND');
expect(output.present).not.toHaveBeenCalled();
});
it('should return domain error if race is already cancelled', async () => {
const raceId = 'race-1';
const race = Race.create({
id: raceId,
leagueId: 'league-1',
scheduledAt: new Date(),
track: 'Track 1',
car: 'Car 1',
sessionType: SessionType.main(),
status: 'cancelled',
});
raceRepository.findById.mockResolvedValue(race);
const result = await useCase.execute({ raceId, cancelledById: 'admin-1' });
expect(result.isErr()).toBe(true);
expect(result.unwrapErr().code).toBe('NOT_AUTHORIZED');
expect(result.unwrapErr().details?.message).toContain('already cancelled');
expect(output.present).not.toHaveBeenCalled();
});
it('should return domain error if race is completed', async () => {
const raceId = 'race-1';
const race = Race.create({
id: raceId,
leagueId: 'league-1',
scheduledAt: new Date(),
track: 'Track 1',
car: 'Car 1',
sessionType: SessionType.main(),
status: 'completed',
});
raceRepository.findById.mockResolvedValue(race);
const result = await useCase.execute({ raceId, cancelledById: 'admin-1' });
expect(result.isErr()).toBe(true);
expect(result.unwrapErr().code).toBe('NOT_AUTHORIZED');
expect(result.unwrapErr().details?.message).toContain('completed race');
expect(output.present).not.toHaveBeenCalled();
});
});