refactor use cases
This commit is contained in:
@@ -9,7 +9,6 @@ import type { IRaceRegistrationRepository } from '../../domain/repositories/IRac
|
||||
import type { IResultRepository } from '../../domain/repositories/IResultRepository';
|
||||
import type { IStandingRepository } from '../../domain/repositories/IStandingRepository';
|
||||
import { RatingUpdateService } from '@core/identity/domain/services/RatingUpdateService';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import type { IRaceResultsProvider } from '@core/identity/application/ports/IRaceResultsProvider';
|
||||
|
||||
describe('CompleteRaceUseCaseWithRatings', () => {
|
||||
@@ -67,7 +66,6 @@ describe('CompleteRaceUseCaseWithRatings', () => {
|
||||
getRaceResults: vi.fn(),
|
||||
hasRaceResults: vi.fn(),
|
||||
};
|
||||
output = { present: vi.fn() };
|
||||
|
||||
// Test without raceResultsProvider (backward compatible mode)
|
||||
useCase = new CompleteRaceUseCaseWithRatings(
|
||||
@@ -77,7 +75,6 @@ describe('CompleteRaceUseCaseWithRatings', () => {
|
||||
standingRepository as unknown as IStandingRepository,
|
||||
driverRatingProvider,
|
||||
ratingUpdateService as unknown as RatingUpdateService,
|
||||
output as unknown as UseCaseOutputPort<CompleteRaceWithRatingsResult>,
|
||||
);
|
||||
});
|
||||
|
||||
@@ -109,7 +106,11 @@ describe('CompleteRaceUseCaseWithRatings', () => {
|
||||
const result = await useCase.execute(command);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
const value = result.unwrap();
|
||||
expect(value).toEqual({
|
||||
raceId: 'race-1',
|
||||
ratingsUpdatedForDriverIds: ['driver-1', 'driver-2'],
|
||||
});
|
||||
expect(raceRepository.findById).toHaveBeenCalledWith('race-1');
|
||||
expect(raceRegistrationRepository.getRegisteredDrivers).toHaveBeenCalledWith('race-1');
|
||||
expect(driverRatingProvider.getRatings).toHaveBeenCalledWith(['driver-1', 'driver-2']);
|
||||
@@ -118,12 +119,7 @@ describe('CompleteRaceUseCaseWithRatings', () => {
|
||||
expect(ratingUpdateService.updateDriverRatingsAfterRace).toHaveBeenCalled();
|
||||
expect(mockRace.complete).toHaveBeenCalled();
|
||||
expect(raceRepository.update).toHaveBeenCalledWith({ id: 'race-1', status: 'completed' });
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
expect(output.present).toHaveBeenCalledWith({
|
||||
raceId: 'race-1',
|
||||
ratingsUpdatedForDriverIds: ['driver-1', 'driver-2'],
|
||||
});
|
||||
});
|
||||
|
||||
it('returns error when race does not exist', async () => {
|
||||
const command: CompleteRaceWithRatingsInput = {
|
||||
@@ -136,8 +132,7 @@ describe('CompleteRaceUseCaseWithRatings', () => {
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr().code).toBe('RACE_NOT_FOUND');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('returns error when race is already completed', async () => {
|
||||
const command: CompleteRaceWithRatingsInput = {
|
||||
@@ -156,7 +151,6 @@ describe('CompleteRaceUseCaseWithRatings', () => {
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr().code).toBe('ALREADY_COMPLETED');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
expect(raceRegistrationRepository.getRegisteredDrivers).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -178,8 +172,7 @@ describe('CompleteRaceUseCaseWithRatings', () => {
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr().code).toBe('NO_REGISTERED_DRIVERS');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('returns rating update error when rating service throws', async () => {
|
||||
const command: CompleteRaceWithRatingsInput = {
|
||||
@@ -206,8 +199,7 @@ describe('CompleteRaceUseCaseWithRatings', () => {
|
||||
const error = result.unwrapErr();
|
||||
expect(error.code).toBe('RATING_UPDATE_FAILED');
|
||||
expect(error.details?.message).toBe('Rating error');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('returns repository error when persistence fails', async () => {
|
||||
const command: CompleteRaceWithRatingsInput = {
|
||||
@@ -231,8 +223,7 @@ describe('CompleteRaceUseCaseWithRatings', () => {
|
||||
const error = result.unwrapErr();
|
||||
expect(error.code).toBe('REPOSITORY_ERROR');
|
||||
expect(error.details?.message).toBe('DB error');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
// SLICE 7: New tests for ledger-based approach
|
||||
describe('Ledger-based rating updates (Slice 7)', () => {
|
||||
@@ -247,7 +238,6 @@ describe('CompleteRaceUseCaseWithRatings', () => {
|
||||
standingRepository as unknown as IStandingRepository,
|
||||
driverRatingProvider,
|
||||
ratingUpdateService as unknown as RatingUpdateService,
|
||||
output as unknown as UseCaseOutputPort<CompleteRaceWithRatingsResult>,
|
||||
raceResultsProvider as unknown as IRaceResultsProvider,
|
||||
);
|
||||
});
|
||||
@@ -285,9 +275,13 @@ describe('CompleteRaceUseCaseWithRatings', () => {
|
||||
raceRepository.update.mockResolvedValue(undefined);
|
||||
|
||||
const result = await useCaseWithLedger.execute(command);
|
||||
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
const value = result.unwrap();
|
||||
expect(value).toEqual({
|
||||
raceId: 'race-1',
|
||||
ratingsUpdatedForDriverIds: ['driver-1', 'driver-2'],
|
||||
});
|
||||
|
||||
// Verify ledger-based approach was used
|
||||
expect(ratingUpdateService.recordRaceRatingEvents).toHaveBeenCalledWith(
|
||||
@@ -307,11 +301,7 @@ describe('CompleteRaceUseCaseWithRatings', () => {
|
||||
expect(ratingUpdateService.updateDriverRatingsAfterRace).not.toHaveBeenCalled();
|
||||
|
||||
expect(raceRepository.update).toHaveBeenCalledWith({ id: 'race-1', status: 'completed' });
|
||||
expect(output.present).toHaveBeenCalledWith({
|
||||
raceId: 'race-1',
|
||||
ratingsUpdatedForDriverIds: ['driver-1', 'driver-2'],
|
||||
});
|
||||
});
|
||||
|
||||
it('falls back to legacy approach when ledger update fails', async () => {
|
||||
const command: CompleteRaceWithRatingsInput = {
|
||||
|
||||
Reference in New Issue
Block a user