-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathhandler_test.go
More file actions
106 lines (93 loc) · 3.11 KB
/
Copy pathhandler_test.go
File metadata and controls
106 lines (93 loc) · 3.11 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
package anthropic
import (
"bufio"
"encoding/json"
"io"
"strings"
"testing"
)
func TestReadSSEEventMultiLineData(t *testing.T) {
input := strings.Join([]string{
"event: response.output_text.delta",
"data: {\"type\":\"response.output_text.delta\",",
"data: \"delta\":\"hello\"}",
"",
}, "\n")
reader := bufio.NewReader(strings.NewReader(input))
event, err := readSSEEvent(reader)
if err != nil {
t.Fatalf("readSSEEvent returned error: %v", err)
}
if event == nil {
t.Fatal("readSSEEvent returned nil event")
}
if event.Event != "response.output_text.delta" {
t.Fatalf("event type = %q, want %q", event.Event, "response.output_text.delta")
}
wantData := "{\"type\":\"response.output_text.delta\",\n\"delta\":\"hello\"}"
if event.Data != wantData {
t.Fatalf("event data = %q, want %q", event.Data, wantData)
}
}
func TestReadSSEEventEOFWithoutData(t *testing.T) {
reader := bufio.NewReader(strings.NewReader(""))
event, err := readSSEEvent(reader)
if err == nil {
t.Fatal("expected EOF error")
}
if err != io.EOF {
t.Fatalf("error = %v, want io.EOF", err)
}
if event != nil {
t.Fatalf("event = %#v, want nil", event)
}
}
func TestNormalizeNativeMessagesBody_RemovesCacheControlScope(t *testing.T) {
body := []byte(`{
"model": "claude-opus-4-6-20250514",
"context_management": {"type": "auto"},
"system": [
{"type": "text", "text": "one"},
{"type": "text", "text": "two", "cache_control": {"type": "ephemeral", "ttl": "1h", "scope": "workspace"}}
],
"messages": [
{"role": "user", "content": [{"type": "text", "text": "hi", "cache_control": {"type": "ephemeral", "scope": "tool"}}]}
],
"max_tokens": 16
}`)
normalized, err := normalizeNativeMessagesBody(body, "claude-opus-4.6", true)
if err != nil {
t.Fatalf("normalizeNativeMessagesBody returned error: %v", err)
}
info := inspectCacheControl(normalized)
if info.ScopeCount != 0 {
t.Fatalf("ScopeCount = %d, want 0; paths=%v", info.ScopeCount, info.ScopePaths)
}
var decoded map[string]interface{}
if err := json.Unmarshal(normalized, &decoded); err != nil {
t.Fatalf("failed to decode normalized body: %v", err)
}
if decoded["model"] != "claude-opus-4.6" {
t.Fatalf("model = %v, want claude-opus-4.6", decoded["model"])
}
if _, ok := decoded["context_management"]; ok {
t.Fatalf("context_management still present")
}
system := decoded["system"].([]interface{})
cacheControl := system[1].(map[string]interface{})["cache_control"].(map[string]interface{})
if cacheControl["type"] != "ephemeral" {
t.Fatalf("system cache_control.type = %v, want ephemeral", cacheControl["type"])
}
if cacheControl["ttl"] != "1h" {
t.Fatalf("system cache_control.ttl = %v, want 1h", cacheControl["ttl"])
}
if _, ok := cacheControl["scope"]; ok {
t.Fatalf("system cache_control.scope still present")
}
messages := decoded["messages"].([]interface{})
parts := messages[0].(map[string]interface{})["content"].([]interface{})
messageCacheControl := parts[0].(map[string]interface{})["cache_control"].(map[string]interface{})
if _, ok := messageCacheControl["scope"]; ok {
t.Fatalf("message cache_control.scope still present")
}
}