This commit is contained in:
2025-12-16 21:44:20 +01:00
parent 7532c7ed6d
commit 8c67081953
38 changed files with 818 additions and 1321 deletions

View File

@@ -1,37 +1,83 @@
import { describe, it, expect } from 'vitest';
import {
InMemorySeasonRepository,
} from '@core/racing/infrastructure/repositories/InMemoryScoringRepositories';
import { Season } from '@core/racing/domain/entities/Season';
import type { ILeagueRepository } from '@core/racing/domain/repositories/ILeagueRepository';
import {
ManageSeasonLifecycleUseCase,
type ManageSeasonLifecycleCommand,
} from '@core/racing/application/use-cases/ManageSeasonLifecycleUseCase';
import type { Logger } from '@core/shared/application';
const logger: Logger = {
debug: () => {},
info: () => {},
warn: () => {},
error: () => {},
};
function createFakeLeagueRepository(seed: Array<{ id: string }>): ILeagueRepository {
return {
findById: async (id: string) => seed.find((l) => l.id === id) ?? null,
findAll: async () => seed,
create: async (league: any) => league,
update: async (league: any) => league,
} as unknown as ILeagueRepository;
}
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import { ManageSeasonLifecycleUseCase, type ManageSeasonLifecycleCommand } from './ManageSeasonLifecycleUseCase';
import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
import { Season } from '../../domain/entities/Season';
describe('ManageSeasonLifecycleUseCase', () => {
function setupLifecycleTest() {
const leagueRepo = createFakeLeagueRepository([{ id: 'league-1' }]);
const seasonRepo = new InMemorySeasonRepository(logger);
let useCase: ManageSeasonLifecycleUseCase;
let leagueRepository: {
findById: Mock;
};
let seasonRepository: {
findById: Mock;
update: Mock;
};
beforeEach(() => {
leagueRepository = {
findById: vi.fn(),
};
seasonRepository = {
findById: vi.fn(),
update: vi.fn(),
};
useCase = new ManageSeasonLifecycleUseCase(
leagueRepository as unknown as ILeagueRepository,
seasonRepository as unknown as ISeasonRepository,
);
});
it('applies activate → complete → archive transitions and persists state', async () => {
const league = { id: 'league-1' };
let currentSeason = Season.create({
id: 'season-1',
leagueId: 'league-1',
gameId: 'iracing',
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 = {
leagueId: 'league-1',
seasonId: currentSeason.id,
transition: 'activate',
};
const activated = await useCase.execute(activateCommand);
expect(activated.isOk()).toBe(true);
expect(activated.unwrap().status).toBe('active');
const completeCommand: ManageSeasonLifecycleCommand = {
leagueId: 'league-1',
seasonId: currentSeason.id,
transition: 'complete',
};
const completed = await useCase.execute(completeCommand);
expect(completed.isOk()).toBe(true);
expect(completed.unwrap().status).toBe('completed');
const archiveCommand: ManageSeasonLifecycleCommand = {
leagueId: 'league-1',
seasonId: currentSeason.id,
transition: 'archive',
};
const archived = await useCase.execute(archiveCommand);
expect(archived.isOk()).toBe(true);
expect(archived.unwrap().status).toBe('archived');
});
it('propagates domain invariant errors for invalid transitions', async () => {
const league = { id: 'league-1' };
const season = Season.create({
id: 'season-1',
leagueId: 'league-1',
@@ -40,52 +86,8 @@ describe('ManageSeasonLifecycleUseCase', () => {
status: 'planned',
});
seasonRepo.seed(season);
const useCase = new ManageSeasonLifecycleUseCase(leagueRepo, seasonRepo);
return { leagueRepo, seasonRepo, useCase, season };
}
it('applies activate → complete → archive transitions and persists state', async () => {
const { useCase, seasonRepo, season } = setupLifecycleTest();
const activateCommand: ManageSeasonLifecycleCommand = {
leagueId: 'league-1',
seasonId: season.id,
transition: 'activate',
};
const activated = await useCase.execute(activateCommand);
expect(activated.isOk()).toBe(true);
expect(activated.value.status).toBe('active');
const completeCommand: ManageSeasonLifecycleCommand = {
leagueId: 'league-1',
seasonId: season.id,
transition: 'complete',
};
const completed = await useCase.execute(completeCommand);
expect(completed.isOk()).toBe(true);
expect(completed.value.status).toBe('completed');
const archiveCommand: ManageSeasonLifecycleCommand = {
leagueId: 'league-1',
seasonId: season.id,
transition: 'archive',
};
const archived = await useCase.execute(archiveCommand);
expect(archived.isOk()).toBe(true);
expect(archived.value.status).toBe('archived');
const persisted = await seasonRepo.findById(season.id);
expect(persisted!.status).toBe('archived');
});
it('propagates domain invariant errors for invalid transitions', async () => {
const { useCase, seasonRepo, season } = setupLifecycleTest();
leagueRepository.findById.mockResolvedValue(league);
seasonRepository.findById.mockResolvedValue(season);
const completeCommand: ManageSeasonLifecycleCommand = {
leagueId: 'league-1',
@@ -95,9 +97,42 @@ describe('ManageSeasonLifecycleUseCase', () => {
const result = await useCase.execute(completeCommand);
expect(result.isErr()).toBe(true);
expect(result.error.code).toBe('INVALID_TRANSITION');
expect(result.unwrapErr().code).toEqual('INVALID_TRANSITION');
});
const persisted = await seasonRepo.findById(season.id);
expect(persisted!.status).toBe('planned');
it('returns error when league not found', async () => {
leagueRepository.findById.mockResolvedValue(null);
const command: ManageSeasonLifecycleCommand = {
leagueId: 'league-1',
seasonId: 'season-1',
transition: 'activate',
};
const result = await useCase.execute(command);
expect(result.isErr()).toBe(true);
expect(result.unwrapErr()).toEqual({
code: 'LEAGUE_NOT_FOUND',
details: { message: 'League not found: league-1' },
});
});
it('returns error when season not found', async () => {
const league = { id: 'league-1' };
leagueRepository.findById.mockResolvedValue(league);
seasonRepository.findById.mockResolvedValue(null);
const command: ManageSeasonLifecycleCommand = {
leagueId: 'league-1',
seasonId: 'season-1',
transition: 'activate',
};
const result = await useCase.execute(command);
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' },
});
});
});