This commit is contained in:
Boki 2025-06-25 11:38:23 -04:00
parent 3a7254708e
commit b63e58784c
41 changed files with 5762 additions and 4477 deletions

View file

@ -1,9 +1,9 @@
import { describe, expect, it, mock } from 'bun:test';
import { createContainer, asValue } from 'awilix';
import { asValue, createContainer } from 'awilix';
import type { AwilixContainer } from 'awilix';
import { CacheFactory } from '../src/factories';
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', () => {
@ -18,7 +18,9 @@ describe('DI Factories', () => {
type: 'memory',
};
const createMockContainer = (cache: CacheProvider | null = mockCache): AwilixContainer<ServiceDefinitions> => {
const createMockContainer = (
cache: CacheProvider | null = mockCache
): AwilixContainer<ServiceDefinitions> => {
const container = createContainer<ServiceDefinitions>();
container.register({
cache: asValue(cache),
@ -32,7 +34,7 @@ describe('DI Factories', () => {
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
@ -40,54 +42,54 @@ describe('DI Factories', () => {
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();
});
});
});
});