tool_test.go
453 lines1
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
package assistant_test
import (
"encoding/json"
"testing"
"congo.gg/pkg/assistant"
)
func TestToolBuilderBasic(t *testing.T) {
tool := assistant.NewTool("search", "Search the web").
String("query", "Search query", true).
Build()
if tool.Name != "search" {
t.Errorf("Name = %q, want %q", tool.Name, "search")
}
if tool.Description != "Search the web" {
t.Errorf("Description = %q, want %q", tool.Description, "Search the web")
}
if tool.Parameters == nil {
t.Fatal("Parameters is nil")
}
if tool.Parameters.Type != "object" {
t.Errorf("Parameters.Type = %q, want %q", tool.Parameters.Type, "object")
}
prop, ok := tool.Parameters.Properties["query"]
if !ok {
t.Fatal("missing property 'query'")
}
if prop.Type != "string" {
t.Errorf("query.Type = %q, want %q", prop.Type, "string")
}
if prop.Description != "Search query" {
t.Errorf("query.Description = %q, want %q", prop.Description, "Search query")
}
if len(tool.Parameters.Required) != 1 || tool.Parameters.Required[0] != "query" {
t.Errorf("Required = %v, want [query]", tool.Parameters.Required)
}
}
func TestToolBuilderAllTypes(t *testing.T) {
tool := assistant.NewTool("complex_tool", "A tool with all param types").
String("name", "A name", true).
Int("count", "Number of items", true).
Number("price", "Item price", false).
Bool("active", "Is active", false).
Enum("status", "Current status", []string{"open", "closed", "pending"}, true).
Array("tags", "List of tags", "string", false).
Build()
if tool.Parameters == nil {
t.Fatal("Parameters is nil")
}
// Verify each property type
tests := []struct {
name string
wantType string
}{
{"name", "string"},
{"count", "integer"},
{"price", "number"},
{"active", "boolean"},
{"status", "string"},
{"tags", "array"},
}
for _, tt := range tests {
prop, ok := tool.Parameters.Properties[tt.name]
if !ok {
t.Errorf("missing property %q", tt.name)
continue
}
if prop.Type != tt.wantType {
t.Errorf("%s.Type = %q, want %q", tt.name, prop.Type, tt.wantType)
}
}
// Verify enum values
statusProp := tool.Parameters.Properties["status"]
if len(statusProp.Enum) != 3 {
t.Fatalf("status.Enum length = %d, want 3", len(statusProp.Enum))
}
expectedEnums := []string{"open", "closed", "pending"}
for i, v := range expectedEnums {
if statusProp.Enum[i] != v {
t.Errorf("status.Enum[%d] = %q, want %q", i, statusProp.Enum[i], v)
}
}
// Verify array items
tagsProp := tool.Parameters.Properties["tags"]
if tagsProp.Items == nil {
t.Fatal("tags.Items is nil")
}
if tagsProp.Items.Type != "string" {
t.Errorf("tags.Items.Type = %q, want %q", tagsProp.Items.Type, "string")
}
// Verify required fields (name, count, status)
requiredSet := make(map[string]bool)
for _, r := range tool.Parameters.Required {
requiredSet[r] = true
}
if len(tool.Parameters.Required) != 3 {
t.Errorf("Required length = %d, want 3", len(tool.Parameters.Required))
}
for _, expected := range []string{"name", "count", "status"} {
if !requiredSet[expected] {
t.Errorf("expected %q in Required, got %v", expected, tool.Parameters.Required)
}
}
}
func TestToolBuilderNoParameters(t *testing.T) {
tool := assistant.NewTool("ping", "Ping the server").Build()
if tool.Name != "ping" {
t.Errorf("Name = %q, want %q", tool.Name, "ping")
}
if tool.Parameters != nil {
t.Errorf("Parameters should be nil for tool with no properties, got %+v", tool.Parameters)
}
}
func TestToolBuilderOptionalOnly(t *testing.T) {
tool := assistant.NewTool("configure", "Configure settings").
String("theme", "UI theme", false).
Bool("dark_mode", "Enable dark mode", false).
Build()
if tool.Parameters == nil {
t.Fatal("Parameters is nil")
}
if len(tool.Parameters.Required) != 0 {
t.Errorf("Required = %v, want empty", tool.Parameters.Required)
}
if len(tool.Parameters.Properties) != 2 {
t.Errorf("Properties length = %d, want 2", len(tool.Parameters.Properties))
}
}
func TestToolBuilderRequiredTracking(t *testing.T) {
tool := assistant.NewTool("test", "test").
String("a", "field a", true).
String("b", "field b", false).
String("c", "field c", true).
Int("d", "field d", false).
Int("e", "field e", true).
Build()
requiredSet := make(map[string]bool)
for _, r := range tool.Parameters.Required {
requiredSet[r] = true
}
if !requiredSet["a"] {
t.Error("expected 'a' in Required")
}
if requiredSet["b"] {
t.Error("did not expect 'b' in Required")
}
if !requiredSet["c"] {
t.Error("expected 'c' in Required")
}
if requiredSet["d"] {
t.Error("did not expect 'd' in Required")
}
if !requiredSet["e"] {
t.Error("expected 'e' in Required")
}
}
func TestToolCallParseArguments(t *testing.T) {
tc := assistant.ToolCall{
ID: "call_123",
Name: "get_weather",
Arguments: `{"city":"London","units":"celsius"}`,
}
var args struct {
City string `json:"city"`
Units string `json:"units"`
}
err := tc.ParseArguments(&args)
if err != nil {
t.Fatalf("ParseArguments returned error: %v", err)
}
if args.City != "London" {
t.Errorf("City = %q, want %q", args.City, "London")
}
if args.Units != "celsius" {
t.Errorf("Units = %q, want %q", args.Units, "celsius")
}
}
func TestToolCallParseArgumentsNumericFields(t *testing.T) {
tc := assistant.ToolCall{
ID: "call_456",
Name: "set_volume",
Arguments: `{"level":75,"muted":false}`,
}
var args struct {
Level int `json:"level"`
Muted bool `json:"muted"`
}
err := tc.ParseArguments(&args)
if err != nil {
t.Fatalf("ParseArguments returned error: %v", err)
}
if args.Level != 75 {
t.Errorf("Level = %d, want 75", args.Level)
}
if args.Muted != false {
t.Errorf("Muted = %v, want false", args.Muted)
}
}
func TestToolCallParseArgumentsNestedStruct(t *testing.T) {
tc := assistant.ToolCall{
ID: "call_789",
Name: "create_user",
Arguments: `{"name":"Alice","address":{"city":"NYC","zip":"10001"}}`,
}
var args struct {
Name string `json:"name"`
Address struct {
City string `json:"city"`
Zip string `json:"zip"`
} `json:"address"`
}
err := tc.ParseArguments(&args)
if err != nil {
t.Fatalf("ParseArguments returned error: %v", err)
}
if args.Name != "Alice" {
t.Errorf("Name = %q, want %q", args.Name, "Alice")
}
if args.Address.City != "NYC" {
t.Errorf("Address.City = %q, want %q", args.Address.City, "NYC")
}
if args.Address.Zip != "10001" {
t.Errorf("Address.Zip = %q, want %q", args.Address.Zip, "10001")
}
}
func TestToolCallParseArgumentsInvalidJSON(t *testing.T) {
tc := assistant.ToolCall{
ID: "call_bad",
Name: "test",
Arguments: `{not valid json`,
}
var args struct {
Field string `json:"field"`
}
err := tc.ParseArguments(&args)
if err == nil {
t.Error("expected error for invalid JSON, got nil")
}
}
func TestToolCallParseArgumentsEmptyObject(t *testing.T) {
tc := assistant.ToolCall{
ID: "call_empty",
Name: "ping",
Arguments: `{}`,
}
var args struct {
Field string `json:"field"`
}
err := tc.ParseArguments(&args)
if err != nil {
t.Fatalf("ParseArguments returned error: %v", err)
}
if args.Field != "" {
t.Errorf("Field = %q, want empty string", args.Field)
}
}
func TestParametersConstruction(t *testing.T) {
params := assistant.Parameters{
Type: "object",
Properties: map[string]assistant.Property{
"query": {Type: "string", Description: "search query"},
},
Required: []string{"query"},
}
if params.Type != "object" {
t.Errorf("Type = %q, want %q", params.Type, "object")
}
if len(params.Properties) != 1 {
t.Errorf("Properties length = %d, want 1", len(params.Properties))
}
if len(params.Required) != 1 || params.Required[0] != "query" {
t.Errorf("Required = %v, want [query]", params.Required)
}
}
func TestPropertyConstruction(t *testing.T) {
prop := assistant.Property{
Type: "string",
Description: "A test property",
Enum: []string{"a", "b", "c"},
}
if prop.Type != "string" {
t.Errorf("Type = %q, want %q", prop.Type, "string")
}
if prop.Description != "A test property" {
t.Errorf("Description = %q, want %q", prop.Description, "A test property")
}
if len(prop.Enum) != 3 {
t.Errorf("Enum length = %d, want 3", len(prop.Enum))
}
}
func TestPropertyWithItems(t *testing.T) {
prop := assistant.Property{
Type: "array",
Description: "list of IDs",
Items: &assistant.Items{Type: "string"},
}
if prop.Items == nil {
t.Fatal("Items is nil")
}
if prop.Items.Type != "string" {
t.Errorf("Items.Type = %q, want %q", prop.Items.Type, "string")
}
}
func TestItemsConstruction(t *testing.T) {
items := assistant.Items{Type: "integer"}
if items.Type != "integer" {
t.Errorf("Type = %q, want %q", items.Type, "integer")
}
}
func TestToolJSONSerialization(t *testing.T) {
tool := assistant.NewTool("weather", "Get weather").
String("city", "City name", true).
Enum("units", "Temperature units", []string{"celsius", "fahrenheit"}, false).
Build()
data, err := json.Marshal(tool)
if err != nil {
t.Fatalf("json.Marshal returned error: %v", err)
}
var decoded assistant.Tool
err = json.Unmarshal(data, &decoded)
if err != nil {
t.Fatalf("json.Unmarshal returned error: %v", err)
}
if decoded.Name != "weather" {
t.Errorf("decoded.Name = %q, want %q", decoded.Name, "weather")
}
if decoded.Description != "Get weather" {
t.Errorf("decoded.Description = %q, want %q", decoded.Description, "Get weather")
}
if decoded.Parameters == nil {
t.Fatal("decoded.Parameters is nil")
}
if decoded.Parameters.Type != "object" {
t.Errorf("decoded.Parameters.Type = %q, want %q", decoded.Parameters.Type, "object")
}
cityProp, ok := decoded.Parameters.Properties["city"]
if !ok {
t.Fatal("missing property 'city' after round-trip")
}
if cityProp.Type != "string" {
t.Errorf("city.Type = %q, want %q", cityProp.Type, "string")
}
unitsProp, ok := decoded.Parameters.Properties["units"]
if !ok {
t.Fatal("missing property 'units' after round-trip")
}
if len(unitsProp.Enum) != 2 {
t.Errorf("units.Enum length = %d, want 2", len(unitsProp.Enum))
}
}
func TestToolCallConstruction(t *testing.T) {
tc := assistant.ToolCall{
ID: "call_abc123",
Name: "search",
Arguments: `{"query":"Go"}`,
}
if tc.ID != "call_abc123" {
t.Errorf("ID = %q, want %q", tc.ID, "call_abc123")
}
if tc.Name != "search" {
t.Errorf("Name = %q, want %q", tc.Name, "search")
}
if tc.Arguments != `{"query":"Go"}` {
t.Errorf("Arguments = %q, want %q", tc.Arguments, `{"query":"Go"}`)
}
}
func TestToolBuilderArrayWithIntItems(t *testing.T) {
tool := assistant.NewTool("sum", "Sum numbers").
Array("numbers", "List of numbers", "integer", true).
Build()
prop := tool.Parameters.Properties["numbers"]
if prop.Type != "array" {
t.Errorf("Type = %q, want %q", prop.Type, "array")
}
if prop.Items == nil {
t.Fatal("Items is nil")
}
if prop.Items.Type != "integer" {
t.Errorf("Items.Type = %q, want %q", prop.Items.Type, "integer")
}
requiredSet := make(map[string]bool)
for _, r := range tool.Parameters.Required {
requiredSet[r] = true
}
if !requiredSet["numbers"] {
t.Error("expected 'numbers' in Required")
}
}
func TestToolCallParseArgumentsIntoMap(t *testing.T) {
tc := assistant.ToolCall{
ID: "call_map",
Name: "test",
Arguments: `{"key":"value","num":42}`,
}
var args map[string]any
err := tc.ParseArguments(&args)
if err != nil {
t.Fatalf("ParseArguments returned error: %v", err)
}
if args["key"] != "value" {
t.Errorf("args[key] = %v, want %q", args["key"], "value")
}
if args["num"] != float64(42) {
t.Errorf("args[num] = %v, want 42", args["num"])
}
}