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