removed some old code
This commit is contained in:
parent
bd26ecf3bc
commit
c5f6b37022
11 changed files with 1 additions and 767 deletions
|
|
@ -1,47 +0,0 @@
|
|||
import type { AwilixContainer } from 'awilix';
|
||||
import { NamespacedCache, type CacheProvider } from '@stock-bot/cache';
|
||||
import type { ServiceDefinitions } from '../container/types';
|
||||
|
||||
export class CacheFactory {
|
||||
static createNamespacedCache(baseCache: CacheProvider, namespace: string): NamespacedCache {
|
||||
return new NamespacedCache(baseCache, namespace);
|
||||
}
|
||||
|
||||
static createCacheForService(
|
||||
container: AwilixContainer<ServiceDefinitions>,
|
||||
serviceName: string
|
||||
): CacheProvider | null {
|
||||
const baseCache = container.cradle.cache;
|
||||
if (!baseCache) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.createNamespacedCache(baseCache, serviceName);
|
||||
}
|
||||
|
||||
static createCacheForHandler(
|
||||
container: AwilixContainer<ServiceDefinitions>,
|
||||
handlerName: string
|
||||
): CacheProvider | null {
|
||||
const baseCache = container.cradle.cache;
|
||||
if (!baseCache) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.createNamespacedCache(baseCache, `handler:${handlerName}`);
|
||||
}
|
||||
|
||||
static createCacheWithPrefix(
|
||||
container: AwilixContainer<ServiceDefinitions>,
|
||||
prefix: string
|
||||
): CacheProvider | null {
|
||||
const baseCache = container.cradle.cache;
|
||||
if (!baseCache) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Remove 'cache:' prefix if already included
|
||||
const cleanPrefix = prefix.replace(/^cache:/, '');
|
||||
return this.createNamespacedCache(baseCache, cleanPrefix);
|
||||
}
|
||||
}
|
||||
|
|
@ -1 +1 @@
|
|||
export { CacheFactory } from './cache.factory';
|
||||
// This directory is reserved for future factory implementations
|
||||
|
|
@ -18,5 +18,3 @@ export {
|
|||
queueConfigSchema
|
||||
} from './config/schemas';
|
||||
|
||||
// Export factories
|
||||
export { CacheFactory } from './factories';
|
||||
|
|
|
|||
|
|
@ -1,95 +0,0 @@
|
|||
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();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -28,9 +28,6 @@ describe('DI Package Exports', () => {
|
|||
expect(diExports.HandlerScanner).toBeDefined();
|
||||
});
|
||||
|
||||
it('should export factories', () => {
|
||||
expect(diExports.CacheFactory).toBeDefined();
|
||||
});
|
||||
|
||||
it('should export schemas', () => {
|
||||
expect(diExports.appConfigSchema).toBeDefined();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue