Files
gridpilot.gg/adapters/racing/persistence/inmemory/InMemoryDriverRepository.test.ts
2025-12-17 12:05:00 +01:00

191 lines
6.9 KiB
TypeScript

import { vi, describe, it, expect, beforeEach } from 'vitest';
import { InMemoryDriverRepository } from './InMemoryDriverRepository';
import { Driver } from '@core/racing/domain/entities/Driver';
import type { Logger } from '@core/shared/application';
describe('InMemoryDriverRepository', () => {
let repository: InMemoryDriverRepository;
let mockLogger: Logger;
beforeEach(() => {
mockLogger = {
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
};
repository = new InMemoryDriverRepository(mockLogger);
});
const createTestDriver = (id: string, iracingId: string, name: string, country: string) => {
return Driver.create({
id,
iracingId,
name,
country,
});
};
describe('constructor', () => {
it('should initialize with a logger', () => {
expect(repository).toBeDefined();
expect(mockLogger.info).toHaveBeenCalledWith('InMemoryDriverRepository initialized.');
});
});
describe('findById', () => {
it('should return null if driver not found', async () => {
const result = await repository.findById('nonexistent');
expect(result).toBeNull();
expect(mockLogger.debug).toHaveBeenCalledWith('[InMemoryDriverRepository] Finding driver by ID: nonexistent');
expect(mockLogger.warn).toHaveBeenCalledWith('Driver with ID nonexistent not found.');
});
it('should return the driver if found', async () => {
const driver = createTestDriver('1', '12345', 'Test Driver', 'US');
await repository.create(driver);
const result = await repository.findById('1');
expect(result).toEqual(driver);
expect(mockLogger.info).toHaveBeenCalledWith('Found driver by ID: 1.');
});
});
describe('findByIRacingId', () => {
it('should return null if driver not found', async () => {
const result = await repository.findByIRacingId('nonexistent');
expect(result).toBeNull();
expect(mockLogger.debug).toHaveBeenCalledWith('[InMemoryDriverRepository] Finding driver by iRacing ID: nonexistent');
expect(mockLogger.warn).toHaveBeenCalledWith('Driver with iRacing ID nonexistent not found.');
});
it('should return the driver if found', async () => {
const driver = createTestDriver('1', '12345', 'Test Driver', 'US');
await repository.create(driver);
const result = await repository.findByIRacingId('12345');
expect(result).toEqual(driver);
});
});
describe('findAll', () => {
it('should return all drivers', async () => {
const driver1 = createTestDriver('1', '12345', 'Driver 1', 'US');
const driver2 = createTestDriver('2', '67890', 'Driver 2', 'UK');
await repository.create(driver1);
await repository.create(driver2);
const result = await repository.findAll();
expect(result).toHaveLength(2);
expect(result).toContain(driver1);
expect(result).toContain(driver2);
});
});
describe('create', () => {
it('should create a new driver', async () => {
const driver = createTestDriver('1', '12345', 'Test Driver', 'US');
const result = await repository.create(driver);
expect(result).toEqual(driver);
expect(mockLogger.info).toHaveBeenCalledWith('Driver 1 created successfully.');
});
it('should throw error if driver already exists', async () => {
const driver = createTestDriver('1', '12345', 'Test Driver', 'US');
await repository.create(driver);
await expect(repository.create(driver)).rejects.toThrow('Driver already exists');
});
it('should throw error if iRacing ID already registered', async () => {
const driver1 = createTestDriver('1', '12345', 'Driver 1', 'US');
const driver2 = createTestDriver('2', '12345', 'Driver 2', 'UK');
await repository.create(driver1);
await expect(repository.create(driver2)).rejects.toThrow('iRacing ID already registered');
});
});
describe('update', () => {
it('should update an existing driver', async () => {
const driver = createTestDriver('1', '12345', 'Test Driver', 'US');
await repository.create(driver);
const updatedDriver = driver.update({ name: 'Updated Driver' });
const result = await repository.update(updatedDriver);
expect(result).toEqual(updatedDriver);
expect(mockLogger.info).toHaveBeenCalledWith('Driver 1 updated successfully.');
});
it('should throw error if driver does not exist', async () => {
const driver = createTestDriver('1', '12345', 'Test Driver', 'US');
await expect(repository.update(driver)).rejects.toThrow('Driver not found');
});
it('should re-index iRacing ID if changed', async () => {
const driver = createTestDriver('1', '12345', 'Test Driver', 'US');
await repository.create(driver);
const updatedDriver = Driver.create({
id: '1',
iracingId: '67890',
name: 'Test Driver',
country: 'US',
});
await repository.update(updatedDriver);
const foundByOldId = await repository.findByIRacingId('12345');
expect(foundByOldId).toBeNull();
const foundByNewId = await repository.findByIRacingId('67890');
expect(foundByNewId).toEqual(updatedDriver);
});
});
describe('delete', () => {
it('should delete an existing driver', async () => {
const driver = createTestDriver('1', '12345', 'Test Driver', 'US');
await repository.create(driver);
await repository.delete('1');
expect(mockLogger.info).toHaveBeenCalledWith('Driver 1 deleted successfully.');
const found = await repository.findById('1');
expect(found).toBeNull();
const foundByIRacingId = await repository.findByIRacingId('12345');
expect(foundByIRacingId).toBeNull();
});
it('should not throw error if driver does not exist', async () => {
await repository.delete('nonexistent');
expect(mockLogger.warn).toHaveBeenCalledWith('Driver with ID nonexistent not found for deletion.');
});
});
describe('exists', () => {
it('should return true if driver exists', async () => {
const driver = createTestDriver('1', '12345', 'Test Driver', 'US');
await repository.create(driver);
const result = await repository.exists('1');
expect(result).toBe(true);
});
it('should return false if driver does not exist', async () => {
const result = await repository.exists('nonexistent');
expect(result).toBe(false);
});
});
describe('existsByIRacingId', () => {
it('should return true if iRacing ID exists', async () => {
const driver = createTestDriver('1', '12345', 'Test Driver', 'US');
await repository.create(driver);
const result = await repository.existsByIRacingId('12345');
expect(result).toBe(true);
});
it('should return false if iRacing ID does not exist', async () => {
const result = await repository.existsByIRacingId('nonexistent');
expect(result).toBe(false);
});
});
});