/** * Simple test to verify error handling and logging functionality */ const loggingService = require('./src/services/LoggingService'); const { AppError, handleDatabaseError, handleJWTError } = require('./src/middleware/errorHandler'); async function testErrorHandling() { console.log('๐Ÿงช Testing Error Handling and Logging System...\n'); try { // Test 1: Logging Service console.log('1. Testing Logging Service...'); await loggingService.info('Test info message', { testData: 'info test' }); await loggingService.warn('Test warning message', { testData: 'warning test' }); await loggingService.error('Test error message', { testData: 'error test' }); await loggingService.debug('Test debug message', { testData: 'debug test' }); console.log('โœ… Logging service test completed'); // Test 2: Authentication Event Logging console.log('\n2. Testing Authentication Event Logging...'); await loggingService.logAuthEvent('login_success', 'user123', 'test@example.com', { ip: '127.0.0.1', userAgent: 'Test Browser' }); await loggingService.logAuthEvent('login_failed', 'unknown', 'test@example.com', { ip: '127.0.0.1', userAgent: 'Test Browser' }); console.log('โœ… Authentication event logging test completed'); // Test 3: Database Event Logging console.log('\n3. Testing Database Event Logging...'); await loggingService.logDatabaseEvent('connection_established', { database: 'test_db' }); await loggingService.logDatabaseEvent('query_executed', { query: 'SELECT * FROM users', duration: '15ms' }); console.log('โœ… Database event logging test completed'); // Test 4: Security Event Logging console.log('\n4. Testing Security Event Logging...'); await loggingService.logSecurityEvent('rate_limit_exceeded', { ip: '127.0.0.1', endpoint: '/api/auth/login', attempts: 10 }); console.log('โœ… Security event logging test completed'); // Test 5: AppError Class console.log('\n5. Testing AppError Class...'); const appError = new AppError('Test application error', 400, 'TEST_ERROR'); console.log('AppError created:', { message: appError.message, statusCode: appError.statusCode, code: appError.code, timestamp: appError.timestamp }); console.log('โœ… AppError class test completed'); // Test 6: Database Error Handler console.log('\n6. Testing Database Error Handler...'); const dbError = { code: '23505', message: 'duplicate key value violates unique constraint' }; const handledDbError = handleDatabaseError(dbError); console.log('Database error handled:', { message: handledDbError.message, statusCode: handledDbError.statusCode, code: handledDbError.code }); console.log('โœ… Database error handler test completed'); // Test 7: JWT Error Handler console.log('\n7. Testing JWT Error Handler...'); const jwtError = { name: 'TokenExpiredError', message: 'jwt expired' }; const handledJwtError = handleJWTError(jwtError); console.log('JWT error handled:', { message: handledJwtError.message, statusCode: handledJwtError.statusCode, code: handledJwtError.code }); console.log('โœ… JWT error handler test completed'); // Test 8: Log Statistics console.log('\n8. Testing Log Statistics...'); const logStats = await loggingService.getLogStats(); console.log('Log statistics:', logStats); console.log('โœ… Log statistics test completed'); console.log('\n๐ŸŽ‰ All error handling and logging tests completed successfully!'); console.log('\n๐Ÿ“ Check the backend/logs directory for generated log files.'); } catch (error) { console.error('โŒ Test failed:', error); } } // Run the test testErrorHandling();