-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparser_test.go
More file actions
284 lines (229 loc) · 7.85 KB
/
Copy pathparser_test.go
File metadata and controls
284 lines (229 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
package main
import (
"testing"
)
func TestParseClaudeRequest(t *testing.T) {
body := `{
"model": "claude-sonnet-4-20250514",
"max_tokens": 8096,
"messages": [
{"role": "user", "content": "What is 2+2?"}
]
}`
parsed := ParseRequestBody(body, "api.anthropic.com")
if parsed.Model != "claude-sonnet-4-20250514" {
t.Errorf("Expected claude-sonnet-4-20250514, got %s", parsed.Model)
}
if len(parsed.Messages) != 1 {
t.Fatalf("Expected 1 message, got %d", len(parsed.Messages))
}
if parsed.Messages[0].Role != "user" {
t.Errorf("Expected role user, got %s", parsed.Messages[0].Role)
}
if parsed.Messages[0].TextContent != "What is 2+2?" {
t.Errorf("Expected 'What is 2+2?', got %s", parsed.Messages[0].TextContent)
}
}
func TestParseClaudeResponse(t *testing.T) {
body := `{
"content": [
{"type": "text", "text": "2+2 equals 4."}
],
"usage": {"input_tokens": 10, "output_tokens": 8}
}`
parsed := ParseResponseBody(body, "api.anthropic.com")
if len(parsed.Content) != 1 {
t.Fatalf("Expected 1 content block, got %d", len(parsed.Content))
}
if parsed.Content[0].Type != "text" {
t.Errorf("Expected type text, got %s", parsed.Content[0].Type)
}
if parsed.Content[0].Text != "2+2 equals 4." {
t.Errorf("Expected '2+2 equals 4.', got %s", parsed.Content[0].Text)
}
}
func TestParseClaudeThinkingBlock(t *testing.T) {
body := `{
"content": [
{"type": "thinking", "thinking": "Let me calculate this step by step..."},
{"type": "text", "text": "The answer is 4."}
]
}`
parsed := ParseResponseBody(body, "api.anthropic.com")
if len(parsed.Content) != 2 {
t.Fatalf("Expected 2 content blocks, got %d", len(parsed.Content))
}
if parsed.Content[0].Type != "thinking" {
t.Errorf("Expected thinking block first")
}
if parsed.Content[0].Thinking != "Let me calculate this step by step..." {
t.Error("Thinking content not parsed correctly")
}
}
func TestParseClaudeToolUse(t *testing.T) {
body := `{
"content": [
{"type": "text", "text": "I'll read that file."},
{"type": "tool_use", "id": "tool_123", "name": "Read", "input": {"path": "/tmp/test.txt"}}
]
}`
parsed := ParseResponseBody(body, "api.anthropic.com")
if len(parsed.Content) != 2 {
t.Fatalf("Expected 2 content blocks, got %d", len(parsed.Content))
}
toolBlock := parsed.Content[1]
if toolBlock.Type != "tool_use" {
t.Errorf("Expected tool_use, got %s", toolBlock.Type)
}
if toolBlock.ToolName != "Read" {
t.Errorf("Expected tool name Read, got %s", toolBlock.ToolName)
}
}
func TestParseToolResultWithIsError(t *testing.T) {
// Request with tool_result containing is_error: true
body := `{
"model": "claude-sonnet-4-20250514",
"messages": [
{
"role": "user",
"content": [
{"type": "tool_result", "tool_use_id": "tool_123", "content": "Error: file not found", "is_error": true}
]
}
]
}`
parsed := ParseRequestBody(body, "api.anthropic.com")
if len(parsed.Messages) != 1 {
t.Fatalf("Expected 1 message, got %d", len(parsed.Messages))
}
if len(parsed.Messages[0].Content) != 1 {
t.Fatalf("Expected 1 content block, got %d", len(parsed.Messages[0].Content))
}
toolResult := parsed.Messages[0].Content[0]
if toolResult.Type != "tool_result" {
t.Errorf("Expected tool_result type, got %s", toolResult.Type)
}
if toolResult.ToolID != "tool_123" {
t.Errorf("Expected tool_use_id tool_123, got %s", toolResult.ToolID)
}
if !toolResult.IsError {
t.Error("Expected IsError to be true for tool_result with is_error: true")
}
}
func TestParseToolResultWithoutIsError(t *testing.T) {
// Request with tool_result without is_error (success case)
body := `{
"model": "claude-sonnet-4-20250514",
"messages": [
{
"role": "user",
"content": [
{"type": "tool_result", "tool_use_id": "tool_456", "content": "File contents here"}
]
}
]
}`
parsed := ParseRequestBody(body, "api.anthropic.com")
toolResult := parsed.Messages[0].Content[0]
if toolResult.IsError {
t.Error("Expected IsError to be false when is_error is not present")
}
}
func TestParseToolResultWithIsErrorFalse(t *testing.T) {
// Request with explicit is_error: false
body := `{
"model": "claude-sonnet-4-20250514",
"messages": [
{
"role": "user",
"content": [
{"type": "tool_result", "tool_use_id": "tool_789", "content": "Success", "is_error": false}
]
}
]
}`
parsed := ParseRequestBody(body, "api.anthropic.com")
toolResult := parsed.Messages[0].Content[0]
if toolResult.IsError {
t.Error("Expected IsError to be false when is_error: false")
}
}
func TestParseCacheTokens(t *testing.T) {
// Response with cache token fields
body := `{
"content": [{"type": "text", "text": "Hello"}],
"usage": {
"input_tokens": 100,
"output_tokens": 50,
"cache_read_input_tokens": 80,
"cache_creation_input_tokens": 20
}
}`
parsed := ParseResponseBody(body, "api.anthropic.com")
if parsed.Usage.InputTokens != 100 {
t.Errorf("Expected input_tokens 100, got %d", parsed.Usage.InputTokens)
}
if parsed.Usage.OutputTokens != 50 {
t.Errorf("Expected output_tokens 50, got %d", parsed.Usage.OutputTokens)
}
if parsed.Usage.CacheReadInputTokens != 80 {
t.Errorf("Expected cache_read_input_tokens 80, got %d", parsed.Usage.CacheReadInputTokens)
}
if parsed.Usage.CacheCreationInputTokens != 20 {
t.Errorf("Expected cache_creation_input_tokens 20, got %d", parsed.Usage.CacheCreationInputTokens)
}
}
func TestParseCacheTokensMissing(t *testing.T) {
// Response without cache token fields (should default to 0)
body := `{
"content": [{"type": "text", "text": "Hello"}],
"usage": {
"input_tokens": 100,
"output_tokens": 50
}
}`
parsed := ParseResponseBody(body, "api.anthropic.com")
if parsed.Usage.CacheReadInputTokens != 0 {
t.Errorf("Expected cache_read_input_tokens to default to 0, got %d", parsed.Usage.CacheReadInputTokens)
}
if parsed.Usage.CacheCreationInputTokens != 0 {
t.Errorf("Expected cache_creation_input_tokens to default to 0, got %d", parsed.Usage.CacheCreationInputTokens)
}
}
func TestParseStreamingResponseCacheTokens(t *testing.T) {
// Streaming response with cache tokens in message_start
chunks := []StreamChunk{
{Raw: `data: {"type":"message_start","message":{"usage":{"input_tokens":100,"cache_read_input_tokens":80,"cache_creation_input_tokens":20}}}`},
{Raw: `data: {"type":"content_block_start","index":0,"content_block":{"type":"text"}}`},
{Raw: `data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hello"}}`},
{Raw: `data: {"type":"content_block_stop","index":0}`},
{Raw: `data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":5}}`},
}
parsed := ParseStreamingResponse(chunks)
if parsed.Usage.InputTokens != 100 {
t.Errorf("Expected input_tokens 100, got %d", parsed.Usage.InputTokens)
}
if parsed.Usage.CacheReadInputTokens != 80 {
t.Errorf("Expected cache_read_input_tokens 80, got %d", parsed.Usage.CacheReadInputTokens)
}
if parsed.Usage.CacheCreationInputTokens != 20 {
t.Errorf("Expected cache_creation_input_tokens 20, got %d", parsed.Usage.CacheCreationInputTokens)
}
if parsed.Usage.OutputTokens != 5 {
t.Errorf("Expected output_tokens 5, got %d", parsed.Usage.OutputTokens)
}
}
func TestParseStreamingResponseNoCacheTokens(t *testing.T) {
// Streaming response without cache tokens (should default to 0)
chunks := []StreamChunk{
{Raw: `data: {"type":"message_start","message":{"usage":{"input_tokens":50}}}`},
{Raw: `data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":10}}`},
}
parsed := ParseStreamingResponse(chunks)
if parsed.Usage.CacheReadInputTokens != 0 {
t.Errorf("Expected cache_read_input_tokens to default to 0, got %d", parsed.Usage.CacheReadInputTokens)
}
if parsed.Usage.CacheCreationInputTokens != 0 {
t.Errorf("Expected cache_creation_input_tokens to default to 0, got %d", parsed.Usage.CacheCreationInputTokens)
}
}