128 lines
3.7 KiB
TypeScript
128 lines
3.7 KiB
TypeScript
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
|
|
import {
|
|
PreviewLeagueScheduleUseCase,
|
|
type PreviewLeagueScheduleResult,
|
|
type PreviewLeagueScheduleInput,
|
|
type PreviewLeagueScheduleErrorCode,
|
|
} from './PreviewLeagueScheduleUseCase';
|
|
import type { Logger } from '@core/shared/application';
|
|
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;
|
|
info: Mock;
|
|
warn: Mock;
|
|
error: Mock;
|
|
};
|
|
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: PreviewLeagueScheduleInput = {
|
|
schedule: {
|
|
seasonStartDate: '2024-01-01',
|
|
recurrenceStrategy: 'weekly',
|
|
weekdays: ['Mon'],
|
|
raceStartTime: '20:00',
|
|
timezoneId: 'UTC',
|
|
plannedRounds: 5,
|
|
},
|
|
maxRounds: 3,
|
|
};
|
|
|
|
const result = await useCase.execute(params);
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
expect(output.present).toHaveBeenCalledTimes(1);
|
|
const presentedRaw = output.present.mock.calls[0]?.[0];
|
|
expect(presentedRaw).toBeDefined();
|
|
const presented = presentedRaw 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,
|
|
},
|
|
};
|
|
|
|
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();
|
|
});
|
|
|
|
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();
|
|
});
|
|
}); |