-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprovider_http_test.go
More file actions
182 lines (159 loc) · 5.05 KB
/
provider_http_test.go
File metadata and controls
182 lines (159 loc) · 5.05 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
package ai
import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestOpenAIProvider_Send_BuildsExpectedRequest(t *testing.T) {
cleanup := withTestGlobals(t)
defer cleanup()
var gotAuth string
var gotBody map[string]any
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Fatalf("expected POST, got %s", r.Method)
}
if r.URL.Path != "/chat/completions" {
t.Fatalf("expected /chat/completions, got %s", r.URL.Path)
}
gotAuth = r.Header.Get("Authorization")
body, _ := io.ReadAll(r.Body)
_ = json.Unmarshal(body, &gotBody)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{
"choices":[{"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}],
"usage":{"prompt_tokens":1,"completion_tokens":2,"total_tokens":3}
}`))
}))
defer srv.Close()
p := NewOpenAIProvider(ProviderConfig{
APIKey: "k",
BaseURL: srv.URL,
})
temp := 0.2
resp, err := p.Send(context.Background(), &ProviderRequest{
Model: string(ModelGPT5),
Messages: []Message{{Role: "user", Content: "hi"}},
Temperature: &temp,
Thinking: ThinkingHigh,
JSONMode: true,
Tools: []Tool{
{
Type: "function",
Function: ToolFunction{
Name: "get_weather",
Description: "Get weather",
Parameters: Params().String("city", "City", true).Build(),
},
},
},
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if gotAuth != "Bearer k" {
t.Fatalf("expected Authorization header, got %q", gotAuth)
}
if gotBody["model"] != "gpt-5.2" {
t.Fatalf("expected resolved model gpt-5.2, got %#v", gotBody["model"])
}
if gotBody["reasoning_effort"] != "high" {
t.Fatalf("expected reasoning_effort=high, got %#v", gotBody["reasoning_effort"])
}
if gotBody["response_format"] == nil {
t.Fatalf("expected response_format to be set for JSON mode")
}
if _, ok := gotBody["tools"]; !ok {
t.Fatalf("expected tools to be present")
}
if resp.Content != "ok" || resp.TotalTokens != 3 || resp.PromptTokens != 1 || resp.CompletionTokens != 2 {
t.Fatalf("unexpected response: %#v", resp)
}
}
func TestOpenRouterProvider_Send_BuildsExpectedRequest(t *testing.T) {
cleanup := withTestGlobals(t)
defer cleanup()
var gotAuth string
var gotReferer string
var gotBody map[string]any
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/chat/completions" {
t.Fatalf("expected /chat/completions, got %s", r.URL.Path)
}
gotAuth = r.Header.Get("Authorization")
gotReferer = r.Header.Get("HTTP-Referer")
body, _ := io.ReadAll(r.Body)
_ = json.Unmarshal(body, &gotBody)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{
"choices":[{"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}],
"usage":{"prompt_tokens":10,"completion_tokens":20,"total_tokens":30}
}`))
}))
defer srv.Close()
p := NewOpenRouterProvider(ProviderConfig{
APIKey: "k",
BaseURL: srv.URL,
})
temp := 0.3
resp, err := p.Send(context.Background(), &ProviderRequest{
Model: string(ModelClaudeOpus),
Messages: []Message{{Role: "user", Content: "hi"}},
Temperature: &temp,
Thinking: ThinkingLow,
JSONMode: true,
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if gotAuth != "Bearer k" {
t.Fatalf("expected Authorization header, got %q", gotAuth)
}
if gotReferer == "" {
t.Fatalf("expected HTTP-Referer header to be set")
}
if gotBody["model"] != "anthropic/claude-opus-4.5" {
t.Fatalf("expected resolved model anthropic/claude-opus-4.5, got %#v", gotBody["model"])
}
if gotBody["reasoning"] != string(ThinkingLow) {
t.Fatalf("expected reasoning=%q, got %#v", ThinkingLow, gotBody["reasoning"])
}
if gotBody["response_format"] == nil {
t.Fatalf("expected response_format to be set for JSON mode")
}
if resp.Content != "ok" || resp.TotalTokens != 30 {
t.Fatalf("unexpected response: %#v", resp)
}
}
func TestOpenAIProvider_WithTimeout_IsRespected(t *testing.T) {
cleanup := withTestGlobals(t)
defer cleanup()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(200 * time.Millisecond)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"choices":[{"message":{"role":"assistant","content":"late"},"finish_reason":"stop"}],"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}}`))
}))
defer srv.Close()
p := NewOpenAIProvider(ProviderConfig{
APIKey: "k",
BaseURL: srv.URL,
Timeout: 50 * time.Millisecond,
Headers: map[string]string{"X-Test": "1"},
})
_, err := p.Send(context.Background(), &ProviderRequest{
Model: string(ModelGPT4o),
Messages: []Message{{Role: "user", Content: "hi"}},
})
if err == nil {
t.Fatalf("expected timeout error")
}
// Provider wraps the underlying timeout error; assert via Unwrap().
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("expected context deadline exceeded via unwrap, got: %v", err)
}
}