import { LogLevels, getLogLevel, logWithPrefix, setLogLevel } from '../../../core/logging/logUtils'; // Mock the autocapture module jest.mock('../../../autocapture/csqAutocapture', () => ({ setLogLevel: jest.fn(), })); describe('logUtils', () => { beforeEach(() => { // Reset the log level before each test setLogLevel('INFO'); jest.clearAllMocks(); }); describe('getLogLevel', () => { it('returns the current log level', () => { setLogLevel('INFO'); expect(getLogLevel()).toBe('INFO'); }); }); describe('setLogLevel', () => { describe('for valid log levels', () => { it('should set the log level correctly to verbose', () => { const consoleSpy = jest.spyOn(console, 'info').mockImplementation(); setLogLevel('VERBOSE'); expect(getLogLevel()).toBe('VERBOSE'); expect(consoleSpy).toHaveBeenCalledWith( 'CSLIB ℹ️ Info: Log level set to VERBOSE' ); }); it('should set the log level correctly to info', () => { const consoleSpy = jest.spyOn(console, 'info').mockImplementation(); setLogLevel('INFO'); expect(getLogLevel()).toBe('INFO'); expect(consoleSpy).toHaveBeenCalledWith( 'CSLIB ℹ️ Info: Log level set to INFO' ); }); it('should set the log level correctly to warning', () => { const consoleSpy = jest.spyOn(console, 'info').mockImplementation(); setLogLevel('WARN'); expect(getLogLevel()).toBe('WARN'); expect(consoleSpy).toHaveBeenCalledWith( 'CSLIB ℹ️ Info: Log level set to WARN' ); }); it('should set the log level correctly to error', () => { const consoleSpy = jest.spyOn(console, 'info').mockImplementation(); setLogLevel('ERROR'); expect(getLogLevel()).toBe('ERROR'); expect(consoleSpy).toHaveBeenCalledWith( 'CSLIB ℹ️ Info: Log level set to ERROR' ); }); }); describe('for invalid log levels', () => { it('should default to info for invalid log levels', () => { const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(); setLogLevel('INVALID' as keyof typeof LogLevels); expect(getLogLevel()).toBe('INFO'); expect(consoleWarnSpy).toHaveBeenCalledWith( 'CSLIB ⚠️ Warning: Invalid log level provided, defaulting to INFO' ); }); it('should set the log level correctly to NONE', () => { const consoleSpy = jest.spyOn(console, 'info').mockImplementation(); setLogLevel('NONE'); expect(getLogLevel()).toBe('NONE'); expect(consoleSpy).toHaveBeenCalledWith( 'CSLIB ℹ️ Info: Log level set to NONE' ); }); }); }); describe('logWithPrefix', () => { it('should log messages at or above the current log level', () => { const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(); const consoleInfoSpy = jest.spyOn(console, 'info').mockImplementation(); const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(); const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(); setLogLevel('VERBOSE'); logWithPrefix(LogLevels.VERBOSE, 'Verbose message'); logWithPrefix(LogLevels.INFO, 'Info message'); logWithPrefix(LogLevels.WARN, 'Warning message'); logWithPrefix(LogLevels.ERROR, 'Error message'); expect(consoleLogSpy).toHaveBeenCalledWith( 'CSLIB 💬 Verbose: Verbose message' ); expect(consoleInfoSpy).toHaveBeenCalledWith( 'CSLIB ℹ️ Info: Info message' ); expect(consoleWarnSpy).toHaveBeenCalledWith( 'CSLIB ⚠️ Warning: Warning message' ); expect(consoleErrorSpy).toHaveBeenCalledWith( 'CSLIB ⛔️ Error: Error message' ); }); it('should not log messages below the current log level, except for log level changes', () => { const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(); const consoleInfoSpy = jest.spyOn(console, 'info').mockImplementation(); const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(); const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(); setLogLevel('WARN'); // This will log an info message about the log level change logWithPrefix(LogLevels.VERBOSE, 'Verbose message'); logWithPrefix(LogLevels.INFO, 'Info message'); logWithPrefix(LogLevels.WARN, 'Warning message'); logWithPrefix(LogLevels.ERROR, 'Error message'); expect(consoleLogSpy).not.toHaveBeenCalled(); expect(consoleInfoSpy).toHaveBeenCalledTimes(1); // Only the log level change expect(consoleInfoSpy).toHaveBeenCalledWith( 'CSLIB ℹ️ Info: Log level set to WARN' ); expect(consoleWarnSpy).toHaveBeenCalledWith( 'CSLIB ⚠️ Warning: Warning message' ); expect(consoleErrorSpy).toHaveBeenCalledWith( 'CSLIB ⛔️ Error: Error message' ); }); it('should not log any messages when log level is set to NONE', () => { const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(); const consoleInfoSpy = jest.spyOn(console, 'info').mockImplementation(); const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(); const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(); setLogLevel('NONE'); // This will log an info message about the log level change logWithPrefix(LogLevels.VERBOSE, 'Verbose message'); logWithPrefix(LogLevels.INFO, 'Info message'); logWithPrefix(LogLevels.WARN, 'Warning message'); logWithPrefix(LogLevels.ERROR, 'Error message'); logWithPrefix(LogLevels.NONE, 'None message'); expect(consoleLogSpy).not.toHaveBeenCalled(); expect(consoleInfoSpy).toHaveBeenCalledTimes(1); // Only the log level change expect(consoleInfoSpy).toHaveBeenCalledWith( 'CSLIB ℹ️ Info: Log level set to NONE' ); expect(consoleWarnSpy).not.toHaveBeenCalled(); expect(consoleErrorSpy).not.toHaveBeenCalled(); }); it('should handle NONE log level messages correctly (do nothing)', () => { const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(); const consoleInfoSpy = jest.spyOn(console, 'info').mockImplementation(); const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(); const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(); setLogLevel('VERBOSE'); // This will log an info message about the log level change logWithPrefix(LogLevels.NONE, 'None message'); // Should not call any console methods for NONE level messages expect(consoleLogSpy).not.toHaveBeenCalledWith(expect.stringContaining('None message')); expect(consoleWarnSpy).not.toHaveBeenCalledWith(expect.stringContaining('None message')); expect(consoleErrorSpy).not.toHaveBeenCalledWith(expect.stringContaining('None message')); // Only the initial log level change should be logged expect(consoleInfoSpy).toHaveBeenCalledTimes(1); expect(consoleInfoSpy).toHaveBeenCalledWith( 'CSLIB ℹ️ Info: Log level set to VERBOSE' ); }); }); });