import DatabaseService from '../../../src/services/DatabaseService'; describe('DatabaseService', () => { let databaseService: DatabaseService; beforeEach(() => { databaseService = new DatabaseService(); // Mock console methods to reduce test noise jest.spyOn(console, 'log').mockImplementation(() => {}); jest.spyOn(console, 'error').mockImplementation(() => {}); }); afterEach(() => { jest.clearAllMocks(); jest.restoreAllMocks(); try { databaseService.close(); } catch { // Ignore close errors in tests } }); describe('initial state', () => { it('should have null database connections initially', () => { expect(databaseService.getMainDb()).toBeNull(); expect(databaseService.getProfanityDb()).toBeNull(); }); it('should throw error when accessing models before initialization', () => { expect(() => databaseService.getLocationModel()).toThrow('Database not initialized. Call initialize() first.'); expect(() => databaseService.getProfanityWordModel()).toThrow('Database not initialized. Call initialize() first.'); }); }); describe('close method', () => { it('should handle close when not initialized', () => { expect(() => databaseService.close()).not.toThrow(); }); it('should be callable multiple times', () => { expect(() => { databaseService.close(); databaseService.close(); databaseService.close(); }).not.toThrow(); }); }); describe('type safety', () => { it('should have correct method signatures', () => { expect(typeof databaseService.initialize).toBe('function'); expect(typeof databaseService.initializeMainDatabase).toBe('function'); expect(typeof databaseService.initializeProfanityDatabase).toBe('function'); expect(typeof databaseService.getLocationModel).toBe('function'); expect(typeof databaseService.getProfanityWordModel).toBe('function'); expect(typeof databaseService.getMainDb).toBe('function'); expect(typeof databaseService.getProfanityDb).toBe('function'); expect(typeof databaseService.close).toBe('function'); }); it('should return correct types', () => { expect(databaseService.getMainDb()).toBeNull(); expect(databaseService.getProfanityDb()).toBeNull(); }); }); describe('error handling', () => { it('should handle method calls on uninitialized service', () => { expect(() => databaseService.getLocationModel()).toThrow(); expect(() => databaseService.getProfanityWordModel()).toThrow(); }); it('should provide meaningful error messages', () => { expect(() => databaseService.getLocationModel()).toThrow('Database not initialized. Call initialize() first.'); expect(() => databaseService.getProfanityWordModel()).toThrow('Database not initialized. Call initialize() first.'); }); }); describe('service instantiation', () => { it('should create a new instance successfully', () => { const newService = new DatabaseService(); expect(newService).toBeInstanceOf(DatabaseService); expect(newService.getMainDb()).toBeNull(); expect(newService.getProfanityDb()).toBeNull(); }); it('should allow multiple instances', () => { const service1 = new DatabaseService(); const service2 = new DatabaseService(); expect(service1).toBeInstanceOf(DatabaseService); expect(service2).toBeInstanceOf(DatabaseService); expect(service1).not.toBe(service2); }); }); });