refactor racing use cases
This commit is contained in:
@@ -1,8 +1,16 @@
|
||||
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
|
||||
import { ManageSeasonLifecycleUseCase, type ManageSeasonLifecycleCommand } from './ManageSeasonLifecycleUseCase';
|
||||
import {
|
||||
ManageSeasonLifecycleUseCase,
|
||||
type ManageSeasonLifecycleInput,
|
||||
type ManageSeasonLifecycleResult,
|
||||
type ManageSeasonLifecycleErrorCode,
|
||||
} from './ManageSeasonLifecycleUseCase';
|
||||
import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
|
||||
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
|
||||
import { Season } from '../../domain/entities/Season';
|
||||
import { Season } from '../../domain/entities/season/Season';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
|
||||
describe('ManageSeasonLifecycleUseCase', () => {
|
||||
let useCase: ManageSeasonLifecycleUseCase;
|
||||
@@ -13,7 +21,10 @@ describe('ManageSeasonLifecycleUseCase', () => {
|
||||
findById: Mock;
|
||||
update: Mock;
|
||||
};
|
||||
|
||||
let output: UseCaseOutputPort<ManageSeasonLifecycleResult> & {
|
||||
present: Mock;
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
leagueRepository = {
|
||||
findById: vi.fn(),
|
||||
@@ -22,12 +33,18 @@ describe('ManageSeasonLifecycleUseCase', () => {
|
||||
findById: vi.fn(),
|
||||
update: vi.fn(),
|
||||
};
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
} as unknown as UseCaseOutputPort<ManageSeasonLifecycleResult> & {
|
||||
present: Mock;
|
||||
};
|
||||
useCase = new ManageSeasonLifecycleUseCase(
|
||||
leagueRepository as unknown as ILeagueRepository,
|
||||
seasonRepository as unknown as ISeasonRepository,
|
||||
output,
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
it('applies activate → complete → archive transitions and persists state', async () => {
|
||||
const league = { id: 'league-1' };
|
||||
let currentSeason = Season.create({
|
||||
@@ -37,45 +54,63 @@ describe('ManageSeasonLifecycleUseCase', () => {
|
||||
name: 'Lifecycle Season',
|
||||
status: 'planned',
|
||||
});
|
||||
|
||||
|
||||
leagueRepository.findById.mockResolvedValue(league);
|
||||
seasonRepository.findById.mockImplementation(() => Promise.resolve(currentSeason));
|
||||
seasonRepository.update.mockImplementation((s) => {
|
||||
currentSeason = s;
|
||||
return Promise.resolve(s);
|
||||
});
|
||||
|
||||
const activateCommand: ManageSeasonLifecycleCommand = {
|
||||
|
||||
const activateInput: ManageSeasonLifecycleInput = {
|
||||
leagueId: 'league-1',
|
||||
seasonId: currentSeason.id,
|
||||
transition: 'activate',
|
||||
};
|
||||
|
||||
const activated = await useCase.execute(activateCommand);
|
||||
|
||||
const activated = await useCase.execute(activateInput);
|
||||
expect(activated.isOk()).toBe(true);
|
||||
expect(activated.unwrap().status).toBe('active');
|
||||
|
||||
const completeCommand: ManageSeasonLifecycleCommand = {
|
||||
expect(activated.unwrap()).toBeUndefined();
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const [firstCall] = output.present.mock.calls;
|
||||
const [firstArg] = firstCall as [ManageSeasonLifecycleResult];
|
||||
let presented = firstArg;
|
||||
expect(presented.season.status).toBe('active');
|
||||
|
||||
(output.present as Mock).mockClear();
|
||||
|
||||
const completeInput: ManageSeasonLifecycleInput = {
|
||||
leagueId: 'league-1',
|
||||
seasonId: currentSeason.id,
|
||||
transition: 'complete',
|
||||
};
|
||||
|
||||
const completed = await useCase.execute(completeCommand);
|
||||
|
||||
const completed = await useCase.execute(completeInput);
|
||||
expect(completed.isOk()).toBe(true);
|
||||
expect(completed.unwrap().status).toBe('completed');
|
||||
|
||||
const archiveCommand: ManageSeasonLifecycleCommand = {
|
||||
expect(completed.unwrap()).toBeUndefined();
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
{
|
||||
const [[arg]] = output.present.mock.calls as [[ManageSeasonLifecycleResult]];
|
||||
presented = arg;
|
||||
}
|
||||
expect(presented.season.status).toBe('completed');
|
||||
|
||||
(output.present as Mock).mockClear();
|
||||
|
||||
const archiveInput: ManageSeasonLifecycleInput = {
|
||||
leagueId: 'league-1',
|
||||
seasonId: currentSeason.id,
|
||||
transition: 'archive',
|
||||
};
|
||||
|
||||
const archived = await useCase.execute(archiveCommand);
|
||||
|
||||
const archived = await useCase.execute(archiveInput);
|
||||
expect(archived.isOk()).toBe(true);
|
||||
expect(archived.unwrap().status).toBe('archived');
|
||||
expect(archived.unwrap()).toBeUndefined();
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
presented = output.present.mock.calls[0][0] as ManageSeasonLifecycleResult;
|
||||
expect(presented.season.status).toBe('archived');
|
||||
});
|
||||
|
||||
|
||||
it('propagates domain invariant errors for invalid transitions', async () => {
|
||||
const league = { id: 'league-1' };
|
||||
const season = Season.create({
|
||||
@@ -85,54 +120,67 @@ describe('ManageSeasonLifecycleUseCase', () => {
|
||||
name: 'Lifecycle Season',
|
||||
status: 'planned',
|
||||
});
|
||||
|
||||
|
||||
leagueRepository.findById.mockResolvedValue(league);
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
|
||||
const completeCommand: ManageSeasonLifecycleCommand = {
|
||||
|
||||
const completeInput: ManageSeasonLifecycleInput = {
|
||||
leagueId: 'league-1',
|
||||
seasonId: season.id,
|
||||
transition: 'complete',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(completeCommand);
|
||||
|
||||
const result = await useCase.execute(completeInput);
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr().code).toEqual('INVALID_TRANSITION');
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
ManageSeasonLifecycleErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toEqual('INVALID_TRANSITION');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
|
||||
it('returns error when league not found', async () => {
|
||||
leagueRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const command: ManageSeasonLifecycleCommand = {
|
||||
|
||||
const input: ManageSeasonLifecycleInput = {
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
transition: 'activate',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(command);
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr()).toEqual({
|
||||
code: 'LEAGUE_NOT_FOUND',
|
||||
details: { message: 'League not found: league-1' },
|
||||
});
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
ManageSeasonLifecycleErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toEqual('LEAGUE_NOT_FOUND');
|
||||
expect(error.details).toEqual({ message: 'League not found: league-1' });
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
|
||||
it('returns error when season not found', async () => {
|
||||
const league = { id: 'league-1' };
|
||||
leagueRepository.findById.mockResolvedValue(league);
|
||||
seasonRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const command: ManageSeasonLifecycleCommand = {
|
||||
|
||||
const input: ManageSeasonLifecycleInput = {
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
transition: 'activate',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(command);
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr()).toEqual({
|
||||
code: 'SEASON_NOT_FOUND',
|
||||
details: { message: 'Season season-1 does not belong to league league-1' },
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
ManageSeasonLifecycleErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toEqual('SEASON_NOT_FOUND');
|
||||
expect(error.details).toEqual({
|
||||
message: 'Season season-1 does not belong to league league-1',
|
||||
});
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user