UNPKG

judgeval

Version:

Judgment SDK for TypeScript/JavaScript

149 lines 7.41 kB
"use strict"; /** * Tests for the logger module */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const globals_1 = require("@jest/globals"); const logger_js_1 = require("../../common/logger.js"); const logger_instance_js_1 = __importDefault(require("../../common/logger-instance.js")); // Mock winston logger globals_1.jest.mock('winston', () => ({ createLogger: globals_1.jest.fn().mockReturnValue({ debug: globals_1.jest.fn(), info: globals_1.jest.fn(), warn: globals_1.jest.fn(), error: globals_1.jest.fn() }), format: { combine: globals_1.jest.fn().mockReturnValue({}), timestamp: globals_1.jest.fn().mockReturnValue({}), printf: globals_1.jest.fn().mockReturnValue({}), colorize: globals_1.jest.fn().mockReturnValue({}) }, transports: { Console: globals_1.jest.fn(), File: globals_1.jest.fn() } })); (0, globals_1.describe)('Logger', () => { let debugSpy; let infoSpy; let warnSpy; let errorSpy; (0, globals_1.beforeEach)(() => { globals_1.jest.clearAllMocks(); debugSpy = globals_1.jest.spyOn(logger_instance_js_1.default, 'debug'); infoSpy = globals_1.jest.spyOn(logger_instance_js_1.default, 'info'); warnSpy = globals_1.jest.spyOn(logger_instance_js_1.default, 'warn'); errorSpy = globals_1.jest.spyOn(logger_instance_js_1.default, 'error'); }); (0, globals_1.afterEach)(() => { (0, logger_js_1.clearExampleContext)(); globals_1.jest.restoreAllMocks(); }); (0, globals_1.describe)('Logging Functions', () => { (0, globals_1.it)('should log debug messages', () => { (0, logger_js_1.debug)('test debug message', { key: 'value' }); (0, globals_1.expect)(debugSpy).toHaveBeenCalledWith('test debug message', globals_1.expect.any(Object)); }); (0, globals_1.it)('should log info messages', () => { (0, logger_js_1.info)('test info message', { key: 'value' }); (0, globals_1.expect)(infoSpy).toHaveBeenCalledWith('test info message', globals_1.expect.any(Object)); }); (0, globals_1.it)('should log warning messages', () => { (0, logger_js_1.warning)('test warning message', { key: 'value' }); (0, globals_1.expect)(warnSpy).toHaveBeenCalledWith('test warning message', globals_1.expect.any(Object)); }); (0, globals_1.it)('should log error messages', () => { (0, logger_js_1.error)('test error message', { key: 'value' }); (0, globals_1.expect)(errorSpy).toHaveBeenCalledWith('test error message', globals_1.expect.any(Object)); }); }); (0, globals_1.describe)('Example Context', () => { (0, globals_1.it)('should set and clear example context', () => { (0, logger_js_1.setExampleContext)('test-id', '2024-01-01'); (0, logger_js_1.info)('test message'); (0, logger_js_1.clearExampleContext)(); (0, globals_1.expect)(infoSpy).toHaveBeenCalledWith('test message', globals_1.expect.any(Object)); }); (0, globals_1.it)('should handle example context in withExampleContext', () => { const result = (0, logger_js_1.withExampleContext)('test-id', '2024-01-01', () => { (0, logger_js_1.info)('test message'); return 'test result'; }); (0, globals_1.expect)(result).toBe('test result'); (0, globals_1.expect)(infoSpy).toHaveBeenCalledWith('test message', globals_1.expect.any(Object)); }); }); (0, globals_1.describe)('Result Formatting', () => { (0, globals_1.it)('should format empty results', () => { const formatted = (0, logger_js_1.formatEvaluationResults)([]); (0, globals_1.expect)(formatted).toBe(''); }); (0, globals_1.it)('should format successful results', () => { const results = [ { success: true, example: { input: 'test input' } } ]; const formatted = (0, logger_js_1.formatEvaluationResults)(results); (0, globals_1.expect)(formatted).toContain('Success Rate: 100.00%'); }); (0, globals_1.it)('should format failed results', () => { const results = [ { success: false, example: { input: 'test input' }, scorersData: [ { name: 'test scorer', success: false, error: 'test error' } ] } ]; const formatted = (0, logger_js_1.formatEvaluationResults)(results); (0, globals_1.expect)(formatted).toContain('Failures (1)'); (0, globals_1.expect)(formatted).toContain('test error'); }); }); (0, globals_1.describe)('Print Functions', () => { (0, globals_1.it)('should print results with project and eval names', () => { const consoleSpy = globals_1.jest.spyOn(console, 'log'); (0, logger_js_1.printResults)([], 'test-project', 'test-eval'); (0, globals_1.expect)(consoleSpy).toHaveBeenCalledWith(globals_1.expect.stringContaining('app.judgmentlabs.ai')); }); (0, globals_1.it)('should print array results', () => { const consoleSpy = globals_1.jest.spyOn(console, 'log'); (0, logger_js_1.print)([{ success: true }]); (0, globals_1.expect)(consoleSpy).toHaveBeenCalled(); }); (0, globals_1.it)('should print trace data', () => { const consoleSpy = globals_1.jest.spyOn(console, 'log'); (0, logger_js_1.print)({ traceId: 'test-id', name: 'test trace' }); (0, globals_1.expect)(consoleSpy).toHaveBeenCalledWith(globals_1.expect.stringContaining('Trace: test trace')); }); (0, globals_1.it)('should print workflow analysis results', () => { const consoleSpy = globals_1.jest.spyOn(console, 'log'); (0, logger_js_1.print)({ title: 'Workflow Analysis Results', scorerPerformance: [{ name: 'test', score: 1, rating: 'good' }], areasForImprovement: ['test area'], strengths: ['test strength'] }); (0, globals_1.expect)(consoleSpy).toHaveBeenCalledWith(globals_1.expect.stringContaining('Workflow Analysis Results')); }); (0, globals_1.it)('should print recommendations', () => { const consoleSpy = globals_1.jest.spyOn(console, 'log'); (0, logger_js_1.print)({ title: 'Test Title', recommendations: ['test recommendation'] }); (0, globals_1.expect)(consoleSpy).toHaveBeenCalledWith(globals_1.expect.stringContaining('Test Title')); }); (0, globals_1.it)('should print other data as JSON', () => { const consoleSpy = globals_1.jest.spyOn(console, 'log'); (0, logger_js_1.print)({ test: 'data' }); (0, globals_1.expect)(consoleSpy).toHaveBeenCalledWith(globals_1.expect.stringContaining('test')); }); }); }); //# sourceMappingURL=logger.test.js.map