|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { |
| 3 | + TOOL_USE_RENDERERS, |
| 4 | + TOOL_RESULT_RENDERERS, |
| 5 | + renderToolUse, |
| 6 | + renderToolResult, |
| 7 | + getToolSummary, |
| 8 | +} from './registry.js'; |
| 9 | + |
| 10 | +const CORE_TOOL_USE = ['Bash', 'Read', 'Write', 'Edit', 'Glob', 'Grep', 'Task', 'TodoWrite', 'AskUserQuestion']; |
| 11 | + |
| 12 | +const CORE_TOOL_RESULT = [ |
| 13 | + 'bash', |
| 14 | + 'file_read', |
| 15 | + 'file_edit', |
| 16 | + 'file_write', |
| 17 | + 'glob', |
| 18 | + 'grep', |
| 19 | + 'web_search', |
| 20 | + 'web_fetch', |
| 21 | + 'task', |
| 22 | + 'todo_write', |
| 23 | + 'user_input', |
| 24 | + 'plan', |
| 25 | +]; |
| 26 | + |
| 27 | +describe('TOOL_USE_RENDERERS', () => { |
| 28 | + it('registers core tool names', () => { |
| 29 | + for (const name of CORE_TOOL_USE) { |
| 30 | + expect(TOOL_USE_RENDERERS[name], name).toBeTypeOf('function'); |
| 31 | + } |
| 32 | + }); |
| 33 | + |
| 34 | + it('renderBashUse escapes HTML in command', () => { |
| 35 | + const html = renderToolUse({ |
| 36 | + name: 'Bash', |
| 37 | + input: { command: '<script>alert(1)</script>' }, |
| 38 | + }); |
| 39 | + expect(html).not.toContain('<script>'); |
| 40 | + expect(html).toContain('<script>'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('renderReadUse escapes file path in body', () => { |
| 44 | + const html = renderToolUse({ |
| 45 | + name: 'Read', |
| 46 | + input: { file_path: 'C:\\tmp\\<evil>.txt' }, |
| 47 | + }); |
| 48 | + expect(html).toContain('<evil>'); |
| 49 | + expect(html).not.toContain('<evil>'); |
| 50 | + }); |
| 51 | +}); |
| 52 | + |
| 53 | +describe('TOOL_RESULT_RENDERERS', () => { |
| 54 | + it('registers core result types', () => { |
| 55 | + for (const rt of CORE_TOOL_RESULT) { |
| 56 | + expect(TOOL_RESULT_RENDERERS[rt], rt).toBeTypeOf('function'); |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + it('renderBashResult escapes stdout', () => { |
| 61 | + const html = renderToolResult({ |
| 62 | + result_type: 'bash', |
| 63 | + exit_code: 0, |
| 64 | + stdout: '<img onerror=alert(1)>', |
| 65 | + }); |
| 66 | + expect(html).not.toContain('<img'); |
| 67 | + expect(html).toContain('<img'); |
| 68 | + }); |
| 69 | +}); |
| 70 | + |
| 71 | +describe('getToolSummary', () => { |
| 72 | + it('formats Bash summary', () => { |
| 73 | + expect(getToolSummary('Bash', { command: 'ls -la' })).toMatch(/Bash:/); |
| 74 | + expect(getToolSummary('Bash', { command: 'ls -la' })).toContain('ls -la'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('formats Read summary with escaped path', () => { |
| 78 | + expect(getToolSummary('Read', { file_path: 'a<b' })).toContain('<b'); |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
| 82 | +describe('renderToolUse fallback', () => { |
| 83 | + it('uses JSON fallback for unknown tools', () => { |
| 84 | + const html = renderToolUse({ |
| 85 | + name: 'WebFetch', |
| 86 | + input: { url: 'https://example.com' }, |
| 87 | + }); |
| 88 | + expect(html).toContain('tool-call'); |
| 89 | + expect(html).toContain('example.com'); |
| 90 | + }); |
| 91 | +}); |
| 92 | + |
| 93 | +describe('renderToolResult fallback', () => { |
| 94 | + it('renders summary-only for unknown result types', () => { |
| 95 | + const html = renderToolResult({ result_type: 'custom_type' }); |
| 96 | + expect(html).toContain('Tool result (custom_type)'); |
| 97 | + expect(html).toContain('tool-result'); |
| 98 | + }); |
| 99 | +}); |
0 commit comments