Repository navigation
Expand file tree
/
Copy pathrequest_controls_test.go
More file actions
177 lines (165 loc) · 4.11 KB
/
Copy pathrequest_controls_test.go
File metadata and controls
177 lines (165 loc) · 4.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
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
package llm
import (
"context"
"encoding/json"
"errors"
"testing"
)
func decodeObject(t *testing.T, body []byte) map[string]any {
t.Helper()
var got map[string]any
if err := json.Unmarshal(body, &got); err != nil {
t.Fatalf("decode request: %v\nbody: %s", err, body)
}
return got
}
func TestRequestControlsMapAcrossFormats(t *testing.T) {
req := &ChatRequest{
Messages: []Message{{Role: RoleUser, Content: "hello"}},
TopP: -1, // canonical explicit zero
Stop: []string{"END", "DONE"},
}
t.Run("openai", func(t *testing.T) {
wire := buildOpenAIRequest(
ProviderConfig{ID: "openai", Format: FormatOpenAI},
req,
"gpt-4o",
false,
false,
)
body, err := json.Marshal(wire)
if err != nil {
t.Fatal(err)
}
got := decodeObject(t, body)
assertSamplingControls(t, got, "top_p", "stop")
})
t.Run("gemini", func(t *testing.T) {
body, err := buildGeminiRequest(req, "gemini-2.5-pro", false)
if err != nil {
t.Fatal(err)
}
got := decodeObject(t, body)
cfg := got["generationConfig"].(map[string]any)
assertSamplingControls(t, cfg, "topP", "stopSequences")
})
t.Run("anthropic", func(t *testing.T) {
body, err := buildAnthropicRequest(req, "claude-sonnet-4", false)
if err != nil {
t.Fatal(err)
}
got := decodeObject(t, body)
assertSamplingControls(t, got, "top_p", "stop_sequences")
})
}
func assertSamplingControls(t *testing.T, got map[string]any, topPKey, stopKey string) {
t.Helper()
if got[topPKey] != float64(0) {
t.Errorf("%s = %v, want explicit 0", topPKey, got[topPKey])
}
stop, ok := got[stopKey].([]any)
if !ok || len(stop) != 2 || stop[0] != "END" || stop[1] != "DONE" {
t.Errorf("%s = %#v, want [END DONE]", stopKey, got[stopKey])
}
}
func TestRequestControlsOmitDefaultsAcrossFormats(t *testing.T) {
req := &ChatRequest{Messages: []Message{{Role: RoleUser, Content: "hello"}}}
tests := []struct {
name string
build func(t *testing.T) map[string]any
topPKey string
stopKey string
}{
{
name: "openai",
build: func(t *testing.T) map[string]any {
body, err := json.Marshal(buildOpenAIRequest(ProviderConfig{Format: FormatOpenAI}, req, "gpt-4o", false, false))
if err != nil {
t.Fatal(err)
}
return decodeObject(t, body)
},
topPKey: "top_p",
stopKey: "stop",
},
{
name: "gemini",
build: func(t *testing.T) map[string]any {
body, err := buildGeminiRequest(req, "gemini-2.5-pro", false)
if err != nil {
t.Fatal(err)
}
got := decodeObject(t, body)
cfg, _ := got["generationConfig"].(map[string]any)
return cfg
},
topPKey: "topP",
stopKey: "stopSequences",
},
{
name: "anthropic",
build: func(t *testing.T) map[string]any {
body, err := buildAnthropicRequest(req, "claude-sonnet-4", false)
if err != nil {
t.Fatal(err)
}
return decodeObject(t, body)
},
topPKey: "top_p",
stopKey: "stop_sequences",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.build(t)
if _, ok := got[tt.topPKey]; ok {
t.Errorf("%s must be omitted at its default", tt.topPKey)
}
if _, ok := got[tt.stopKey]; ok {
t.Errorf("%s must be omitted when empty", tt.stopKey)
}
})
}
}
func TestChatClientRejectsNilRequest(t *testing.T) {
pc := newProviderClient(
ProviderConfig{ID: "openai", Format: FormatOpenAI, BaseURL: "http://127.0.0.1:1", APIKey: "k"},
nil,
nil,
)
client := &ChatClient{pc: pc, model: "gpt-4o"}
tests := []struct {
name string
call func() error
}{
{
name: "buffered",
call: func() error {
_, err := client.Call(context.Background(), nil)
return err
},
},
{
name: "streaming",
call: func() error {
_, err := client.CallStream(context.Background(), nil, func(Delta) error { return nil })
return err
},
},
{
name: "nil handler",
call: func() error {
_, err := client.CallStream(context.Background(), nil, nil)
return err
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var cfgErr *ConfigError
if err := tt.call(); !errors.As(err, &cfgErr) {
t.Fatalf("error = %v, want ConfigError", err)
}
})
}
}