import { describe, expect, it } from 'vitest'; import { ToolUsageRecorder } from '../src/tool-usage.js'; function assistant(blocks: unknown[]) { return { type: 'assistant', message: { content: blocks } }; } describe('ToolUsageRecorder', () => { it('records tool names and Bash commands from assistant tool_use blocks', () => { const recorder = new ToolUsageRecorder(); recorder.recordAssistantMessage( assistant([ { type: 'text', text: 'checking' }, { type: 'tool_use', name: 'Read', input: { file_path: '/a' } }, { type: 'tool_use', name: 'Bash', input: { command: 'git status' } }, ]), ); recorder.recordAssistantMessage( assistant([{ type: 'tool_use', name: 'Edit', input: {} }]), ); expect(recorder.snapshot()).toEqual({ tools: ['Read', 'Bash', 'Edit'], bashCommands: ['git status'], }); }); it('marks the report truncated when a Bash command cannot be read', () => { const recorder = new ToolUsageRecorder(); recorder.recordAssistantMessage( assistant([{ type: 'tool_use', name: 'Bash', input: {} }]), ); expect(recorder.snapshot().truncated).toBe(true); }); it('marks the report truncated instead of keeping oversized commands', () => { const recorder = new ToolUsageRecorder(); recorder.recordAssistantMessage( assistant([ { type: 'tool_use', name: 'Bash', input: { command: 'x'.repeat(5000) }, }, ]), ); const usage = recorder.snapshot(); expect(usage.bashCommands).toEqual([]); expect(usage.truncated).toBe(true); }); it('ignores messages without content blocks', () => { const recorder = new ToolUsageRecorder(); recorder.recordAssistantMessage({ type: 'assistant' }); expect(recorder.snapshot()).toEqual({ tools: [], bashCommands: [] }); }); });