|
| 1 | +import { ManagedAgent } from '../src/api/agent/ManagedAgent'; |
| 2 | +import { LDAIAgentConfig } from '../src/api/config/types'; |
| 3 | +import { LDAIConfigTracker } from '../src/api/config/LDAIConfigTracker'; |
| 4 | +import { Evaluator } from '../src/api/judge/Evaluator'; |
| 5 | +import { LDJudgeResult } from '../src/api/judge/types'; |
| 6 | +import { AIProvider } from '../src/api/providers/AIProvider'; |
| 7 | +import { ChatResponse } from '../src/api/chat/types'; |
| 8 | + |
| 9 | +describe('ManagedAgent', () => { |
| 10 | + let mockProvider: jest.Mocked<AIProvider>; |
| 11 | + let mockTracker: jest.Mocked<LDAIConfigTracker>; |
| 12 | + let agentConfig: LDAIAgentConfig; |
| 13 | + |
| 14 | + const mockResponse: ChatResponse = { |
| 15 | + message: { role: 'assistant', content: 'Agent response' }, |
| 16 | + metrics: { success: true }, |
| 17 | + }; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + mockProvider = { |
| 21 | + invokeModel: jest.fn().mockResolvedValue(mockResponse), |
| 22 | + } as any; |
| 23 | + |
| 24 | + mockTracker = { |
| 25 | + trackMetricsOf: jest.fn().mockImplementation(async (_extractor: any, func: any) => func()), |
| 26 | + trackJudgeResult: jest.fn(), |
| 27 | + resumptionToken: 'agent-resumption-token', |
| 28 | + getTrackData: jest.fn().mockReturnValue({}), |
| 29 | + trackDuration: jest.fn(), |
| 30 | + trackTokens: jest.fn(), |
| 31 | + trackSuccess: jest.fn(), |
| 32 | + trackError: jest.fn(), |
| 33 | + trackFeedback: jest.fn(), |
| 34 | + trackTimeToFirstToken: jest.fn(), |
| 35 | + trackDurationOf: jest.fn(), |
| 36 | + trackOpenAIMetrics: jest.fn(), |
| 37 | + trackBedrockConverseMetrics: jest.fn(), |
| 38 | + trackVercelAISDKGenerateTextMetrics: jest.fn(), |
| 39 | + trackStreamMetricsOf: jest.fn(), |
| 40 | + trackToolCall: jest.fn(), |
| 41 | + trackToolCalls: jest.fn(), |
| 42 | + getSummary: jest.fn(), |
| 43 | + } as any; |
| 44 | + |
| 45 | + agentConfig = { |
| 46 | + key: 'test-agent', |
| 47 | + enabled: true, |
| 48 | + instructions: 'You are a helpful agent.', |
| 49 | + model: { name: 'gpt-4' }, |
| 50 | + provider: { name: 'openai' }, |
| 51 | + createTracker: () => mockTracker, |
| 52 | + }; |
| 53 | + }); |
| 54 | + |
| 55 | + it('run() returns a ManagedResult with content and metrics', async () => { |
| 56 | + const agent = new ManagedAgent(agentConfig, mockProvider); |
| 57 | + const result = await agent.run('Hello agent'); |
| 58 | + |
| 59 | + expect(result.content).toBe('Agent response'); |
| 60 | + expect(result.metrics.success).toBe(true); |
| 61 | + expect(result.metrics.resumptionToken).toBe('agent-resumption-token'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('run() invokes the provider with the prompt as user message', async () => { |
| 65 | + const agent = new ManagedAgent(agentConfig, mockProvider); |
| 66 | + await agent.run('My question'); |
| 67 | + |
| 68 | + expect(mockProvider.invokeModel).toHaveBeenCalledWith([ |
| 69 | + { role: 'user', content: 'My question' }, |
| 70 | + ]); |
| 71 | + }); |
| 72 | + |
| 73 | + it('run() resolves to empty evaluations when no evaluator configured', async () => { |
| 74 | + const agent = new ManagedAgent(agentConfig, mockProvider); |
| 75 | + const result = await agent.run('Hello'); |
| 76 | + const evaluations = await result.evaluations; |
| 77 | + expect(evaluations).toEqual([]); |
| 78 | + }); |
| 79 | + |
| 80 | + it('run() resolves to empty evaluations with noop evaluator', async () => { |
| 81 | + const configWithNoop: LDAIAgentConfig = { |
| 82 | + ...agentConfig, |
| 83 | + evaluator: Evaluator.noop(), |
| 84 | + }; |
| 85 | + const agent = new ManagedAgent(configWithNoop, mockProvider); |
| 86 | + const result = await agent.run('Hello'); |
| 87 | + const evaluations = await result.evaluations; |
| 88 | + expect(evaluations).toEqual([]); |
| 89 | + }); |
| 90 | + |
| 91 | + it('awaiting evaluations calls tracker.trackJudgeResult', async () => { |
| 92 | + const judgeResult: LDJudgeResult = { |
| 93 | + success: true, |
| 94 | + sampled: true, |
| 95 | + score: 0.85, |
| 96 | + metricKey: 'quality', |
| 97 | + }; |
| 98 | + const mockEvaluator = { |
| 99 | + judgeConfiguration: { judges: [{ key: 'judge-1', samplingRate: 1.0 }] }, |
| 100 | + evaluate: jest.fn().mockResolvedValue([judgeResult]), |
| 101 | + judges: new Map(), |
| 102 | + } as unknown as Evaluator; |
| 103 | + |
| 104 | + const configWithEvaluator: LDAIAgentConfig = { |
| 105 | + ...agentConfig, |
| 106 | + evaluator: mockEvaluator, |
| 107 | + }; |
| 108 | + |
| 109 | + const agent = new ManagedAgent(configWithEvaluator, mockProvider); |
| 110 | + const result = await agent.run('Hello'); |
| 111 | + |
| 112 | + await result.evaluations; |
| 113 | + expect(mockTracker.trackJudgeResult).toHaveBeenCalledWith(judgeResult); |
| 114 | + }); |
| 115 | + |
| 116 | + it('evaluate() is called with prompt as input and response content as output', async () => { |
| 117 | + const mockEvaluator = { |
| 118 | + judgeConfiguration: { judges: [{ key: 'judge-1', samplingRate: 1.0 }] }, |
| 119 | + evaluate: jest.fn().mockResolvedValue([]), |
| 120 | + judges: new Map(), |
| 121 | + } as unknown as Evaluator; |
| 122 | + |
| 123 | + const configWithEvaluator: LDAIAgentConfig = { |
| 124 | + ...agentConfig, |
| 125 | + evaluator: mockEvaluator, |
| 126 | + }; |
| 127 | + |
| 128 | + const agent = new ManagedAgent(configWithEvaluator, mockProvider); |
| 129 | + const result = await agent.run('user prompt'); |
| 130 | + await result.evaluations; |
| 131 | + |
| 132 | + expect(mockEvaluator.evaluate).toHaveBeenCalledWith('user prompt', 'Agent response'); |
| 133 | + }); |
| 134 | + |
| 135 | + it('getConfig() returns the agent config', () => { |
| 136 | + const agent = new ManagedAgent(agentConfig, mockProvider); |
| 137 | + expect(agent.getConfig()).toBe(agentConfig); |
| 138 | + }); |
| 139 | + |
| 140 | + it('getProvider() returns the provider', () => { |
| 141 | + const agent = new ManagedAgent(agentConfig, mockProvider); |
| 142 | + expect(agent.getProvider()).toBe(mockProvider); |
| 143 | + }); |
| 144 | +}); |
0 commit comments