import { describe, expect, it, beforeEach, mock } from 'bun:test'; import { autoRegisterHandlers, createAutoHandlerRegistry, } from '../src/registry/auto-register'; import type { IServiceContainer } from '@stock-bot/types'; import { Handler, Operation } from '../src/decorators/decorators'; describe('Auto Registration', () => { const mockServices: IServiceContainer = { getService: mock(() => null), hasService: mock(() => false), registerService: mock(() => {}), } as any; const mockLogger = { info: mock(() => {}), error: mock(() => {}), warn: mock(() => {}), debug: mock(() => {}), }; beforeEach(() => { // Reset all mocks mockLogger.info = mock(() => {}); mockLogger.error = mock(() => {}); mockLogger.warn = mock(() => {}); mockLogger.debug = mock(() => {}); }); describe('autoRegisterHandlers', () => { it('should auto-register handlers', async () => { // Since this function reads from file system, we'll create a temporary directory const result = await autoRegisterHandlers('./non-existent-dir', mockServices, { pattern: '.handler.', dryRun: true, }); expect(result).toHaveProperty('registered'); expect(result).toHaveProperty('failed'); expect(Array.isArray(result.registered)).toBe(true); expect(Array.isArray(result.failed)).toBe(true); }); it('should use default options when not provided', async () => { const result = await autoRegisterHandlers('./non-existent-dir', mockServices); expect(result).toHaveProperty('registered'); expect(result).toHaveProperty('failed'); }); it('should handle directory not found gracefully', async () => { // This should not throw but return empty results 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 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 a directory', async () => { const registry = createAutoHandlerRegistry(mockServices); const result = await registry.registerDirectory('./non-existent-dir', { dryRun: true, }); expect(result).toHaveProperty('registered'); expect(result).toHaveProperty('failed'); }); it('should register from multiple directories', async () => { const registry = createAutoHandlerRegistry(mockServices); const result = await registry.registerDirectories([ './dir1', './dir2', ], { dryRun: true, }); expect(result).toHaveProperty('registered'); expect(result).toHaveProperty('failed'); expect(Array.isArray(result.registered)).toBe(true); expect(Array.isArray(result.failed)).toBe(true); }); }); });