-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathtool_test.go
More file actions
762 lines (685 loc) · 19.2 KB
/
tool_test.go
File metadata and controls
762 lines (685 loc) · 19.2 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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
package ai
import (
"context"
"fmt"
"reflect"
"strings"
"testing"
)
// Example of a simple typed tool using the function approach
type CalculatorInput struct {
Expression string `json:"expression" description:"Mathematical expression to evaluate"`
}
func TestTypedToolFuncExample(t *testing.T) {
// Create a typed tool using the function API
tool := NewAgentTool(
"calculator",
"Evaluates simple mathematical expressions",
func(ctx context.Context, input CalculatorInput, _ ToolCall) (ToolResponse, error) {
if input.Expression == "2+2" {
return NewTextResponse("4"), nil
}
return NewTextErrorResponse("unsupported expression"), nil
},
)
// Check the tool info
info := tool.Info()
if info.Name != "calculator" {
t.Errorf("Expected tool name 'calculator', got %s", info.Name)
}
if len(info.Required) != 1 || info.Required[0] != "expression" {
t.Errorf("Expected required field 'expression', got %v", info.Required)
}
// Test execution
call := ToolCall{
ID: "test-1",
Name: "calculator",
Input: `{"expression": "2+2"}`,
}
result, err := tool.Run(context.Background(), call)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if result.Content != "4" {
t.Errorf("Expected result '4', got %s", result.Content)
}
if result.IsError {
t.Errorf("Expected successful result, got error")
}
}
func TestEnumToolExample(t *testing.T) {
type WeatherInput struct {
Location string `json:"location" description:"City name"`
Units string `json:"units" enum:"celsius,fahrenheit" description:"Temperature units"`
}
// Create a weather tool with enum support
tool := NewAgentTool(
"weather",
"Gets current weather for a location",
func(ctx context.Context, input WeatherInput, _ ToolCall) (ToolResponse, error) {
temp := "22°C"
if input.Units == "fahrenheit" {
temp = "72°F"
}
return NewTextResponse(fmt.Sprintf("Weather in %s: %s, sunny", input.Location, temp)), nil
},
)
// Check that the schema includes enum values
info := tool.Info()
unitsParam, ok := info.Parameters["units"].(map[string]any)
if !ok {
t.Fatal("Expected units parameter to exist")
}
enumValues, ok := unitsParam["enum"].([]any)
if !ok || len(enumValues) != 2 {
t.Errorf("Expected 2 enum values, got %v", enumValues)
}
// Test execution with enum value
call := ToolCall{
ID: "test-2",
Name: "weather",
Input: `{"location": "San Francisco", "units": "fahrenheit"}`,
}
result, err := tool.Run(context.Background(), call)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if !strings.Contains(result.Content, "San Francisco") {
t.Errorf("Expected result to contain 'San Francisco', got %s", result.Content)
}
if !strings.Contains(result.Content, "72°F") {
t.Errorf("Expected result to contain '72°F', got %s", result.Content)
}
}
func TestEnumSupport(t *testing.T) {
// Test enum via struct tags
type WeatherInput struct {
Location string `json:"location" description:"City name"`
Units string `json:"units" enum:"celsius,fahrenheit,kelvin" description:"Temperature units"`
Format string `json:"format,omitempty" enum:"json,xml,text"`
}
schema := generateSchema(reflect.TypeOf(WeatherInput{}))
if schema.Type != "object" {
t.Errorf("Expected schema type 'object', got %s", schema.Type)
}
// Check units field has enum values
unitsSchema := schema.Properties["units"]
if unitsSchema == nil {
t.Fatal("Expected units property to exist")
}
if len(unitsSchema.Enum) != 3 {
t.Errorf("Expected 3 enum values for units, got %d", len(unitsSchema.Enum))
}
expectedUnits := []string{"celsius", "fahrenheit", "kelvin"}
for i, expected := range expectedUnits {
if unitsSchema.Enum[i] != expected {
t.Errorf("Expected enum value %s, got %v", expected, unitsSchema.Enum[i])
}
}
// Check required fields (format should not be required due to omitempty)
expectedRequired := []string{"location", "units"}
if len(schema.Required) != len(expectedRequired) {
t.Errorf("Expected %d required fields, got %d", len(expectedRequired), len(schema.Required))
}
}
func TestSchemaToParameters(t *testing.T) {
schema := Schema{
Type: "object",
Properties: map[string]*Schema{
"name": {
Type: "string",
Description: "The name field",
},
"age": {
Type: "integer",
Minimum: func() *float64 { v := 0.0; return &v }(),
Maximum: func() *float64 { v := 120.0; return &v }(),
},
"tags": {
Type: "array",
Items: &Schema{
Type: "string",
},
},
"priority": {
Type: "string",
Enum: []any{"low", "medium", "high"},
},
},
Required: []string{"name"},
}
params := schemaToParameters(schema)
// Check name parameter
nameParam, ok := params["name"].(map[string]any)
if !ok {
t.Fatal("Expected name parameter to exist")
}
if nameParam["type"] != "string" {
t.Errorf("Expected name type 'string', got %v", nameParam["type"])
}
if nameParam["description"] != "The name field" {
t.Errorf("Expected name description 'The name field', got %v", nameParam["description"])
}
// Check age parameter with min/max
ageParam, ok := params["age"].(map[string]any)
if !ok {
t.Fatal("Expected age parameter to exist")
}
if ageParam["type"] != "integer" {
t.Errorf("Expected age type 'integer', got %v", ageParam["type"])
}
if ageParam["minimum"] != 0.0 {
t.Errorf("Expected age minimum 0, got %v", ageParam["minimum"])
}
if ageParam["maximum"] != 120.0 {
t.Errorf("Expected age maximum 120, got %v", ageParam["maximum"])
}
// Check priority parameter with enum
priorityParam, ok := params["priority"].(map[string]any)
if !ok {
t.Fatal("Expected priority parameter to exist")
}
if priorityParam["type"] != "string" {
t.Errorf("Expected priority type 'string', got %v", priorityParam["type"])
}
enumValues, ok := priorityParam["enum"].([]any)
if !ok || len(enumValues) != 3 {
t.Errorf("Expected 3 enum values, got %v", enumValues)
}
}
func TestGenerateSchemaBasicTypes(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input any
expected Schema
}{
{
name: "string type",
input: "",
expected: Schema{Type: "string"},
},
{
name: "int type",
input: 0,
expected: Schema{Type: "integer"},
},
{
name: "int64 type",
input: int64(0),
expected: Schema{Type: "integer"},
},
{
name: "uint type",
input: uint(0),
expected: Schema{Type: "integer"},
},
{
name: "float64 type",
input: 0.0,
expected: Schema{Type: "number"},
},
{
name: "float32 type",
input: float32(0.0),
expected: Schema{Type: "number"},
},
{
name: "bool type",
input: false,
expected: Schema{Type: "boolean"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
schema := generateSchema(reflect.TypeOf(tt.input))
if schema.Type != tt.expected.Type {
t.Errorf("Expected type %s, got %s", tt.expected.Type, schema.Type)
}
})
}
}
func TestGenerateSchemaArrayTypes(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input any
expected Schema
}{
{
name: "string slice",
input: []string{},
expected: Schema{
Type: "array",
Items: &Schema{Type: "string"},
},
},
{
name: "int slice",
input: []int{},
expected: Schema{
Type: "array",
Items: &Schema{Type: "integer"},
},
},
{
name: "string array",
input: [3]string{},
expected: Schema{
Type: "array",
Items: &Schema{Type: "string"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
schema := generateSchema(reflect.TypeOf(tt.input))
if schema.Type != tt.expected.Type {
t.Errorf("Expected type %s, got %s", tt.expected.Type, schema.Type)
}
if schema.Items == nil {
t.Fatal("Expected items schema to exist")
}
if schema.Items.Type != tt.expected.Items.Type {
t.Errorf("Expected items type %s, got %s", tt.expected.Items.Type, schema.Items.Type)
}
})
}
}
func TestGenerateSchemaMapTypes(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input any
expected string
}{
{
name: "string to string map",
input: map[string]string{},
expected: "object",
},
{
name: "string to int map",
input: map[string]int{},
expected: "object",
},
{
name: "int to string map",
input: map[int]string{},
expected: "object",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
schema := generateSchema(reflect.TypeOf(tt.input))
if schema.Type != tt.expected {
t.Errorf("Expected type %s, got %s", tt.expected, schema.Type)
}
})
}
}
func TestGenerateSchemaStructTypes(t *testing.T) {
t.Parallel()
type SimpleStruct struct {
Name string `json:"name" description:"The name field"`
Age int `json:"age"`
}
type StructWithOmitEmpty struct {
Required string `json:"required"`
Optional string `json:"optional,omitempty"`
}
type StructWithJSONIgnore struct {
Visible string `json:"visible"`
Hidden string `json:"-"`
}
type StructWithoutJSONTags struct {
FirstName string
LastName string
}
tests := []struct {
name string
input any
validate func(t *testing.T, schema Schema)
}{
{
name: "simple struct",
input: SimpleStruct{},
validate: func(t *testing.T, schema Schema) {
if schema.Type != "object" {
t.Errorf("Expected type object, got %s", schema.Type)
}
if len(schema.Properties) != 2 {
t.Errorf("Expected 2 properties, got %d", len(schema.Properties))
}
if schema.Properties["name"] == nil {
t.Error("Expected name property to exist")
}
if schema.Properties["name"].Description != "The name field" {
t.Errorf("Expected description 'The name field', got %s", schema.Properties["name"].Description)
}
if len(schema.Required) != 2 {
t.Errorf("Expected 2 required fields, got %d", len(schema.Required))
}
},
},
{
name: "struct with omitempty",
input: StructWithOmitEmpty{},
validate: func(t *testing.T, schema Schema) {
if len(schema.Required) != 1 {
t.Errorf("Expected 1 required field, got %d", len(schema.Required))
}
if schema.Required[0] != "required" {
t.Errorf("Expected required field 'required', got %s", schema.Required[0])
}
},
},
{
name: "struct with json ignore",
input: StructWithJSONIgnore{},
validate: func(t *testing.T, schema Schema) {
if len(schema.Properties) != 1 {
t.Errorf("Expected 1 property, got %d", len(schema.Properties))
}
if schema.Properties["visible"] == nil {
t.Error("Expected visible property to exist")
}
if schema.Properties["hidden"] != nil {
t.Error("Expected hidden property to not exist")
}
},
},
{
name: "struct without json tags",
input: StructWithoutJSONTags{},
validate: func(t *testing.T, schema Schema) {
if schema.Properties["first_name"] == nil {
t.Error("Expected first_name property to exist")
}
if schema.Properties["last_name"] == nil {
t.Error("Expected last_name property to exist")
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
schema := generateSchema(reflect.TypeOf(tt.input))
tt.validate(t, schema)
})
}
}
func TestGenerateSchemaPointerTypes(t *testing.T) {
t.Parallel()
type StructWithPointers struct {
Name *string `json:"name"`
Age *int `json:"age"`
}
schema := generateSchema(reflect.TypeOf(StructWithPointers{}))
if schema.Type != "object" {
t.Errorf("Expected type object, got %s", schema.Type)
}
if schema.Properties["name"] == nil {
t.Fatal("Expected name property to exist")
}
if schema.Properties["name"].Type != "string" {
t.Errorf("Expected name type string, got %s", schema.Properties["name"].Type)
}
if schema.Properties["age"] == nil {
t.Fatal("Expected age property to exist")
}
if schema.Properties["age"].Type != "integer" {
t.Errorf("Expected age type integer, got %s", schema.Properties["age"].Type)
}
}
func TestGenerateSchemaNestedStructs(t *testing.T) {
t.Parallel()
type Address struct {
Street string `json:"street"`
City string `json:"city"`
}
type Person struct {
Name string `json:"name"`
Address Address `json:"address"`
}
schema := generateSchema(reflect.TypeOf(Person{}))
if schema.Type != "object" {
t.Errorf("Expected type object, got %s", schema.Type)
}
if schema.Properties["address"] == nil {
t.Fatal("Expected address property to exist")
}
addressSchema := schema.Properties["address"]
if addressSchema.Type != "object" {
t.Errorf("Expected address type object, got %s", addressSchema.Type)
}
if addressSchema.Properties["street"] == nil {
t.Error("Expected street property in address to exist")
}
if addressSchema.Properties["city"] == nil {
t.Error("Expected city property in address to exist")
}
}
func TestGenerateSchemaRecursiveStructs(t *testing.T) {
t.Parallel()
type Node struct {
Value string `json:"value"`
Next *Node `json:"next,omitempty"`
}
schema := generateSchema(reflect.TypeOf(Node{}))
if schema.Type != "object" {
t.Errorf("Expected type object, got %s", schema.Type)
}
if schema.Properties["value"] == nil {
t.Error("Expected value property to exist")
}
if schema.Properties["next"] == nil {
t.Error("Expected next property to exist")
}
// The recursive reference should be handled gracefully
nextSchema := schema.Properties["next"]
if nextSchema.Type != "object" {
t.Errorf("Expected next type object, got %s", nextSchema.Type)
}
}
func TestGenerateSchemaWithEnumTags(t *testing.T) {
t.Parallel()
type ConfigInput struct {
Level string `json:"level" enum:"debug,info,warn,error" description:"Log level"`
Format string `json:"format" enum:"json,text"`
Optional string `json:"optional,omitempty" enum:"a,b,c"`
}
schema := generateSchema(reflect.TypeOf(ConfigInput{}))
// Check level field
levelSchema := schema.Properties["level"]
if levelSchema == nil {
t.Fatal("Expected level property to exist")
}
if len(levelSchema.Enum) != 4 {
t.Errorf("Expected 4 enum values for level, got %d", len(levelSchema.Enum))
}
expectedLevels := []string{"debug", "info", "warn", "error"}
for i, expected := range expectedLevels {
if levelSchema.Enum[i] != expected {
t.Errorf("Expected enum value %s, got %v", expected, levelSchema.Enum[i])
}
}
// Check format field
formatSchema := schema.Properties["format"]
if formatSchema == nil {
t.Fatal("Expected format property to exist")
}
if len(formatSchema.Enum) != 2 {
t.Errorf("Expected 2 enum values for format, got %d", len(formatSchema.Enum))
}
// Check required fields (optional should not be required due to omitempty)
expectedRequired := []string{"level", "format"}
if len(schema.Required) != len(expectedRequired) {
t.Errorf("Expected %d required fields, got %d", len(expectedRequired), len(schema.Required))
}
}
func TestGenerateSchemaComplexTypes(t *testing.T) {
t.Parallel()
type ComplexInput struct {
StringSlice []string `json:"string_slice"`
IntMap map[string]int `json:"int_map"`
NestedSlice []map[string]string `json:"nested_slice"`
Interface any `json:"interface"`
}
schema := generateSchema(reflect.TypeOf(ComplexInput{}))
// Check string slice
stringSliceSchema := schema.Properties["string_slice"]
if stringSliceSchema == nil {
t.Fatal("Expected string_slice property to exist")
}
if stringSliceSchema.Type != "array" {
t.Errorf("Expected string_slice type array, got %s", stringSliceSchema.Type)
}
if stringSliceSchema.Items.Type != "string" {
t.Errorf("Expected string_slice items type string, got %s", stringSliceSchema.Items.Type)
}
// Check int map
intMapSchema := schema.Properties["int_map"]
if intMapSchema == nil {
t.Fatal("Expected int_map property to exist")
}
if intMapSchema.Type != "object" {
t.Errorf("Expected int_map type object, got %s", intMapSchema.Type)
}
// Check nested slice
nestedSliceSchema := schema.Properties["nested_slice"]
if nestedSliceSchema == nil {
t.Fatal("Expected nested_slice property to exist")
}
if nestedSliceSchema.Type != "array" {
t.Errorf("Expected nested_slice type array, got %s", nestedSliceSchema.Type)
}
if nestedSliceSchema.Items.Type != "object" {
t.Errorf("Expected nested_slice items type object, got %s", nestedSliceSchema.Items.Type)
}
// Check interface
interfaceSchema := schema.Properties["interface"]
if interfaceSchema == nil {
t.Fatal("Expected interface property to exist")
}
if interfaceSchema.Type != "object" {
t.Errorf("Expected interface type object, got %s", interfaceSchema.Type)
}
}
func TestToSnakeCase(t *testing.T) {
t.Parallel()
tests := []struct {
input string
expected string
}{
{"FirstName", "first_name"},
{"XMLHttpRequest", "x_m_l_http_request"},
{"ID", "i_d"},
{"HTTPSProxy", "h_t_t_p_s_proxy"},
{"simple", "simple"},
{"", ""},
{"A", "a"},
{"AB", "a_b"},
{"CamelCase", "camel_case"},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
t.Parallel()
result := toSnakeCase(tt.input)
if result != tt.expected {
t.Errorf("toSnakeCase(%s) = %s, expected %s", tt.input, result, tt.expected)
}
})
}
}
func TestSchemaToParametersEdgeCases(t *testing.T) {
t.Parallel()
tests := []struct {
name string
schema Schema
expected map[string]any
}{
{
name: "non-object schema",
schema: Schema{
Type: "string",
},
expected: map[string]any{},
},
{
name: "object with no properties",
schema: Schema{
Type: "object",
Properties: nil,
},
expected: map[string]any{},
},
{
name: "object with empty properties",
schema: Schema{
Type: "object",
Properties: map[string]*Schema{},
},
expected: map[string]any{},
},
{
name: "schema with all constraint types",
schema: Schema{
Type: "object",
Properties: map[string]*Schema{
"text": {
Type: "string",
Format: "email",
MinLength: func() *int { v := 5; return &v }(),
MaxLength: func() *int { v := 100; return &v }(),
},
"number": {
Type: "number",
Minimum: func() *float64 { v := 0.0; return &v }(),
Maximum: func() *float64 { v := 100.0; return &v }(),
},
},
},
expected: map[string]any{
"text": map[string]any{
"type": "string",
"format": "email",
"minLength": 5,
"maxLength": 100,
},
"number": map[string]any{
"type": "number",
"minimum": 0.0,
"maximum": 100.0,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := schemaToParameters(tt.schema)
if len(result) != len(tt.expected) {
t.Errorf("Expected %d parameters, got %d", len(tt.expected), len(result))
}
for key, expectedValue := range tt.expected {
if result[key] == nil {
t.Errorf("Expected parameter %s to exist", key)
continue
}
// Deep comparison would be complex, so we'll check key properties
resultParam := result[key].(map[string]any)
expectedParam := expectedValue.(map[string]any)
for propKey, propValue := range expectedParam {
if resultParam[propKey] != propValue {
t.Errorf("Expected %s.%s = %v, got %v", key, propKey, propValue, resultParam[propKey])
}
}
}
})
}
}