Elffuss BraidHub

kikocis/continue public

main · 5.4 KiBRawPermalink
1import { extractToolCalls, getToolDisplayName } from "./index.js";
2
3describe("tools/index utilities", () => {
4 describe("getToolDisplayName", () => {
5 it("should return display name for known tool", () => {
6 // The actual implementation returns the displayName when tool is found
7 const result = getToolDisplayName("Read");
8 expect(result).toBe("Read");
9 });
10
11 it("should return original name for unknown tool", () => {
12 const result = getToolDisplayName("unknown_tool");
13 expect(result).toBe("unknown_tool");
14 });
15
16 it("should handle empty string", () => {
17 const result = getToolDisplayName("");
18 expect(result).toBe("");
19 });
20 });
21
22 describe("extractToolCalls", () => {
23 it("should extract single tool call", () => {
24 const response = `Here's the result:
25<tool>{"name": "Read", "arguments": {"filepath": "/path/to/file.txt"}}</tool>
26Done.`;
27
28 const result = extractToolCalls(response);
29 expect(result).toEqual([
30 {
31 name: "Read",
32 arguments: { filepath: "/path/to/file.txt" },
33 },
34 ]);
35 });
36
37 it("should extract multiple tool calls", () => {
38 const response = `First call:
39<tool>{"name": "Read", "arguments": {"filepath": "/file1.txt"}}</tool>
40Second call:
41<tool>{"name": "Write", "arguments": {"filepath": "/file2.txt", "content": "hello"}}</tool>
42Done.`;
43
44 const result = extractToolCalls(response);
45 expect(result).toEqual([
46 {
47 name: "Read",
48 arguments: { filepath: "/file1.txt" },
49 },
50 {
51 name: "Write",
52 arguments: { filepath: "/file2.txt", content: "hello" },
53 },
54 ]);
55 });
56
57 it("should handle tool calls with complex arguments", () => {
58 const response = `<tool>{"name": "Search", "arguments": {"pattern": "function.*test", "path": "/src", "options": {"recursive": true, "ignoreCase": false}}}</tool>`;
59
60 const result = extractToolCalls(response);
61 expect(result).toEqual([
62 {
63 name: "Search",
64 arguments: {
65 pattern: "function.*test",
66 path: "/src",
67 options: { recursive: true, ignoreCase: false },
68 },
69 },
70 ]);
71 });
72
73 it("should handle multiline tool calls", () => {
74 const response = `<tool>{
75 "name": "Write",
76 "arguments": {
77 "filepath": "/path/to/file.txt",
78 "content": "Line 1\\nLine 2\\nLine 3"
79 }
80}</tool>`;
81
82 const result = extractToolCalls(response);
83 expect(result).toEqual([
84 {
85 name: "Write",
86 arguments: {
87 filepath: "/path/to/file.txt",
88 content: "Line 1\nLine 2\nLine 3",
89 },
90 },
91 ]);
92 });
93
94 it("should return empty array when no tool calls found", () => {
95 const response = `This is just regular text with no tool calls.`;
96
97 const result = extractToolCalls(response);
98 expect(result).toEqual([]);
99 });
100
101 it("should handle empty response", () => {
102 const response = "";
103
104 const result = extractToolCalls(response);
105 expect(result).toEqual([]);
106 });
107
108 it("should ignore malformed tool calls", () => {
109 // Capture console.error to avoid polluting test output
110 const originalConsoleError = console.error;
111 console.error = () => {};
112
113 const response = `Good call:
114<tool>{"name": "Read", "arguments": {"filepath": "/file.txt"}}</tool>
115Bad call:
116<tool>{"name": "invalid_json", "arguments": {malformed json}}</tool>
117Another good call:
118<tool>{"name": "Write", "arguments": {"filepath": "/file2.txt", "content": "test"}}</tool>`;
119
120 const result = extractToolCalls(response);
121 expect(result).toEqual([
122 {
123 name: "Read",
124 arguments: { filepath: "/file.txt" },
125 },
126 {
127 name: "Write",
128 arguments: { filepath: "/file2.txt", content: "test" },
129 },
130 ]);
131
132 // Restore console.error
133 console.error = originalConsoleError;
134 });
135
136 it("should ignore tool calls missing required fields", () => {
137 const response = `Missing name:
138<tool>{"arguments": {"filepath": "/file.txt"}}</tool>
139Missing arguments:
140<tool>{"name": "Read"}</tool>
141Valid call:
142<tool>{"name": "Read", "arguments": {"filepath": "/file.txt"}}</tool>`;
143
144 const result = extractToolCalls(response);
145 expect(result).toEqual([
146 {
147 name: "Read",
148 arguments: { filepath: "/file.txt" },
149 },
150 ]);
151 });
152
153 it("should handle tool calls with empty arguments", () => {
154 const response = `<tool>{"name": "List", "arguments": {}}</tool>`;
155
156 const result = extractToolCalls(response);
157 expect(result).toEqual([
158 {
159 name: "List",
160 arguments: {},
161 },
162 ]);
163 });
164
165 it("should handle tool calls with null arguments", () => {
166 const response = `<tool>{"name": "exit", "arguments": null}</tool>`;
167
168 const result = extractToolCalls(response);
169 expect(result).toEqual([]);
170 });
171
172 it("should handle nested tool tags", () => {
173 const response = `<tool>{"name": "Write", "arguments": {"filepath": "/file.txt", "content": "Some content with <tool> tags inside"}}</tool>`;
174
175 const result = extractToolCalls(response);
176 expect(result).toEqual([
177 {
178 name: "Write",
179 arguments: {
180 filepath: "/file.txt",
181 content: "Some content with <tool> tags inside",
182 },
183 },
184 ]);
185 });
186 });
187});