-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathparamswrap_test.go
More file actions
303 lines (291 loc) · 7.85 KB
/
paramswrap_test.go
File metadata and controls
303 lines (291 loc) · 7.85 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
package messages
import (
"testing"
"github.com/anthropics/anthropic-sdk-go"
"github.com/stretchr/testify/require"
)
func TestMessageNewParamsWrapperUnmarshalJSON(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input string
expectedStream bool
checkContent func(t *testing.T, w *MessageNewParamsWrapper)
}{
{
name: "message with string content converts to array",
input: `{"model":"claude-3","max_tokens":1000,"messages":[{"role":"user","content":"Hello world"}]}`,
expectedStream: false,
checkContent: func(t *testing.T, w *MessageNewParamsWrapper) {
require.Len(t, w.Messages, 1)
require.Equal(t, anthropic.MessageParamRoleUser, w.Messages[0].Role)
text := w.Messages[0].Content[0].GetText()
require.NotNil(t, text)
require.Equal(t, "Hello world", *text)
},
},
{
name: "stream field extracted",
input: `{"model":"claude-3","max_tokens":1000,"stream":true,"messages":[{"role":"user","content":"Hi"}]}`,
expectedStream: true,
checkContent: func(t *testing.T, w *MessageNewParamsWrapper) {
require.Len(t, w.Messages, 1)
},
},
{
name: "stream false",
input: `{"model":"claude-3","max_tokens":1000,"stream":false,"messages":[{"role":"user","content":"Hi"}]}`,
expectedStream: false,
checkContent: nil,
},
{
name: "array content unchanged",
input: `{"model":"claude-3","max_tokens":1000,"messages":[{"role":"user","content":[{"type":"text","text":"Hello"}]}]}`,
expectedStream: false,
checkContent: func(t *testing.T, w *MessageNewParamsWrapper) {
require.Len(t, w.Messages, 1)
text := w.Messages[0].Content[0].GetText()
require.NotNil(t, text)
require.Equal(t, "Hello", *text)
},
},
{
name: "multiple messages with mixed content",
input: `{"model":"claude-3","max_tokens":1000,"messages":[{"role":"user","content":"First"},{"role":"assistant","content":[{"type":"text","text":"Response"}]},{"role":"user","content":"Second"}]}`,
expectedStream: false,
checkContent: func(t *testing.T, w *MessageNewParamsWrapper) {
require.Len(t, w.Messages, 3)
text0 := w.Messages[0].Content[0].GetText()
require.NotNil(t, text0)
require.Equal(t, "First", *text0)
text2 := w.Messages[2].Content[0].GetText()
require.NotNil(t, text2)
require.Equal(t, "Second", *text2)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var wrapper MessageNewParamsWrapper
err := wrapper.UnmarshalJSON([]byte(tt.input))
require.NoError(t, err)
require.Equal(t, tt.expectedStream, wrapper.Stream)
if tt.checkContent != nil {
tt.checkContent(t, &wrapper)
}
})
}
}
func TestShouldConvertContentField(t *testing.T) {
t.Parallel()
tests := []struct {
name string
obj map[string]any
expected bool
}{
{
name: "message with role",
obj: map[string]any{
"role": "user",
"content": "test",
},
expected: true,
},
{
name: "tool_result type",
obj: map[string]any{
"type": "tool_result",
"content": "result",
},
expected: true,
},
{
name: "mcp_tool_result type",
obj: map[string]any{
"type": "mcp_tool_result",
"content": "result",
},
expected: false,
},
{
name: "other type",
obj: map[string]any{
"type": "text",
"content": "text",
},
expected: false,
},
{
name: "no role or type",
obj: map[string]any{
"content": "test",
},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := shouldConvertContentField(tt.obj)
require.Equal(t, tt.expected, result)
})
}
}
func TestAnthropicLastUserPrompt(t *testing.T) {
t.Parallel()
tests := []struct {
name string
wrapper *MessageNewParamsWrapper
expected string
expectError bool
errorMsg string
}{
{
name: "nil struct",
expectError: true,
errorMsg: "nil struct",
},
{
name: "no messages",
wrapper: &MessageNewParamsWrapper{
MessageNewParams: anthropic.MessageNewParams{
Messages: []anthropic.MessageParam{},
},
},
expectError: true,
errorMsg: "no messages",
},
{
name: "last message not from user",
wrapper: &MessageNewParamsWrapper{
MessageNewParams: anthropic.MessageNewParams{
Messages: []anthropic.MessageParam{
{
Role: anthropic.MessageParamRoleUser,
Content: []anthropic.ContentBlockParamUnion{
anthropic.NewTextBlock("user message"),
},
},
{
Role: anthropic.MessageParamRoleAssistant,
Content: []anthropic.ContentBlockParamUnion{
anthropic.NewTextBlock("assistant message"),
},
},
},
},
},
},
{
name: "last user message with empty content",
wrapper: &MessageNewParamsWrapper{
MessageNewParams: anthropic.MessageNewParams{
Messages: []anthropic.MessageParam{
{
Role: anthropic.MessageParamRoleUser,
Content: []anthropic.ContentBlockParamUnion{},
},
},
},
},
},
{
name: "last user message with single text content",
wrapper: &MessageNewParamsWrapper{
MessageNewParams: anthropic.MessageNewParams{
Messages: []anthropic.MessageParam{
{
Role: anthropic.MessageParamRoleUser,
Content: []anthropic.ContentBlockParamUnion{
anthropic.NewTextBlock("Hello, world!"),
},
},
},
},
},
expected: "Hello, world!",
},
{
name: "last user message with multiple content blocks - text at end",
wrapper: &MessageNewParamsWrapper{
MessageNewParams: anthropic.MessageNewParams{
Messages: []anthropic.MessageParam{
{
Role: anthropic.MessageParamRoleUser,
Content: []anthropic.ContentBlockParamUnion{
anthropic.NewImageBlockBase64("image/png", "base64data"),
anthropic.NewTextBlock("First text"),
anthropic.NewImageBlockBase64("image/jpeg", "moredata"),
anthropic.NewTextBlock("Last text"),
},
},
},
},
},
expected: "Last text",
},
{
name: "last user message with only non-text content",
wrapper: &MessageNewParamsWrapper{
MessageNewParams: anthropic.MessageNewParams{
Messages: []anthropic.MessageParam{
{
Role: anthropic.MessageParamRoleUser,
Content: []anthropic.ContentBlockParamUnion{
anthropic.NewImageBlockBase64("image/png", "base64data"),
anthropic.NewImageBlockBase64("image/jpeg", "moredata"),
},
},
},
},
},
},
{
name: "multiple messages with last being user",
wrapper: &MessageNewParamsWrapper{
MessageNewParams: anthropic.MessageNewParams{
Messages: []anthropic.MessageParam{
{
Role: anthropic.MessageParamRoleUser,
Content: []anthropic.ContentBlockParamUnion{
anthropic.NewTextBlock("First user message"),
},
},
{
Role: anthropic.MessageParamRoleAssistant,
Content: []anthropic.ContentBlockParamUnion{
anthropic.NewTextBlock("Assistant response"),
},
},
{
Role: anthropic.MessageParamRoleUser,
Content: []anthropic.ContentBlockParamUnion{
anthropic.NewTextBlock("Second user message"),
},
},
},
},
},
expected: "Second user message",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := tt.wrapper.lastUserPrompt()
if tt.expectError {
require.Error(t, err)
require.Contains(t, err.Error(), tt.errorMsg)
require.Nil(t, result)
} else {
require.NoError(t, err)
// Check pointer equality - both nil or both non-nil
if tt.expected == "" {
require.Nil(t, result)
} else {
require.NotNil(t, result)
// The result should point to the same string from the content block
require.Equal(t, tt.expected, *result)
}
}
})
}
}