Repository navigation
Expand file tree
/
Copy pathtoolcall_test.go
More file actions
156 lines (144 loc) · 4.52 KB
/
Copy pathtoolcall_test.go
File metadata and controls
156 lines (144 loc) · 4.52 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
package session
import (
"encoding/json"
"reflect"
"strings"
"testing"
)
// TestParseArgs locks the canonical arg-parse mechanic the agent loop and the
// adapter toolkit both delegate to: empty payload → zero-value dst + ok, valid
// JSON decodes, malformed JSON yields a model-facing error string + !ok.
func TestParseArgs(t *testing.T) {
type payload struct {
Key string `json:"key"`
}
t.Run("valid", func(t *testing.T) {
var p payload
msg, ok := ParseArgs(ToolCall{Args: json.RawMessage(`{"key":"v"}`)}, &p)
if !ok || msg != "" {
t.Fatalf("valid payload: ok=%v msg=%q", ok, msg)
}
if p.Key != "v" {
t.Fatalf("decoded Key=%q, want v", p.Key)
}
})
t.Run("empty leaves zero value", func(t *testing.T) {
p := payload{Key: "untouched"}
// A zero-value (no Args) call must not error; dst is left as-is.
var fresh payload
if msg, ok := ParseArgs(ToolCall{}, &fresh); !ok || msg != "" {
t.Fatalf("empty payload: ok=%v msg=%q", ok, msg)
}
if fresh != (payload{}) {
t.Fatalf("empty payload mutated dst: %+v", fresh)
}
_ = p
})
t.Run("malformed", func(t *testing.T) {
var p payload
msg, ok := ParseArgs(ToolCall{Args: json.RawMessage(`{bad`)}, &p)
if ok {
t.Fatal("malformed JSON accepted")
}
if msg == "" {
t.Fatal("malformed JSON produced no model-facing message")
}
})
t.Run("ambiguous objects are refused", func(t *testing.T) {
for _, args := range []string{
`{"key":"a","key":"b"}`,
`{"key":"a","Key":"b"}`,
`{"KEY":"b"}`,
`{"\u212aey":"b"}`,
`{"key":"a","nested":{"x":1,"x":2}}`,
} {
var p payload
msg, ok := ParseArgs(ToolCall{Args: json.RawMessage(args)}, &p)
if ok || !strings.Contains(msg, "ambiguous tool arguments") {
t.Errorf("ParseArgs(%s) ok=%v msg=%q, want an ambiguous-arguments refusal", args, ok, msg)
}
if p != (payload{}) {
t.Errorf("ParseArgs(%s) decoded %+v before refusing", args, p)
}
}
})
}
// TestToolResultPartsRoundTrip locks the JSON shape of ToolResult.Parts: a Parts
// slice round-trips through marshal/unmarshal, and a zero-value Parts is absent
// from JSON (backward compat with the pre-Parts string-only shape).
func TestToolResultPartsRoundTrip(t *testing.T) {
parts := []Content{
NewTextBlock("hello"),
NewResourceLinkBlock("https://example.com/r", "n", "t", "d", "application/json", 7, []string{"user"}),
}
src := NewToolResultWithParts("call-1", "summary", parts)
out, err := json.Marshal(src)
if err != nil {
t.Fatalf("marshal: %v", err)
}
var got ToolResult
if err := json.Unmarshal(out, &got); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if got.CallID != src.CallID || got.Content != src.Content || got.IsError != src.IsError {
t.Fatalf("round-trip mismatch: %+v != %+v", got, src)
}
if !reflect.DeepEqual(got.Parts, src.Parts) {
t.Fatalf("parts round-trip mismatch:\n got=%+v\n src=%+v", got.Parts, src.Parts)
}
// Zero-value Parts absent from JSON (backward compat).
plain, err := json.Marshal(NewToolResult("call-2", "just text"))
if err != nil {
t.Fatalf("marshal plain: %v", err)
}
if string(plain) == "" {
t.Fatal("empty marshal")
}
var m map[string]any
if err := json.Unmarshal(plain, &m); err != nil {
t.Fatalf("unmarshal plain to map: %v", err)
}
if _, ok := m["Parts"]; ok {
t.Fatalf("zero-value Parts present in JSON: %s", plain)
}
}
func TestNewToolResultWithParts(t *testing.T) {
parts := []Content{NewTextBlock("block")}
r := NewToolResultWithParts("c1", "summary", parts)
if r.CallID != "c1" {
t.Fatalf("callid = %q", r.CallID)
}
if r.Content != "summary" {
t.Fatalf("content = %q", r.Content)
}
if r.IsError {
t.Fatal("isError = true, want false")
}
if len(r.Parts) != 1 || r.Parts[0].Text != "block" {
t.Fatalf("parts = %+v", r.Parts)
}
}
// toolCallIDCases are tool call IDs at the edges of ValidToolCallID.
var toolCallIDCases = map[ToolCallID]bool{
"toolu_01A": true,
"call_abc-123:x/y": true,
"呼び出し": true,
ToolCallID(strings.Repeat("a", MaxToolCallIDBytes)): true,
ToolCallID(strings.Repeat("a", MaxToolCallIDBytes+1)): false,
"": false,
"toolu 1": false,
"toolu\t1": false,
"toolu\x01": false,
"toolu\u200b": false,
"toolu\u202e": false,
"\xff": false,
}
// TestValidToolCallID pins which model tool call IDs can name a call outside
// its conversation: printable, space-free, valid UTF-8, bounded.
func TestValidToolCallID(t *testing.T) {
for id, want := range toolCallIDCases {
if got := ValidToolCallID(id); got != want {
t.Errorf("ValidToolCallID(%q) = %v, want %v", id, got, want)
}
}
}