import { LogMonitorLevel, monitorLog } from '../../../core/logging/logMonitor'; import { LogLevels, logWithPrefix } from '../../../core/logging/logUtils'; import { ContentsquareModule } from '../../../core/nativeModules'; jest.mock('../../../core/nativeModules', () => ({ ContentsquareModule: { monitorWarn: jest.fn(), monitorError: jest.fn(), }, })); jest.mock('../../../core/logging/logUtils', () => ({ logWithPrefix: jest.fn(), LogLevels: { VERBOSE: 0, INFO: 1, WARN: 2, ERROR: 3, }, })); describe('monitorLog', () => { const args = ['arg1', 'arg2']; const monitorWarn = ContentsquareModule.monitorWarn as jest.Mock; beforeEach(() => { jest.clearAllMocks(); }); it('should report a warning to the native module and log success', async () => { const description = 'Test warning description'; await monitorLog(LogMonitorLevel.WARN, description, ...args); expect(monitorWarn).toHaveBeenCalledWith({ description, args, }); expect(logWithPrefix).toHaveBeenCalledWith( LogLevels.INFO, 'Success reporting warning to native module log monitoring.', ...args ); }); it('should report an error to the native module and log success', async () => { const description = 'Test error description'; await monitorLog(LogMonitorLevel.ERROR, description, ...args); expect(ContentsquareModule.monitorError).toHaveBeenCalledWith({ description, args, }); expect(logWithPrefix).toHaveBeenCalledWith( LogLevels.INFO, 'Success reporting error to native module log monitoring.', ...args ); }); it('should log a warning for an unknown log level', async () => { const description = 'Test unknown log level'; await monitorLog(-1 as LogMonitorLevel, description, ...args); expect(monitorWarn).not.toHaveBeenCalled(); expect(ContentsquareModule.monitorError).not.toHaveBeenCalled(); expect(logWithPrefix).toHaveBeenCalledWith( LogLevels.WARN, 'Unknown log level for log monitoring provided.', ...args ); }); it('should handle error when reporting to the native module fails', async () => { const description = 'Test error handling'; (ContentsquareModule.monitorError as jest.Mock).mockImplementation(() => { throw new Error('Native module error'); }); await monitorLog(LogMonitorLevel.ERROR, description, ...args); expect(ContentsquareModule.monitorError).toHaveBeenCalledWith({ description, args, }); expect(logWithPrefix).toHaveBeenCalledWith( LogLevels.ERROR, 'Failed to report 1 to native module log monitoring: Native module error.', ...args ); }); it('should handle unknown error gracefully', async () => { const description = 'Test unknown error handling'; monitorWarn.mockImplementation(() => { throw null; }); await monitorLog(LogMonitorLevel.WARN, description, ...args); expect(monitorWarn).toHaveBeenCalledWith({ description, args, }); expect(logWithPrefix).toHaveBeenCalledWith( LogLevels.ERROR, 'Failed to report 0 to native module log monitoring: Unknown error occurred.', ...args ); }); });