|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { |
| 3 | + TOOL_USE_RENDERERS, |
| 4 | + TOOL_RESULT_RENDERERS, |
| 5 | + renderToolUse, |
| 6 | + renderToolResult, |
| 7 | + getToolSummary, |
| 8 | + toolResultHasBody, |
| 9 | +} from './registry.js'; |
| 10 | +import { UNKNOWN_DISPATCH_KEY } from './constants.js'; |
| 11 | +import { renderWebFetchUse } from './tool_use/web_fetch.js'; |
| 12 | + |
| 13 | +const CORE_TOOL_USE = ['Bash', 'Read', 'Write', 'Edit', 'Glob', 'Grep', 'Task', 'TodoWrite', 'AskUserQuestion', 'WebFetch', 'WebSearch']; |
| 14 | + |
| 15 | +const CORE_TOOL_RESULT = [ |
| 16 | + 'bash', |
| 17 | + 'file_read', |
| 18 | + 'file_edit', |
| 19 | + 'file_write', |
| 20 | + 'glob', |
| 21 | + 'grep', |
| 22 | + 'web_search', |
| 23 | + 'web_fetch', |
| 24 | + 'task', |
| 25 | + 'todo_write', |
| 26 | + 'user_input', |
| 27 | + 'plan', |
| 28 | +]; |
| 29 | + |
| 30 | +describe('TOOL_USE_RENDERERS', () => { |
| 31 | + it('registers core tool names', () => { |
| 32 | + for (const name of CORE_TOOL_USE) { |
| 33 | + expect(TOOL_USE_RENDERERS[name], name).toBeTypeOf('function'); |
| 34 | + } |
| 35 | + }); |
| 36 | + |
| 37 | + it('does not register the unknown dispatch sentinel as a tool renderer', () => { |
| 38 | + expect(Object.prototype.hasOwnProperty.call(TOOL_USE_RENDERERS, UNKNOWN_DISPATCH_KEY)).toBe(false); |
| 39 | + }); |
| 40 | + |
| 41 | + it('renderBashUse escapes HTML in command', () => { |
| 42 | + const html = renderToolUse({ |
| 43 | + name: 'Bash', |
| 44 | + input: { command: '<script>alert(1)</script>' }, |
| 45 | + }); |
| 46 | + expect(html).not.toContain('<script>'); |
| 47 | + expect(html).toContain('<script>'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('returns empty string for null or undefined tool', () => { |
| 51 | + expect(renderToolUse(null)).toBe(''); |
| 52 | + expect(renderToolUse(undefined)).toBe(''); |
| 53 | + }); |
| 54 | + |
| 55 | + it('renderReadUse escapes file path in body and summary', () => { |
| 56 | + const html = renderToolUse({ |
| 57 | + name: 'Read', |
| 58 | + input: { file_path: 'C:\\tmp\\<evil>.txt' }, |
| 59 | + }); |
| 60 | + expect(html).toContain('<evil>'); |
| 61 | + expect(html).not.toContain('<evil>'); |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +describe('TOOL_RESULT_RENDERERS', () => { |
| 66 | + it('registers core result types', () => { |
| 67 | + for (const rt of CORE_TOOL_RESULT) { |
| 68 | + expect(TOOL_RESULT_RENDERERS[rt], rt).toBeTypeOf('function'); |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + it('renderBashResult escapes stdout', () => { |
| 73 | + const html = renderToolResult({ |
| 74 | + result_type: 'bash', |
| 75 | + exit_code: 0, |
| 76 | + stdout: '<img onerror=alert(1)>', |
| 77 | + }); |
| 78 | + expect(html).not.toContain('<img'); |
| 79 | + expect(html).toContain('<img'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('renderBashResult avoids undefined in summary when exit_code missing', () => { |
| 83 | + const html = renderToolResult({ result_type: 'bash' }); |
| 84 | + expect(html).toContain('Bash Result (unknown)'); |
| 85 | + expect(html).not.toContain('undefined'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('returns empty string for null or undefined parsed', () => { |
| 89 | + expect(renderToolResult(null)).toBe(''); |
| 90 | + expect(renderToolResult(undefined)).toBe(''); |
| 91 | + }); |
| 92 | +}); |
| 93 | + |
| 94 | +describe('toolResultHasBody', () => { |
| 95 | + it('returns false for null or undefined', () => { |
| 96 | + expect(toolResultHasBody(null)).toBe(false); |
| 97 | + expect(toolResultHasBody(undefined)).toBe(false); |
| 98 | + }); |
| 99 | + |
| 100 | + it('returns true for bash with stdout or stderr', () => { |
| 101 | + expect(toolResultHasBody({ result_type: 'bash', stdout: 'ok' })).toBe(true); |
| 102 | + expect(toolResultHasBody({ result_type: 'bash', stderr: 'err' })).toBe(true); |
| 103 | + expect(toolResultHasBody({ result_type: 'bash' })).toBe(false); |
| 104 | + }); |
| 105 | + |
| 106 | + it('returns false for summary-only result types', () => { |
| 107 | + expect(toolResultHasBody({ result_type: 'file_read', file_path: '/a' })).toBe(false); |
| 108 | + expect(toolResultHasBody({ result_type: 'glob', num_files: 3 })).toBe(false); |
| 109 | + }); |
| 110 | + |
| 111 | + it('returns true for user_input and todo_write with todos', () => { |
| 112 | + expect(toolResultHasBody({ result_type: 'user_input' })).toBe(true); |
| 113 | + expect(toolResultHasBody({ result_type: 'todo_write', todos: [{ content: 'x' }] })).toBe(true); |
| 114 | + expect(toolResultHasBody({ result_type: 'todo_write', todo_count: 1 })).toBe(false); |
| 115 | + }); |
| 116 | + |
| 117 | + it('returns true for task when duration, retrieval, or description is set', () => { |
| 118 | + expect(toolResultHasBody({ result_type: 'task', description: 'subagent' })).toBe(true); |
| 119 | + expect(toolResultHasBody({ result_type: 'task', total_duration_ms: 100 })).toBe(true); |
| 120 | + expect(toolResultHasBody({ result_type: 'task', status: 'completed' })).toBe(false); |
| 121 | + }); |
| 122 | +}); |
| 123 | + |
| 124 | +describe('getToolSummary', () => { |
| 125 | + it('formats Bash summary', () => { |
| 126 | + expect(getToolSummary('Bash', { command: 'ls -la' })).toMatch(/Bash:/); |
| 127 | + expect(getToolSummary('Bash', { command: 'ls -la' })).toContain('ls -la'); |
| 128 | + }); |
| 129 | + |
| 130 | + it('formats Read summary as plain text (escaping deferred to wrapToolUse)', () => { |
| 131 | + expect(getToolSummary('Read', { file_path: 'a<b' })).toBe('Read: a<b'); |
| 132 | + }); |
| 133 | +}); |
| 134 | + |
| 135 | +describe('renderToolUse fallback', () => { |
| 136 | + it('uses JSON fallback for unknown tools', () => { |
| 137 | + const html = renderToolUse({ |
| 138 | + name: 'UnknownToolXYZ', |
| 139 | + input: { foo: 'bar' }, |
| 140 | + }); |
| 141 | + expect(html).toContain('tool-call'); |
| 142 | + expect(html).toContain('"foo"'); |
| 143 | + expect(TOOL_USE_RENDERERS.UnknownToolXYZ).toBeUndefined(); |
| 144 | + }); |
| 145 | + |
| 146 | + it('uses fallback when name is an inherited property (e.g. constructor)', () => { |
| 147 | + const html = renderToolUse({ |
| 148 | + name: 'constructor', |
| 149 | + input: { foo: 'bar' }, |
| 150 | + }); |
| 151 | + expect(html).toContain('tool-call'); |
| 152 | + expect(html).toContain('"foo"'); |
| 153 | + }); |
| 154 | + |
| 155 | + it('dispatches WebFetch to registered renderer (not generic unknown-tool fallback)', () => { |
| 156 | + expect(TOOL_USE_RENDERERS.WebFetch).toBe(renderWebFetchUse); |
| 157 | + const html = renderToolUse({ |
| 158 | + name: 'WebFetch', |
| 159 | + input: { url: 'https://example.com' }, |
| 160 | + }); |
| 161 | + expect(html).toContain('tool-call'); |
| 162 | + expect(html).toContain('example.com'); |
| 163 | + }); |
| 164 | + |
| 165 | + it('renders WebSearch via registry', () => { |
| 166 | + const html = renderToolUse({ |
| 167 | + name: 'WebSearch', |
| 168 | + input: { query: 'vitest registry' }, |
| 169 | + }); |
| 170 | + expect(html).toContain('tool-call'); |
| 171 | + expect(html).toContain('vitest registry'); |
| 172 | + }); |
| 173 | +}); |
| 174 | + |
| 175 | +describe('renderTodoWriteResult', () => { |
| 176 | + it('summary count matches parsed.todos length when todos are present', () => { |
| 177 | + const html = renderToolResult({ |
| 178 | + result_type: 'todo_write', |
| 179 | + todo_count: 99, |
| 180 | + todos: [ |
| 181 | + { status: 'pending', content: 'one' }, |
| 182 | + { status: 'completed', content: 'two' }, |
| 183 | + ], |
| 184 | + }); |
| 185 | + expect(html).toContain('Todos updated (2 items)'); |
| 186 | + expect(html).not.toContain('99 items'); |
| 187 | + }); |
| 188 | +}); |
| 189 | + |
| 190 | +describe('renderToolResult fallback', () => { |
| 191 | + it('renders summary-only for unknown result types', () => { |
| 192 | + const html = renderToolResult({ result_type: 'custom_type' }); |
| 193 | + expect(html).toContain('Tool result (custom_type)'); |
| 194 | + expect(html).toContain('tool-result'); |
| 195 | + }); |
| 196 | + |
| 197 | + it('uses fallback when result_type is an inherited property (e.g. constructor)', () => { |
| 198 | + const html = renderToolResult({ result_type: 'constructor' }); |
| 199 | + expect(html).toContain('Tool result (constructor)'); |
| 200 | + expect(html).toContain('tool-result'); |
| 201 | + expect(Object.prototype.hasOwnProperty.call(TOOL_RESULT_RENDERERS, 'constructor')).toBe(false); |
| 202 | + }); |
| 203 | +}); |
0 commit comments