-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathregistry.test.js
More file actions
257 lines (228 loc) · 9.61 KB
/
Copy pathregistry.test.js
File metadata and controls
257 lines (228 loc) · 9.61 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import { describe, it, expect } from 'vitest';
import {
TOOL_USE_RENDERERS,
TOOL_RESULT_RENDERERS,
renderToolUse,
renderToolResult,
getToolSummary,
toolResultHasBody,
} from './registry.js';
import { UNKNOWN_DISPATCH_KEY } from './constants.js';
import { renderWebFetchUse } from './tool_use/web_fetch.js';
const CORE_TOOL_USE = ['Bash', 'Read', 'Write', 'Edit', 'Glob', 'Grep', 'Task', 'TodoWrite', 'AskUserQuestion', 'WebFetch', 'WebSearch'];
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('does not register the unknown dispatch sentinel as a tool renderer', () => {
expect(Object.prototype.hasOwnProperty.call(TOOL_USE_RENDERERS, UNKNOWN_DISPATCH_KEY)).toBe(false);
});
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('returns empty string for null or undefined tool', () => {
expect(renderToolUse(null)).toBe('');
expect(renderToolUse(undefined)).toBe('');
});
it('renderReadUse escapes file path in body and summary', () => {
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');
});
it('renderBashResult avoids undefined in summary when exit_code missing', () => {
const html = renderToolResult({ result_type: 'bash' });
expect(html).toContain('Bash Result (unknown)');
expect(html).not.toContain('undefined');
});
it('returns empty string for null or undefined parsed', () => {
expect(renderToolResult(null)).toBe('');
expect(renderToolResult(undefined)).toBe('');
});
});
describe('toolResultHasBody', () => {
it('returns false for null or undefined', () => {
expect(toolResultHasBody(null)).toBe(false);
expect(toolResultHasBody(undefined)).toBe(false);
});
it('returns true for bash with stdout or stderr', () => {
expect(toolResultHasBody({ result_type: 'bash', stdout: 'ok' })).toBe(true);
expect(toolResultHasBody({ result_type: 'bash', stderr: 'err' })).toBe(true);
expect(toolResultHasBody({ result_type: 'bash' })).toBe(false);
});
it('returns false for summary-only result types', () => {
expect(toolResultHasBody({ result_type: 'file_read', file_path: '/a' })).toBe(false);
expect(toolResultHasBody({ result_type: 'glob', num_files: 3 })).toBe(false);
});
it('returns true for user_input and todo_write with todos', () => {
expect(toolResultHasBody({ result_type: 'user_input' })).toBe(true);
expect(toolResultHasBody({ result_type: 'todo_write', todos: [{ content: 'x' }] })).toBe(true);
expect(toolResultHasBody({ result_type: 'todo_write', todo_count: 1 })).toBe(false);
});
it('returns true for task when duration, retrieval, or description is set', () => {
expect(toolResultHasBody({ result_type: 'task', description: 'subagent' })).toBe(true);
expect(toolResultHasBody({ result_type: 'task', total_duration_ms: 100 })).toBe(true);
expect(toolResultHasBody({ result_type: 'task', status: 'completed' })).toBe(false);
});
});
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 as plain text (escaping deferred to wrapToolUse)', () => {
expect(getToolSummary('Read', { file_path: 'a<b' })).toBe('Read: a<b');
});
});
describe('renderToolUse fallback', () => {
it('uses JSON fallback for unknown tools with raw type name', () => {
const html = renderToolUse({
name: 'UnknownToolXYZ',
input: { foo: 'bar' },
});
expect(html).toContain('tool-call');
expect(html).toContain('Unknown tool: UnknownToolXYZ');
expect(html).toContain('"foo"');
expect(TOOL_USE_RENDERERS.UnknownToolXYZ).toBeUndefined();
});
it('uses fallback when name is an inherited property (e.g. constructor)', () => {
const html = renderToolUse({
name: 'constructor',
input: { foo: 'bar' },
});
expect(html).toContain('tool-call');
expect(html).toContain('"foo"');
});
it('dispatches WebFetch to registered renderer (not generic unknown-tool fallback)', () => {
expect(TOOL_USE_RENDERERS.WebFetch).toBe(renderWebFetchUse);
const html = renderToolUse({
name: 'WebFetch',
input: { url: 'https://example.com' },
});
expect(html).toContain('tool-call');
expect(html).toContain('example.com');
});
it('renders WebSearch via registry', () => {
const html = renderToolUse({
name: 'WebSearch',
input: { query: 'vitest registry' },
});
expect(html).toContain('tool-call');
expect(html).toContain('vitest registry');
});
});
describe('renderTodoWriteResult', () => {
it('summary count matches parsed.todos length when todos are present', () => {
const html = renderToolResult({
result_type: 'todo_write',
todo_count: 99,
todos: [
{ status: 'pending', content: 'one' },
{ status: 'completed', content: 'two' },
],
});
expect(html).toContain('Todos updated (2 items)');
expect(html).not.toContain('99 items');
});
});
/** Representative fixtures for registry-driven behavioral smoke tests. */
const TOOL_USE_FIXTURES = {
Bash: { input: { command: 'echo hi', description: 'say hi' } },
Read: { input: { file_path: 'README.md' } },
Write: { input: { file_path: 'out.txt', content: 'data' } },
Edit: { input: { file_path: 'a.js', old_string: 'x', new_string: 'y' } },
Glob: { input: { pattern: '*.js', path: 'src' } },
Grep: { input: { pattern: 'TODO', path: 'lib' } },
Task: { input: { subagent_type: 'explore', description: 'scan', prompt: 'go' } },
TodoWrite: { input: { todos: [{ status: 'pending', content: 'task' }] } },
AskUserQuestion: { input: { questions: [{ question: 'OK?' }] } },
WebFetch: { input: { url: 'https://example.com' } },
WebSearch: { input: { query: 'vitest' } },
};
const TOOL_RESULT_FIXTURES = {
bash: { exit_code: 0, stdout: 'ok' },
file_read: { file_path: '/a.txt', num_lines: 10 },
file_edit: { file_path: 'b.js' },
file_write: { file_path: 'c.txt' },
glob: { num_files: 3 },
grep: { num_files: 2, num_lines: 5 },
web_search: { query: 'test', result_count: 1 },
web_fetch: { url: 'https://x.com', status_code: 200 },
task: { status: 'completed', total_duration_ms: 1000 },
todo_write: { todos: [{ status: 'pending', content: 'x' }] },
user_input: { questions: [{ question: 'Q' }], answers: { Q: 'A' } },
plan: { file_path: 'plan.md' },
};
describe('registry behavioral smoke tests', () => {
it('every registered tool_use renderer produces non-empty HTML', () => {
for (const name of CORE_TOOL_USE) {
const html = renderToolUse({ name, ...TOOL_USE_FIXTURES[name] });
expect(html, name).toContain('tool-call');
expect(html.length, name).toBeGreaterThan(20);
}
});
it('every registered tool_result renderer produces non-empty HTML', () => {
for (const rt of CORE_TOOL_RESULT) {
const html = renderToolResult({ result_type: rt, ...TOOL_RESULT_FIXTURES[rt] });
expect(html, rt).toContain('tool-result');
expect(html.length, rt).toBeGreaterThan(20);
}
});
});
describe('renderToolResult fallback', () => {
it('renders raw result type and JSON payload for unknown result types', () => {
const html = renderToolResult({
result_type: 'custom_type',
payload: { answer: 42 },
});
expect(html).toContain('Unknown tool result: custom_type');
expect(html).toContain('tool-result');
expect(html).toContain('"answer"');
expect(html).toContain('42');
});
it('uses fallback when result_type is an inherited property (e.g. constructor)', () => {
const html = renderToolResult({ result_type: 'constructor' });
expect(html).toContain('Unknown tool result: constructor');
expect(html).toContain('tool-result');
expect(Object.prototype.hasOwnProperty.call(TOOL_RESULT_RENDERERS, 'constructor')).toBe(false);
});
});