|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +import { MockLogger } from '../utils/mockLogger.js'; |
| 4 | + |
| 5 | +import { toolAgent } from './toolAgent.js'; |
| 6 | + |
| 7 | +const logger = new MockLogger(); |
| 8 | + |
| 9 | +process.env.ANTHROPIC_API_KEY = 'sk-ant-api03-1234567890'; |
| 10 | + |
| 11 | +// Mock Anthropic client |
| 12 | +vi.mock('@anthropic-ai/sdk', () => { |
| 13 | + return { |
| 14 | + default: class MockAnthropic { |
| 15 | + messages = { |
| 16 | + create: vi.fn().mockImplementation(() => { |
| 17 | + return { |
| 18 | + id: 'msg_123', |
| 19 | + model: 'claude-3-7-sonnet-latest', |
| 20 | + type: 'message', |
| 21 | + role: 'assistant', |
| 22 | + content: [ |
| 23 | + { |
| 24 | + type: 'text', |
| 25 | + text: 'I will help with that.', |
| 26 | + }, |
| 27 | + { |
| 28 | + type: 'tool_use', |
| 29 | + id: 'tu_123', |
| 30 | + name: 'sequenceComplete', |
| 31 | + input: { |
| 32 | + result: 'Test complete', |
| 33 | + }, |
| 34 | + }, |
| 35 | + ], |
| 36 | + usage: { |
| 37 | + input_tokens: 100, |
| 38 | + output_tokens: 50, |
| 39 | + // Simulating cached tokens |
| 40 | + cache_read_input_tokens: 30, |
| 41 | + cache_creation_input_tokens: 70, |
| 42 | + }, |
| 43 | + }; |
| 44 | + }), |
| 45 | + }; |
| 46 | + constructor() {} |
| 47 | + }, |
| 48 | + }; |
| 49 | +}); |
| 50 | + |
| 51 | +// Mock tool |
| 52 | +const mockTool = { |
| 53 | + name: 'sequenceComplete', |
| 54 | + description: 'Completes the sequence', |
| 55 | + parameters: { |
| 56 | + type: 'object' as const, |
| 57 | + properties: { |
| 58 | + result: { |
| 59 | + type: 'string' as const, |
| 60 | + }, |
| 61 | + }, |
| 62 | + additionalProperties: false, |
| 63 | + required: ['result'], |
| 64 | + }, |
| 65 | + returns: { |
| 66 | + type: 'string' as const, |
| 67 | + }, |
| 68 | + execute: vi.fn().mockImplementation(async (params) => { |
| 69 | + console.log(' Parameters:'); |
| 70 | + Object.entries(params).forEach(([key, value]) => { |
| 71 | + console.log(` - ${key}: ${JSON.stringify(value)}`); |
| 72 | + }); |
| 73 | + console.log(); |
| 74 | + console.log(' Results:'); |
| 75 | + console.log(` - ${params.result}`); |
| 76 | + console.log(); |
| 77 | + return params.result; |
| 78 | + }), |
| 79 | +}; |
| 80 | + |
| 81 | +describe('toolAgent input token caching', () => { |
| 82 | + beforeEach(() => { |
| 83 | + vi.clearAllMocks(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should track cached tokens in the result', async () => { |
| 87 | + const result = await toolAgent('test prompt', [mockTool], undefined, { |
| 88 | + logger, |
| 89 | + headless: true, |
| 90 | + workingDirectory: '.', |
| 91 | + tokenLevel: 'debug', |
| 92 | + }); |
| 93 | + |
| 94 | + // Verify that cached tokens are tracked |
| 95 | + expect(result.tokens.inputCacheReads).toBeDefined(); |
| 96 | + expect(result.tokens.inputCacheReads).toBe(30); |
| 97 | + |
| 98 | + // Verify total token counts |
| 99 | + expect(result.tokens.input).toBe(100); |
| 100 | + expect(result.tokens.output).toBe(50); |
| 101 | + }); |
| 102 | +}); |
0 commit comments