add tests to core
This commit is contained in:
174
core/racing/application/use-cases/ReopenRaceUseCase.test.ts
Normal file
174
core/racing/application/use-cases/ReopenRaceUseCase.test.ts
Normal file
@@ -0,0 +1,174 @@
|
||||
import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
|
||||
import {
|
||||
ReopenRaceUseCase,
|
||||
type ReopenRaceInput,
|
||||
type ReopenRaceResult,
|
||||
type ReopenRaceErrorCode,
|
||||
} from './ReopenRaceUseCase';
|
||||
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';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
describe('ReopenRaceUseCase', () => {
|
||||
let raceRepository: {
|
||||
findById: Mock;
|
||||
update: Mock;
|
||||
};
|
||||
let logger: {
|
||||
debug: Mock;
|
||||
warn: Mock;
|
||||
info: Mock;
|
||||
error: Mock;
|
||||
};
|
||||
let output: UseCaseOutputPort<ReopenRaceResult> & { present: Mock };
|
||||
|
||||
let useCase: ReopenRaceUseCase;
|
||||
|
||||
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<ReopenRaceResult> & { present: Mock };
|
||||
|
||||
useCase = new ReopenRaceUseCase(
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
logger as unknown as Logger,
|
||||
output,
|
||||
);
|
||||
});
|
||||
|
||||
it('returns RACE_NOT_FOUND when race does not exist', async () => {
|
||||
const input: ReopenRaceInput = { raceId: 'race-404', reopenedById: 'admin-1' };
|
||||
|
||||
raceRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
ReopenRaceErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(err.code).toBe('RACE_NOT_FOUND');
|
||||
expect(err.details.message).toContain('race-404');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('reopens a completed race, persists, and presents the result', async () => {
|
||||
const race = Race.create({
|
||||
id: 'race-1',
|
||||
leagueId: 'league-1',
|
||||
scheduledAt: new Date('2025-01-01T00:00:00.000Z'),
|
||||
track: 'Track 1',
|
||||
car: 'Car 1',
|
||||
sessionType: SessionType.main(),
|
||||
status: 'completed',
|
||||
});
|
||||
|
||||
raceRepository.findById.mockResolvedValue(race);
|
||||
raceRepository.update.mockResolvedValue(race.reopen());
|
||||
|
||||
const input: ReopenRaceInput = { raceId: 'race-1', reopenedById: 'admin-1' };
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
|
||||
expect(raceRepository.update).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
id: 'race-1',
|
||||
status: 'scheduled',
|
||||
}),
|
||||
);
|
||||
|
||||
expect(output.present).toHaveBeenCalledWith({
|
||||
race: expect.objectContaining({
|
||||
id: 'race-1',
|
||||
status: 'scheduled',
|
||||
}),
|
||||
});
|
||||
|
||||
expect(logger.info).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns INVALID_RACE_STATE when race is already scheduled', async () => {
|
||||
const race = Race.create({
|
||||
id: 'race-1',
|
||||
leagueId: 'league-1',
|
||||
scheduledAt: new Date('2025-01-01T00:00:00.000Z'),
|
||||
track: 'Track 1',
|
||||
car: 'Car 1',
|
||||
sessionType: SessionType.main(),
|
||||
status: 'scheduled',
|
||||
});
|
||||
|
||||
raceRepository.findById.mockResolvedValue(race);
|
||||
|
||||
const input: ReopenRaceInput = { raceId: 'race-1', reopenedById: 'admin-1' };
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
ReopenRaceErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(err.code).toBe('INVALID_RACE_STATE');
|
||||
expect(err.details.message).toContain('already scheduled');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns INVALID_RACE_STATE when race is running', async () => {
|
||||
const race = Race.create({
|
||||
id: 'race-1',
|
||||
leagueId: 'league-1',
|
||||
scheduledAt: new Date('2025-01-01T00:00:00.000Z'),
|
||||
track: 'Track 1',
|
||||
car: 'Car 1',
|
||||
sessionType: SessionType.main(),
|
||||
status: 'running',
|
||||
});
|
||||
|
||||
raceRepository.findById.mockResolvedValue(race);
|
||||
|
||||
const input: ReopenRaceInput = { raceId: 'race-1', reopenedById: 'admin-1' };
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
ReopenRaceErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(err.code).toBe('INVALID_RACE_STATE');
|
||||
expect(err.details.message).toContain('running race');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns REPOSITORY_ERROR when repository throws unexpected error', async () => {
|
||||
raceRepository.findById.mockRejectedValue(new Error('DB error'));
|
||||
|
||||
const input: ReopenRaceInput = { raceId: 'race-1', reopenedById: 'admin-1' };
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
ReopenRaceErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(err.code).toBe('REPOSITORY_ERROR');
|
||||
expect(err.details.message).toBe('DB error');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user