345 lines
9.0 KiB
TypeScript
345 lines
9.0 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { StewardingMutation } from './StewardingMutation';
|
|
import { LeagueService } from '@/lib/services/leagues/LeagueService';
|
|
|
|
// Mock dependencies
|
|
vi.mock('@/lib/services/leagues/LeagueService', () => {
|
|
return {
|
|
LeagueService: vi.fn(),
|
|
};
|
|
});
|
|
|
|
vi.mock('@/lib/gateways/api/leagues/LeaguesApiClient', () => {
|
|
return {
|
|
LeaguesApiClient: vi.fn(),
|
|
};
|
|
});
|
|
|
|
vi.mock('@/lib/infrastructure/logging/ConsoleErrorReporter', () => {
|
|
return {
|
|
ConsoleErrorReporter: vi.fn(),
|
|
};
|
|
});
|
|
|
|
vi.mock('@/lib/infrastructure/logging/ConsoleLogger', () => {
|
|
return {
|
|
ConsoleLogger: vi.fn(),
|
|
};
|
|
});
|
|
|
|
describe('StewardingMutation', () => {
|
|
let mutation: StewardingMutation;
|
|
let mockServiceInstance: any;
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
mutation = new StewardingMutation();
|
|
mockServiceInstance = {
|
|
// No actual service methods since these are TODO implementations
|
|
};
|
|
// Use mockImplementation to return the instance
|
|
(LeagueService as any).mockImplementation(function() {
|
|
return mockServiceInstance;
|
|
});
|
|
});
|
|
|
|
describe('applyPenalty', () => {
|
|
describe('happy paths', () => {
|
|
it('should successfully apply penalty with valid input', async () => {
|
|
// Arrange
|
|
const input = {
|
|
protestId: 'protest-123',
|
|
penaltyType: 'time_penalty',
|
|
penaltyValue: 30,
|
|
stewardNotes: 'Test notes',
|
|
raceId: 'race-456',
|
|
accusedDriverId: 'driver-789',
|
|
reason: 'Test reason',
|
|
};
|
|
|
|
// Act
|
|
const result = await mutation.applyPenalty(input);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe('failure modes', () => {
|
|
it('should handle service failure during penalty application', async () => {
|
|
// Arrange
|
|
const input = {
|
|
protestId: 'protest-123',
|
|
penaltyType: 'time_penalty',
|
|
penaltyValue: 30,
|
|
stewardNotes: 'Test notes',
|
|
raceId: 'race-456',
|
|
accusedDriverId: 'driver-789',
|
|
reason: 'Test reason',
|
|
};
|
|
// const serviceError = new Error('Service error');
|
|
|
|
// Act
|
|
const result = await mutation.applyPenalty(input);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe('input validation', () => {
|
|
it('should accept valid input', async () => {
|
|
// Arrange
|
|
const input = {
|
|
protestId: 'protest-123',
|
|
penaltyType: 'time_penalty',
|
|
penaltyValue: 30,
|
|
stewardNotes: 'Test notes',
|
|
raceId: 'race-456',
|
|
accusedDriverId: 'driver-789',
|
|
reason: 'Test reason',
|
|
};
|
|
|
|
// Act
|
|
const result = await mutation.applyPenalty(input);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
});
|
|
|
|
it('should handle empty steward notes gracefully', async () => {
|
|
// Arrange
|
|
const input = {
|
|
protestId: 'protest-123',
|
|
penaltyType: 'time_penalty',
|
|
penaltyValue: 30,
|
|
stewardNotes: '',
|
|
raceId: 'race-456',
|
|
accusedDriverId: 'driver-789',
|
|
reason: 'Test reason',
|
|
};
|
|
|
|
// Act
|
|
const result = await mutation.applyPenalty(input);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('requestDefense', () => {
|
|
describe('happy paths', () => {
|
|
it('should successfully request defense with valid input', async () => {
|
|
// Arrange
|
|
const input = {
|
|
protestId: 'protest-123',
|
|
stewardId: 'steward-456',
|
|
};
|
|
|
|
// Act
|
|
const result = await mutation.requestDefense(input);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe('failure modes', () => {
|
|
it('should handle service failure during defense request', async () => {
|
|
// Arrange
|
|
const input = {
|
|
protestId: 'protest-123',
|
|
stewardId: 'steward-456',
|
|
};
|
|
// const serviceError = new Error('Service error');
|
|
|
|
// Act
|
|
const result = await mutation.requestDefense(input);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe('input validation', () => {
|
|
it('should accept valid input', async () => {
|
|
// Arrange
|
|
const input = {
|
|
protestId: 'protest-123',
|
|
stewardId: 'steward-456',
|
|
};
|
|
|
|
// Act
|
|
const result = await mutation.requestDefense(input);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('quickPenalty', () => {
|
|
describe('happy paths', () => {
|
|
it('should successfully apply quick penalty with valid input', async () => {
|
|
// Arrange
|
|
const input = {
|
|
leagueId: 'league-123',
|
|
driverId: 'driver-456',
|
|
raceId: 'race-789',
|
|
penaltyType: 'time_penalty',
|
|
penaltyValue: 30,
|
|
reason: 'Test reason',
|
|
adminId: 'admin-999',
|
|
};
|
|
|
|
// Act
|
|
const result = await mutation.quickPenalty(input);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe('failure modes', () => {
|
|
it('should handle service failure during quick penalty', async () => {
|
|
// Arrange
|
|
const input = {
|
|
leagueId: 'league-123',
|
|
driverId: 'driver-456',
|
|
raceId: 'race-789',
|
|
penaltyType: 'time_penalty',
|
|
penaltyValue: 30,
|
|
reason: 'Test reason',
|
|
adminId: 'admin-999',
|
|
};
|
|
// const serviceError = new Error('Service error');
|
|
|
|
// Act
|
|
const result = await mutation.quickPenalty(input);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe('input validation', () => {
|
|
it('should accept valid input', async () => {
|
|
// Arrange
|
|
const input = {
|
|
leagueId: 'league-123',
|
|
driverId: 'driver-456',
|
|
raceId: 'race-789',
|
|
penaltyType: 'time_penalty',
|
|
penaltyValue: 30,
|
|
reason: 'Test reason',
|
|
adminId: 'admin-999',
|
|
};
|
|
|
|
// Act
|
|
const result = await mutation.quickPenalty(input);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
});
|
|
|
|
it('should handle empty reason gracefully', async () => {
|
|
// Arrange
|
|
const input = {
|
|
leagueId: 'league-123',
|
|
driverId: 'driver-456',
|
|
raceId: 'race-789',
|
|
penaltyType: 'time_penalty',
|
|
penaltyValue: 30,
|
|
reason: '',
|
|
adminId: 'admin-999',
|
|
};
|
|
|
|
// Act
|
|
const result = await mutation.quickPenalty(input);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('service instantiation', () => {
|
|
it('should create LeagueService instance', () => {
|
|
// Arrange & Act
|
|
const mutation = new StewardingMutation();
|
|
|
|
// Assert
|
|
expect(mutation).toBeInstanceOf(StewardingMutation);
|
|
});
|
|
});
|
|
|
|
describe('result shape', () => {
|
|
it('should return void on successful penalty application', async () => {
|
|
// Arrange
|
|
const input = {
|
|
protestId: 'protest-123',
|
|
penaltyType: 'time_penalty',
|
|
penaltyValue: 30,
|
|
stewardNotes: 'Test notes',
|
|
raceId: 'race-456',
|
|
accusedDriverId: 'driver-789',
|
|
reason: 'Test reason',
|
|
};
|
|
|
|
// Act
|
|
const result = await mutation.applyPenalty(input);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
});
|
|
|
|
it('should return void on successful defense request', async () => {
|
|
// Arrange
|
|
const input = {
|
|
protestId: 'protest-123',
|
|
stewardId: 'steward-456',
|
|
};
|
|
|
|
// Act
|
|
const result = await mutation.requestDefense(input);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
});
|
|
|
|
it('should return void on successful quick penalty', async () => {
|
|
// Arrange
|
|
const input = {
|
|
leagueId: 'league-123',
|
|
driverId: 'driver-456',
|
|
raceId: 'race-789',
|
|
penaltyType: 'time_penalty',
|
|
penaltyValue: 30,
|
|
reason: 'Test reason',
|
|
adminId: 'admin-999',
|
|
};
|
|
|
|
// Act
|
|
const result = await mutation.quickPenalty(input);
|
|
|
|
// Assert
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
});
|
|
});
|
|
});
|