refactor racing use cases
This commit is contained in:
@@ -1,25 +1,84 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
|
||||
import {
|
||||
SendFinalResultsUseCase,
|
||||
type SendFinalResultsInput,
|
||||
type SendFinalResultsResult,
|
||||
type SendFinalResultsErrorCode,
|
||||
} from './SendFinalResultsUseCase';
|
||||
import type { NotificationService } from '../../../notifications/application/ports/NotificationService';
|
||||
import type { RaceEventStewardingClosedEvent } from '../../domain/events/RaceEventStewardingClosed';
|
||||
import type { IRaceEventRepository } from '../../domain/repositories/IRaceEventRepository';
|
||||
import type { IResultRepository } from '../../domain/repositories/IResultRepository';
|
||||
import { SendFinalResultsUseCase } from './SendFinalResultsUseCase';
|
||||
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
|
||||
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { Logger } from '@core/shared/application/Logger';
|
||||
|
||||
const unwrapError = (
|
||||
result: Result<void, ApplicationErrorCode<SendFinalResultsErrorCode, { message: string }>>,
|
||||
): ApplicationErrorCode<SendFinalResultsErrorCode, { message: string }> => {
|
||||
expect(result.isErr()).toBe(true);
|
||||
return result.unwrapErr();
|
||||
};
|
||||
|
||||
describe('SendFinalResultsUseCase', () => {
|
||||
it('sends final results notifications to all participating drivers', async () => {
|
||||
const mockNotificationService = {
|
||||
sendNotification: vi.fn(),
|
||||
} as unknown as NotificationService;
|
||||
let notificationService: { sendNotification: Mock };
|
||||
let raceEventRepository: { findById: Mock };
|
||||
let resultRepository: { findByRaceId: Mock };
|
||||
let leagueRepository: { findById: Mock };
|
||||
let membershipRepository: { getMembership: Mock };
|
||||
let logger: Logger;
|
||||
let output: UseCaseOutputPort<SendFinalResultsResult> & { present: Mock };
|
||||
let useCase: SendFinalResultsUseCase;
|
||||
|
||||
beforeEach(() => {
|
||||
notificationService = { sendNotification: vi.fn() };
|
||||
raceEventRepository = { findById: vi.fn() };
|
||||
resultRepository = { findByRaceId: vi.fn() };
|
||||
leagueRepository = { findById: vi.fn() };
|
||||
membershipRepository = { getMembership: vi.fn() };
|
||||
logger = {
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
};
|
||||
output = { present: vi.fn() } as unknown as UseCaseOutputPort<SendFinalResultsResult> & { present: Mock };
|
||||
|
||||
useCase = new SendFinalResultsUseCase(
|
||||
notificationService as unknown as NotificationService,
|
||||
raceEventRepository as unknown as IRaceEventRepository,
|
||||
resultRepository as unknown as IResultRepository,
|
||||
leagueRepository as unknown as ILeagueRepository,
|
||||
membershipRepository as unknown as ILeagueMembershipRepository,
|
||||
logger,
|
||||
output,
|
||||
);
|
||||
});
|
||||
|
||||
const createInput = (overrides: Partial<SendFinalResultsInput> = {}): SendFinalResultsInput => ({
|
||||
leagueId: 'league-1',
|
||||
raceId: 'race-1',
|
||||
triggeredById: 'user-1',
|
||||
...overrides,
|
||||
});
|
||||
|
||||
it('sends final results notifications to all participating drivers and presents result', async () => {
|
||||
const mockRaceEvent = {
|
||||
id: 'race-1',
|
||||
leagueId: 'league-1',
|
||||
name: 'Test Race',
|
||||
status: 'closed',
|
||||
getMainRaceSession: vi.fn().mockReturnValue({ id: 'session-1' }),
|
||||
};
|
||||
|
||||
const mockRaceEventRepository = {
|
||||
findById: vi.fn().mockResolvedValue(mockRaceEvent),
|
||||
} as unknown as IRaceEventRepository;
|
||||
const mockLeague = { id: 'league-1' };
|
||||
const mockMembership = { role: 'steward' };
|
||||
|
||||
leagueRepository.findById.mockResolvedValue(mockLeague);
|
||||
raceEventRepository.findById.mockResolvedValue(mockRaceEvent);
|
||||
membershipRepository.getMembership.mockResolvedValue(mockMembership);
|
||||
|
||||
const mockResults = [
|
||||
{
|
||||
@@ -36,155 +95,111 @@ describe('SendFinalResultsUseCase', () => {
|
||||
},
|
||||
];
|
||||
|
||||
const mockResultRepository = {
|
||||
findByRaceId: vi.fn().mockResolvedValue(mockResults),
|
||||
} as unknown as IResultRepository;
|
||||
resultRepository.findByRaceId.mockResolvedValue(mockResults);
|
||||
|
||||
const useCase = new SendFinalResultsUseCase(
|
||||
mockNotificationService,
|
||||
mockRaceEventRepository,
|
||||
mockResultRepository,
|
||||
);
|
||||
const input = createInput();
|
||||
|
||||
const event: RaceEventStewardingClosedEvent = {
|
||||
eventType: 'RaceEventStewardingClosed',
|
||||
aggregateId: 'race-1',
|
||||
occurredAt: new Date(),
|
||||
eventData: {
|
||||
raceEventId: 'race-1',
|
||||
seasonId: 'season-1',
|
||||
leagueId: 'league-1',
|
||||
driverIds: ['driver-1', 'driver-2'],
|
||||
hadPenaltiesApplied: false,
|
||||
closedAt: new Date(),
|
||||
},
|
||||
};
|
||||
|
||||
const result = await useCase.execute(event);
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(mockRaceEventRepository.findById).toHaveBeenCalledWith('race-1');
|
||||
expect(mockResultRepository.findByRaceId).toHaveBeenCalledWith('session-1');
|
||||
expect(mockNotificationService.sendNotification).toHaveBeenCalledTimes(2);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
|
||||
// Check first notification
|
||||
expect(mockNotificationService.sendNotification).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
recipientId: 'driver-1',
|
||||
type: 'race_final_results',
|
||||
title: 'Final Results: Test Race',
|
||||
body: expect.stringContaining('Final result: P1 (+2 positions). Clean race! +35 rating.'),
|
||||
data: expect.objectContaining({
|
||||
raceEventId: 'race-1',
|
||||
sessionId: 'session-1',
|
||||
leagueId: 'league-1',
|
||||
position: 1,
|
||||
positionChange: 2,
|
||||
incidents: 0,
|
||||
finalRatingChange: 35,
|
||||
hadPenaltiesApplied: false,
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(leagueRepository.findById).toHaveBeenCalledWith('league-1');
|
||||
expect(raceEventRepository.findById).toHaveBeenCalledWith('race-1');
|
||||
expect(resultRepository.findByRaceId).toHaveBeenCalledWith('session-1');
|
||||
expect(notificationService.sendNotification).toHaveBeenCalledTimes(2);
|
||||
|
||||
// Check second notification
|
||||
expect(mockNotificationService.sendNotification).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
recipientId: 'driver-2',
|
||||
type: 'race_final_results',
|
||||
title: 'Final Results: Test Race',
|
||||
body: expect.stringContaining('Final result: P2 (-1 positions). 1 incident +20 rating.'),
|
||||
data: expect.objectContaining({
|
||||
position: 2,
|
||||
positionChange: -1,
|
||||
incidents: 1,
|
||||
finalRatingChange: 20,
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const presented = output.present.mock.calls[0][0] as SendFinalResultsResult;
|
||||
expect(presented).toEqual({
|
||||
leagueId: 'league-1',
|
||||
raceId: 'race-1',
|
||||
notificationsSent: 2,
|
||||
});
|
||||
});
|
||||
|
||||
it('skips sending notifications if race event not found', async () => {
|
||||
const mockNotificationService = {
|
||||
sendNotification: vi.fn(),
|
||||
} as unknown as NotificationService;
|
||||
it('returns LEAGUE_NOT_FOUND when league does not exist', async () => {
|
||||
leagueRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const mockRaceEventRepository = {
|
||||
findById: vi.fn().mockResolvedValue(null),
|
||||
} as unknown as IRaceEventRepository;
|
||||
const result = await useCase.execute(createInput());
|
||||
|
||||
const mockResultRepository = {
|
||||
findByRaceId: vi.fn(),
|
||||
} as unknown as IResultRepository;
|
||||
|
||||
const useCase = new SendFinalResultsUseCase(
|
||||
mockNotificationService,
|
||||
mockRaceEventRepository,
|
||||
mockResultRepository,
|
||||
);
|
||||
|
||||
const event: RaceEventStewardingClosedEvent = {
|
||||
eventType: 'RaceEventStewardingClosed',
|
||||
aggregateId: 'race-1',
|
||||
occurredAt: new Date(),
|
||||
eventData: {
|
||||
raceEventId: 'race-1',
|
||||
seasonId: 'season-1',
|
||||
leagueId: 'league-1',
|
||||
driverIds: ['driver-1'],
|
||||
hadPenaltiesApplied: false,
|
||||
closedAt: new Date(),
|
||||
},
|
||||
};
|
||||
|
||||
const result = await useCase.execute(event);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(mockNotificationService.sendNotification).not.toHaveBeenCalled();
|
||||
const error = unwrapError(result);
|
||||
expect(error.code).toBe('LEAGUE_NOT_FOUND');
|
||||
expect(error.details?.message).toBe('League not found');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('skips sending notifications if no main race session', async () => {
|
||||
const mockNotificationService = {
|
||||
sendNotification: vi.fn(),
|
||||
} as unknown as NotificationService;
|
||||
it('returns RACE_NOT_FOUND when race event does not exist', async () => {
|
||||
leagueRepository.findById.mockResolvedValue({ id: 'league-1' });
|
||||
raceEventRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const mockRaceEvent = {
|
||||
const result = await useCase.execute(createInput());
|
||||
|
||||
const error = unwrapError(result);
|
||||
expect(error.code).toBe('RACE_NOT_FOUND');
|
||||
expect(error.details?.message).toBe('Race event not found');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns INSUFFICIENT_PERMISSIONS when user is not steward or higher', async () => {
|
||||
leagueRepository.findById.mockResolvedValue({ id: 'league-1' });
|
||||
raceEventRepository.findById.mockResolvedValue({ id: 'race-1', leagueId: 'league-1', status: 'closed' });
|
||||
membershipRepository.getMembership.mockResolvedValue(null);
|
||||
|
||||
const result = await useCase.execute(createInput());
|
||||
|
||||
const error = unwrapError(result);
|
||||
expect(error.code).toBe('INSUFFICIENT_PERMISSIONS');
|
||||
expect(error.details?.message).toBe('Insufficient permissions to send final results');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns RESULTS_NOT_FINAL when race is not closed', async () => {
|
||||
leagueRepository.findById.mockResolvedValue({ id: 'league-1' });
|
||||
raceEventRepository.findById.mockResolvedValue({
|
||||
id: 'race-1',
|
||||
leagueId: 'league-1',
|
||||
name: 'Test Race',
|
||||
getMainRaceSession: vi.fn().mockReturnValue(null),
|
||||
};
|
||||
status: 'in_progress',
|
||||
getMainRaceSession: vi.fn(),
|
||||
});
|
||||
|
||||
const mockRaceEventRepository = {
|
||||
findById: vi.fn().mockResolvedValue(mockRaceEvent),
|
||||
} as unknown as IRaceEventRepository;
|
||||
const result = await useCase.execute(createInput());
|
||||
|
||||
const mockResultRepository = {
|
||||
findByRaceId: vi.fn(),
|
||||
} as unknown as IResultRepository;
|
||||
const error = unwrapError(result);
|
||||
expect(error.code).toBe('RESULTS_NOT_FINAL');
|
||||
expect(error.details?.message).toBe('Race results are not in a final state');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
const useCase = new SendFinalResultsUseCase(
|
||||
mockNotificationService,
|
||||
mockRaceEventRepository,
|
||||
mockResultRepository,
|
||||
);
|
||||
it('returns RESULTS_NOT_FINAL when main race session is missing', async () => {
|
||||
leagueRepository.findById.mockResolvedValue({ id: 'league-1' });
|
||||
raceEventRepository.findById.mockResolvedValue({
|
||||
id: 'race-1',
|
||||
leagueId: 'league-1',
|
||||
name: 'Test Race',
|
||||
status: 'closed',
|
||||
getMainRaceSession: vi.fn().mockReturnValue(undefined),
|
||||
});
|
||||
|
||||
const event: RaceEventStewardingClosedEvent = {
|
||||
eventType: 'RaceEventStewardingClosed',
|
||||
aggregateId: 'race-1',
|
||||
occurredAt: new Date(),
|
||||
eventData: {
|
||||
raceEventId: 'race-1',
|
||||
seasonId: 'season-1',
|
||||
leagueId: 'league-1',
|
||||
driverIds: ['driver-1'],
|
||||
hadPenaltiesApplied: false,
|
||||
closedAt: new Date(),
|
||||
},
|
||||
};
|
||||
const result = await useCase.execute(createInput());
|
||||
|
||||
const result = await useCase.execute(event);
|
||||
const error = unwrapError(result);
|
||||
expect(error.code).toBe('RESULTS_NOT_FINAL');
|
||||
expect(error.details?.message).toBe('Main race session not found for race event');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(mockNotificationService.sendNotification).not.toHaveBeenCalled();
|
||||
it('wraps repository errors into REPOSITORY_ERROR and does not present output', async () => {
|
||||
const mockError = new Error('Repository failure');
|
||||
leagueRepository.findById.mockRejectedValue(mockError);
|
||||
|
||||
const result = await useCase.execute(createInput());
|
||||
|
||||
const error = unwrapError(result);
|
||||
expect(error.code).toBe('REPOSITORY_ERROR');
|
||||
expect(error.details?.message).toBe('Repository failure');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
expect(logger.error).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user