refactor racing use cases
This commit is contained in:
@@ -1,8 +1,15 @@
|
||||
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
|
||||
import { PreviewLeagueScheduleUseCase } from './PreviewLeagueScheduleUseCase';
|
||||
import {
|
||||
PreviewLeagueScheduleUseCase,
|
||||
type PreviewLeagueScheduleResult,
|
||||
type PreviewLeagueScheduleInput,
|
||||
type PreviewLeagueScheduleErrorCode,
|
||||
} from './PreviewLeagueScheduleUseCase';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
|
||||
describe('PreviewLeagueScheduleUseCase', () => {
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
describe('PreviewLeagueScheduleUseCase', () => {
|
||||
let useCase: PreviewLeagueScheduleUseCase;
|
||||
let logger: {
|
||||
debug: Mock;
|
||||
@@ -10,59 +17,111 @@ describe('PreviewLeagueScheduleUseCase', () => {
|
||||
warn: Mock;
|
||||
error: Mock;
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
let output: { present: Mock } &
|
||||
UseCaseOutputPort<PreviewLeagueScheduleResult>;
|
||||
|
||||
beforeEach(() => {
|
||||
logger = {
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
};
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
} as unknown as typeof output;
|
||||
useCase = new PreviewLeagueScheduleUseCase(
|
||||
undefined,
|
||||
logger as unknown as Logger,
|
||||
output,
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
it('should preview schedule successfully', async () => {
|
||||
const params = {
|
||||
const params: PreviewLeagueScheduleInput = {
|
||||
schedule: {
|
||||
seasonStartDate: '2024-01-01',
|
||||
recurrenceStrategy: 'weekly' as const,
|
||||
weekdays: ['Mon' as const],
|
||||
recurrenceStrategy: 'weekly',
|
||||
weekdays: ['Mon'],
|
||||
raceStartTime: '20:00',
|
||||
timezoneId: 'UTC',
|
||||
plannedRounds: 5,
|
||||
},
|
||||
maxRounds: 3,
|
||||
};
|
||||
|
||||
|
||||
const result = await useCase.execute(params);
|
||||
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
const preview = result.unwrap();
|
||||
expect(preview.rounds.length).toBeGreaterThan(0);
|
||||
expect(preview.summary).toContain('Every Mon');
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const presented =
|
||||
output.present.mock.calls[0][0] as PreviewLeagueScheduleResult;
|
||||
expect(presented.rounds.length).toBeGreaterThan(0);
|
||||
expect(presented.summary).toContain('Every Mon');
|
||||
});
|
||||
|
||||
it('should return error for invalid schedule', async () => {
|
||||
const params: PreviewLeagueScheduleInput = {
|
||||
schedule: {
|
||||
seasonStartDate: 'invalid',
|
||||
recurrenceStrategy: 'weekly',
|
||||
weekdays: ['Mon'],
|
||||
raceStartTime: '20:00',
|
||||
timezoneId: 'UTC',
|
||||
plannedRounds: 5,
|
||||
},
|
||||
};
|
||||
|
||||
it('should return error for invalid schedule', async () => {
|
||||
const params = {
|
||||
schedule: {
|
||||
seasonStartDate: 'invalid',
|
||||
recurrenceStrategy: 'weekly' as const,
|
||||
weekdays: ['Mon' as const],
|
||||
raceStartTime: '20:00',
|
||||
timezoneId: 'UTC',
|
||||
plannedRounds: 5,
|
||||
},
|
||||
};
|
||||
const result = await useCase.execute(params);
|
||||
|
||||
const result = await useCase.execute(params);
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
PreviewLeagueScheduleErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(err.code).toBe('INVALID_SCHEDULE');
|
||||
expect(err.details.message).toBe('Invalid schedule data');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr()).toEqual({
|
||||
code: 'INVALID_SCHEDULE',
|
||||
details: { message: 'Invalid schedule data' },
|
||||
});
|
||||
});
|
||||
it('should return REPOSITORY_ERROR when generator throws', async () => {
|
||||
const throwingGenerator = {
|
||||
generateSlotsUpTo: vi.fn(() => {
|
||||
throw new Error('boom');
|
||||
}),
|
||||
} as unknown as Pick<
|
||||
typeof import('../../domain/services/SeasonScheduleGenerator').SeasonScheduleGenerator,
|
||||
'generateSlotsUpTo'
|
||||
>;
|
||||
|
||||
const throwingUseCase = new PreviewLeagueScheduleUseCase(
|
||||
throwingGenerator,
|
||||
logger as unknown as Logger,
|
||||
output,
|
||||
);
|
||||
|
||||
const params: PreviewLeagueScheduleInput = {
|
||||
schedule: {
|
||||
seasonStartDate: '2024-01-01',
|
||||
recurrenceStrategy: 'weekly',
|
||||
weekdays: ['Mon'],
|
||||
raceStartTime: '20:00',
|
||||
timezoneId: 'UTC',
|
||||
plannedRounds: 5,
|
||||
},
|
||||
maxRounds: 3,
|
||||
};
|
||||
|
||||
const result = await throwingUseCase.execute(params);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
PreviewLeagueScheduleErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(err.code).toBe('REPOSITORY_ERROR');
|
||||
expect(err.details.message).toBe('boom');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user