tests
This commit is contained in:
@@ -0,0 +1,222 @@
|
||||
import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
import { Race } from '../../domain/entities/Race';
|
||||
import { Season } from '../../domain/entities/season/Season';
|
||||
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
||||
import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
|
||||
|
||||
import {
|
||||
CreateLeagueSeasonScheduleRaceUseCase,
|
||||
type CreateLeagueSeasonScheduleRaceErrorCode,
|
||||
} from './CreateLeagueSeasonScheduleRaceUseCase';
|
||||
|
||||
function createLogger(): Logger {
|
||||
return {
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
} as unknown as Logger;
|
||||
}
|
||||
|
||||
function createSeasonWithinWindow(overrides?: Partial<{ leagueId: string }>): Season {
|
||||
return Season.create({
|
||||
id: 'season-1',
|
||||
leagueId: overrides?.leagueId ?? 'league-1',
|
||||
gameId: 'iracing',
|
||||
name: 'Schedule Season',
|
||||
status: 'planned',
|
||||
startDate: new Date('2025-01-01T00:00:00Z'),
|
||||
endDate: new Date('2025-01-31T00:00:00Z'),
|
||||
});
|
||||
}
|
||||
|
||||
describe('CreateLeagueSeasonScheduleRaceUseCase', () => {
|
||||
let seasonRepository: { findById: Mock };
|
||||
let raceRepository: { create: Mock };
|
||||
let logger: Logger;
|
||||
|
||||
beforeEach(() => {
|
||||
seasonRepository = { findById: vi.fn() };
|
||||
raceRepository = { create: vi.fn() };
|
||||
logger = createLogger();
|
||||
});
|
||||
|
||||
it('creates a race when season belongs to league and scheduledAt is within season window', async () => {
|
||||
const season = createSeasonWithinWindow();
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
raceRepository.create.mockImplementation(async (race: Race) => race);
|
||||
|
||||
const useCase = new CreateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
logger,
|
||||
{ generateRaceId: () => 'race-123' },
|
||||
);
|
||||
|
||||
const scheduledAt = new Date('2025-01-10T20:00:00Z');
|
||||
const result = await useCase.execute({
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
track: 'Road Atlanta',
|
||||
car: 'MX-5',
|
||||
scheduledAt,
|
||||
});
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(raceRepository.create).toHaveBeenCalledTimes(1);
|
||||
const createdRace = raceRepository.create.mock.calls[0]?.[0] as Race;
|
||||
expect(createdRace.id).toBe('race-123');
|
||||
expect(createdRace.leagueId).toBe('league-1');
|
||||
expect(createdRace.track).toBe('Road Atlanta');
|
||||
expect(createdRace.car).toBe('MX-5');
|
||||
expect(createdRace.scheduledAt.getTime()).toBe(scheduledAt.getTime());
|
||||
});
|
||||
|
||||
it('returns SEASON_NOT_FOUND when season does not belong to league and does not create', async () => {
|
||||
const season = createSeasonWithinWindow({ leagueId: 'other-league' });
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
|
||||
const useCase = new CreateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
logger,
|
||||
{ generateRaceId: () => 'race-123' },
|
||||
);
|
||||
|
||||
const result = await useCase.execute({
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
track: 'Road Atlanta',
|
||||
car: 'MX-5',
|
||||
scheduledAt: new Date('2025-01-10T20:00:00Z'),
|
||||
});
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
CreateLeagueSeasonScheduleRaceErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('SEASON_NOT_FOUND');
|
||||
expect(raceRepository.create).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
|
||||
it('returns INVALID_INPUT when Race.create throws due to invalid data', async () => {
|
||||
const season = createSeasonWithinWindow();
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
|
||||
const useCase = new CreateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
logger,
|
||||
{ generateRaceId: () => 'race-123' },
|
||||
);
|
||||
|
||||
// Mock Race.create to throw (this would happen with invalid data)
|
||||
const originalCreate = Race.create;
|
||||
Race.create = vi.fn().mockImplementation(() => {
|
||||
throw new Error('Invalid race data');
|
||||
});
|
||||
|
||||
const result = await useCase.execute({
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
track: '', // Invalid empty track
|
||||
car: 'MX-5',
|
||||
scheduledAt: new Date('2025-01-10T20:00:00Z'),
|
||||
});
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
CreateLeagueSeasonScheduleRaceErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('INVALID_INPUT');
|
||||
expect(error.details.message).toBe('Invalid race data');
|
||||
expect(raceRepository.create).not.toHaveBeenCalled();
|
||||
|
||||
// Restore original
|
||||
Race.create = originalCreate;
|
||||
});
|
||||
|
||||
it('returns REPOSITORY_ERROR when repository throws during create', async () => {
|
||||
const season = createSeasonWithinWindow();
|
||||
const repositoryError = new Error('DB write failed');
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
raceRepository.create.mockRejectedValue(repositoryError);
|
||||
|
||||
const useCase = new CreateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
logger,
|
||||
{ generateRaceId: () => 'race-123' },
|
||||
);
|
||||
|
||||
const result = await useCase.execute({
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
track: 'Road Atlanta',
|
||||
car: 'MX-5',
|
||||
scheduledAt: new Date('2025-01-10T20:00:00Z'),
|
||||
});
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
CreateLeagueSeasonScheduleRaceErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('REPOSITORY_ERROR');
|
||||
expect(error.details.message).toBe('DB write failed');
|
||||
});
|
||||
|
||||
it('returns SEASON_NOT_FOUND when season does not exist', async () => {
|
||||
seasonRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const useCase = new CreateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
logger,
|
||||
{ generateRaceId: () => 'race-123' },
|
||||
);
|
||||
|
||||
const result = await useCase.execute({
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
track: 'Road Atlanta',
|
||||
car: 'MX-5',
|
||||
scheduledAt: new Date('2025-01-10T20:00:00Z'),
|
||||
});
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
CreateLeagueSeasonScheduleRaceErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('SEASON_NOT_FOUND');
|
||||
expect(raceRepository.create).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns REPOSITORY_ERROR when repository throws during find', async () => {
|
||||
const repositoryError = new Error('DB connection failed');
|
||||
seasonRepository.findById.mockRejectedValue(repositoryError);
|
||||
|
||||
const useCase = new CreateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
logger,
|
||||
{ generateRaceId: () => 'race-123' },
|
||||
);
|
||||
|
||||
const result = await useCase.execute({
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
track: 'Road Atlanta',
|
||||
car: 'MX-5',
|
||||
scheduledAt: new Date('2025-01-10T20:00:00Z'),
|
||||
});
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
CreateLeagueSeasonScheduleRaceErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('REPOSITORY_ERROR');
|
||||
expect(error.details.message).toBe('DB connection failed');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,259 @@
|
||||
import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
import { Race } from '../../domain/entities/Race';
|
||||
import { Season } from '../../domain/entities/season/Season';
|
||||
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
||||
import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
|
||||
|
||||
import {
|
||||
DeleteLeagueSeasonScheduleRaceUseCase,
|
||||
type DeleteLeagueSeasonScheduleRaceErrorCode,
|
||||
} from './DeleteLeagueSeasonScheduleRaceUseCase';
|
||||
|
||||
function createLogger(): Logger {
|
||||
return {
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
} as unknown as Logger;
|
||||
}
|
||||
|
||||
function createSeasonWithinWindow(overrides?: Partial<{ leagueId: string }>): Season {
|
||||
return Season.create({
|
||||
id: 'season-1',
|
||||
leagueId: overrides?.leagueId ?? 'league-1',
|
||||
gameId: 'iracing',
|
||||
name: 'Schedule Season',
|
||||
status: 'planned',
|
||||
startDate: new Date('2025-01-01T00:00:00Z'),
|
||||
endDate: new Date('2025-01-31T00:00:00Z'),
|
||||
});
|
||||
}
|
||||
|
||||
describe('DeleteLeagueSeasonScheduleRaceUseCase', () => {
|
||||
let seasonRepository: { findById: Mock };
|
||||
let raceRepository: { findById: Mock; delete: Mock };
|
||||
let logger: Logger;
|
||||
|
||||
beforeEach(() => {
|
||||
seasonRepository = { findById: vi.fn() };
|
||||
raceRepository = { findById: vi.fn(), delete: vi.fn() };
|
||||
logger = createLogger();
|
||||
});
|
||||
|
||||
it('deletes race when season belongs to league and race belongs to league', async () => {
|
||||
const season = createSeasonWithinWindow();
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
|
||||
const existing = Race.create({
|
||||
id: 'race-1',
|
||||
leagueId: 'league-1',
|
||||
track: 'Track',
|
||||
car: 'Car',
|
||||
scheduledAt: new Date('2025-01-05T20:00:00Z'),
|
||||
});
|
||||
raceRepository.findById.mockResolvedValue(existing);
|
||||
raceRepository.delete.mockResolvedValue(undefined);
|
||||
|
||||
const useCase = new DeleteLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
raceId: 'race-1',
|
||||
});
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(raceRepository.delete).toHaveBeenCalledTimes(1);
|
||||
expect(raceRepository.delete).toHaveBeenCalledWith('race-1');
|
||||
});
|
||||
|
||||
it('returns SEASON_NOT_FOUND when season does not belong to league and does not read/delete race', async () => {
|
||||
const season = createSeasonWithinWindow({ leagueId: 'other-league' });
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
|
||||
const useCase = new DeleteLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
raceId: 'race-1',
|
||||
});
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
DeleteLeagueSeasonScheduleRaceErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('SEASON_NOT_FOUND');
|
||||
expect(raceRepository.findById).not.toHaveBeenCalled();
|
||||
expect(raceRepository.delete).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns RACE_NOT_FOUND when race does not exist for league and does not delete', async () => {
|
||||
const season = createSeasonWithinWindow();
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
raceRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const useCase = new DeleteLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
raceId: 'race-404',
|
||||
});
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
DeleteLeagueSeasonScheduleRaceErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('RACE_NOT_FOUND');
|
||||
expect(raceRepository.delete).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns RACE_NOT_FOUND when race belongs to different league', async () => {
|
||||
const season = createSeasonWithinWindow();
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
|
||||
const existing = Race.create({
|
||||
id: 'race-1',
|
||||
leagueId: 'other-league',
|
||||
track: 'Track',
|
||||
car: 'Car',
|
||||
scheduledAt: new Date('2025-01-05T20:00:00Z'),
|
||||
});
|
||||
raceRepository.findById.mockResolvedValue(existing);
|
||||
|
||||
const useCase = new DeleteLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
raceId: 'race-1',
|
||||
});
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
DeleteLeagueSeasonScheduleRaceErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('RACE_NOT_FOUND');
|
||||
expect(raceRepository.delete).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns SEASON_NOT_FOUND when season does not exist', async () => {
|
||||
seasonRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const useCase = new DeleteLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
raceId: 'race-1',
|
||||
});
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
DeleteLeagueSeasonScheduleRaceErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('SEASON_NOT_FOUND');
|
||||
expect(raceRepository.findById).not.toHaveBeenCalled();
|
||||
expect(raceRepository.delete).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns REPOSITORY_ERROR when repository throws during find', async () => {
|
||||
const repositoryError = new Error('DB connection failed');
|
||||
seasonRepository.findById.mockRejectedValue(repositoryError);
|
||||
|
||||
const useCase = new DeleteLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
raceId: 'race-1',
|
||||
});
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
DeleteLeagueSeasonScheduleRaceErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('REPOSITORY_ERROR');
|
||||
expect(error.details.message).toBe('DB connection failed');
|
||||
});
|
||||
|
||||
it('returns REPOSITORY_ERROR when repository throws during delete', async () => {
|
||||
const season = createSeasonWithinWindow();
|
||||
const existing = Race.create({
|
||||
id: 'race-1',
|
||||
leagueId: 'league-1',
|
||||
track: 'Track',
|
||||
car: 'Car',
|
||||
scheduledAt: new Date('2025-01-05T20:00:00Z'),
|
||||
});
|
||||
const repositoryError = new Error('DB delete failed');
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
raceRepository.findById.mockResolvedValue(existing);
|
||||
raceRepository.delete.mockRejectedValue(repositoryError);
|
||||
|
||||
const useCase = new DeleteLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
raceId: 'race-1',
|
||||
});
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
DeleteLeagueSeasonScheduleRaceErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('REPOSITORY_ERROR');
|
||||
expect(error.details.message).toBe('DB delete failed');
|
||||
});
|
||||
|
||||
it('returns REPOSITORY_ERROR when repository throws during race find', async () => {
|
||||
const season = createSeasonWithinWindow();
|
||||
const repositoryError = new Error('DB connection failed');
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
raceRepository.findById.mockRejectedValue(repositoryError);
|
||||
|
||||
const useCase = new DeleteLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
raceId: 'race-1',
|
||||
});
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
DeleteLeagueSeasonScheduleRaceErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('REPOSITORY_ERROR');
|
||||
expect(error.details.message).toBe('DB connection failed');
|
||||
expect(raceRepository.delete).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,132 @@
|
||||
import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
import { Season } from '../../domain/entities/season/Season';
|
||||
import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
|
||||
|
||||
import {
|
||||
PublishLeagueSeasonScheduleUseCase,
|
||||
type PublishLeagueSeasonScheduleErrorCode,
|
||||
} from './PublishLeagueSeasonScheduleUseCase';
|
||||
|
||||
function createLogger(): Logger {
|
||||
return {
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
} as unknown as Logger;
|
||||
}
|
||||
|
||||
function createSeasonWithinWindow(overrides?: Partial<{ leagueId: string }>): Season {
|
||||
return Season.create({
|
||||
id: 'season-1',
|
||||
leagueId: overrides?.leagueId ?? 'league-1',
|
||||
gameId: 'iracing',
|
||||
name: 'Schedule Season',
|
||||
status: 'planned',
|
||||
startDate: new Date('2025-01-01T00:00:00Z'),
|
||||
endDate: new Date('2025-01-31T00:00:00Z'),
|
||||
});
|
||||
}
|
||||
|
||||
describe('PublishLeagueSeasonScheduleUseCase', () => {
|
||||
let seasonRepository: { findById: Mock; update: Mock };
|
||||
let logger: Logger;
|
||||
|
||||
beforeEach(() => {
|
||||
seasonRepository = { findById: vi.fn(), update: vi.fn() };
|
||||
logger = createLogger();
|
||||
});
|
||||
|
||||
it('publishes schedule deterministically (schedulePublished=true) and persists', async () => {
|
||||
const season = createSeasonWithinWindow();
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
seasonRepository.update.mockResolvedValue(undefined);
|
||||
|
||||
const useCase = new PublishLeagueSeasonScheduleUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({ leagueId: 'league-1', seasonId: 'season-1' });
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(seasonRepository.update).toHaveBeenCalledTimes(1);
|
||||
const updatedSeason = seasonRepository.update.mock.calls[0]?.[0] as Season;
|
||||
expect(updatedSeason.id).toBe('season-1');
|
||||
expect(updatedSeason.leagueId).toBe('league-1');
|
||||
expect(updatedSeason.schedulePublished).toBe(true);
|
||||
});
|
||||
|
||||
it('returns SEASON_NOT_FOUND when season does not belong to league and does not update', async () => {
|
||||
const season = createSeasonWithinWindow({ leagueId: 'other-league' });
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
|
||||
const useCase = new PublishLeagueSeasonScheduleUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({ leagueId: 'league-1', seasonId: 'season-1' });
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
PublishLeagueSeasonScheduleErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('SEASON_NOT_FOUND');
|
||||
expect(seasonRepository.update).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns SEASON_NOT_FOUND when season does not exist', async () => {
|
||||
seasonRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const useCase = new PublishLeagueSeasonScheduleUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({ leagueId: 'league-1', seasonId: 'season-1' });
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
PublishLeagueSeasonScheduleErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('SEASON_NOT_FOUND');
|
||||
expect(seasonRepository.update).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns REPOSITORY_ERROR when repository throws during find', async () => {
|
||||
const repositoryError = new Error('DB connection failed');
|
||||
seasonRepository.findById.mockRejectedValue(repositoryError);
|
||||
|
||||
const useCase = new PublishLeagueSeasonScheduleUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({ leagueId: 'league-1', seasonId: 'season-1' });
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
PublishLeagueSeasonScheduleErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('REPOSITORY_ERROR');
|
||||
expect(error.details.message).toBe('DB connection failed');
|
||||
});
|
||||
|
||||
it('returns REPOSITORY_ERROR when repository throws during update', async () => {
|
||||
const season = createSeasonWithinWindow();
|
||||
const repositoryError = new Error('DB write failed');
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
seasonRepository.update.mockRejectedValue(repositoryError);
|
||||
|
||||
const useCase = new PublishLeagueSeasonScheduleUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({ leagueId: 'league-1', seasonId: 'season-1' });
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
PublishLeagueSeasonScheduleErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('REPOSITORY_ERROR');
|
||||
expect(error.details.message).toBe('DB write failed');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,132 @@
|
||||
import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
import { Season } from '../../domain/entities/season/Season';
|
||||
import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
|
||||
|
||||
import {
|
||||
UnpublishLeagueSeasonScheduleUseCase,
|
||||
type UnpublishLeagueSeasonScheduleErrorCode,
|
||||
} from './UnpublishLeagueSeasonScheduleUseCase';
|
||||
|
||||
function createLogger(): Logger {
|
||||
return {
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
} as unknown as Logger;
|
||||
}
|
||||
|
||||
function createSeasonWithinWindow(overrides?: Partial<{ leagueId: string }>): Season {
|
||||
return Season.create({
|
||||
id: 'season-1',
|
||||
leagueId: overrides?.leagueId ?? 'league-1',
|
||||
gameId: 'iracing',
|
||||
name: 'Schedule Season',
|
||||
status: 'planned',
|
||||
startDate: new Date('2025-01-01T00:00:00Z'),
|
||||
endDate: new Date('2025-01-31T00:00:00Z'),
|
||||
});
|
||||
}
|
||||
|
||||
describe('UnpublishLeagueSeasonScheduleUseCase', () => {
|
||||
let seasonRepository: { findById: Mock; update: Mock };
|
||||
let logger: Logger;
|
||||
|
||||
beforeEach(() => {
|
||||
seasonRepository = { findById: vi.fn(), update: vi.fn() };
|
||||
logger = createLogger();
|
||||
});
|
||||
|
||||
it('unpublishes schedule deterministically (schedulePublished=false) and persists', async () => {
|
||||
const season = createSeasonWithinWindow().withSchedulePublished(true);
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
seasonRepository.update.mockResolvedValue(undefined);
|
||||
|
||||
const useCase = new UnpublishLeagueSeasonScheduleUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({ leagueId: 'league-1', seasonId: 'season-1' });
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(seasonRepository.update).toHaveBeenCalledTimes(1);
|
||||
const updatedSeason = seasonRepository.update.mock.calls[0]?.[0] as Season;
|
||||
expect(updatedSeason.id).toBe('season-1');
|
||||
expect(updatedSeason.leagueId).toBe('league-1');
|
||||
expect(updatedSeason.schedulePublished).toBe(false);
|
||||
});
|
||||
|
||||
it('returns SEASON_NOT_FOUND when season does not belong to league and does not update', async () => {
|
||||
const season = createSeasonWithinWindow({ leagueId: 'other-league' }).withSchedulePublished(true);
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
|
||||
const useCase = new UnpublishLeagueSeasonScheduleUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({ leagueId: 'league-1', seasonId: 'season-1' });
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
UnpublishLeagueSeasonScheduleErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('SEASON_NOT_FOUND');
|
||||
expect(seasonRepository.update).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns SEASON_NOT_FOUND when season does not exist', async () => {
|
||||
seasonRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const useCase = new UnpublishLeagueSeasonScheduleUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({ leagueId: 'league-1', seasonId: 'season-1' });
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
UnpublishLeagueSeasonScheduleErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('SEASON_NOT_FOUND');
|
||||
expect(seasonRepository.update).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns REPOSITORY_ERROR when repository throws during find', async () => {
|
||||
const repositoryError = new Error('DB connection failed');
|
||||
seasonRepository.findById.mockRejectedValue(repositoryError);
|
||||
|
||||
const useCase = new UnpublishLeagueSeasonScheduleUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({ leagueId: 'league-1', seasonId: 'season-1' });
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
UnpublishLeagueSeasonScheduleErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('REPOSITORY_ERROR');
|
||||
expect(error.details.message).toBe('DB connection failed');
|
||||
});
|
||||
|
||||
it('returns REPOSITORY_ERROR when repository throws during update', async () => {
|
||||
const season = createSeasonWithinWindow().withSchedulePublished(true);
|
||||
const repositoryError = new Error('DB write failed');
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
seasonRepository.update.mockRejectedValue(repositoryError);
|
||||
|
||||
const useCase = new UnpublishLeagueSeasonScheduleUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({ leagueId: 'league-1', seasonId: 'season-1' });
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
UnpublishLeagueSeasonScheduleErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('REPOSITORY_ERROR');
|
||||
expect(error.details.message).toBe('DB write failed');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,424 @@
|
||||
import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
import { Race } from '../../domain/entities/Race';
|
||||
import { Season } from '../../domain/entities/season/Season';
|
||||
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
||||
import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
|
||||
|
||||
import {
|
||||
UpdateLeagueSeasonScheduleRaceUseCase,
|
||||
type UpdateLeagueSeasonScheduleRaceErrorCode,
|
||||
} from './UpdateLeagueSeasonScheduleRaceUseCase';
|
||||
|
||||
function createLogger(): Logger {
|
||||
return {
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
} as unknown as Logger;
|
||||
}
|
||||
|
||||
function createSeasonWithinWindow(overrides?: Partial<{ leagueId: string }>): Season {
|
||||
return Season.create({
|
||||
id: 'season-1',
|
||||
leagueId: overrides?.leagueId ?? 'league-1',
|
||||
gameId: 'iracing',
|
||||
name: 'Schedule Season',
|
||||
status: 'planned',
|
||||
startDate: new Date('2025-01-01T00:00:00Z'),
|
||||
endDate: new Date('2025-01-31T00:00:00Z'),
|
||||
});
|
||||
}
|
||||
|
||||
describe('UpdateLeagueSeasonScheduleRaceUseCase', () => {
|
||||
let seasonRepository: { findById: Mock };
|
||||
let raceRepository: { findById: Mock; update: Mock };
|
||||
let logger: Logger;
|
||||
|
||||
beforeEach(() => {
|
||||
seasonRepository = { findById: vi.fn() };
|
||||
raceRepository = { findById: vi.fn(), update: vi.fn() };
|
||||
logger = createLogger();
|
||||
});
|
||||
|
||||
it('updates race when season belongs to league and updated scheduledAt stays within window', async () => {
|
||||
const season = createSeasonWithinWindow();
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
|
||||
const existing = Race.create({
|
||||
id: 'race-1',
|
||||
leagueId: 'league-1',
|
||||
track: 'Old Track',
|
||||
car: 'Old Car',
|
||||
scheduledAt: new Date('2025-01-05T20:00:00Z'),
|
||||
});
|
||||
raceRepository.findById.mockResolvedValue(existing);
|
||||
raceRepository.update.mockImplementation(async (race: Race) => race);
|
||||
|
||||
const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
logger);
|
||||
|
||||
const newScheduledAt = new Date('2025-01-20T20:00:00Z');
|
||||
const result = await useCase.execute({
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
raceId: 'race-1',
|
||||
track: 'New Track',
|
||||
car: 'New Car',
|
||||
scheduledAt: newScheduledAt,
|
||||
});
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(raceRepository.update).toHaveBeenCalledTimes(1);
|
||||
const updated = raceRepository.update.mock.calls[0]?.[0] as Race;
|
||||
expect(updated.id).toBe('race-1');
|
||||
expect(updated.leagueId).toBe('league-1');
|
||||
expect(updated.track).toBe('New Track');
|
||||
expect(updated.car).toBe('New Car');
|
||||
expect(updated.scheduledAt.getTime()).toBe(newScheduledAt.getTime());
|
||||
});
|
||||
|
||||
it('updates race with partial fields (only track)', async () => {
|
||||
const season = createSeasonWithinWindow();
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
|
||||
const existing = Race.create({
|
||||
id: 'race-1',
|
||||
leagueId: 'league-1',
|
||||
track: 'Old Track',
|
||||
car: 'Old Car',
|
||||
scheduledAt: new Date('2025-01-05T20:00:00Z'),
|
||||
});
|
||||
raceRepository.findById.mockResolvedValue(existing);
|
||||
raceRepository.update.mockImplementation(async (race: Race) => race);
|
||||
|
||||
const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
raceId: 'race-1',
|
||||
track: 'New Track',
|
||||
});
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(raceRepository.update).toHaveBeenCalledTimes(1);
|
||||
const updated = raceRepository.update.mock.calls[0]?.[0] as Race;
|
||||
expect(updated.track).toBe('New Track');
|
||||
expect(updated.car).toBe('Old Car'); // Unchanged
|
||||
expect(updated.scheduledAt.getTime()).toBe(existing.scheduledAt.getTime()); // Unchanged
|
||||
});
|
||||
|
||||
it('updates race with partial fields (only car)', async () => {
|
||||
const season = createSeasonWithinWindow();
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
|
||||
const existing = Race.create({
|
||||
id: 'race-1',
|
||||
leagueId: 'league-1',
|
||||
track: 'Old Track',
|
||||
car: 'Old Car',
|
||||
scheduledAt: new Date('2025-01-05T20:00:00Z'),
|
||||
});
|
||||
raceRepository.findById.mockResolvedValue(existing);
|
||||
raceRepository.update.mockImplementation(async (race: Race) => race);
|
||||
|
||||
const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
raceId: 'race-1',
|
||||
car: 'New Car',
|
||||
});
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(raceRepository.update).toHaveBeenCalledTimes(1);
|
||||
const updated = raceRepository.update.mock.calls[0]?.[0] as Race;
|
||||
expect(updated.track).toBe('Old Track'); // Unchanged
|
||||
expect(updated.car).toBe('New Car');
|
||||
expect(updated.scheduledAt.getTime()).toBe(existing.scheduledAt.getTime()); // Unchanged
|
||||
});
|
||||
|
||||
it('updates race with partial fields (only scheduledAt)', async () => {
|
||||
const season = createSeasonWithinWindow();
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
|
||||
const existing = Race.create({
|
||||
id: 'race-1',
|
||||
leagueId: 'league-1',
|
||||
track: 'Old Track',
|
||||
car: 'Old Car',
|
||||
scheduledAt: new Date('2025-01-05T20:00:00Z'),
|
||||
});
|
||||
raceRepository.findById.mockResolvedValue(existing);
|
||||
raceRepository.update.mockImplementation(async (race: Race) => race);
|
||||
|
||||
const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
logger);
|
||||
|
||||
const newScheduledAt = new Date('2025-01-15T20:00:00Z');
|
||||
const result = await useCase.execute({
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
raceId: 'race-1',
|
||||
scheduledAt: newScheduledAt,
|
||||
});
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(raceRepository.update).toHaveBeenCalledTimes(1);
|
||||
const updated = raceRepository.update.mock.calls[0]?.[0] as Race;
|
||||
expect(updated.track).toBe('Old Track'); // Unchanged
|
||||
expect(updated.car).toBe('Old Car'); // Unchanged
|
||||
expect(updated.scheduledAt.getTime()).toBe(newScheduledAt.getTime());
|
||||
});
|
||||
|
||||
it('returns SEASON_NOT_FOUND when season does not belong to league and does not read/update race', async () => {
|
||||
const season = createSeasonWithinWindow({ leagueId: 'other-league' });
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
|
||||
const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
raceId: 'race-1',
|
||||
track: 'New Track',
|
||||
});
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
UpdateLeagueSeasonScheduleRaceErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('SEASON_NOT_FOUND');
|
||||
expect(raceRepository.findById).not.toHaveBeenCalled();
|
||||
expect(raceRepository.update).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns RACE_OUTSIDE_SEASON_WINDOW when updated scheduledAt is outside window and does not update', async () => {
|
||||
const season = createSeasonWithinWindow();
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
|
||||
const existing = Race.create({
|
||||
id: 'race-1',
|
||||
leagueId: 'league-1',
|
||||
track: 'Old Track',
|
||||
car: 'Old Car',
|
||||
scheduledAt: new Date('2025-01-05T20:00:00Z'),
|
||||
});
|
||||
raceRepository.findById.mockResolvedValue(existing);
|
||||
|
||||
const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
raceId: 'race-1',
|
||||
scheduledAt: new Date('2025-02-01T00:00:01Z'),
|
||||
});
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
UpdateLeagueSeasonScheduleRaceErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('RACE_OUTSIDE_SEASON_WINDOW');
|
||||
expect(raceRepository.update).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns RACE_NOT_FOUND when race does not exist for league and does not update', async () => {
|
||||
const season = createSeasonWithinWindow();
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
raceRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
raceId: 'race-404',
|
||||
track: 'New Track',
|
||||
});
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
UpdateLeagueSeasonScheduleRaceErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('RACE_NOT_FOUND');
|
||||
expect(raceRepository.update).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns RACE_NOT_FOUND when race belongs to different league', async () => {
|
||||
const season = createSeasonWithinWindow();
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
|
||||
const existing = Race.create({
|
||||
id: 'race-1',
|
||||
leagueId: 'other-league',
|
||||
track: 'Track',
|
||||
car: 'Car',
|
||||
scheduledAt: new Date('2025-01-05T20:00:00Z'),
|
||||
});
|
||||
raceRepository.findById.mockResolvedValue(existing);
|
||||
|
||||
const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
raceId: 'race-1',
|
||||
track: 'New Track',
|
||||
});
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
UpdateLeagueSeasonScheduleRaceErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('RACE_NOT_FOUND');
|
||||
expect(raceRepository.update).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns INVALID_INPUT when Race.create throws due to invalid data', async () => {
|
||||
const season = createSeasonWithinWindow();
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
|
||||
const existing = Race.create({
|
||||
id: 'race-1',
|
||||
leagueId: 'league-1',
|
||||
track: 'Old Track',
|
||||
car: 'Old Car',
|
||||
scheduledAt: new Date('2025-01-05T20:00:00Z'),
|
||||
});
|
||||
raceRepository.findById.mockResolvedValue(existing);
|
||||
|
||||
const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
logger);
|
||||
|
||||
// Mock Race.create to throw
|
||||
const originalCreate = Race.create;
|
||||
Race.create = vi.fn().mockImplementation(() => {
|
||||
throw new Error('Invalid race data');
|
||||
});
|
||||
|
||||
const result = await useCase.execute({
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
raceId: 'race-1',
|
||||
track: '', // Invalid
|
||||
});
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
UpdateLeagueSeasonScheduleRaceErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('INVALID_INPUT');
|
||||
expect(raceRepository.update).not.toHaveBeenCalled();
|
||||
|
||||
// Restore original
|
||||
Race.create = originalCreate;
|
||||
});
|
||||
|
||||
it('returns REPOSITORY_ERROR when repository throws during find', async () => {
|
||||
const season = createSeasonWithinWindow();
|
||||
const repositoryError = new Error('DB connection failed');
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
raceRepository.findById.mockRejectedValue(repositoryError);
|
||||
|
||||
const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
raceId: 'race-1',
|
||||
track: 'New Track',
|
||||
});
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
UpdateLeagueSeasonScheduleRaceErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('REPOSITORY_ERROR');
|
||||
expect(error.details.message).toBe('DB connection failed');
|
||||
});
|
||||
|
||||
it('returns REPOSITORY_ERROR when repository throws during update', async () => {
|
||||
const season = createSeasonWithinWindow();
|
||||
const existing = Race.create({
|
||||
id: 'race-1',
|
||||
leagueId: 'league-1',
|
||||
track: 'Old Track',
|
||||
car: 'Old Car',
|
||||
scheduledAt: new Date('2025-01-05T20:00:00Z'),
|
||||
});
|
||||
const repositoryError = new Error('DB write failed');
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
raceRepository.findById.mockResolvedValue(existing);
|
||||
raceRepository.update.mockRejectedValue(repositoryError);
|
||||
|
||||
const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
raceId: 'race-1',
|
||||
track: 'New Track',
|
||||
});
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
UpdateLeagueSeasonScheduleRaceErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('REPOSITORY_ERROR');
|
||||
expect(error.details.message).toBe('DB write failed');
|
||||
});
|
||||
|
||||
it('returns SEASON_NOT_FOUND when season does not exist', async () => {
|
||||
seasonRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({
|
||||
leagueId: 'league-1',
|
||||
seasonId: 'season-1',
|
||||
raceId: 'race-1',
|
||||
track: 'New Track',
|
||||
});
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
UpdateLeagueSeasonScheduleRaceErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toBe('SEASON_NOT_FOUND');
|
||||
expect(raceRepository.findById).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user