forked from feiskyer/swarm-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
276 lines (251 loc) · 6.3 KB
/
util_test.go
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
package swarm
import (
"reflect"
"sort"
"strings"
"testing"
)
func TestMapToStruct(t *testing.T) {
type testStruct struct {
Name string `json:"name"`
Age int `json:"age"`
IsAdmin bool `json:"is_admin"`
}
tests := []struct {
name string
input map[string]interface{}
expected testStruct
wantErr bool
}{
{
name: "valid conversion",
input: map[string]interface{}{
"name": "John",
"age": 30,
"is_admin": true,
},
expected: testStruct{
Name: "John",
Age: 30,
IsAdmin: true,
},
wantErr: false,
},
{
name: "invalid type",
input: map[string]interface{}{
"name": "John",
"age": "invalid",
"is_admin": true,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var result testStruct
err := mapToStruct(tt.input, &result)
if tt.wantErr {
if err == nil {
t.Error("Expected error but got none")
}
return
}
if err != nil {
t.Errorf("Unexpected error: %v", err)
return
}
if result != tt.expected {
t.Errorf("Expected %+v, got %+v", tt.expected, result)
}
})
}
}
func TestFormatArgs(t *testing.T) {
tests := []struct {
name string
args map[string]interface{}
expected string
}{
{
name: "simple args",
args: map[string]interface{}{
"name": "John",
"age": 30,
},
expected: "age=30, name=John",
},
{
name: "empty args",
args: map[string]interface{}{},
expected: "",
},
{
name: "complex args",
args: map[string]interface{}{
"nested": map[string]interface{}{
"key": "value",
},
"array": []interface{}{1, 2, 3},
},
expected: "array=[1 2 3], nested=map[key:value]",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := formatArgs(tt.args)
// Sort both strings for comparison since map iteration order is random
if sortString(result) != sortString(tt.expected) {
t.Errorf("Expected %q, got %q", tt.expected, result)
}
})
}
}
// Helper function to sort comma-separated strings
func sortString(s string) string {
parts := strings.Split(s, ", ")
sort.Strings(parts)
return strings.Join(parts, ", ")
}
func TestStreamResponse(t *testing.T) {
tests := []struct {
name string
input map[string]interface{}
check func(*testing.T, StreamResponse)
}{
{
name: "content only",
input: map[string]interface{}{
"content": "test content",
"sender": "TestAgent",
},
check: func(t *testing.T, sr StreamResponse) {
if sr.Content != "test content" {
t.Errorf("Expected content 'test content', got %q", sr.Content)
}
if sr.Sender != "TestAgent" {
t.Errorf("Expected sender 'TestAgent', got %q", sr.Sender)
}
},
},
{
name: "tool calls",
input: map[string]interface{}{
"tool_calls": []map[string]interface{}{
{
"function": map[string]interface{}{
"name": "test_func",
"arguments": "{}",
},
},
},
},
check: func(t *testing.T, sr StreamResponse) {
if len(sr.ToolCalls) != 1 {
t.Errorf("Expected 1 tool call, got %d", len(sr.ToolCalls))
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var sr StreamResponse
if err := mapToStruct(tt.input, &sr); err != nil {
t.Errorf("Unexpected error: %v", err)
return
}
tt.check(t, sr)
})
}
}
func TestFunctionToJSON(t *testing.T) {
testFunc := func(args map[string]interface{}) (interface{}, error) {
return "test", nil
}
result := FunctionToJSON(NewAgentFunction(
"testFunc",
"Test function description",
testFunc,
[]Parameter{{Name: "name", Type: reflect.TypeOf("string")}},
))
if result["type"] != "function" {
t.Error("Expected type to be 'function'")
}
function, ok := result["function"].(map[string]interface{})
if !ok {
t.Fatal("Expected function field to be a map")
}
if function["name"] == "" {
t.Error("Expected non-empty function name")
}
params, ok := function["parameters"].(map[string]interface{})
if !ok {
t.Fatal("Expected parameters field to be a map")
}
if params["type"] != "object" {
t.Error("Expected parameters type to be 'object'")
}
}
func TestMergeFields(t *testing.T) {
target := map[string]interface{}{
"a": "original",
"nested": map[string]interface{}{
"x": 1,
},
}
source := map[string]interface{}{
"a": "new",
"b": "added",
"nested": map[string]interface{}{
"y": 2,
},
}
MergeFields(target, source)
if target["a"] != "new" {
t.Error("Expected value to be overwritten")
}
if target["b"] != "added" {
t.Error("Expected new field to be added")
}
nested, ok := target["nested"].(map[string]interface{})
if !ok {
t.Fatal("Expected nested to remain a map")
}
if nested["x"] != 1 || nested["y"] != 2 {
t.Error("Expected nested fields to be merged correctly")
}
}
func TestGetJSONType(t *testing.T) {
tests := []struct {
input reflect.Type
expected string
}{
{input: reflect.TypeOf("string"), expected: "string"},
{input: reflect.TypeOf(123), expected: "integer"},
{input: reflect.TypeOf(int8(1)), expected: "integer"},
{input: reflect.TypeOf(int16(1)), expected: "integer"},
{input: reflect.TypeOf(int32(1)), expected: "integer"},
{input: reflect.TypeOf(int64(1)), expected: "integer"},
{input: reflect.TypeOf(uint(1)), expected: "integer"},
{input: reflect.TypeOf(uint8(1)), expected: "integer"},
{input: reflect.TypeOf(uint16(1)), expected: "integer"},
{input: reflect.TypeOf(uint32(1)), expected: "integer"},
{input: reflect.TypeOf(uint64(1)), expected: "integer"},
{input: reflect.TypeOf(123.45), expected: "number"},
{input: reflect.TypeOf(float32(1.23)), expected: "number"},
{input: reflect.TypeOf(float64(1.23)), expected: "number"},
{input: reflect.TypeOf(true), expected: "boolean"},
{input: reflect.TypeOf([]int{}), expected: "array"},
{input: reflect.TypeOf([3]int{}), expected: "array"},
{input: reflect.TypeOf(map[string]interface{}{}), expected: "object"},
{input: reflect.TypeOf(struct{}{}), expected: "object"},
{input: reflect.TypeOf(interface{}(nil)), expected: "string"},
{input: reflect.TypeOf(nil), expected: "string"},
}
for _, tt := range tests {
got := getJSONType(tt.input)
if got != tt.expected {
t.Errorf("getJSONType(%v) = %v, want %v", tt.input, got, tt.expected)
}
}
}