-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathmessageutil_test.go
More file actions
56 lines (49 loc) · 2.36 KB
/
messageutil_test.go
File metadata and controls
56 lines (49 loc) · 2.36 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
package anthropic_test
import (
"encoding/json"
"testing"
"github.com/anthropics/anthropic-sdk-go"
"github.com/anthropics/anthropic-sdk-go/shared/constant"
)
func unmarshalContentBlockParam(t *testing.T, jsonData string) anthropic.ContentBlockParamUnion {
var block anthropic.ContentBlockUnion
err := json.Unmarshal([]byte(jsonData), &block)
if err != nil {
t.Fatalf("Failed to unmarshal JSON: %v", err)
}
result := block.ToParam()
return result
}
func TestContentBlockUnionToParam(t *testing.T) {
t.Run("TextBlock with text only", func(t *testing.T) {
result := unmarshalContentBlockParam(t, `{"type":"text","text":"Hello, world!"}`)
if result.OfText == nil {
t.Fatal("Expected OfText to be non-nil")
}
if result.OfText.Text != "Hello, world!" {
t.Errorf("Expected text 'Hello, world!', got '%s'", result.OfText.Text)
}
if result.OfText.Type != constant.Text("text") {
t.Errorf("Expected type 'text', got '%s'", result.OfText.Type)
}
})
t.Run("WebSearchToolResultBlock with search results", func(t *testing.T) {
result := unmarshalContentBlockParam(t, `{"type":"web_search_tool_result","tool_use_id":"test123","content":[{"type":"web_search_result","title":"Test Web Title","url":"https://test.com","encrypted_content":"abc123","page_age":"1 day ago"}]}`)
var block anthropic.ContentBlockUnion
if err := json.Unmarshal([]byte(`{"type":"web_search_tool_result","tool_use_id":"test123","content":[{"type":"web_search_result","title":"Test Web Title","url":"https://test.com","encrypted_content":"abc123","page_age":"1 day ago"}]}`), &block); err != nil {
t.Fatalf("Failed to unmarshal: %v", err)
}
if len(block.Content.OfWebSearchResultBlockArray) != 1 {
t.Errorf("Expected Content.OfWebSearchResultBlockArray to have 1 result, got %d", len(block.Content.OfWebSearchResultBlockArray))
}
if len(block.Content.OfWebSearchResultBlockArray) > 0 && block.Content.OfWebSearchResultBlockArray[0].Title != "Test Web Title" {
t.Errorf("Expected title '', got '%s'", block.Content.OfWebSearchResultBlockArray[0].Title)
}
if result.OfWebSearchToolResult == nil {
t.Fatal("Expected OfWebSearchToolResult to be non-nil")
}
if len(result.OfWebSearchToolResult.Content.OfWebSearchToolResultBlockItem) != 1 {
t.Errorf("Expected 1 search result in param, got %d", len(result.OfWebSearchToolResult.Content.OfWebSearchToolResultBlockItem))
}
})
}