removed some old code

This commit is contained in:
Boki 2025-06-26 18:17:09 -04:00
parent bd26ecf3bc
commit c5f6b37022
11 changed files with 1 additions and 767 deletions

View file

@ -1,75 +0,0 @@
import { afterEach, beforeEach, describe, expect, it, mock } from 'bun:test';
import type { IServiceContainer } from '@stock-bot/types';
import { BaseHandler } from '../src/base/BaseHandler';
import { autoRegisterHandlers, createAutoHandlerRegistry } from '../src/registry/auto-register';
describe('Auto Registration - Simple Tests', () => {
describe('autoRegisterHandlers', () => {
it('should return empty results for non-existent directory', async () => {
const mockServices = {} as IServiceContainer;
const result = await autoRegisterHandlers('./non-existent-directory', mockServices);
expect(result.registered).toEqual([]);
expect(result.failed).toEqual([]);
});
it('should handle directory with no handler files', async () => {
const mockServices = {} as IServiceContainer;
// Use the test directory itself which has no handler files
const result = await autoRegisterHandlers('./test', mockServices);
expect(result.registered).toEqual([]);
expect(result.failed).toEqual([]);
});
it('should support dry run mode', async () => {
const mockServices = {} as IServiceContainer;
const result = await autoRegisterHandlers('./non-existent', mockServices, { dryRun: true });
expect(result.registered).toEqual([]);
expect(result.failed).toEqual([]);
});
it('should handle excluded patterns', async () => {
const mockServices = {} as IServiceContainer;
const result = await autoRegisterHandlers('./test', mockServices, {
exclude: ['test'],
});
expect(result.registered).toEqual([]);
expect(result.failed).toEqual([]);
});
it('should accept custom pattern', async () => {
const mockServices = {} as IServiceContainer;
const result = await autoRegisterHandlers('./test', mockServices, {
pattern: '.custom.',
});
expect(result.registered).toEqual([]);
expect(result.failed).toEqual([]);
});
});
describe('createAutoHandlerRegistry', () => {
it('should create registry with registerDirectory method', () => {
const mockServices = {} as IServiceContainer;
const registry = createAutoHandlerRegistry(mockServices);
expect(registry).toHaveProperty('registerDirectory');
expect(registry).toHaveProperty('registerDirectories');
expect(typeof registry.registerDirectory).toBe('function');
expect(typeof registry.registerDirectories).toBe('function');
});
it('should register from multiple directories', async () => {
const mockServices = {} as IServiceContainer;
const registry = createAutoHandlerRegistry(mockServices);
const result = await registry.registerDirectories(['./non-existent-1', './non-existent-2']);
expect(result.registered).toEqual([]);
expect(result.failed).toEqual([]);
});
});
});

View file

