/** * Integration tests for structured error handling patterns * Verifies that error codes are properly handled across the application */ import { jest } from '@jest/globals'; import { EventEmitter } from 'events'; import { Readable } from 'stream'; describe('Structured Error Handling Patterns', () => { describe('RuvSwarmWrapper Error Code Handling', () => { let RuvSwarmWrapper; let startRuvSwarmMCP; beforeEach(async () => { const module = await import('../../src/mcp/ruv-swarm-wrapper.js'); RuvSwarmWrapper = module.RuvSwarmWrapper; startRuvSwarmMCP = module.startRuvSwarmMCP; }); test('should properly handle known error codes', async () => { const wrapper = new RuvSwarmWrapper({ silent: false }); const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(); const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(); // Create mock streams const mockStderr = new Readable({ read() {} }); // Mock the readline interface const mockRlErr = new EventEmitter(); jest.doMock('readline', () => ({ createInterface: jest.fn(() => mockRlErr) })); // Test various error codes const testCases = [ { error: { code: 'LOGGER_METHOD_MISSING', message: 'logger.logMemoryUsage is not a function' }, expectedLog: 'Known ruv-swarm logger issue detected' }, { error: { code: 'ERR_LOGGER_MEMORY_USAGE', message: 'Memory usage logging failed' }, expectedLog: 'Known ruv-swarm logger issue detected' }, { error: { code: 'ERR_INITIALIZATION', message: 'Failed to initialize swarm' }, expectedLog: 'RuvSwarm initialization error' }, { error: { code: 'UNKNOWN_ERROR', message: 'Something unexpected happened' }, expectedLog: 'RuvSwarm error [UNKNOWN_ERROR]' } ]; for (const testCase of testCases) { consoleErrorSpy.mockClear(); // Emit structured error mockRlErr.emit('line', JSON.stringify({ error: testCase.error })); // Verify appropriate logging if (testCase.expectedLog) { expect(consoleErrorSpy).toHaveBeenCalledWith( expect.stringContaining(testCase.expectedLog) ); } } consoleErrorSpy.mockRestore(); consoleWarnSpy.mockRestore(); }); test('should handle malformed JSON gracefully', () => { const wrapper = new RuvSwarmWrapper({ silent: false }); const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(); const mockRlErr = new EventEmitter(); jest.doMock('readline', () => ({ createInterface: jest.fn(() => mockRlErr) })); // Test malformed JSON const malformedErrors = [ 'Not JSON at all', '{ invalid json', '{ "error": }', 'null', 'undefined' ]; for (const malformed of malformedErrors) { expect(() => { mockRlErr.emit('line', malformed); }).not.toThrow(); } consoleErrorSpy.mockRestore(); }); test('should use pattern matching as fallback', () => { const wrapper = new RuvSwarmWrapper({ silent: false }); const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(); const mockRlErr = new EventEmitter(); jest.doMock('readline', () => ({ createInterface: jest.fn(() => mockRlErr) })); // Test pattern-based error detection const patternTests = [ { line: 'TypeError: logger.logMemoryUsage is not a function at RuvSwarm.init', expectedLog: 'Known ruv-swarm logger issue' }, { line: 'Error: Cannot find module ruv-swarm', expectedLog: 'Module not found error' }, { line: 'Error: connect ECONNREFUSED 127.0.0.1:8080', expectedLog: 'Connection refused error' } ]; for (const test of patternTests) { consoleErrorSpy.mockClear(); mockRlErr.emit('line', test.line); expect(consoleErrorSpy).toHaveBeenCalledWith( expect.stringContaining(test.expectedLog) ); } consoleErrorSpy.mockRestore(); }); }); describe('Error Code Standards', () => { test('should follow consistent error code naming convention', () => { // Define expected error code patterns const validErrorCodePatterns = [ /^ERR_[A-Z_]+$/, // ERR_INITIALIZATION /^[A-Z_]+_ERROR$/, // CONNECTION_ERROR /^[A-Z_]+_MISSING$/, // LOGGER_METHOD_MISSING /^[A-Z_]+_NOT_FOUND$/ // MODULE_NOT_FOUND ]; // Error codes used in the application const errorCodes = [ 'LOGGER_METHOD_MISSING', 'ERR_LOGGER_MEMORY_USAGE', 'ERR_INITIALIZATION', 'MODULE_NOT_FOUND', 'CONNECTION_REFUSED' ]; for (const code of errorCodes) { const isValid = validErrorCodePatterns.some(pattern => pattern.test(code)); expect(isValid).toBe(true); } }); }); describe('Error Recovery and Resilience', () => { test('should continue operation after handling known errors', async () => { const wrapper = new RuvSwarmWrapper({ silent: true, autoRestart: true, maxRestarts: 3 }); // Track if the wrapper continues to function let isOperational = true; const mockRlErr = new EventEmitter(); jest.doMock('readline', () => ({ createInterface: jest.fn(() => mockRlErr) })); // Emit a known error mockRlErr.emit('line', JSON.stringify({ error: { code: 'LOGGER_METHOD_MISSING', message: 'logger.logMemoryUsage is not a function' } })); // Wrapper should still be operational expect(isOperational).toBe(true); expect(wrapper.options.autoRestart).toBe(true); }); }); describe('Cross-Component Error Propagation', () => { test('should properly propagate errors between components', async () => { // This test ensures error codes are consistent across components const components = [ { name: 'RuvSwarmWrapper', path: '../../src/mcp/ruv-swarm-wrapper.js' }, { name: 'SwarmUI', path: '../../src/cli/simple-commands/swarm-ui.js' }, { name: 'GitHub', path: '../../src/cli/simple-commands/github.js' } ]; for (const component of components) { try { const module = await import(component.path); // Verify error handling is present if (component.name === 'RuvSwarmWrapper') { expect(module.RuvSwarmWrapper).toBeDefined(); const instance = new module.RuvSwarmWrapper(); expect(instance.handleProcessExit).toBeDefined(); } } catch (e) { // Module might have dependencies that aren't available in test console.warn(`Could not fully test ${component.name}: ${e.message}`); } } }); }); });