forked from 0xeb/copilot-sdk-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_tool_builder.cpp
More file actions
501 lines (414 loc) · 16.8 KB
/
Copy pathtest_tool_builder.cpp
File metadata and controls
501 lines (414 loc) · 16.8 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
// Copyright (c) 2025 Elias Bachaalany
// SPDX-License-Identifier: MIT
/// @file test_tool_builder.cpp
/// @brief Tests for the fluent tool builder API
#include <gtest/gtest.h>
#include <copilot/tool_builder.hpp>
using namespace copilot;
// =============================================================================
// Schema Generation Tests
// =============================================================================
TEST(ToolBuilderTest, SchemaTypeTraits)
{
// Basic types - use STREQ for C-string comparison
EXPECT_STREQ(detail::schema_type<int>::type_name, "integer");
EXPECT_STREQ(detail::schema_type<long>::type_name, "integer");
EXPECT_STREQ(detail::schema_type<double>::type_name, "number");
EXPECT_STREQ(detail::schema_type<float>::type_name, "number");
EXPECT_STREQ(detail::schema_type<bool>::type_name, "boolean");
EXPECT_STREQ(detail::schema_type<std::string>::type_name, "string");
// Array type
auto arr_schema = detail::schema_type<std::vector<int>>::schema();
EXPECT_EQ(arr_schema["type"], "array");
EXPECT_EQ(arr_schema["items"]["type"], "integer");
}
TEST(ToolBuilderTest, SingleParamTool)
{
auto echo = ToolBuilder("echo", "Echo back input")
.param<std::string>("message", "Message to echo")
.handler([](const std::string& msg) { return msg; });
EXPECT_EQ(echo.name, "echo");
EXPECT_EQ(echo.description, "Echo back input");
// Check schema
EXPECT_EQ(echo.parameters_schema["type"], "object");
EXPECT_TRUE(echo.parameters_schema["properties"].contains("message"));
EXPECT_EQ(echo.parameters_schema["properties"]["message"]["type"], "string");
EXPECT_EQ(echo.parameters_schema["properties"]["message"]["description"], "Message to echo");
// Required should contain "message"
auto& required = echo.parameters_schema["required"];
EXPECT_EQ(required.size(), 1);
EXPECT_EQ(required[0], "message");
}
TEST(ToolBuilderTest, MultiParamTool)
{
auto calc = ToolBuilder("calc", "Calculator")
.param<double>("a", "First number")
.param<double>("b", "Second number")
.param<std::string>("op", "Operation")
.handler([](double a, double b, const std::string& op) {
if (op == "add")
return std::to_string(a + b);
if (op == "sub")
return std::to_string(a - b);
return std::string("unknown");
});
EXPECT_EQ(calc.name, "calc");
// Check all params present
auto& props = calc.parameters_schema["properties"];
EXPECT_TRUE(props.contains("a"));
EXPECT_TRUE(props.contains("b"));
EXPECT_TRUE(props.contains("op"));
EXPECT_EQ(props["a"]["type"], "number");
EXPECT_EQ(props["b"]["type"], "number");
EXPECT_EQ(props["op"]["type"], "string");
// All should be required
auto& required = calc.parameters_schema["required"];
EXPECT_EQ(required.size(), 3);
}
TEST(ToolBuilderTest, EnumConstraint)
{
auto mood = ToolBuilder("set_mood", "Set mood")
.param<std::string>("mood", "Mood to set")
.one_of("happy", "sad", "neutral")
.handler([](const std::string& m) { return "Mood: " + m; });
auto& mood_prop = mood.parameters_schema["properties"]["mood"];
EXPECT_TRUE(mood_prop.contains("enum"));
auto& enum_vals = mood_prop["enum"];
EXPECT_EQ(enum_vals.size(), 3);
EXPECT_EQ(enum_vals[0], "happy");
EXPECT_EQ(enum_vals[1], "sad");
EXPECT_EQ(enum_vals[2], "neutral");
}
TEST(ToolBuilderTest, DefaultValue)
{
auto fetch = ToolBuilder("fetch", "Fetch URL")
.param<std::string>("url", "URL to fetch")
.param<int>("timeout", "Timeout in seconds")
.default_value(30)
.handler([](const std::string& url, int timeout) {
return url + " (timeout=" + std::to_string(timeout) + ")";
});
// URL should be required
auto& required = fetch.parameters_schema["required"];
bool url_required = false;
bool timeout_required = false;
for (const auto& r : required)
{
if (r == "url")
url_required = true;
if (r == "timeout")
timeout_required = true;
}
EXPECT_TRUE(url_required);
EXPECT_FALSE(timeout_required); // timeout has default, so not required
}
// =============================================================================
// Handler Invocation Tests
// =============================================================================
TEST(ToolBuilderTest, HandlerInvocationSuccess)
{
auto add = ToolBuilder("add", "Add numbers")
.param<double>("a", "First")
.param<double>("b", "Second")
.handler([](double a, double b) { return std::to_string(a + b); });
ToolInvocation inv;
inv.session_id = "test";
inv.tool_call_id = "call1";
inv.tool_name = "add";
inv.arguments = json{{"a", 10.0}, {"b", 32.0}};
auto result = add.handler(inv);
EXPECT_EQ(result.result_type, ToolResultType::Success);
EXPECT_EQ(result.text_result_for_llm, "42.000000");
}
TEST(ToolBuilderTest, HandlerInvocationWithStrings)
{
auto greet = ToolBuilder("greet", "Greet someone")
.param<std::string>("name", "Name")
.handler([](const std::string& name) { return "Hello, " + name + "!"; });
ToolInvocation inv;
inv.session_id = "test";
inv.tool_call_id = "call1";
inv.tool_name = "greet";
inv.arguments = json{{"name", "World"}};
auto result = greet.handler(inv);
EXPECT_EQ(result.result_type, ToolResultType::Success);
EXPECT_EQ(result.text_result_for_llm, "Hello, World!");
}
TEST(ToolBuilderTest, HandlerWithDefaultValue)
{
auto fetch = ToolBuilder("fetch", "Fetch")
.param<std::string>("url", "URL")
.param<int>("timeout", "Timeout")
.default_value(30)
.handler([](const std::string& url, int timeout) {
return url + ":" + std::to_string(timeout);
});
// Without timeout provided
ToolInvocation inv1;
inv1.arguments = json{{"url", "http://example.com"}};
auto result1 = fetch.handler(inv1);
EXPECT_EQ(result1.text_result_for_llm, "http://example.com:30");
// With timeout provided
ToolInvocation inv2;
inv2.arguments = json{{"url", "http://example.com"}, {"timeout", 60}};
auto result2 = fetch.handler(inv2);
EXPECT_EQ(result2.text_result_for_llm, "http://example.com:60");
}
TEST(ToolBuilderTest, HandlerErrorHandling)
{
auto div = ToolBuilder("divide", "Divide")
.param<double>("a", "Numerator")
.param<double>("b", "Denominator")
.handler([](double a, double b) -> std::string {
if (b == 0)
throw std::runtime_error("Division by zero");
return std::to_string(a / b);
});
ToolInvocation inv;
inv.arguments = json{{"a", 10.0}, {"b", 0.0}};
auto result = div.handler(inv);
EXPECT_EQ(result.result_type, ToolResultType::Failure);
EXPECT_TRUE(result.error.has_value());
EXPECT_EQ(*result.error, "Division by zero");
}
TEST(ToolBuilderTest, MissingRequiredArg)
{
auto greet = ToolBuilder("greet", "Greet")
.param<std::string>("name", "Name")
.handler([](const std::string& name) { return "Hi " + name; });
ToolInvocation inv;
inv.arguments = json::object(); // Missing "name"
auto result = greet.handler(inv);
EXPECT_EQ(result.result_type, ToolResultType::Failure);
EXPECT_TRUE(result.error.has_value());
}
// =============================================================================
// Struct-based Builder Tests (Option B)
// =============================================================================
struct SearchArgs
{
std::string query;
int limit;
NLOHMANN_DEFINE_TYPE_INTRUSIVE(SearchArgs, query, limit)
};
TEST(ToolBuilderTest, StructBasedBuilder)
{
auto search = ToolBuilder::create<SearchArgs>("search", "Search documents")
.describe(&SearchArgs::query, "query", "Search query text")
.describe(&SearchArgs::limit, "limit", "Maximum results")
.handler([](const SearchArgs& args) {
return "Searching: " + args.query + " (limit=" + std::to_string(args.limit) + ")";
});
EXPECT_EQ(search.name, "search");
EXPECT_EQ(search.description, "Search documents");
// Check schema
auto& props = search.parameters_schema["properties"];
EXPECT_TRUE(props.contains("query"));
EXPECT_TRUE(props.contains("limit"));
}
TEST(ToolBuilderTest, StructBasedHandlerInvocation)
{
auto search = ToolBuilder::create<SearchArgs>("search", "Search")
.describe(&SearchArgs::query, "query", "Query")
.describe(&SearchArgs::limit, "limit", "Limit")
.handler([](const SearchArgs& args) {
return args.query + ":" + std::to_string(args.limit);
});
ToolInvocation inv;
inv.arguments = json{{"query", "test"}, {"limit", 5}};
auto result = search.handler(inv);
EXPECT_EQ(result.result_type, ToolResultType::Success);
EXPECT_EQ(result.text_result_for_llm, "test:5");
}
// =============================================================================
// Convenience Function Tests
// =============================================================================
TEST(ToolBuilderTest, ConvenienceFunction)
{
auto echo = copilot::tool("echo", "Echo")
.param<std::string>("msg", "Message")
.handler([](const std::string& m) { return m; });
EXPECT_EQ(echo.name, "echo");
}
// =============================================================================
// Integration with SessionConfig
// =============================================================================
TEST(ToolBuilderTest, SessionConfigIntegration)
{
auto tool1 = ToolBuilder("tool1", "First tool")
.param<std::string>("x", "X param")
.handler([](const std::string& x) { return x; });
auto tool2 = ToolBuilder("tool2", "Second tool")
.param<int>("n", "N param")
.handler([](int n) { return std::to_string(n * 2); });
SessionConfig config;
config.tools = {tool1, tool2};
EXPECT_EQ(config.tools.size(), 2);
EXPECT_EQ(config.tools[0].name, "tool1");
EXPECT_EQ(config.tools[1].name, "tool2");
}
// =============================================================================
// Backward Compatibility Tests
// =============================================================================
TEST(ToolBuilderTest, BackwardCompatibility)
{
// Old way should still work
Tool old_tool;
old_tool.name = "old_tool";
old_tool.description = "Created the old way";
old_tool.parameters_schema = json{
{"type", "object"},
{"properties", {{"x", {{"type", "string"}}}}},
{"required", {"x"}}};
old_tool.handler = [](const ToolInvocation& inv) -> ToolResultObject {
ToolResultObject r;
r.text_result_for_llm = "old style";
r.result_type = ToolResultType::Success;
return r;
};
EXPECT_EQ(old_tool.name, "old_tool");
ToolInvocation inv;
inv.arguments = json{{"x", "test"}};
auto result = old_tool.handler(inv);
EXPECT_EQ(result.text_result_for_llm, "old style");
}
// =============================================================================
// make_tool Function Tests (Claude SDK compatible API)
// =============================================================================
TEST(MakeToolTest, SingleParam)
{
auto tool = copilot::make_tool(
"echo", "Echo message", [](std::string msg) { return msg; }, {"message"});
EXPECT_EQ(tool.name, "echo");
EXPECT_EQ(tool.description, "Echo message");
// Check schema
EXPECT_EQ(tool.parameters_schema["type"], "object");
EXPECT_TRUE(tool.parameters_schema["properties"].contains("message"));
EXPECT_EQ(tool.parameters_schema["properties"]["message"]["type"], "string");
// Test invocation
ToolInvocation inv;
inv.arguments = json{{"message", "Hello"}};
auto result = tool.handler(inv);
EXPECT_EQ(result.text_result_for_llm, "Hello");
}
TEST(MakeToolTest, MultipleParams)
{
auto tool = copilot::make_tool(
"calc", "Calculator",
[](double a, double b) { return std::to_string(a + b); },
{"first", "second"});
EXPECT_EQ(tool.name, "calc");
// Check schema
auto& props = tool.parameters_schema["properties"];
EXPECT_TRUE(props.contains("first"));
EXPECT_TRUE(props.contains("second"));
EXPECT_EQ(props["first"]["type"], "number");
EXPECT_EQ(props["second"]["type"], "number");
// Test invocation
ToolInvocation inv;
inv.arguments = json{{"first", 10.0}, {"second", 32.0}};
auto result = tool.handler(inv);
EXPECT_EQ(result.text_result_for_llm, "42.000000");
}
TEST(MakeToolTest, AutoParamNames)
{
// Use make_tool without param names - should get arg0, arg1, etc.
auto tool = copilot::make_tool(
"greet", "Greet",
[](std::string name, int count) { return name + ":" + std::to_string(count); });
auto& props = tool.parameters_schema["properties"];
EXPECT_TRUE(props.contains("arg0"));
EXPECT_TRUE(props.contains("arg1"));
// Test invocation
ToolInvocation inv;
inv.arguments = json{{"arg0", "test"}, {"arg1", 5}};
auto result = tool.handler(inv);
EXPECT_EQ(result.text_result_for_llm, "test:5");
}
TEST(MakeToolTest, ParamCountMismatch)
{
EXPECT_THROW(
copilot::make_tool(
"bad", "Bad",
[](std::string a, std::string b) { return a + b; },
{"only_one"} // Mismatch: 2 params, 1 name
),
std::invalid_argument);
}
TEST(MakeToolTest, ErrorHandling)
{
auto tool = copilot::make_tool(
"fail", "Always fails",
[](std::string) -> std::string { throw std::runtime_error("boom"); },
{"input"});
ToolInvocation inv;
inv.arguments = json{{"input", "test"}};
auto result = tool.handler(inv);
EXPECT_EQ(result.result_type, ToolResultType::Failure);
EXPECT_TRUE(result.error.has_value());
EXPECT_EQ(*result.error, "boom");
}
TEST(MakeToolTest, IntAndBoolParams)
{
auto tool = copilot::make_tool(
"config", "Config tool",
[](int port, bool enabled) {
return "port=" + std::to_string(port) + ",enabled=" + (enabled ? "true" : "false");
},
{"port", "enabled"});
auto& props = tool.parameters_schema["properties"];
EXPECT_EQ(props["port"]["type"], "integer");
EXPECT_EQ(props["enabled"]["type"], "boolean");
ToolInvocation inv;
inv.arguments = json{{"port", 8080}, {"enabled", true}};
auto result = tool.handler(inv);
EXPECT_EQ(result.text_result_for_llm, "port=8080,enabled=true");
}
// =============================================================================
// Fluent Setter Tests (PR #11 contribution by @tilakpatell)
// =============================================================================
TEST(ToolFluentSetterTest, SkipPermissionOnLvalue)
{
Tool tool;
tool.name = "safe_read";
tool.description = "Read-only lookup";
EXPECT_FALSE(tool.skip_permission);
tool.with_skip_permission();
EXPECT_TRUE(tool.skip_permission);
tool.with_skip_permission(false);
EXPECT_FALSE(tool.skip_permission);
}
TEST(ToolFluentSetterTest, OverridesBuiltInOnLvalue)
{
Tool tool;
tool.name = "edit_file";
tool.description = "Custom editor";
EXPECT_FALSE(tool.overrides_built_in_tool);
tool.with_overrides_built_in_tool();
EXPECT_TRUE(tool.overrides_built_in_tool);
tool.with_overrides_built_in_tool(false);
EXPECT_FALSE(tool.overrides_built_in_tool);
}
TEST(ToolFluentSetterTest, ChainingOnMakeToolResult)
{
auto tool = copilot::make_tool(
"safe_lookup", "Read-only lookup",
[](std::string query) { return "result: " + query; },
{"query"})
.with_skip_permission()
.with_overrides_built_in_tool();
EXPECT_EQ(tool.name, "safe_lookup");
EXPECT_TRUE(tool.skip_permission);
EXPECT_TRUE(tool.overrides_built_in_tool);
}
TEST(ToolFluentSetterTest, ChainingOnToolBuilder)
{
auto tool = ToolBuilder("my_tool", "A tool")
.param<std::string>("input", "Input text")
.handler([](const std::string& s) { return s; })
.with_skip_permission()
.with_overrides_built_in_tool();
EXPECT_TRUE(tool.skip_permission);
EXPECT_TRUE(tool.overrides_built_in_tool);
EXPECT_EQ(tool.name, "my_tool");
}