-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse_test.go
More file actions
374 lines (299 loc) · 8.04 KB
/
response_test.go
File metadata and controls
374 lines (299 loc) · 8.04 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
package sdk
import (
"encoding/json"
"strings"
"testing"
)
func TestContentPartTypes(t *testing.T) {
t.Run("TextPart", func(t *testing.T) {
part := &TextPart{
Text: "Hello, world!",
Format: TextFormatPlain,
}
if part.Type() != PartTypeText {
t.Errorf("expected type %s, got %s", PartTypeText, part.Type())
}
data, err := part.ToJSON()
if err != nil {
t.Fatalf("ToJSON failed: %v", err)
}
if !strings.Contains(string(data), `"text"`) {
t.Error("JSON should contain type field")
}
})
t.Run("CodePart", func(t *testing.T) {
part := &CodePart{
Code: "func main() {}",
Language: "go",
Copyable: true,
}
if part.Type() != PartTypeCode {
t.Errorf("expected type %s, got %s", PartTypeCode, part.Type())
}
data, err := part.ToJSON()
if err != nil {
t.Fatalf("ToJSON failed: %v", err)
}
if !strings.Contains(string(data), `"code"`) {
t.Error("JSON should contain code type")
}
})
t.Run("TablePart", func(t *testing.T) {
part := &TablePart{
Headers: []TableHeader{
{Label: "Name", Key: "name"},
{Label: "Age", Key: "age"},
},
Rows: [][]TableCell{
{{Value: "Alice", Display: "Alice"}, {Value: 30, Display: "30"}},
{{Value: "Bob", Display: "Bob"}, {Value: 25, Display: "25"}},
},
}
if part.Type() != PartTypeTable {
t.Errorf("expected type %s, got %s", PartTypeTable, part.Type())
}
})
t.Run("CardPart", func(t *testing.T) {
part := &CardPart{
Title: "My Card",
Description: "A test card",
Actions: []CardAction{
{Label: "Click me", Action: "click"},
},
}
if part.Type() != PartTypeCard {
t.Errorf("expected type %s, got %s", PartTypeCard, part.Type())
}
})
t.Run("ListPart", func(t *testing.T) {
part := &ListPart{
Items: []ListItem{
{Text: "Item 1"},
{Text: "Item 2"},
},
Ordered: true,
}
if part.Type() != PartTypeList {
t.Errorf("expected type %s, got %s", PartTypeList, part.Type())
}
})
t.Run("AlertPart", func(t *testing.T) {
part := &AlertPart{
Message: "Warning!",
Severity: AlertWarning,
}
if part.Type() != PartTypeAlert {
t.Errorf("expected type %s, got %s", PartTypeAlert, part.Type())
}
})
t.Run("ThinkingPart", func(t *testing.T) {
part := &ThinkingPart{
Content: "Let me think about this...",
}
if part.Type() != PartTypeThinking {
t.Errorf("expected type %s, got %s", PartTypeThinking, part.Type())
}
})
}
func TestResponseBuilder(t *testing.T) {
t.Run("build simple response", func(t *testing.T) {
builder := NewResponseBuilder()
response := builder.
AddText("Hello").
AddCode("print('hi')", "python").
AddDivider().
Build()
if len(response.Parts) != 3 {
t.Errorf("expected 3 parts, got %d", len(response.Parts))
}
if response.ID == "" {
t.Error("response should have an ID")
}
})
t.Run("build response with table", func(t *testing.T) {
builder := NewResponseBuilder()
response := builder.
AddTable(
[]string{"Name", "Age"},
[][]any{{"Alice", 30}, {"Bob", 25}},
).
Build()
if len(response.Parts) != 1 {
t.Errorf("expected 1 part, got %d", len(response.Parts))
}
tablePart, ok := response.Parts[0].(*TablePart)
if !ok {
t.Fatal("expected TablePart")
}
if len(tablePart.Headers) != 2 {
t.Errorf("expected 2 headers, got %d", len(tablePart.Headers))
}
if len(tablePart.Rows) != 2 {
t.Errorf("expected 2 rows, got %d", len(tablePart.Rows))
}
})
t.Run("build response with list", func(t *testing.T) {
builder := NewResponseBuilder()
response := builder.
AddList([]string{"First", "Second", "Third"}, true).
Build()
listPart, ok := response.Parts[0].(*ListPart)
if !ok {
t.Fatal("expected ListPart")
}
if !listPart.Ordered {
t.Error("list should be ordered")
}
if len(listPart.Items) != 3 {
t.Errorf("expected 3 items, got %d", len(listPart.Items))
}
})
t.Run("build response with metadata", func(t *testing.T) {
builder := NewResponseBuilder()
response := builder.
WithMetadata("gpt-4", "openai", 1000).
WithTokenUsage(100, 50).
AddText("Test").
Build()
if response.Metadata.Model != "gpt-4" {
t.Errorf("expected model gpt-4, got %s", response.Metadata.Model)
}
if response.Metadata.Provider != "openai" {
t.Errorf("expected provider openai, got %s", response.Metadata.Provider)
}
if response.Metadata.InputTokens != 100 {
t.Errorf("expected 100 input tokens, got %d", response.Metadata.InputTokens)
}
})
}
func TestResponseParser(t *testing.T) {
parser := NewResponseParser()
t.Run("parse plain text", func(t *testing.T) {
parts := parser.Parse("Hello, world!")
if len(parts) != 1 {
t.Fatalf("expected 1 part, got %d", len(parts))
}
textPart, ok := parts[0].(*TextPart)
if !ok {
t.Fatal("expected TextPart")
}
if textPart.Text != "Hello, world!" {
t.Errorf("expected 'Hello, world!', got '%s'", textPart.Text)
}
})
t.Run("parse code block", func(t *testing.T) {
content := "Here's some code:\n\n```python\nprint('hello')\n```\n\nThat's it."
parts := parser.Parse(content)
if len(parts) < 2 {
t.Fatalf("expected at least 2 parts, got %d", len(parts))
}
// Find the code part
var codePart *CodePart
for _, part := range parts {
if cp, ok := part.(*CodePart); ok {
codePart = cp
break
}
}
if codePart == nil {
t.Fatal("expected to find a CodePart")
}
if codePart.Language != "python" {
t.Errorf("expected language 'python', got '%s'", codePart.Language)
}
if !strings.Contains(codePart.Code, "print") {
t.Error("code should contain 'print'")
}
})
t.Run("parse thinking block", func(t *testing.T) {
content := "<thinking>Let me think about this...</thinking>\n\nHere's my answer."
parts := parser.Parse(content)
// Find the thinking part
var thinkingPart *ThinkingPart
for _, part := range parts {
if tp, ok := part.(*ThinkingPart); ok {
thinkingPart = tp
break
}
}
if thinkingPart == nil {
t.Fatal("expected to find a ThinkingPart")
}
if !strings.Contains(thinkingPart.Content, "think about") {
t.Error("thinking content should contain 'think about'")
}
})
t.Run("parse markdown table", func(t *testing.T) {
content := `Here's a table:
| Name | Age |
|------|-----|
| Alice | 30 |
| Bob | 25 |
Done.`
parts := parser.Parse(content)
// Find the table part
var tablePart *TablePart
for _, part := range parts {
if tp, ok := part.(*TablePart); ok {
tablePart = tp
break
}
}
if tablePart == nil {
t.Fatal("expected to find a TablePart")
}
if len(tablePart.Headers) != 2 {
t.Errorf("expected 2 headers, got %d", len(tablePart.Headers))
}
if len(tablePart.Rows) != 2 {
t.Errorf("expected 2 rows, got %d", len(tablePart.Rows))
}
})
}
func TestStructuredResponseSerialization(t *testing.T) {
response := NewResponseBuilder().
WithID("test-123").
AddText("Hello").
AddCode("print('hi')", "python").
Build()
data, err := response.ToJSON()
if err != nil {
t.Fatalf("ToJSON failed: %v", err)
}
// Verify it's valid JSON
var decoded map[string]any
if err := json.Unmarshal(data, &decoded); err != nil {
t.Fatalf("invalid JSON: %v", err)
}
if decoded["id"] != "test-123" {
t.Errorf("expected id 'test-123', got %v", decoded["id"])
}
parts, ok := decoded["parts"].([]any)
if !ok {
t.Fatal("parts should be an array")
}
if len(parts) != 2 {
t.Errorf("expected 2 parts, got %d", len(parts))
}
}
func TestStructuredResponseToPlainText(t *testing.T) {
response := NewResponseBuilder().
AddText("Hello, world!").
AddCode("print('hi')", "python").
AddList([]string{"Item 1", "Item 2"}, true).
AddAlert("Warning!", AlertWarning).
Build()
plainText := response.ToPlainText()
if !strings.Contains(plainText, "Hello, world!") {
t.Error("plain text should contain text content")
}
if !strings.Contains(plainText, "```python") {
t.Error("plain text should contain code block")
}
if !strings.Contains(plainText, "1. Item 1") {
t.Error("plain text should contain numbered list")
}
if !strings.Contains(plainText, "[warning]") {
t.Error("plain text should contain alert")
}
}