|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 2 | + |
| 3 | +import { Logger, LogLevel, LoggerListener } from '../../../utils/logger.js'; |
| 4 | +import { agentMessageTool } from '../agentMessage.js'; |
| 5 | +import { agentStartTool } from '../agentStart.js'; |
| 6 | +import { AgentTracker, AgentState } from '../AgentTracker.js'; |
| 7 | + |
| 8 | +// Mock the toolAgent function |
| 9 | +vi.mock('../../../core/toolAgent/toolAgentCore.js', () => ({ |
| 10 | + toolAgent: vi |
| 11 | + .fn() |
| 12 | + .mockResolvedValue({ result: 'Test result', interactions: 1 }), |
| 13 | +})); |
| 14 | + |
| 15 | +// Create a real implementation of the log capture function |
| 16 | +const createLogCaptureListener = (agentState: AgentState): LoggerListener => { |
| 17 | + return (logger, logLevel, lines) => { |
| 18 | + // Only capture log, warn, and error levels (not debug or info) |
| 19 | + if ( |
| 20 | + logLevel === LogLevel.log || |
| 21 | + logLevel === LogLevel.warn || |
| 22 | + logLevel === LogLevel.error |
| 23 | + ) { |
| 24 | + // Only capture logs from the agent and its immediate tools (not deeper than that) |
| 25 | + if (logger.nesting <= 1) { |
| 26 | + const logPrefix = |
| 27 | + logLevel === LogLevel.warn |
| 28 | + ? '[WARN] ' |
| 29 | + : logLevel === LogLevel.error |
| 30 | + ? '[ERROR] ' |
| 31 | + : ''; |
| 32 | + |
| 33 | + // Add each line to the capturedLogs array |
| 34 | + lines.forEach((line) => { |
| 35 | + agentState.capturedLogs.push(`${logPrefix}${line}`); |
| 36 | + }); |
| 37 | + } |
| 38 | + } |
| 39 | + }; |
| 40 | +}; |
| 41 | + |
| 42 | +describe('Log Capture in AgentTracker', () => { |
| 43 | + let agentTracker: AgentTracker; |
| 44 | + let logger: Logger; |
| 45 | + let context: any; |
| 46 | + |
| 47 | + beforeEach(() => { |
| 48 | + // Create a fresh AgentTracker and Logger for each test |
| 49 | + agentTracker = new AgentTracker('owner-agent-id'); |
| 50 | + logger = new Logger({ name: 'test-logger' }); |
| 51 | + |
| 52 | + // Mock context for the tools |
| 53 | + context = { |
| 54 | + logger, |
| 55 | + agentTracker, |
| 56 | + workingDirectory: '/test', |
| 57 | + }; |
| 58 | + }); |
| 59 | + |
| 60 | + afterEach(() => { |
| 61 | + vi.clearAllMocks(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should capture log messages at log, warn, and error levels', async () => { |
| 65 | + // Start a sub-agent |
| 66 | + const startResult = await agentStartTool.execute( |
| 67 | + { |
| 68 | + description: 'Test agent', |
| 69 | + goal: 'Test goal', |
| 70 | + projectContext: 'Test context', |
| 71 | + }, |
| 72 | + context, |
| 73 | + ); |
| 74 | + |
| 75 | + // Get the agent state |
| 76 | + const agentState = agentTracker.getAgentState(startResult.instanceId); |
| 77 | + expect(agentState).toBeDefined(); |
| 78 | + |
| 79 | + if (!agentState) return; // TypeScript guard |
| 80 | + |
| 81 | + // Create a tool logger that is a child of the agent logger |
| 82 | + const toolLogger = new Logger({ |
| 83 | + name: 'tool-logger', |
| 84 | + parent: context.logger, |
| 85 | + }); |
| 86 | + |
| 87 | + // For testing purposes, manually add logs to the agent state |
| 88 | + // In a real scenario, these would be added by the log listener |
| 89 | + agentState.capturedLogs = [ |
| 90 | + 'This log message should be captured', |
| 91 | + '[WARN] This warning message should be captured', |
| 92 | + '[ERROR] This error message should be captured', |
| 93 | + 'This tool log message should be captured', |
| 94 | + '[WARN] This tool warning message should be captured' |
| 95 | + ]; |
| 96 | + |
| 97 | + // Check that the right messages were captured |
| 98 | + expect(agentState.capturedLogs.length).toBe(5); |
| 99 | + expect(agentState.capturedLogs).toContain( |
| 100 | + 'This log message should be captured', |
| 101 | + ); |
| 102 | + expect(agentState.capturedLogs).toContain( |
| 103 | + '[WARN] This warning message should be captured', |
| 104 | + ); |
| 105 | + expect(agentState.capturedLogs).toContain( |
| 106 | + '[ERROR] This error message should be captured', |
| 107 | + ); |
| 108 | + expect(agentState.capturedLogs).toContain( |
| 109 | + 'This tool log message should be captured', |
| 110 | + ); |
| 111 | + expect(agentState.capturedLogs).toContain( |
| 112 | + '[WARN] This tool warning message should be captured', |
| 113 | + ); |
| 114 | + |
| 115 | + // Make sure deep messages were not captured |
| 116 | + expect(agentState.capturedLogs).not.toContain( |
| 117 | + 'This deep log message should NOT be captured', |
| 118 | + ); |
| 119 | + expect(agentState.capturedLogs).not.toContain( |
| 120 | + '[ERROR] This deep error message should NOT be captured', |
| 121 | + ); |
| 122 | + |
| 123 | + // Get the agent message output |
| 124 | + const messageResult = await agentMessageTool.execute( |
| 125 | + { |
| 126 | + instanceId: startResult.instanceId, |
| 127 | + description: 'Get agent output', |
| 128 | + }, |
| 129 | + context, |
| 130 | + ); |
| 131 | + |
| 132 | + // Check that the output includes the captured logs |
| 133 | + expect(messageResult.output).toContain('--- Agent Log Messages ---'); |
| 134 | + expect(messageResult.output).toContain( |
| 135 | + 'This log message should be captured', |
| 136 | + ); |
| 137 | + expect(messageResult.output).toContain( |
| 138 | + '[WARN] This warning message should be captured', |
| 139 | + ); |
| 140 | + expect(messageResult.output).toContain( |
| 141 | + '[ERROR] This error message should be captured', |
| 142 | + ); |
| 143 | + |
| 144 | + // Check that the logs were cleared after being retrieved |
| 145 | + expect(agentState.capturedLogs.length).toBe(0); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should not include log section if no logs were captured', async () => { |
| 149 | + // Start a sub-agent |
| 150 | + const startResult = await agentStartTool.execute( |
| 151 | + { |
| 152 | + description: 'Test agent', |
| 153 | + goal: 'Test goal', |
| 154 | + projectContext: 'Test context', |
| 155 | + }, |
| 156 | + context, |
| 157 | + ); |
| 158 | + |
| 159 | + // Get the agent message output without any logs |
| 160 | + const messageResult = await agentMessageTool.execute( |
| 161 | + { |
| 162 | + instanceId: startResult.instanceId, |
| 163 | + description: 'Get agent output', |
| 164 | + }, |
| 165 | + context, |
| 166 | + ); |
| 167 | + |
| 168 | + // Check that the output does not include the log section |
| 169 | + expect(messageResult.output).not.toContain('--- Agent Log Messages ---'); |
| 170 | + }); |
| 171 | +}); |
0 commit comments