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

158 lines
6.0 KiB
TypeScript

import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import { FileProtestUseCase, type FileProtestInput, type FileProtestResult, type FileProtestErrorCode } from './FileProtestUseCase';
import type { IProtestRepository } from '../../domain/repositories/IProtestRepository';
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { Result } from '@core/shared/application/Result';
describe('FileProtestUseCase', () => {
let mockProtestRepo: {
create: Mock;
};
let mockRaceRepo: {
findById: Mock;
};
let mockLeagueMembershipRepo: {
getLeagueMembers: Mock;
};
let output: UseCaseOutputPort<FileProtestResult> & { present: Mock };
beforeEach(() => {
mockProtestRepo = {
create: vi.fn(),
};
mockRaceRepo = {
findById: vi.fn(),
};
mockLeagueMembershipRepo = {
getLeagueMembers: vi.fn(),
};
output = {
present: vi.fn(),
} as unknown as UseCaseOutputPort<FileProtestResult> & { present: Mock };
});
it('should return error when race does not exist', async () => {
const useCase = new FileProtestUseCase(
mockProtestRepo as unknown as IProtestRepository,
mockRaceRepo as unknown as IRaceRepository,
mockLeagueMembershipRepo as unknown as ILeagueMembershipRepository,
output,
);
mockRaceRepo.findById.mockResolvedValue(null);
const result = await useCase.execute({
raceId: 'nonexistent',
protestingDriverId: 'driver1',
accusedDriverId: 'driver2',
incident: { lap: 5, description: 'Collision' },
} as FileProtestInput);
expect(result.isErr()).toBe(true);
const err = result.unwrapErr() as ApplicationErrorCode<FileProtestErrorCode, { message: string }>;
expect(err.code).toBe('RACE_NOT_FOUND');
expect(err.details?.message).toBe('Race not found');
expect(output.present).not.toHaveBeenCalled();
});
it('should return error when protesting against self', async () => {
const useCase = new FileProtestUseCase(
mockProtestRepo as unknown as IProtestRepository,
mockRaceRepo as unknown as IRaceRepository,
mockLeagueMembershipRepo as unknown as ILeagueMembershipRepository,
output,
);
mockRaceRepo.findById.mockResolvedValue({ id: 'race1', leagueId: 'league1' });
const result = await useCase.execute({
raceId: 'race1',
protestingDriverId: 'driver1',
accusedDriverId: 'driver1',
incident: { lap: 5, description: 'Collision' },
} as FileProtestInput);
expect(result.isErr()).toBe(true);
const err = result.unwrapErr() as ApplicationErrorCode<FileProtestErrorCode, { message: string }>;
expect(err.code).toBe('SELF_PROTEST');
expect(err.details?.message).toBe('Cannot file a protest against yourself');
expect(output.present).not.toHaveBeenCalled();
});
it('should return error when protesting driver is not an active member', async () => {
const useCase = new FileProtestUseCase(
mockProtestRepo as unknown as IProtestRepository,
mockRaceRepo as unknown as IRaceRepository,
mockLeagueMembershipRepo as unknown as ILeagueMembershipRepository,
output,
);
mockRaceRepo.findById.mockResolvedValue({ id: 'race1', leagueId: 'league1' });
mockLeagueMembershipRepo.getLeagueMembers.mockResolvedValue([
{ driverId: 'driver2', status: 'active' },
]);
const result = await useCase.execute({
raceId: 'race1',
protestingDriverId: 'driver1',
accusedDriverId: 'driver2',
incident: { lap: 5, description: 'Collision' },
} as FileProtestInput);
expect(result.isErr()).toBe(true);
const err = result.unwrapErr() as ApplicationErrorCode<FileProtestErrorCode, { message: string }>;
expect(err.code).toBe('NOT_MEMBER');
expect(err.details?.message).toBe('Protesting driver is not an active member of this league');
expect(output.present).not.toHaveBeenCalled();
});
it('should create protest and return protestId on success', async () => {
const useCase = new FileProtestUseCase(
mockProtestRepo as unknown as IProtestRepository,
mockRaceRepo as unknown as IRaceRepository,
mockLeagueMembershipRepo as unknown as ILeagueMembershipRepository,
output,
);
mockRaceRepo.findById.mockResolvedValue({ id: 'race1', leagueId: 'league1' });
mockLeagueMembershipRepo.getLeagueMembers.mockResolvedValue([
{ driverId: 'driver1', status: 'active' },
]);
mockProtestRepo.create.mockResolvedValue(undefined);
const result = await useCase.execute({
raceId: 'race1',
protestingDriverId: 'driver1',
accusedDriverId: 'driver2',
incident: { lap: 5, description: 'Collision' },
comment: 'Test comment',
proofVideoUrl: 'http://example.com/video',
} as FileProtestInput);
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeUndefined();
expect(mockProtestRepo.create).toHaveBeenCalledWith(
expect.objectContaining({
raceId: 'race1',
protestingDriverId: 'driver1',
accusedDriverId: 'driver2',
incident: { lap: 5, description: 'Collision' },
comment: 'Test comment',
proofVideoUrl: 'http://example.com/video',
status: 'pending',
})
);
expect(output.present).toHaveBeenCalledTimes(1);
const presented = (output.present as unknown as Mock).mock.calls[0][0] as FileProtestResult;
expect(presented.protest.raceId).toBe('race1');
expect(presented.protest.protestingDriverId).toBe('driver1');
expect(presented.protest.accusedDriverId).toBe('driver2');
expect(presented.protest.incident).toEqual({ lap: 5, description: 'Collision', timeInRace: undefined });
expect(presented.protest.comment).toBe('Test comment');
expect(presented.protest.proofVideoUrl).toBe('http://example.com/video');
});
});