1package provider
2
3import (
4 "context"
5 "math"
6 "net/http"
7 "net/http/httptest"
8 "strings"
9 "testing"
10)
11
12func TestSSEParse(t *testing.T) {
13 r := strings.NewReader("event: foo\ndata: {\"a\":1}\n\ndata: hello\ndata: world\n\n")
14 ch := make(chan sseEvent, 4)
15 go readSSE(r, ch)
16
17 e := <-ch
18 if e.Event != "foo" || e.Data != `{"a":1}` {
19 t.Fatalf("event 1: %+v", e)
20 }
21 e = <-ch
22 if e.Event != "" || e.Data != "hello\nworld" {
23 t.Fatalf("event 2: %+v", e)
24 }
25 if _, ok := <-ch; ok {
26 t.Fatalf("channel not closed")
27 }
28}
29
30func TestModelCatalog(t *testing.T) {
31 if len(Catalog) == 0 {
32 t.Fatal("empty catalog")
33 }
34 if _, err := FindModel("anthropic", "claude-sonnet-4-5"); err != nil {
35 t.Fatal(err)
36 }
37 if _, err := FindModel("openai", "gpt-5"); err != nil {
38 t.Fatal(err)
39 }
40 if _, err := FindModel("", "nope"); err == nil {
41 t.Fatal("expected error")
42 }
43}
44
45func TestVertexGeminiFlashCatalogLimits(t *testing.T) {
46 for _, id := range []string{"gemini-3.1-flash-lite", "gemini-3.5-flash"} {
47 m, err := FindModel("google-vertex", id)
48 if err != nil {
49 t.Fatal(err)
50 }
51 if m.ContextWindow != 1048576 || m.MaxOutput != 65535 || !m.Reasoning {
52 t.Errorf("%s metadata = %+v", id, m)
53 }
54 }
55}
56
57func TestComputeCost(t *testing.T) {
58 m, _ := FindModel("anthropic", "claude-sonnet-4-5")
59 cost := ComputeCost(m, Usage{InputTokens: 1_000_000, OutputTokens: 1_000_000})
60 want := m.PriceInput + m.PriceOutput
61 if cost != want {
62 t.Fatalf("cost=%v want=%v", cost, want)
63 }
64}
65
66func TestComputeCostInputTier(t *testing.T) {
67 m := Model{
68 PriceInput: 1, PriceOutput: 2, PriceCacheRead: 0.1,
69 PriceTierInputTokens: 100,
70 PriceInputAbove: 3, PriceOutputAbove: 4, PriceCacheReadAbove: 0.2,
71 }
72 below := ComputeCost(m, Usage{InputTokens: 50, OutputTokens: 10, CacheReadTokens: 50})
73 wantBelow := (50.0*1 + 10*2 + 50*0.1) / 1_000_000
74 if math.Abs(below-wantBelow) > 1e-12 {
75 t.Fatalf("below-tier cost=%v want=%v", below, wantBelow)
76 }
77
78 above := ComputeCost(m, Usage{InputTokens: 51, OutputTokens: 10, CacheReadTokens: 50})
79 wantAbove := (51.0*3 + 10*4 + 50*0.2) / 1_000_000
80 if math.Abs(above-wantAbove) > 1e-12 {
81 t.Fatalf("above-tier cost=%v want=%v", above, wantAbove)
82 }
83}
84
85func TestAnthropicErrorStatus(t *testing.T) {
86 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
87 w.WriteHeader(http.StatusUnauthorized)
88 _, _ = w.Write([]byte(`{"error":{"type":"auth","message":"bad key"}}`))
89 }))
90 defer srv.Close()
91
92 c := NewAnthropic("x", srv.URL)
93 _, err := c.Stream(context.Background(), Request{Model: "claude-sonnet-4-5"})
94 if err == nil || !strings.Contains(err.Error(), "401") {
95 t.Fatalf("want 401 err, got %v", err)
96 }
97}
98
99func TestAnthropicOAuthUsesCurrentClaudeCodeVersion(t *testing.T) {
100 var userAgent string
101 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
102 userAgent = r.Header.Get("user-agent")
103 w.WriteHeader(http.StatusBadRequest)
104 }))
105 defer srv.Close()
106
107 c := NewAnthropicOAuth("test-token", srv.URL)
108 _, err := c.Stream(context.Background(), Request{Model: "claude-opus-5-5"})
109 if err == nil {
110 t.Fatal("expected test server error")
111 }
112 if want := "claude-cli/2.1.280"; userAgent != want {
113 t.Fatalf("user-agent = %q, want %q", userAgent, want)
114 }
115}
116
117func TestOpenAIErrorStatus(t *testing.T) {
118 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
119 w.WriteHeader(http.StatusBadRequest)
120 _, _ = w.Write([]byte(`{"error":{"message":"nope"}}`))
121 }))
122 defer srv.Close()
123
124 c := NewOpenAI("x", srv.URL)
125 _, err := c.Stream(context.Background(), Request{Model: "gpt-5"})
126 if err == nil || !strings.Contains(err.Error(), "400") {
127 t.Fatalf("want 400 err, got %v", err)
128 }
129}
130
131func TestMiniMaxM3CatalogAndRequest(t *testing.T) {
132 for _, provider := range []string{"minimax", "minimax-cn"} {
133 m, err := FindModel(provider, "MiniMax-M3")
134 if err != nil {
135 t.Fatal(err)
136 }
137 if m.ContextWindow != 1000000 || m.MaxOutput != 131072 || !m.Reasoning || !m.AdaptiveThinkingCompat {
138 t.Errorf("%s metadata = %+v", provider, m)
139 }
140 if m.PriceInput != 0.3 || m.PriceOutput != 1.2 || m.PriceCacheRead != 0.06 || m.PriceTierInputTokens != 512000 || m.PriceInputAbove != 0.6 || m.PriceOutputAbove != 2.4 || m.PriceCacheReadAbove != 0.12 {
141 t.Errorf("%s pricing = %+v", provider, m)
142 }
143 }
144
145 c := NewAnthropicCompat("minimax", "x", "https://api.minimax.io/anthropic").(*anthropicClient)
146 temp := float32(0.7)
147 wire, err := c.buildRequest(Request{
148 Model: "MiniMax-M3",
149 Reasoning: "high",
150 Temperature: &temp,
151 Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
152 })
153 if err != nil {
154 t.Fatal(err)
155 }
156 if wire.Thinking == nil || wire.Thinking.Type != "adaptive" || wire.Thinking.BudgetTokens != 0 {
157 t.Fatalf("want adaptive thinking without a budget, got %+v", wire.Thinking)
158 }
159 if wire.OutputConfig != nil {
160 t.Fatalf("MiniMax-M3 must not send output_config, got %+v", wire.OutputConfig)
161 }
162 if wire.Temperature == nil || *wire.Temperature != temp {
163 t.Fatalf("MiniMax-M3 should keep temperature, got %v", wire.Temperature)
164 }
165}
166
167func TestAnthropicAdaptiveThinking(t *testing.T) {
168 c := NewAnthropic("x", "").(*anthropicClient)
169 temp := float32(0.7)
170
171 // Opus 5 -> adaptive thinking, effort set, no budget, no temperature.
172 wire, err := c.buildRequest(Request{
173 Model: "claude-opus-5",
174 Reasoning: "high",
175 Temperature: &temp,
176 Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
177 })
178 if err != nil {
179 t.Fatal(err)
180 }
181 if wire.Thinking == nil || wire.Thinking.Type != "adaptive" {
182 t.Fatalf("want adaptive thinking, got %+v", wire.Thinking)
183 }
184 if wire.Thinking.BudgetTokens != 0 {
185 t.Fatalf("adaptive must not send budget_tokens, got %d", wire.Thinking.BudgetTokens)
186 }
187 if wire.OutputConfig == nil || wire.OutputConfig.Effort != "high" {
188 t.Fatalf("want effort=high, got %+v", wire.OutputConfig)
189 }
190 if wire.Temperature != nil {
191 t.Fatalf("adaptive must drop temperature, got %v", *wire.Temperature)
192 }
193
194 // maximum -> xhigh effort.
195 wire, err = c.buildRequest(Request{
196 Model: "claude-opus-5",
197 Reasoning: "maximum",
198 Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
199 })
200 if err != nil {
201 t.Fatal(err)
202 }
203 if wire.OutputConfig == nil || wire.OutputConfig.Effort != "xhigh" {
204 t.Fatalf("want effort=xhigh, got %+v", wire.OutputConfig)
205 }
206
207 // max is a separate native tier above xhigh on adaptive models.
208 wire, err = c.buildRequest(Request{
209 Model: "claude-opus-5",
210 Reasoning: "max",
211 Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
212 })
213 if err != nil {
214 t.Fatal(err)
215 }
216 if wire.OutputConfig == nil || wire.OutputConfig.Effort != "max" {
217 t.Fatalf("want effort=max, got %+v", wire.OutputConfig)
218 }
219
220 // Sonnet 5 -> adaptive thinking, effort set, no budget, no temperature.
221 wire, err = c.buildRequest(Request{
222 Model: "claude-sonnet-5",
223 Reasoning: "high",
224 Temperature: &temp,
225 Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
226 })
227 if err != nil {
228 t.Fatal(err)
229 }
230 if wire.Thinking == nil || wire.Thinking.Type != "adaptive" {
231 t.Fatalf("want adaptive thinking for Sonnet 5, got %+v", wire.Thinking)
232 }
233 if wire.Thinking.BudgetTokens != 0 {
234 t.Fatalf("Sonnet 5 adaptive must not send budget_tokens, got %d", wire.Thinking.BudgetTokens)
235 }
236 if wire.OutputConfig == nil || wire.OutputConfig.Effort != "high" {
237 t.Fatalf("want Sonnet 5 effort=high, got %+v", wire.OutputConfig)
238 }
239 if wire.Temperature != nil {
240 t.Fatalf("Sonnet 5 adaptive must drop temperature, got %v", *wire.Temperature)
241 }
242
243 // Opus 4.5 -> budget-based thinking, no output_config, temperature kept.
244 wire, err = c.buildRequest(Request{
245 Model: "claude-opus-4-5",
246 Reasoning: "high",
247 Temperature: &temp,
248 Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
249 })
250 if err != nil {
251 t.Fatal(err)
252 }
253 if wire.Thinking == nil || wire.Thinking.Type != "enabled" || wire.Thinking.BudgetTokens <= 0 {
254 t.Fatalf("want budget thinking, got %+v", wire.Thinking)
255 }
256 if wire.OutputConfig != nil {
257 t.Fatalf("budget models must not send output_config, got %+v", wire.OutputConfig)
258 }
259 if wire.Temperature == nil || *wire.Temperature != temp {
260 t.Fatalf("budget model should keep temperature, got %v", wire.Temperature)
261 }
262}
263
264func TestAnthropicBuildRequestStripsAssistantImages(t *testing.T) {
265 c := NewAnthropic("x", "").(*anthropicClient)
266 wire, err := c.buildRequest(Request{
267 Model: "claude-sonnet-4-5",
268 Messages: []Message{
269 {Role: RoleUser, Content: []Content{TextBlock{Text: "make an image"}}},
270 {Role: RoleAssistant, Content: []Content{
271 TextBlock{Text: "done"},
272 ImageBlock{MimeType: "image/png", Data: []byte("png")},
273 TextBlock{Text: "Saved image: `zot-gemini-image-x.png`"},
274 }},
275 {Role: RoleUser, Content: []Content{TextBlock{Text: "hello"}}},
276 },
277 })
278 if err != nil {
279 t.Fatal(err)
280 }
281 if len(wire.Messages) != 3 {
282 t.Fatalf("messages=%d", len(wire.Messages))
283 }
284 assistant := wire.Messages[1]
285 if assistant.Role != "assistant" {
286 t.Fatalf("role=%q", assistant.Role)
287 }
288 if len(assistant.Content) != 2 {
289 t.Fatalf("assistant content=%+v", assistant.Content)
290 }
291 for _, b := range assistant.Content {
292 if _, ok := b.(anthImageBlock); ok {
293 t.Fatalf("assistant image block was not stripped: %+v", assistant.Content)
294 }
295 }
296}
297
298func TestRepairOrphanedToolResultsPreservesRepeatedToolCallPairs(t *testing.T) {
299 msgs := []Message{
300 {Role: RoleAssistant, Content: []Content{ToolCallBlock{ID: "reused", Name: "read"}}},
301 {Role: RoleTool, Content: []Content{ToolResultBlock{CallID: "reused", Content: []Content{TextBlock{Text: "first"}}}}},
302 {Role: RoleAssistant, Content: []Content{ToolCallBlock{ID: "reused", Name: "read"}}},
303 {Role: RoleTool, Content: []Content{ToolResultBlock{CallID: "reused", Content: []Content{TextBlock{Text: "second"}}}}},
304 }
305
306 got := RepairOrphanedToolResults(msgs)
307 if len(got) != len(msgs) {
308 t.Fatalf("messages=%d, want %d valid repeated-ID pairs", len(got), len(msgs))
309 }
310 for _, index := range []int{1, 3} {
311 if len(got[index].Content) != 1 {
312 t.Fatalf("message %d content=%d, want 1 tool result", index, len(got[index].Content))
313 }
314 if _, ok := got[index].Content[0].(ToolResultBlock); !ok {
315 t.Fatalf("message %d content type=%T, want ToolResultBlock", index, got[index].Content[0])
316 }
317 }
318}
319
320func TestAnthropicBuildRequestDeduplicatesToolResults(t *testing.T) {
321 c := NewAnthropic("x", "").(*anthropicClient)
322 wire, err := c.buildRequest(Request{
323 Model: "claude-sonnet-4-5",
324 Messages: []Message{
325 {Role: RoleUser, Content: []Content{TextBlock{Text: "read the file"}}},
326 {Role: RoleAssistant, Content: []Content{
327 ToolCallBlock{ID: "tool-1", Name: "read"},
328 }},
329 {Role: RoleTool, Content: []Content{
330 ToolResultBlock{CallID: "tool-1", Content: []Content{TextBlock{Text: "first result"}}},
331 }},
332 {Role: RoleTool, Content: []Content{
333 ToolResultBlock{CallID: "tool-1", Content: []Content{TextBlock{Text: "duplicate result"}}},
334 }},
335 },
336 })
337 if err != nil {
338 t.Fatal(err)
339 }
340 if len(wire.Messages) != 3 {
341 t.Fatalf("messages=%d, want 3 after dropping duplicate result message", len(wire.Messages))
342 }
343 blocks := wire.Messages[2].Content
344 if len(blocks) != 1 {
345 t.Fatalf("tool result blocks=%d, want 1", len(blocks))
346 }
347 result, ok := blocks[0].(anthToolResultBlock)
348 if !ok {
349 t.Fatalf("tool result block type=%T", blocks[0])
350 }
351 if string(result.Content) != `"first result"` {
352 t.Fatalf("tool result content=%s", result.Content)
353 }
354}
355
356func TestAnthropicBuildRequestPreservesRepeatedToolCallPairs(t *testing.T) {
357 c := NewAnthropic("x", "").(*anthropicClient)
358 wire, err := c.buildRequest(Request{
359 Model: "claude-sonnet-4-5",
360 Messages: []Message{
361 {Role: RoleAssistant, Content: []Content{ToolCallBlock{ID: "reused", Name: "read"}}},
362 {Role: RoleTool, Content: []Content{ToolResultBlock{CallID: "reused", Content: []Content{TextBlock{Text: "first"}}}}},
363 {Role: RoleAssistant, Content: []Content{ToolCallBlock{ID: "reused", Name: "read"}}},
364 {Role: RoleTool, Content: []Content{ToolResultBlock{CallID: "reused", Content: []Content{TextBlock{Text: "second"}}}}},
365 },
366 })
367 if err != nil {
368 t.Fatal(err)
369 }
370 if len(wire.Messages) != 4 {
371 t.Fatalf("messages=%d, want both repeated-ID tool result pairs", len(wire.Messages))
372 }
373 for _, index := range []int{1, 3} {
374 if len(wire.Messages[index].Content) != 1 {
375 t.Fatalf("message %d content=%d, want 1 tool result", index, len(wire.Messages[index].Content))
376 }
377 if _, ok := wire.Messages[index].Content[0].(anthToolResultBlock); !ok {
378 t.Fatalf("message %d content type=%T, want anthToolResultBlock", index, wire.Messages[index].Content[0])
379 }
380 }
381}
382
383func TestAnthropicStreamHappyPath(t *testing.T) {
384 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
385 w.Header().Set("content-type", "text/event-stream")
386 w.WriteHeader(http.StatusOK)
387 fl, _ := w.(http.Flusher)
388 write := func(s string) {
389 _, _ = w.Write([]byte(s))
390 if fl != nil {
391 fl.Flush()
392 }
393 }
394 write("event: message_start\ndata: {\"message\":{\"usage\":{\"input_tokens\":10,\"output_tokens\":0}}}\n\n")
395 write("event: content_block_start\ndata: {\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}\n\n")
396 write("event: content_block_delta\ndata: {\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"hi\"}}\n\n")
397 write("event: content_block_stop\ndata: {\"index\":0}\n\n")
398 write("event: message_delta\ndata: {\"delta\":{\"stop_reason\":\"end_turn\"},\"usage\":{\"output_tokens\":5}}\n\n")
399 write("event: message_stop\ndata: {}\n\n")
400 }))
401 defer srv.Close()
402
403 c := NewAnthropic("x", srv.URL)
404 evs, err := c.Stream(context.Background(), Request{Model: "claude-sonnet-4-5"})
405 if err != nil {
406 t.Fatal(err)
407 }
408 var gotText string
409 var done EventDone
410 for ev := range evs {
411 switch e := ev.(type) {
412 case EventTextDelta:
413 gotText += e.Delta
414 case EventDone:
415 done = e
416 }
417 }
418 if gotText != "hi" {
419 t.Fatalf("text=%q", gotText)
420 }
421 if done.Stop != StopEnd {
422 t.Fatalf("stop=%v", done.Stop)
423 }
424}
425
426func TestClaudeOpus5Catalog(t *testing.T) {
427 m, err := FindModel("anthropic", "claude-opus-5")
428 if err != nil {
429 t.Fatal(err)
430 }
431 if m.DisplayName != "Claude Opus 5" || m.ContextWindow != 1000000 || m.MaxOutput != 128000 || !m.Reasoning || !m.AdaptiveThinking || m.Speculative {
432 t.Fatalf("unexpected Opus 5 model: %+v", m)
433 }
434 if m.PriceInput != 5 || m.PriceOutput != 25 || m.PriceCacheRead != 0.5 || m.PriceCacheWrite != 6.25 {
435 t.Fatalf("unexpected Opus 5 pricing: %+v", m)
436 }
437}
438
439func TestClaudeSonnet5Catalog(t *testing.T) {
440 m, err := FindModel("anthropic", "claude-sonnet-5")
441 if err != nil {
442 t.Fatal(err)
443 }
444 if m.DisplayName != "Claude Sonnet 5" || m.ContextWindow != 1000000 || m.MaxOutput != 128000 || !m.Reasoning || !m.AdaptiveThinking {
445 t.Fatalf("unexpected Sonnet 5 model: %+v", m)
446 }
447 if m.PriceInput != 2 || m.PriceOutput != 10 || m.PriceCacheRead != 0.2 || m.PriceCacheWrite != 2.5 {
448 t.Fatalf("unexpected Sonnet 5 pricing: %+v", m)
449 }
450}
451
452func TestClaudeFable5Catalog(t *testing.T) {
453 m, err := FindModel("anthropic", "claude-fable-5")
454 if err != nil {
455 t.Fatal(err)
456 }
457 if m.DisplayName != "Claude Fable 5" || m.ContextWindow != 1000000 || m.MaxOutput != 128000 || !m.Reasoning || !m.AdaptiveThinking || m.Speculative {
458 t.Fatalf("unexpected Fable 5 model: %+v", m)
459 }
460 if m.PriceInput != 10 || m.PriceOutput != 50 || m.PriceCacheRead != 1 || m.PriceCacheWrite != 12.5 {
461 t.Fatalf("unexpected Fable 5 pricing: %+v", m)
462 }
463}
464
465func TestClaudeFable51Catalog(t *testing.T) {
466 m, err := FindModel("anthropic", "claude-fable-5-1")
467 if err != nil {
468 t.Fatal(err)
469 }
470 if m.DisplayName != "Claude Fable 5.1" || m.ContextWindow != 1000000 || m.MaxOutput != 128000 || !m.Reasoning || !m.AdaptiveThinking || m.Speculative {
471 t.Fatalf("unexpected Fable 5.1 model: %+v", m)
472 }
473 if m.PriceInput != 10 || m.PriceOutput != 50 || m.PriceCacheRead != 0.25 || m.PriceCacheWrite != 12.5 {
474 t.Fatalf("unexpected Fable 5.1 pricing: %+v", m)
475 }
476
477 wire, err := NewAnthropic("x", "").(*anthropicClient).buildRequest(Request{
478 Model: m.ID,
479 Reasoning: "max",
480 Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
481 })
482 if err != nil {
483 t.Fatal(err)
484 }
485 if wire.Thinking == nil || wire.Thinking.Type != "adaptive" || wire.Thinking.BudgetTokens != 0 {
486 t.Fatalf("unexpected Fable 5.1 thinking config: %+v", wire.Thinking)
487 }
488 if wire.OutputConfig == nil || wire.OutputConfig.Effort != "max" {
489 t.Fatalf("unexpected Fable 5.1 output config: %+v", wire.OutputConfig)
490 }
491}
492
493func TestGondolaSeedCatalog(t *testing.T) {
494 kimi, err := FindModel("gondola", "kimi-k3")
495 if err != nil {
496 t.Fatal(err)
497 }
498 if kimi.ContextWindow != 1000000 || kimi.MaxOutput != 131072 || !kimi.Reasoning || kimi.AdaptiveThinking {
499 t.Fatalf("unexpected Kimi K3 model: %+v", kimi)
500 }
501 if kimi.PriceInput != 1.1475 || kimi.PriceOutput != 5.7375 || kimi.PriceCacheRead != 0.11475 {
502 t.Fatalf("unexpected Kimi K3 pricing: %+v", kimi)
503 }
504
505 opus, err := FindModel("gondola", "claude-opus-5")
506 if err != nil {
507 t.Fatal(err)
508 }
509 if !opus.AdaptiveThinking || opus.PriceInput != 1.836 || opus.PriceOutput != 9.18 || opus.PriceCacheRead != 0.1836 || opus.PriceCacheWrite != 2.295 {
510 t.Fatalf("unexpected Claude Opus 5 model: %+v", opus)
511 }
512}
513
514func TestOpenAICompatAnthropicReasoningEffort(t *testing.T) {
515 c := NewOpenRouter("token", "").(*openaiClient)
516 wire, err := c.buildRequest(Request{
517 Model: "anthropic/claude-opus-4.8",
518 Reasoning: "maximum",
519 Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
520 })
521 if err != nil {
522 t.Fatal(err)
523 }
524 if wire.ReasoningEffort != "xhigh" {
525 t.Fatalf("want xhigh for adaptive Anthropic model over OpenAI-compatible wire, got %q", wire.ReasoningEffort)
526 }
527
528 wire, err = c.buildRequest(Request{
529 Model: "anthropic/claude-sonnet-5",
530 Reasoning: "maximum",
531 Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
532 })
533 if err != nil {
534 t.Fatal(err)
535 }
536 if wire.ReasoningEffort != "xhigh" {
537 t.Fatalf("want xhigh for Claude Sonnet 5 over OpenAI-compatible wire, got %q", wire.ReasoningEffort)
538 }
539
540 wire, err = c.buildRequest(Request{
541 Model: "gpt-5.1",
542 Reasoning: "maximum",
543 Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
544 })
545 if err != nil {
546 t.Fatal(err)
547 }
548 if wire.ReasoningEffort != "high" {
549 t.Fatalf("want generic OpenAI-compatible maximum clamped to high, got %q", wire.ReasoningEffort)
550 }
551}
552
553func TestOpenAIBuildRequestSkipsReasoningOnlyAssistantMessages(t *testing.T) {
554 c := NewKimi("token", "").(*openaiClient)
555 wire, err := c.buildRequest(Request{
556 Model: "kimi-for-coding",
557 Messages: []Message{
558 {Role: RoleUser, Content: []Content{TextBlock{Text: "first"}}},
559 {Role: RoleAssistant, Content: []Content{ReasoningBlock{Summary: "thinking only"}}},
560 {Role: RoleUser, Content: []Content{TextBlock{Text: "second"}}},
561 },
562 })
563 if err != nil {
564 t.Fatal(err)
565 }
566 for i, msg := range wire.Messages {
567 if msg.Role == "assistant" && msg.Content == nil && len(msg.ToolCalls) == 0 {
568 t.Fatalf("message %d is empty assistant: %+v", i, msg)
569 }
570 }
571 if got := len(wire.Messages); got != 2 {
572 t.Fatalf("messages=%d want 2 after skipping reasoning-only assistant", got)
573 }
574}
575
576func TestOpenAIStreamHappyPath(t *testing.T) {
577 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
578 w.Header().Set("content-type", "text/event-stream")
579 w.WriteHeader(http.StatusOK)
580 fl, _ := w.(http.Flusher)
581 write := func(s string) {
582 _, _ = w.Write([]byte(s))
583 if fl != nil {
584 fl.Flush()
585 }
586 }
587 write("data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"hel\"},\"finish_reason\":null}]}\n\n")
588 write("data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"lo\"},\"finish_reason\":\"stop\"}]}\n\n")
589 write("data: {\"choices\":[],\"usage\":{\"prompt_tokens\":8,\"completion_tokens\":2}}\n\n")
590 write("data: [DONE]\n\n")
591 }))
592 defer srv.Close()
593
594 c := NewOpenAI("x", srv.URL)
595 evs, err := c.Stream(context.Background(), Request{Model: "gpt-5"})
596 if err != nil {
597 t.Fatal(err)
598 }
599 var gotText string
600 var done EventDone
601 for ev := range evs {
602 switch e := ev.(type) {
603 case EventTextDelta:
604 gotText += e.Delta
605 case EventDone:
606 done = e
607 }
608 }
609 if gotText != "hello" {
610 t.Fatalf("text=%q", gotText)
611 }
612 if done.Stop != StopEnd {
613 t.Fatalf("stop=%v", done.Stop)
614 }
615}
616
617func TestDiscoverOpenRouter(t *testing.T) {
618 const body = `{"data":[
619 {"id":"x/full","pricing":{"prompt":"0.000003","completion":"0.000015"},
620 "context_length":200000,"top_provider":{"max_completion_tokens":64000},
621 "supported_parameters":["reasoning"]},
622 {"id":"x/fallback","top_provider":{"context_length":128000}},
623 {"id":""}]}`
624 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
625 _, _ = w.Write([]byte(body))
626 }))
627 defer srv.Close()
628
629 models, err := DiscoverOpenRouter(context.Background(), srv.URL)
630 if err != nil {
631 t.Fatal(err)
632 }
633 if len(models) != 2 { // empty id dropped
634 t.Fatalf("want 2 models, got %d", len(models))
635 }
636 // per-token USD -> per-1M; reasoning from supported_parameters.
637 if m := models[0]; m.Provider != "openrouter" || m.ContextWindow != 200000 ||
638 m.MaxOutput != 64000 || !m.Reasoning || m.PriceInput != 3 || m.PriceOutput != 15 {
639 t.Errorf("model[0]: %+v", m)
640 }
641 // context falls back to top_provider; no reasoning.
642 if m := models[1]; m.ContextWindow != 128000 || m.MaxOutput != 0 || m.Reasoning {
643 t.Errorf("model[1]: %+v", m)
644 }
645}
646
647func TestGPT56CatalogEntries(t *testing.T) {
648 cases := []struct {
649 provider string
650 id string
651 context int
652 priceInput float64
653 priceOut float64
654 cacheRead float64
655 cacheWrite float64
656 }{
657 {"openai", "gpt-5.6-luna", 272000, 1, 6, 0.1, 1.25},
658 {"openai", "gpt-5.6-sol", 272000, 5, 30, 0.5, 6.25},
659 {"openai", "gpt-5.6-terra", 272000, 2.5, 15, 0.25, 3.125},
660 {"openai-codex", "gpt-5.6-luna", 272000, 1, 6, 0.1, 1.25},
661 {"openai-codex", "gpt-5.6-sol", 272000, 5, 30, 0.5, 6.25},
662 {"openai-codex", "gpt-5.6-terra", 272000, 2.5, 15, 0.25, 3.125},
663 {"azure-openai-responses", "gpt-5.6-luna", 1050000, 1, 6, 0.1, 1.25},
664 {"azure-openai-responses", "gpt-5.6-sol", 1050000, 5, 30, 0.5, 6.25},
665 {"azure-openai-responses", "gpt-5.6-terra", 1050000, 2.5, 15, 0.25, 3.125},
666 {"vercel-ai-gateway", "openai/gpt-5.6-luna", 1050000, 1, 6, 0.1, 1.25},
667 {"vercel-ai-gateway", "openai/gpt-5.6-sol", 1050000, 5, 30, 0.5, 6.25},
668 {"vercel-ai-gateway", "openai/gpt-5.6-terra", 1050000, 2.5, 15, 0.25, 3.125},
669 }
670 for _, tc := range cases {
671 m, err := FindModel(tc.provider, tc.id)
672 if err != nil {
673 t.Fatal(err)
674 }
675 if m.ContextWindow != tc.context || m.MaxOutput != 128000 || !m.Reasoning {
676 t.Fatalf("%s/%s limits: %+v", tc.provider, tc.id, m)
677 }
678 if m.PriceInput != tc.priceInput || m.PriceOutput != tc.priceOut || m.PriceCacheRead != tc.cacheRead || m.PriceCacheWrite != tc.cacheWrite {
679 t.Fatalf("%s/%s prices: %+v", tc.provider, tc.id, m)
680 }
681 }
682}
683
684// TestOpenCodeGoCatalog pins entries added from the vendor's current Go
685// model lineup (https://opencode.ai/docs/go).
686func TestOpenCodeGoCatalog(t *testing.T) {
687 cases := []struct {
688 id string
689 context int
690 maxOut int
691 priceIn float64
692 priceOut float64
693 cacheRead float64
694 }{
695 {"gpt-5.6-luna", 1050000, 128000, 0.2, 1.2, 0.02},
696 {"grok-4.5", 500000, 500000, 2, 6, 0.3},
697 {"kimi-k3", 1048576, 131072, 3, 15, 0.3},
698 }
699 for _, tc := range cases {
700 m, err := FindModel("opencode-go", tc.id)
701 if err != nil {
702 t.Fatal(err)
703 }
704 if m.ContextWindow != tc.context || m.MaxOutput != tc.maxOut || !m.Reasoning {
705 t.Fatalf("opencode-go/%s limits: %+v", tc.id, m)
706 }
707 if m.PriceInput != tc.priceIn || m.PriceOutput != tc.priceOut || m.PriceCacheRead != tc.cacheRead {
708 t.Fatalf("opencode-go/%s prices: %+v", tc.id, m)
709 }
710 if m.BaseURL != "https://opencode.ai/zen/go/v1" {
711 t.Fatalf("opencode-go/%s baseURL: %s", tc.id, m.BaseURL)
712 }
713 }
714
715 luna, err := FindModel("opencode-go", "gpt-5.6-luna")
716 if err != nil {
717 t.Fatal(err)
718 }
719 if luna.API != APIResponses || luna.PriceCacheWrite != 0.25 {
720 t.Fatalf("opencode-go/gpt-5.6-luna API or cache-write price: %+v", luna)
721 }
722 if luna.PriceTierInputTokens != 272000 || luna.PriceInputAbove != 0.4 || luna.PriceOutputAbove != 1.8 || luna.PriceCacheReadAbove != 0.04 || luna.PriceCacheWriteAbove != 0.5 {
723 t.Fatalf("opencode-go/gpt-5.6-luna long-context prices: %+v", luna)
724 }
725}
726
727// TestOpenAIOmitsZeroMaxTokens guards against sending max_tokens: 0 for
728// discovered models that don't advertise an output cap (MaxOutput == 0).
729func TestOpenAIOmitsZeroMaxTokens(t *testing.T) {
730 SetLiveModels([]Model{
731 {Provider: "openrouter", ID: "vendor/no-cap", DisplayName: "No Cap"},
732 {Provider: "openrouter", ID: "vendor/reason-no-cap", DisplayName: "Reason No Cap", Reasoning: true},
733 {Provider: "openrouter", ID: "vendor/capped", DisplayName: "Capped", MaxOutput: 4096},
734 })
735 defer SetLiveModels(nil)
736
737 c := NewOpenAI("x", "").(*openaiClient)
738
739 got, err := c.buildRequest(Request{Model: "vendor/no-cap"})
740 if err != nil {
741 t.Fatal(err)
742 }
743 if got.MaxTokens != nil {
744 t.Errorf("expected max_tokens omitted, got %d", *got.MaxTokens)
745 }
746
747 got, err = c.buildRequest(Request{Model: "vendor/reason-no-cap"})
748 if err != nil {
749 t.Fatal(err)
750 }
751 if got.MaxCompletionTok != nil {
752 t.Errorf("expected max_completion_tokens omitted, got %d", *got.MaxCompletionTok)
753 }
754
755 got, err = c.buildRequest(Request{Model: "vendor/capped"})
756 if err != nil {
757 t.Fatal(err)
758 }
759 if got.MaxTokens == nil || *got.MaxTokens != 4096 {
760 t.Errorf("expected max_tokens 4096, got %v", got.MaxTokens)
761 }
762}