182 lines
6.4 KiB
TypeScript
182 lines
6.4 KiB
TypeScript
import { vi, describe, it, expect, beforeEach } from 'vitest';
|
|
import { InMemorySeasonRepository } from './InMemorySeasonRepository';
|
|
import { Season } from '@core/racing/domain/entities/season/Season';
|
|
import type { Logger } from '@core/shared/application';
|
|
|
|
describe('InMemorySeasonRepository', () => {
|
|
let repository: InMemorySeasonRepository;
|
|
let mockLogger: Logger;
|
|
|
|
beforeEach(() => {
|
|
mockLogger = {
|
|
debug: vi.fn(),
|
|
info: vi.fn(),
|
|
warn: vi.fn(),
|
|
error: vi.fn(),
|
|
};
|
|
repository = new InMemorySeasonRepository(mockLogger);
|
|
});
|
|
|
|
const createTestSeason = (
|
|
id: string,
|
|
leagueId: string,
|
|
name: string = 'Test Season',
|
|
status: 'planned' | 'active' | 'completed' = 'planned'
|
|
) => {
|
|
return Season.create({
|
|
id,
|
|
leagueId,
|
|
gameId: 'iracing',
|
|
name,
|
|
status,
|
|
});
|
|
};
|
|
|
|
describe('constructor', () => {
|
|
it('should initialize with a logger', () => {
|
|
expect(repository).toBeDefined();
|
|
expect(mockLogger.info).toHaveBeenCalledWith('InMemorySeasonRepository initialized.');
|
|
});
|
|
});
|
|
|
|
describe('findById', () => {
|
|
it('should return null if season not found', async () => {
|
|
const result = await repository.findById('nonexistent');
|
|
expect(result).toBeNull();
|
|
expect(mockLogger.debug).toHaveBeenCalledWith('[InMemorySeasonRepository] Finding season by ID: nonexistent');
|
|
expect(mockLogger.warn).toHaveBeenCalledWith('Season with ID nonexistent not found.');
|
|
});
|
|
|
|
it('should return the season if found', async () => {
|
|
const season = createTestSeason('1', 'league1');
|
|
await repository.create(season);
|
|
|
|
const result = await repository.findById('1');
|
|
expect(result).toEqual(season);
|
|
expect(mockLogger.info).toHaveBeenCalledWith('Found season by ID: 1.');
|
|
});
|
|
});
|
|
|
|
describe('findByLeagueId', () => {
|
|
it('should return seasons filtered by league ID', async () => {
|
|
const season1 = createTestSeason('1', 'league1');
|
|
const season2 = createTestSeason('2', 'league2');
|
|
const season3 = createTestSeason('3', 'league1');
|
|
await repository.create(season1);
|
|
await repository.create(season2);
|
|
await repository.create(season3);
|
|
|
|
const result = await repository.findByLeagueId('league1');
|
|
expect(result).toHaveLength(2);
|
|
expect(result).toContain(season1);
|
|
expect(result).toContain(season3);
|
|
});
|
|
});
|
|
|
|
describe('create', () => {
|
|
it('should create a new season', async () => {
|
|
const season = createTestSeason('1', 'league1');
|
|
const result = await repository.create(season);
|
|
expect(result).toEqual(season);
|
|
expect(mockLogger.info).toHaveBeenCalledWith('Season 1 created successfully.');
|
|
});
|
|
|
|
it('should throw error if season already exists', async () => {
|
|
const season = createTestSeason('1', 'league1');
|
|
await repository.create(season);
|
|
await expect(repository.create(season)).rejects.toThrow('Season already exists');
|
|
});
|
|
});
|
|
|
|
describe('add', () => {
|
|
it('should add a new season', async () => {
|
|
const season = createTestSeason('1', 'league1');
|
|
await repository.add(season);
|
|
const found = await repository.findById('1');
|
|
expect(found).toEqual(season);
|
|
expect(mockLogger.info).toHaveBeenCalledWith('Season 1 added successfully.');
|
|
});
|
|
|
|
it('should throw error if season already exists', async () => {
|
|
const season = createTestSeason('1', 'league1');
|
|
await repository.add(season);
|
|
await expect(repository.add(season)).rejects.toThrow('Season already exists');
|
|
});
|
|
});
|
|
|
|
describe('update', () => {
|
|
it('should update an existing season', async () => {
|
|
const season = createTestSeason('1', 'league1');
|
|
await repository.create(season);
|
|
|
|
const updatedSeason = Season.create({
|
|
id: season.id,
|
|
leagueId: season.leagueId.toString(),
|
|
gameId: season.gameId.toString(),
|
|
name: 'Updated Season',
|
|
status: season.status.toString() as 'planned' | 'active' | 'completed',
|
|
year: 2025,
|
|
order: 1,
|
|
startDate: new Date('2025-01-01'),
|
|
endDate: new Date('2025-12-31'),
|
|
});
|
|
await repository.update(updatedSeason);
|
|
const found = await repository.findById('1');
|
|
expect(found?.name).toBe('Updated Season');
|
|
expect(mockLogger.info).toHaveBeenCalledWith('Season 1 updated successfully.');
|
|
});
|
|
|
|
it('should throw error if season does not exist', async () => {
|
|
const season = createTestSeason('1', 'league1');
|
|
await expect(repository.update(season)).rejects.toThrow('Season not found');
|
|
});
|
|
});
|
|
|
|
describe('delete', () => {
|
|
it('should delete an existing season', async () => {
|
|
const season = createTestSeason('1', 'league1');
|
|
await repository.create(season);
|
|
|
|
await repository.delete('1');
|
|
expect(mockLogger.info).toHaveBeenCalledWith('Season 1 deleted successfully.');
|
|
const found = await repository.findById('1');
|
|
expect(found).toBeNull();
|
|
});
|
|
|
|
it('should not throw if season does not exist', async () => {
|
|
await repository.delete('nonexistent');
|
|
expect(mockLogger.warn).toHaveBeenCalledWith('Season with ID nonexistent not found for deletion.');
|
|
});
|
|
});
|
|
|
|
describe('listByLeague', () => {
|
|
it('should list seasons by league ID', async () => {
|
|
const season1 = createTestSeason('1', 'league1');
|
|
const season2 = createTestSeason('2', 'league2');
|
|
const season3 = createTestSeason('3', 'league1');
|
|
await repository.create(season1);
|
|
await repository.create(season2);
|
|
await repository.create(season3);
|
|
|
|
const result = await repository.listByLeague('league1');
|
|
expect(result).toHaveLength(2);
|
|
expect(result).toContain(season1);
|
|
expect(result).toContain(season3);
|
|
});
|
|
});
|
|
|
|
describe('listActiveByLeague', () => {
|
|
it('should list active seasons by league ID', async () => {
|
|
const activeSeason = createTestSeason('1', 'league1', 'Active Season', 'active');
|
|
const plannedSeason = createTestSeason('2', 'league1', 'Planned Season', 'planned');
|
|
const otherLeagueSeason = createTestSeason('3', 'league2', 'Other League', 'active');
|
|
await repository.create(activeSeason);
|
|
await repository.create(plannedSeason);
|
|
await repository.create(otherLeagueSeason);
|
|
|
|
const result = await repository.listActiveByLeague('league1');
|
|
expect(result).toHaveLength(1);
|
|
expect(result).toContain(activeSeason);
|
|
});
|
|
});
|
|
}); |