stock-bot/libs/core/di/test/factories.test.ts
2025-06-25 11:38:23 -04:00

95 lines
3.2 KiB
TypeScript

import { asValue, createContainer } from 'awilix';
import type { AwilixContainer } from 'awilix';
import { describe, expect, it, mock } from 'bun:test';
import type { CacheProvider } from '@stock-bot/cache';
import type { ServiceDefinitions } from '../src/container/types';
import { CacheFactory } from '../src/factories';
describe('DI Factories', () => {
describe('CacheFactory', () => {
const mockCache: CacheProvider = {
get: mock(async () => null),
set: mock(async () => {}),
del: mock(async () => {}),
clear: mock(async () => {}),
has: mock(async () => false),
keys: mock(async () => []),
ttl: mock(async () => -1),
type: 'memory',
};
const createMockContainer = (
cache: CacheProvider | null = mockCache
): AwilixContainer<ServiceDefinitions> => {
const container = createContainer<ServiceDefinitions>();
container.register({
cache: asValue(cache),
});
return container;
};
it('should be exported', () => {
expect(CacheFactory).toBeDefined();
});
it('should create namespaced cache', () => {
const namespacedCache = CacheFactory.createNamespacedCache(mockCache, 'test-namespace');
expect(namespacedCache).toBeDefined();
expect(namespacedCache).toBeInstanceOf(Object);
// NamespacedCache wraps the base cache but doesn't expose type property
});
it('should create cache for service', () => {
const container = createMockContainer();
const serviceCache = CacheFactory.createCacheForService(container, 'test-service');
expect(serviceCache).toBeDefined();
expect(serviceCache).not.toBe(mockCache); // Should be a new namespaced instance
});
it('should return null when no base cache available', () => {
const container = createMockContainer(null);
const serviceCache = CacheFactory.createCacheForService(container, 'test-service');
expect(serviceCache).toBeNull();
});
it('should create cache for handler with prefix', () => {
const container = createMockContainer();
const handlerCache = CacheFactory.createCacheForHandler(container, 'TestHandler');
expect(handlerCache).toBeDefined();
// The namespace should include 'handler:' prefix
});
it('should create cache with custom prefix', () => {
const container = createMockContainer();
const prefixedCache = CacheFactory.createCacheWithPrefix(container, 'custom-prefix');
expect(prefixedCache).toBeDefined();
});
it('should clean duplicate cache: prefix', () => {
const container = createMockContainer();
// Should handle prefix that already includes 'cache:'
const prefixedCache = CacheFactory.createCacheWithPrefix(container, 'cache:custom-prefix');
expect(prefixedCache).toBeDefined();
// Internally it should strip the duplicate 'cache:' prefix
});
it('should handle null cache in all factory methods', () => {
const container = createMockContainer(null);
expect(CacheFactory.createCacheForService(container, 'service')).toBeNull();
expect(CacheFactory.createCacheForHandler(container, 'handler')).toBeNull();
expect(CacheFactory.createCacheWithPrefix(container, 'prefix')).toBeNull();
});
});
});