@ -1,204 +0,0 @@
import { describe, expect, it, mock } from 'bun:test';
import { BaseHandler } from '../src/base/BaseHandler';
// Test the internal functions by mocking module imports
describe('Auto Registration Unit Tests', () => {
describe('extractHandlerClasses', () => {
it('should extract handler classes from module', () => {
// Test handler class
class TestHandler extends BaseHandler {}
class AnotherHandler extends BaseHandler {}
class NotAHandler {}
const module = {
TestHandler,
AnotherHandler,
NotAHandler,
someFunction: () => {},
someVariable: 42,
};
// Access the private function through module internals
const autoRegister = require('../src/registry/auto-register');
// Mock the extractHandlerClasses function behavior
const handlers = [];
for (const key of Object.keys(module)) {
const exported = module[key];
if (
typeof exported === 'function' &&
exported.prototype &&
exported.prototype instanceof BaseHandler
) {
handlers.push(exported);
}
}
expect(handlers).toHaveLength(2);
expect(handlers).toContain(TestHandler);
expect(handlers).toContain(AnotherHandler);
expect(handlers).not.toContain(NotAHandler);
});
});
describe('findHandlerFiles', () => {
it('should filter files by pattern', () => {
const files = [
'test.handler.ts',
'test.service.ts',
'another.handler.ts',
'test.handler.js',
'.hidden.handler.ts',
];
const pattern = '.handler.';
const filtered = files.filter(
file => file.includes(pattern) && file.endsWith('.ts') && !file.startsWith('.')
);
expect(filtered).toEqual(['test.handler.ts', 'another.handler.ts']);
});
it('should handle different patterns', () => {
const files = ['test.handler.ts', 'test.custom.ts', 'another.custom.ts'];
const customPattern = '.custom.';
const filtered = files.filter(file => file.includes(customPattern) && file.endsWith('.ts'));
expect(filtered).toEqual(['test.custom.ts', 'another.custom.ts']);
});
});
describe('Handler Registration Logic', () => {
it('should skip disabled handlers', () => {
class DisabledHandler extends BaseHandler {
static __disabled = true;
}
class EnabledHandler extends BaseHandler {}
const handlers = [DisabledHandler, EnabledHandler];
const registered = handlers.filter(h => !(h as any).__disabled);
expect(registered).toHaveLength(1);
expect(registered).toContain(EnabledHandler);
expect(registered).not.toContain(DisabledHandler);
});
it('should handle handler with auto-registration flag', () => {
class AutoRegisterHandler extends BaseHandler {
static __handlerName = 'auto-handler';
static __needsAutoRegistration = true;
}
expect((AutoRegisterHandler as any).__needsAutoRegistration).toBe(true);
expect((AutoRegisterHandler as any).__handlerName).toBe('auto-handler');
});
it('should create handler instance with services', () => {
const mockServices = {
cache: null,
globalCache: null,
queueManager: null,
proxy: null,
browser: null,
mongodb: null,
postgres: null,
questdb: null,
} as any;
class TestHandler extends BaseHandler {}
const instance = new TestHandler(mockServices);
expect(instance).toBeInstanceOf(BaseHandler);
});
});
describe('Error Handling', () => {
it('should handle module import errors gracefully', () => {
const errors = [];
const modules = ['valid', 'error', 'another'];
for (const mod of modules) {
try {
if (mod === 'error') {
throw new Error('Module not found');
}
// Process module
} catch (error) {
errors.push(mod);
}
}
expect(errors).toEqual(['error']);
});
it('should handle filesystem errors', () => {
let result;
try {
// Simulate filesystem error
throw new Error('EACCES: permission denied');
} catch (error) {
// Should handle gracefully
result = { registered: [], failed: [] };
}
expect(result).toEqual({ registered: [], failed: [] });
});
});
describe('Options Handling', () => {
it('should apply exclude patterns', () => {
const files = ['test.handler.ts', 'excluded.handler.ts', 'another.handler.ts'];
const exclude = ['excluded'];
const filtered = files.filter(file => !exclude.some(ex => file.includes(ex)));
expect(filtered).toEqual(['test.handler.ts', 'another.handler.ts']);
});
it('should handle service name option', () => {
const options = {
pattern: '.handler.',
exclude: [],
dryRun: false,
serviceName: 'test-service',
};
expect(options.serviceName).toBe('test-service');
});
it('should handle dry run mode', () => {
const options = { dryRun: true };
const actions = [];
if (options.dryRun) {
actions.push('[DRY RUN] Would register handler');
} else {
actions.push('Registering handler');
}
expect(actions).toEqual(['[DRY RUN] Would register handler']);
});
});
describe('Registry Methods', () => {
it('should handle multiple directories', () => {
const directories = ['./dir1', './dir2', './dir3'];
const results = {
registered: [] as string[],
failed: [] as string[],
};
for (const dir of directories) {
// Simulate processing each directory
results.registered.push(`${dir}-handler`);
}
expect(results.registered).toHaveLength(3);
expect(results.registered).toContain('./dir1-handler');
expect(results.registered).toContain('./dir2-handler');
expect(results.registered).toContain('./dir3-handler');
});
});
});

View file

