refactor use cases

This commit is contained in:
2026-01-08 15:34:51 +01:00
parent d984ab24a8
commit 52e9a2f6a7
362 changed files with 5192 additions and 8409 deletions

View File

@@ -5,11 +5,10 @@ import {
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', () => {
import type { Logger } from '@core/shared/application';
describe('PreviewLeagueScheduleUseCase', () => {
let useCase: PreviewLeagueScheduleUseCase;
let logger: {
debug: Mock;
@@ -17,26 +16,17 @@ import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorC
warn: Mock;
error: Mock;
};
let output: { present: Mock } &
UseCaseOutputPort<PreviewLeagueScheduleResult>;
beforeEach(() => {
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,
);
useCase = new PreviewLeagueScheduleUseCase(undefined, logger as unknown as Logger);
});
it('should preview schedule successfully', async () => {
const params: PreviewLeagueScheduleInput = {
schedule: {
@@ -49,80 +39,72 @@ import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorC
},
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');
const scheduleResult = result.unwrap();
expect(scheduleResult.rounds).toBeDefined();
expect(scheduleResult.rounds.length).toBeGreaterThan(0);
expect(scheduleResult.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);
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,
},
};
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();
});
const result = await useCase.execute(params);
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'
>;
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');
});
const throwingUseCase = new PreviewLeagueScheduleUseCase(
throwingGenerator,
logger as unknown as Logger,
output,
);
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 params: PreviewLeagueScheduleInput = {
schedule: {
seasonStartDate: '2024-01-01',
recurrenceStrategy: 'weekly',
weekdays: ['Mon'],
raceStartTime: '20:00',
timezoneId: 'UTC',
plannedRounds: 5,
},
maxRounds: 3,
};
const throwingUseCase = new PreviewLeagueScheduleUseCase(throwingGenerator,
logger as unknown as Logger);
const result = await throwingUseCase.execute(params);
const params: PreviewLeagueScheduleInput = {
schedule: {
seasonStartDate: '2024-01-01',
recurrenceStrategy: 'weekly',
weekdays: ['Mon'],
raceStartTime: '20:00',
timezoneId: 'UTC',
plannedRounds: 5,
},
maxRounds: 3,
};
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();
});
});
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');
});
});