Repository navigation
Expand file tree
/
Copy pathmodels_flow_test.go
More file actions
319 lines (300 loc) · 11.7 KB
/
Copy pathmodels_flow_test.go
File metadata and controls
319 lines (300 loc) · 11.7 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package llm
import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"testing"
"time"
)
// ── buffered dispatch per wire format (parseResponse arms) ───────────────
func TestCallBufferedPerFormatDispatch(t *testing.T) {
cases := []struct {
name string
format Format
quirks Quirks
body string
check func(*testing.T, *ChatResult)
}{
{
name: "openai",
format: FormatOpenAI,
body: `{"choices":[{"message":{"role":"assistant","content":"hey","reasoning_content":"thought"},"finish_reason":"tool_calls","tool_calls":[]}],"usage":{"prompt_tokens":2,"completion_tokens":3}}`,
check: func(t *testing.T, r *ChatResult) {
if r.Content != "hey" || r.ReasoningContent != "thought" || r.FinishReason != FinishToolCalls {
t.Fatalf("openai parse = %+v", r)
}
},
},
{
name: "anthropic",
format: FormatAnthropic,
body: `{"content":[{"type":"text","text":"A"}],"stop_reason":"max_tokens","usage":{"input_tokens":4,"output_tokens":6}}`,
check: func(t *testing.T, r *ChatResult) {
if r.Content != "A" || r.FinishReason != FinishLength || r.Usage.CompletionTokens != 6 {
t.Fatalf("anthropic parse = %+v", r)
}
},
},
{
name: "gemini",
format: FormatGemini,
body: `{"candidates":[{"content":{"parts":[{"text":"G"}]},"finishReason":"STOP"}],"usageMetadata":{"promptTokenCount":1,"candidatesTokenCount":2}}`,
check: func(t *testing.T, r *ChatResult) {
if r.Content != "G" || r.FinishReason != FinishStop || r.Usage.PromptTokens != 1 {
t.Fatalf("gemini parse = %+v", r)
}
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
srv := httptestNewServer(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(tc.body))
})
defer srv.Close()
cc := newTestClient(t, ProviderConfig{ID: "x", Format: tc.format, BaseURL: srv.URL, APIKey: "k", Quirks: tc.quirks}, srv)
res, err := cc.Call(context.Background(), &ChatRequest{Messages: []Message{{Role: RoleUser, Content: "hi"}}})
if err != nil {
t.Fatal(err)
}
tc.check(t, res)
})
}
}
// Parse failures must surface as descriptive errors, not empty results.
func TestBufferedParseErrorPaths(t *testing.T) {
cases := []struct {
name string
format Format
body string
want string
}{
{"openai provider error", FormatOpenAI, `{"error":{"message":"boom"}}`, "provider error"},
{"openai no choices", FormatOpenAI, `{"choices":[]}`, "no choices"},
{"gemini no candidates", FormatGemini, `{"candidates":[]}`, "no candidates"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
srv := httptestNewServer(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(tc.body))
})
defer srv.Close()
cc := newTestClient(t, ProviderConfig{ID: "x", Format: tc.format, BaseURL: srv.URL, APIKey: "k"}, srv)
_, err := cc.Call(context.Background(), &ChatRequest{Messages: []Message{{Role: RoleUser, Content: "hi"}}})
if err == nil || !strings.Contains(err.Error(), tc.want) {
t.Fatalf("err = %v, want containing %q", err, tc.want)
}
})
}
}
// ── retry state machine arms ─────────────────────────────────────────────
// A transport-level failure (dropped connection) retries and then succeeds.
func TestCallTransportErrorRetries(t *testing.T) {
var n int
srv := httptestNewServer(func(w http.ResponseWriter, r *http.Request) {
n++
if n <= 2 {
panic(http.ErrAbortHandler) // drop the connection
}
fmt.Fprint(w, `{"choices":[{"message":{"content":"ok"},"finish_reason":"stop"}]}`)
})
defer srv.Close()
cc := newTestClient(t, ProviderConfig{ID: "openai", Format: FormatOpenAI, BaseURL: srv.URL, APIKey: "k"}, srv)
res, err := cc.Call(context.Background(), &ChatRequest{Messages: []Message{{Role: RoleUser, Content: "hi"}}})
if err != nil {
t.Fatalf("Call: %v", err)
}
if res.Content != "ok" || n != 3 {
t.Fatalf("res=%q attempts=%d", res.Content, n)
}
}
// A definitive 4xx after earlier 429s surfaces that 4xx (the request is
// definitively broken — retrying the rate limit was pointless).
func TestCallDefinitiveFailureAfter429(t *testing.T) {
var n int
srv := httptestNewServer(func(w http.ResponseWriter, r *http.Request) {
n++
if n == 1 {
w.Header().Set("Retry-After", "0")
w.WriteHeader(429)
fmt.Fprint(w, `{"error":{"message":"slow"}}`)
return
}
w.WriteHeader(401)
fmt.Fprint(w, `{"error":{"message":"bad key"}}`)
})
defer srv.Close()
cc := newTestClient(t, ProviderConfig{ID: "openai", Format: FormatOpenAI, BaseURL: srv.URL, APIKey: "k"}, srv)
_, err := cc.Call(context.Background(), &ChatRequest{Messages: []Message{{Role: RoleUser, Content: "hi"}}})
var ae *APIError
if !errors.As(err, &ae) || ae.Status != 401 {
t.Fatalf("err = %v, want the definitive 401", err)
}
if n != 2 {
t.Errorf("attempts = %d, want 2", n)
}
}
// A streamed request answered with a non-SSE body learns the buffered
// fallback while consuming that generation directly.
func TestCallStreamLearnsBufferedFromNonSSEBody(t *testing.T) {
var n int
srv := httptestNewServer(func(w http.ResponseWriter, r *http.Request) {
n++
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, `{"choices":[{"message":{"content":"buffered"},"finish_reason":"stop"}]}`)
})
defer srv.Close()
cc := newTestClient(t, ProviderConfig{ID: "openai", Format: FormatOpenAI, BaseURL: srv.URL, APIKey: "k"}, srv)
res, err := cc.CallStream(context.Background(), &ChatRequest{Messages: []Message{{Role: RoleUser, Content: "hi"}}}, func(Delta) error {
return nil
})
if err != nil {
t.Fatalf("CallStream: %v", err)
}
if res.Content != "buffered" {
t.Errorf("res = %q", res.Content)
}
if n != 1 {
t.Errorf("requests = %d, want 1 (consume the successful body)", n)
}
// The learned fallback must short-circuit the NEXT CallStream of the
// same client straight onto the buffered path.
res2, err := cc.CallStream(context.Background(), &ChatRequest{Messages: []Message{{Role: RoleUser, Content: "hi"}}}, func(Delta) error {
return nil
})
if err != nil || res2.Content != "buffered" {
t.Fatalf("second stream: res=%q err=%v", res2.Content, err)
}
if n != 2 {
t.Errorf("requests = %d, want 2 (entry fast-path, no re-learn)", n)
}
}
// ── provider error-body parsing per format ───────────────────────────────
func TestHTTPErrorPerFormat(t *testing.T) {
cases := []struct {
name string
format Format
body string
message string
code string
}{
{"anthropic", FormatAnthropic, `{"error":{"type":"invalid_request_error","message":"am"}}`, "am", "invalid_request_error"},
{"gemini", FormatGemini, `{"error":{"message":"gm","status":"INVALID_ARGUMENT"}}`, "gm", "INVALID_ARGUMENT"},
{"openai numeric code", FormatOpenAI, `{"error":{"message":"om","code":42}}`, "om", ""},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
pc := newProviderClient(ProviderConfig{ID: "p", Format: tc.format}, nil, nil)
e := pc.httpError(400, []byte(tc.body))
if e.Message != tc.message || e.Code != tc.code {
t.Fatalf("APIError = %+v, want message %q code %q", e, tc.message, tc.code)
}
})
}
}
// ── auth headers per format ──────────────────────────────────────────────
func TestSetAuthHeaders(t *testing.T) {
anthropic := newProviderClient(ProviderConfig{ID: "a", Format: FormatAnthropic, APIKey: "ak"}, nil, nil)
h := http.Header{}
anthropic.setAuthHeaders(h)
if h.Get("x-api-key") != "ak" || h.Get("anthropic-version") != "2023-06-01" {
t.Errorf("anthropic headers = %v", h)
}
anthropicCustom := newProviderClient(ProviderConfig{ID: "a", Format: FormatAnthropic, APIKey: "ak", Quirks: Quirks{AnthropicVersion: "2044-01-01"}}, nil, nil)
h = http.Header{}
anthropicCustom.setAuthHeaders(h)
if h.Get("anthropic-version") != "2044-01-01" {
t.Errorf("custom version = %q", h.Get("anthropic-version"))
}
gemini := newProviderClient(ProviderConfig{ID: "g", Format: FormatGemini, APIKey: "gk"}, nil, nil)
h = http.Header{}
gemini.setAuthHeaders(h)
if h.Get("x-goog-api-key") != "gk" {
t.Errorf("gemini headers = %v", h)
}
bearer := newProviderClient(ProviderConfig{ID: "o", Format: FormatOpenAI, APIKey: "ok"}, nil, nil)
h = http.Header{}
bearer.setAuthHeaders(h)
if h.Get("Authorization") != "Bearer ok" {
t.Errorf("bearer = %q", h.Get("Authorization"))
}
}
// ── canonical finish-reason vocabulary ───────────────────────────────────
func TestFinishReasonTables(t *testing.T) {
for _, tc := range []struct{ in, want string }{
{"stop", FinishStop}, {"length", FinishLength},
{"tool_calls", FinishToolCalls}, {"function_call", FinishToolCalls},
{"content_filter", FinishContentFilter}, {"", ""},
{"mystery_reason", ""}, // non-canonical values never leak
} {
if got := mapOpenAIFinishReason(tc.in); got != tc.want {
t.Errorf("mapOpenAIFinishReason(%q) = %q, want %q", tc.in, got, tc.want)
}
}
for _, tc := range []struct{ in, want string }{
{"end_turn", FinishStop}, {"stop_sequence", FinishStop},
{"max_tokens", FinishLength}, {"tool_use", FinishToolCalls},
{"refusal", FinishContentFilter}, {"", ""}, {"pause_turn", ""},
} {
if got := mapAnthropicStopReason(tc.in); got != tc.want {
t.Errorf("mapAnthropicStopReason(%q) = %q, want %q", tc.in, got, tc.want)
}
}
for _, tc := range []struct{ in, want string }{
{"STOP", FinishStop}, {"MAX_TOKENS", FinishLength},
{"SAFETY", FinishContentFilter}, {"RECITATION", FinishContentFilter},
{"BLOCKLIST", FinishContentFilter}, {"", ""}, {"OTHER", ""},
} {
if got := mapGeminiFinishReason(tc.in); got != tc.want {
t.Errorf("mapGeminiFinishReason(%q) = %q, want %q", tc.in, got, tc.want)
}
}
}
// ── SSE edges ────────────────────────────────────────────────────────────
// A single event assembled from multiple data lines may reach the 4 MiB
// event cap; oversized events are rejected with errSSEOversized.
func TestParseSSEStreamOversizedEvent(t *testing.T) {
chunk := strings.Repeat("x", 700*1024) // 700 KiB per line, under the line cap
var b strings.Builder
for i := 0; i < 7; i++ { // 4.9 MiB accumulated into ONE event
b.WriteString("data: " + chunk + "\n")
}
b.WriteString("\n")
done := make(chan struct{})
ch := make(chan sseItem, 2)
go parseSSEStream(strings.NewReader(b.String()), ch, done)
it := <-ch
if it.kind != sseErr || !errors.Is(it.err, errSSEOversized) {
t.Fatalf("oversized event: kind %v err %v", it.kind, it.err)
}
}
// data payload whitespace: "data:x" keeps x, "data: x" strips one space.
func TestSSEPayloadTrimming(t *testing.T) {
ch := make(chan sseItem, 4)
done := make(chan struct{})
go parseSSEStream(strings.NewReader("data:nospace\n\ndata: withspace\n\n"), ch, done)
first := <-ch
if string(first.data) != "nospace" {
t.Fatalf("first = %q", first.data)
}
second := <-ch
if string(second.data) != "withspace" {
t.Fatalf("second = %q", second.data)
}
}
// ── backoff bounds ───────────────────────────────────────────────────────
func TestBackoffDelayCap(t *testing.T) {
old := backoffUnit
backoffUnit = 10 * time.Second
t.Cleanup(func() { backoffUnit = old })
for attempt := 1; attempt <= 8; attempt++ {
if d := backoffDelay(attempt); d > maxRetryBackoff {
t.Fatalf("backoffDelay(%d) = %v exceeds cap", attempt, d)
}
}
if d := backoffDelay(5); d != maxRetryBackoff {
t.Errorf("backoffDelay(5) = %v, want capped %v", d, maxRetryBackoff)
}
}