@ -1,138 +0,0 @@
import { beforeEach, describe, expect, it, mock } from 'bun:test';
import type { IServiceContainer } from '@stock-bot/types';
import { BaseHandler } from '../src/base/BaseHandler';
import { autoRegisterHandlers, createAutoHandlerRegistry } from '../src/registry/auto-register';
describe('Auto Registration', () => {
describe('autoRegisterHandlers', () => {
it('should auto-register handlers', async () => {
const mockServices = {} as IServiceContainer;
// Using a directory that doesn't exist - the function handles this gracefully
const result = await autoRegisterHandlers('./non-existent', mockServices);
expect(result).toHaveProperty('registered');
expect(result).toHaveProperty('failed');
expect(result.registered).toEqual([]);
expect(result.failed).toEqual([]);
});
it('should use default options when not provided', async () => {
const mockServices = {} as IServiceContainer;
const result = await autoRegisterHandlers('./test', mockServices);
expect(result).toBeDefined();
expect(result.registered).toBeInstanceOf(Array);
expect(result.failed).toBeInstanceOf(Array);
});
it('should handle directory not found gracefully', async () => {
const mockServices = {} as IServiceContainer;
// Should not throw for non-existent directory
const result = await autoRegisterHandlers('./non-existent-directory', mockServices);
expect(result.registered).toEqual([]);
expect(result.failed).toEqual([]);
});
});
describe('createAutoHandlerRegistry', () => {
it('should create a registry with registerDirectory method', () => {
const mockServices = {} as IServiceContainer;
const registry = createAutoHandlerRegistry(mockServices);
expect(registry).toHaveProperty('registerDirectory');
expect(typeof registry.registerDirectory).toBe('function');
});
it('should register from a directory', async () => {
const mockServices = {} as IServiceContainer;
const registry = createAutoHandlerRegistry(mockServices);
const result = await registry.registerDirectory('./non-existent-dir');
expect(result).toHaveProperty('registered');
expect(result).toHaveProperty('failed');
});
it('should register from multiple directories', async () => {
const mockServices = {} as IServiceContainer;
const registry = createAutoHandlerRegistry(mockServices);
const result = await registry.registerDirectories(['./dir1', './dir2']);
expect(result).toHaveProperty('registered');
expect(result).toHaveProperty('failed');
expect(result.registered).toBeInstanceOf(Array);
expect(result.failed).toBeInstanceOf(Array);
});
});
describe('Edge Cases', () => {
it('should handle non-existent directories gracefully', async () => {
const mockServices = {} as any;
// Should not throw, just return empty results
const result = await autoRegisterHandlers('./definitely-does-not-exist-12345', mockServices);
expect(result.registered).toEqual([]);
expect(result.failed).toEqual([]);
});
it('should handle empty options', async () => {
const mockServices = {} as any;
// Should use default options
const result = await autoRegisterHandlers('./test', mockServices, {});
expect(result).toBeDefined();
expect(result.registered).toBeInstanceOf(Array);
expect(result.failed).toBeInstanceOf(Array);
});
it('should support service name in options', async () => {
const mockServices = {} as any;
const result = await autoRegisterHandlers('./test', mockServices, {
serviceName: 'test-service',
});
expect(result).toBeDefined();
});
it('should handle dry run mode', async () => {
const mockServices = {} as any;
const result = await autoRegisterHandlers('./test', mockServices, { dryRun: true });
expect(result).toBeDefined();
expect(result.registered).toBeInstanceOf(Array);
expect(result.failed).toBeInstanceOf(Array);
});
it('should handle excluded files', async () => {
const mockServices = {} as any;
const result = await autoRegisterHandlers('./test', mockServices, {
exclude: ['test'],
});
expect(result).toBeDefined();
expect(result.registered).toBeInstanceOf(Array);
expect(result.failed).toBeInstanceOf(Array);
});
it('should handle custom pattern', async () => {
const mockServices = {} as any;
const result = await autoRegisterHandlers('./test', mockServices, { pattern: '.custom.' });
expect(result).toBeDefined();
expect(result.registered).toBeInstanceOf(Array);
expect(result.failed).toBeInstanceOf(Array);
});
it('should handle errors gracefully', async () => {
const mockServices = {} as any;
// Even with a protected directory, it should handle gracefully
const result = await autoRegisterHandlers('./protected-dir', mockServices);
expect(result).toBeDefined();
expect(result.registered).toBeInstanceOf(Array);
expect(result.failed).toBeInstanceOf(Array);
});
});
});

View file

@ -28,12 +28,6 @@ describe('Handlers Package Exports', () => {
expect(typeof handlersExports.Disabled).toBe('function');
});
it('should export auto-registration utilities', () => {
expect(handlersExports.autoRegisterHandlers).toBeDefined();
expect(handlersExports.createAutoHandlerRegistry).toBeDefined();
expect(typeof handlersExports.autoRegisterHandlers).toBe('function');
expect(typeof handlersExports.createAutoHandlerRegistry).toBe('function');
});
it('should export types', () => {
// Type tests - compile-time checks