|
| 1 | +/** |
| 2 | + * Tests for Logger utility |
| 3 | + */ |
| 4 | + |
| 5 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 6 | + |
| 7 | +// Reset singleton between tests |
| 8 | +let Logger: typeof import('../logger.js').Logger; |
| 9 | +let LogLevel: typeof import('../logger.js').LogLevel; |
| 10 | + |
| 11 | +describe('Logger', () => { |
| 12 | + beforeEach(async () => { |
| 13 | + vi.resetModules(); |
| 14 | + // Clear any environment variables that affect logger |
| 15 | + delete process.env['STACKMEMORY_LOG_LEVEL']; |
| 16 | + delete process.env['STACKMEMORY_LOG_FILE']; |
| 17 | + |
| 18 | + // Re-import to get fresh singleton |
| 19 | + const module = await import('../logger.js'); |
| 20 | + Logger = module.Logger; |
| 21 | + LogLevel = module.LogLevel; |
| 22 | + }); |
| 23 | + |
| 24 | + afterEach(() => { |
| 25 | + vi.restoreAllMocks(); |
| 26 | + }); |
| 27 | + |
| 28 | + describe('LogLevel', () => { |
| 29 | + it('should have correct log level values', () => { |
| 30 | + expect(LogLevel.ERROR).toBe(0); |
| 31 | + expect(LogLevel.WARN).toBe(1); |
| 32 | + expect(LogLevel.INFO).toBe(2); |
| 33 | + expect(LogLevel.DEBUG).toBe(3); |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + describe('getInstance', () => { |
| 38 | + it('should return singleton instance', () => { |
| 39 | + const instance1 = Logger.getInstance(); |
| 40 | + const instance2 = Logger.getInstance(); |
| 41 | + expect(instance1).toBe(instance2); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + describe('logging methods', () => { |
| 46 | + it('should log error messages', () => { |
| 47 | + const consoleSpy = vi |
| 48 | + .spyOn(console, 'error') |
| 49 | + .mockImplementation(() => {}); |
| 50 | + const logger = Logger.getInstance(); |
| 51 | + |
| 52 | + logger.error('Test error message'); |
| 53 | + |
| 54 | + expect(consoleSpy).toHaveBeenCalled(); |
| 55 | + const logOutput = consoleSpy.mock.calls[0][0]; |
| 56 | + expect(logOutput).toContain('ERROR'); |
| 57 | + expect(logOutput).toContain('Test error message'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should log error with Error object', () => { |
| 61 | + const consoleSpy = vi |
| 62 | + .spyOn(console, 'error') |
| 63 | + .mockImplementation(() => {}); |
| 64 | + const logger = Logger.getInstance(); |
| 65 | + const testError = new Error('Test error object'); |
| 66 | + |
| 67 | + logger.error('Error occurred', testError); |
| 68 | + |
| 69 | + expect(consoleSpy).toHaveBeenCalled(); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should log error with context', () => { |
| 73 | + const consoleSpy = vi |
| 74 | + .spyOn(console, 'error') |
| 75 | + .mockImplementation(() => {}); |
| 76 | + const logger = Logger.getInstance(); |
| 77 | + |
| 78 | + logger.error('Error with context', { detail: 'some info' }); |
| 79 | + |
| 80 | + expect(consoleSpy).toHaveBeenCalled(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should log warn messages', () => { |
| 84 | + const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 85 | + const logger = Logger.getInstance(); |
| 86 | + |
| 87 | + logger.warn('Test warning message'); |
| 88 | + |
| 89 | + expect(consoleSpy).toHaveBeenCalled(); |
| 90 | + const logOutput = consoleSpy.mock.calls[0][0]; |
| 91 | + expect(logOutput).toContain('WARN'); |
| 92 | + expect(logOutput).toContain('Test warning message'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should log info messages', () => { |
| 96 | + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 97 | + const logger = Logger.getInstance(); |
| 98 | + |
| 99 | + logger.info('Test info message'); |
| 100 | + |
| 101 | + expect(consoleSpy).toHaveBeenCalled(); |
| 102 | + const logOutput = consoleSpy.mock.calls[0][0]; |
| 103 | + expect(logOutput).toContain('INFO'); |
| 104 | + expect(logOutput).toContain('Test info message'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should log info with context', () => { |
| 108 | + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 109 | + const logger = Logger.getInstance(); |
| 110 | + |
| 111 | + logger.info('Info with context', { key: 'value' }); |
| 112 | + |
| 113 | + expect(consoleSpy).toHaveBeenCalled(); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should not log debug messages at INFO level', () => { |
| 117 | + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 118 | + const logger = Logger.getInstance(); |
| 119 | + |
| 120 | + logger.debug('Debug message'); |
| 121 | + |
| 122 | + // Debug should not be logged at INFO level (default) |
| 123 | + const debugCalls = consoleSpy.mock.calls.filter((call) => |
| 124 | + call[0]?.includes?.('DEBUG') |
| 125 | + ); |
| 126 | + expect(debugCalls.length).toBe(0); |
| 127 | + }); |
| 128 | + }); |
| 129 | +}); |
| 130 | + |
| 131 | +describe('Logger with DEBUG level', () => { |
| 132 | + beforeEach(async () => { |
| 133 | + vi.resetModules(); |
| 134 | + process.env['STACKMEMORY_LOG_LEVEL'] = 'DEBUG'; |
| 135 | + |
| 136 | + const module = await import('../logger.js'); |
| 137 | + Logger = module.Logger; |
| 138 | + LogLevel = module.LogLevel; |
| 139 | + }); |
| 140 | + |
| 141 | + afterEach(() => { |
| 142 | + delete process.env['STACKMEMORY_LOG_LEVEL']; |
| 143 | + vi.restoreAllMocks(); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should log debug messages when level is DEBUG', () => { |
| 147 | + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 148 | + const logger = Logger.getInstance(); |
| 149 | + |
| 150 | + logger.debug('Debug message'); |
| 151 | + |
| 152 | + expect(consoleSpy).toHaveBeenCalled(); |
| 153 | + const logOutput = consoleSpy.mock.calls[0][0]; |
| 154 | + expect(logOutput).toContain('DEBUG'); |
| 155 | + expect(logOutput).toContain('Debug message'); |
| 156 | + }); |
| 157 | +}); |
| 158 | + |
| 159 | +describe('Logger with ERROR level', () => { |
| 160 | + beforeEach(async () => { |
| 161 | + vi.resetModules(); |
| 162 | + process.env['STACKMEMORY_LOG_LEVEL'] = 'ERROR'; |
| 163 | + |
| 164 | + const module = await import('../logger.js'); |
| 165 | + Logger = module.Logger; |
| 166 | + LogLevel = module.LogLevel; |
| 167 | + }); |
| 168 | + |
| 169 | + afterEach(() => { |
| 170 | + delete process.env['STACKMEMORY_LOG_LEVEL']; |
| 171 | + vi.restoreAllMocks(); |
| 172 | + }); |
| 173 | + |
| 174 | + it('should not log info messages at ERROR level', () => { |
| 175 | + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 176 | + const logger = Logger.getInstance(); |
| 177 | + |
| 178 | + logger.info('Info message'); |
| 179 | + |
| 180 | + const infoCalls = consoleSpy.mock.calls.filter((call) => |
| 181 | + call[0]?.includes?.('INFO') |
| 182 | + ); |
| 183 | + expect(infoCalls.length).toBe(0); |
| 184 | + }); |
| 185 | + |
| 186 | + it('should not log warn messages at ERROR level', () => { |
| 187 | + const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 188 | + const logger = Logger.getInstance(); |
| 189 | + |
| 190 | + logger.warn('Warn message'); |
| 191 | + |
| 192 | + const warnCalls = consoleSpy.mock.calls.filter((call) => |
| 193 | + call[0]?.includes?.('WARN') |
| 194 | + ); |
| 195 | + expect(warnCalls.length).toBe(0); |
| 196 | + }); |
| 197 | +}); |
0 commit comments