|
| 1 | +import type { AgentGraphRunnerResult } from '../src/api/graph/types'; |
| 2 | +import type { RunnerResult } from '../src/api/model/types'; |
| 3 | +import type { AgentGraphRunner, Runner } from '../src/api/providers/Runner'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Verify that the Runner and AgentGraphRunner protocols can be implemented |
| 7 | + * by a plain object (no abstract class required). |
| 8 | + */ |
| 9 | +describe('Runner protocol', () => { |
| 10 | + it('can be implemented as a plain object (no class extension required)', async () => { |
| 11 | + const runnerResult: RunnerResult = { |
| 12 | + content: 'Hello from runner', |
| 13 | + metrics: { success: true }, |
| 14 | + }; |
| 15 | + |
| 16 | + const myRunner: Runner = { |
| 17 | + run: jest.fn().mockResolvedValue(runnerResult), |
| 18 | + }; |
| 19 | + |
| 20 | + const result = await myRunner.run([{ role: 'user', content: 'Hello' }]); |
| 21 | + |
| 22 | + expect(result.content).toBe('Hello from runner'); |
| 23 | + expect(result.metrics.success).toBe(true); |
| 24 | + }); |
| 25 | + |
| 26 | + it('Runner.run() accepts optional outputType for structured output', async () => { |
| 27 | + const runnerResult: RunnerResult = { |
| 28 | + content: '', |
| 29 | + metrics: { success: true }, |
| 30 | + parsed: { score: 0.9, reasoning: 'good' }, |
| 31 | + }; |
| 32 | + |
| 33 | + const myRunner: Runner = { |
| 34 | + run: jest.fn().mockResolvedValue(runnerResult), |
| 35 | + }; |
| 36 | + |
| 37 | + const schema = { type: 'object', properties: { score: { type: 'number' } } }; |
| 38 | + const result = await myRunner.run([{ role: 'user', content: 'Evaluate' }], schema); |
| 39 | + |
| 40 | + expect(result.parsed).toEqual({ score: 0.9, reasoning: 'good' }); |
| 41 | + expect(myRunner.run).toHaveBeenCalledWith([{ role: 'user', content: 'Evaluate' }], schema); |
| 42 | + }); |
| 43 | + |
| 44 | + it('AgentGraphRunner can be implemented as a plain object', async () => { |
| 45 | + const graphResult: AgentGraphRunnerResult = { |
| 46 | + content: 'Graph output', |
| 47 | + metrics: { |
| 48 | + success: true, |
| 49 | + path: ['node-a'], |
| 50 | + nodeMetrics: { 'node-a': { success: true } }, |
| 51 | + }, |
| 52 | + }; |
| 53 | + |
| 54 | + const myGraphRunner: AgentGraphRunner = { |
| 55 | + run: jest.fn().mockResolvedValue(graphResult), |
| 56 | + }; |
| 57 | + |
| 58 | + const result = await myGraphRunner.run('user input'); |
| 59 | + |
| 60 | + expect(result.content).toBe('Graph output'); |
| 61 | + expect(result.metrics.path).toEqual(['node-a']); |
| 62 | + }); |
| 63 | + |
| 64 | + it('RunnerResult does NOT include evaluations field', () => { |
| 65 | + const result: RunnerResult = { |
| 66 | + content: 'test', |
| 67 | + metrics: { success: true }, |
| 68 | + }; |
| 69 | + |
| 70 | + // TypeScript would catch this at compile time, but verify at runtime shape too |
| 71 | + expect('evaluations' in result).toBe(false); |
| 72 | + }); |
| 73 | +}); |
0 commit comments