tests
This commit is contained in:
parent
54f37f9521
commit
3a7254708e
19 changed files with 1560 additions and 1237 deletions
|
|
@ -1,46 +1,93 @@
|
|||
import { describe, expect, it } from 'bun:test';
|
||||
import { describe, expect, it, mock } from 'bun:test';
|
||||
import { createContainer, asValue } from 'awilix';
|
||||
import type { AwilixContainer } from 'awilix';
|
||||
import { CacheFactory } from '../src/factories';
|
||||
import type { CacheProvider } from '@stock-bot/cache';
|
||||
import type { ServiceDefinitions } from '../src/container/types';
|
||||
|
||||
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 cache with configuration', () => {
|
||||
const cacheConfig = {
|
||||
redisConfig: {
|
||||
host: 'localhost',
|
||||
port: 6379,
|
||||
db: 1,
|
||||
},
|
||||
keyPrefix: 'test:',
|
||||
};
|
||||
|
||||
const cache = CacheFactory.create(cacheConfig);
|
||||
expect(cache).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 null cache without config', () => {
|
||||
const cache = CacheFactory.create();
|
||||
expect(cache).toBeDefined();
|
||||
expect(cache.type).toBe('null');
|
||||
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 create cache with logger', () => {
|
||||
const mockLogger = {
|
||||
info: () => {},
|
||||
error: () => {},
|
||||
warn: () => {},
|
||||
debug: () => {},
|
||||
};
|
||||
it('should return null when no base cache available', () => {
|
||||
const container = createMockContainer(null);
|
||||
|
||||
const serviceCache = CacheFactory.createCacheForService(container, 'test-service');
|
||||
|
||||
expect(serviceCache).toBeNull();
|
||||
});
|
||||
|
||||
const cacheConfig = {
|
||||
logger: mockLogger,
|
||||
};
|
||||
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
|
||||
});
|
||||
|
||||
const cache = CacheFactory.create(cacheConfig);
|
||||
expect(cache).toBeDefined();
|
||||
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();
|
||||
});
|
||||
});
|
||||
})
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue