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

117 lines
4.7 KiB
TypeScript

import { vi, describe, it, expect, beforeEach } from 'vitest';
import { InMemorySponsorshipPricingRepository } from './InMemorySponsorshipPricingRepository';
import { SponsorshipPricing } from '@core/racing/domain/value-objects/SponsorshipPricing';
import type { Logger } from '@core/shared/application';
describe('InMemorySponsorshipPricingRepository', () => {
let repository: InMemorySponsorshipPricingRepository;
let mockLogger: Logger;
beforeEach(() => {
mockLogger = {
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
};
repository = new InMemorySponsorshipPricingRepository(mockLogger);
});
const createTestPricing = (acceptingApplications: boolean = true) => {
return SponsorshipPricing.create({ acceptingApplications });
};
describe('constructor', () => {
it('should initialize with a logger', () => {
expect(repository).toBeDefined();
expect(mockLogger.info).toHaveBeenCalledWith('InMemorySponsorshipPricingRepository initialized.');
});
});
describe('findByEntity', () => {
it('should return null if pricing not found', async () => {
const result = await repository.findByEntity('team', 'nonexistent');
expect(result).toBeNull();
expect(mockLogger.debug).toHaveBeenCalledWith('Finding sponsorship pricing for entity: team, nonexistent');
expect(mockLogger.warn).toHaveBeenCalledWith('Sponsorship pricing for entity team, nonexistent not found.');
});
it('should return the pricing if found', async () => {
const pricing = createTestPricing();
await repository.save('team', '1', pricing);
const result = await repository.findByEntity('team', '1');
expect(result).toEqual(pricing);
expect(mockLogger.info).toHaveBeenCalledWith('Found sponsorship pricing for entity: team, 1.');
});
});
describe('save', () => {
it('should save new pricing', async () => {
const pricing = createTestPricing();
await repository.save('team', '1', pricing);
expect(mockLogger.info).toHaveBeenCalledWith('Creating new sponsorship pricing for entity: team, 1.');
const found = await repository.findByEntity('team', '1');
expect(found).toEqual(pricing);
});
it('should update existing pricing', async () => {
const pricing1 = createTestPricing(true);
const pricing2 = createTestPricing(false);
await repository.save('team', '1', pricing1);
await repository.save('team', '1', pricing2);
expect(mockLogger.info).toHaveBeenCalledWith('Updating existing sponsorship pricing for entity: team, 1.');
const found = await repository.findByEntity('team', '1');
expect(found).toEqual(pricing2);
});
});
describe('delete', () => {
it('should delete existing pricing', async () => {
const pricing = createTestPricing();
await repository.save('team', '1', pricing);
await repository.delete('team', '1');
expect(mockLogger.info).toHaveBeenCalledWith('Sponsorship pricing deleted for entity: team, 1.');
const found = await repository.findByEntity('team', '1');
expect(found).toBeNull();
});
it('should not throw error if pricing does not exist', async () => {
await repository.delete('team', 'nonexistent');
expect(mockLogger.warn).toHaveBeenCalledWith('Sponsorship pricing for entity team, nonexistent not found for deletion.');
});
});
describe('exists', () => {
it('should return true if pricing exists', async () => {
const pricing = createTestPricing();
await repository.save('team', '1', pricing);
const result = await repository.exists('team', '1');
expect(result).toBe(true);
});
it('should return false if pricing does not exist', async () => {
const result = await repository.exists('team', 'nonexistent');
expect(result).toBe(false);
});
});
describe('findAcceptingApplications', () => {
it('should return entities accepting applications', async () => {
const accepting = createTestPricing(true);
const notAccepting = createTestPricing(false);
await repository.save('team', '1', accepting);
await repository.save('team', '2', notAccepting);
await repository.save('driver', '3', accepting);
const result = await repository.findAcceptingApplications('team');
expect(result).toHaveLength(1);
expect(result[0]).toEqual({ entityId: '1', pricing: accepting });
});
it('should return empty array if no entities accepting', async () => {
const notAccepting = createTestPricing(false);
await repository.save('team', '1', notAccepting);
const result = await repository.findAcceptingApplications('team');
expect(result).toEqual([]);
});
});
});