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