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 TestAnthropicStreamHappyPath(t *testing.T) {
476 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
477 w.Header().Set("content-type", "text/event-stream")
478 w.WriteHeader(http.StatusOK)
479 fl, _ := w.(http.Flusher)
480 write := func(s string) {
481 _, _ = w.Write([]byte(s))
482 if fl != nil {
483 fl.Flush()
484 }
485 }
486 write("event: message_start\ndata: {\"message\":{\"usage\":{\"input_tokens\":10,\"output_tokens\":0}}}\n\n")
487 write("event: content_block_start\ndata: {\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}\n\n")
488 write("event: content_block_delta\ndata: {\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"hi\"}}\n\n")
489 write("event: content_block_stop\ndata: {\"index\":0}\n\n")
490 write("event: message_delta\ndata: {\"delta\":{\"stop_reason\":\"end_turn\"},\"usage\":{\"output_tokens\":5}}\n\n")
491 write("event: message_stop\ndata: {}\n\n")
492 }))
493 defer srv.Close()
494
495 c := NewAnthropic("x", srv.URL)
496 evs, err := c.Stream(context.Background(), Request{Model: "claude-sonnet-4-5"})
497 if err != nil {
498 t.Fatal(err)
499 }
500 var gotText string
501 var done EventDone
502 for ev := range evs {
503 switch e := ev.(type) {
504 case EventTextDelta:
505 gotText += e.Delta
506 case EventDone:
507 done = e
508 }
509 }
510 if gotText != "hi" {
511 t.Fatalf("text=%q", gotText)
512 }
513 if done.Stop != StopEnd {
514 t.Fatalf("stop=%v", done.Stop)
515 }
516}
517
518func TestClaudeOpus5Catalog(t *testing.T) {
519 m, err := FindModel("anthropic", "claude-opus-5")
520 if err != nil {
521 t.Fatal(err)
522 }
523 if m.DisplayName != "Claude Opus 5" || m.ContextWindow != 1000000 || m.MaxOutput != 128000 || !m.Reasoning || !m.AdaptiveThinking || m.Speculative {
524 t.Fatalf("unexpected Opus 5 model: %+v", m)
525 }
526 if m.PriceInput != 5 || m.PriceOutput != 25 || m.PriceCacheRead != 0.5 || m.PriceCacheWrite != 6.25 {
527 t.Fatalf("unexpected Opus 5 pricing: %+v", m)
528 }
529}
530
531func TestClaudeSonnet5Catalog(t *testing.T) {
532 m, err := FindModel("anthropic", "claude-sonnet-5")
533 if err != nil {
534 t.Fatal(err)
535 }
536 if m.DisplayName != "Claude Sonnet 5" || m.ContextWindow != 1000000 || m.MaxOutput != 128000 || !m.Reasoning || !m.AdaptiveThinking {
537 t.Fatalf("unexpected Sonnet 5 model: %+v", m)
538 }
539 if m.PriceInput != 2 || m.PriceOutput != 10 || m.PriceCacheRead != 0.2 || m.PriceCacheWrite != 2.5 {
540 t.Fatalf("unexpected Sonnet 5 pricing: %+v", m)
541 }
542}
543
544func TestOpenAICompatAnthropicReasoningEffort(t *testing.T) {
545 c := NewOpenRouter("token", "").(*openaiClient)
546 wire, err := c.buildRequest(Request{
547 Model: "anthropic/claude-opus-4.8",
548 Reasoning: "maximum",
549 Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
550 })
551 if err != nil {
552 t.Fatal(err)
553 }
554 if wire.ReasoningEffort != "xhigh" {
555 t.Fatalf("want xhigh for adaptive Anthropic model over OpenAI-compatible wire, got %q", wire.ReasoningEffort)
556 }
557
558 wire, err = c.buildRequest(Request{
559 Model: "anthropic/claude-sonnet-5",
560 Reasoning: "maximum",
561 Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
562 })
563 if err != nil {
564 t.Fatal(err)
565 }
566 if wire.ReasoningEffort != "xhigh" {
567 t.Fatalf("want xhigh for Claude Sonnet 5 over OpenAI-compatible wire, got %q", wire.ReasoningEffort)
568 }
569
570 wire, err = c.buildRequest(Request{
571 Model: "gpt-5.1",
572 Reasoning: "maximum",
573 Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
574 })
575 if err != nil {
576 t.Fatal(err)
577 }
578 if wire.ReasoningEffort != "high" {
579 t.Fatalf("want generic OpenAI-compatible maximum clamped to high, got %q", wire.ReasoningEffort)
580 }
581}
582
583func TestOpenAIBuildRequestSkipsReasoningOnlyAssistantMessages(t *testing.T) {
584 c := NewKimi("token", "").(*openaiClient)
585 wire, err := c.buildRequest(Request{
586 Model: "kimi-for-coding",
587 Messages: []Message{
588 {Role: RoleUser, Content: []Content{TextBlock{Text: "first"}}},
589 {Role: RoleAssistant, Content: []Content{ReasoningBlock{Summary: "thinking only"}}},
590 {Role: RoleUser, Content: []Content{TextBlock{Text: "second"}}},
591 },
592 })
593 if err != nil {
594 t.Fatal(err)
595 }
596 for i, msg := range wire.Messages {
597 if msg.Role == "assistant" && msg.Content == nil && len(msg.ToolCalls) == 0 {
598 t.Fatalf("message %d is empty assistant: %+v", i, msg)
599 }
600 }
601 if got := len(wire.Messages); got != 2 {
602 t.Fatalf("messages=%d want 2 after skipping reasoning-only assistant", got)
603 }
604}
605
606func TestOpenAIStreamHappyPath(t *testing.T) {
607 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
608 w.Header().Set("content-type", "text/event-stream")
609 w.WriteHeader(http.StatusOK)
610 fl, _ := w.(http.Flusher)
611 write := func(s string) {
612 _, _ = w.Write([]byte(s))
613 if fl != nil {
614 fl.Flush()
615 }
616 }
617 write("data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"hel\"},\"finish_reason\":null}]}\n\n")
618 write("data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"lo\"},\"finish_reason\":\"stop\"}]}\n\n")
619 write("data: {\"choices\":[],\"usage\":{\"prompt_tokens\":8,\"completion_tokens\":2}}\n\n")
620 write("data: [DONE]\n\n")
621 }))
622 defer srv.Close()
623
624 c := NewOpenAI("x", srv.URL)
625 evs, err := c.Stream(context.Background(), Request{Model: "gpt-5"})
626 if err != nil {
627 t.Fatal(err)
628 }
629 var gotText string
630 var done EventDone
631 for ev := range evs {
632 switch e := ev.(type) {
633 case EventTextDelta:
634 gotText += e.Delta
635 case EventDone:
636 done = e
637 }
638 }
639 if gotText != "hello" {
640 t.Fatalf("text=%q", gotText)
641 }
642 if done.Stop != StopEnd {
643 t.Fatalf("stop=%v", done.Stop)
644 }
645}
646
647func TestDiscoverOpenRouter(t *testing.T) {
648 const body = `{"data":[
649 {"id":"x/full","pricing":{"prompt":"0.000003","completion":"0.000015"},
650 "context_length":200000,"top_provider":{"max_completion_tokens":64000},
651 "supported_parameters":["reasoning"]},
652 {"id":"x/fallback","top_provider":{"context_length":128000}},
653 {"id":""}]}`
654 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
655 _, _ = w.Write([]byte(body))
656 }))
657 defer srv.Close()
658
659 models, err := DiscoverOpenRouter(context.Background(), srv.URL)
660 if err != nil {
661 t.Fatal(err)
662 }
663 if len(models) != 2 { // empty id dropped
664 t.Fatalf("want 2 models, got %d", len(models))
665 }
666 // per-token USD -> per-1M; reasoning from supported_parameters.
667 if m := models[0]; m.Provider != "openrouter" || m.ContextWindow != 200000 ||
668 m.MaxOutput != 64000 || !m.Reasoning || m.PriceInput != 3 || m.PriceOutput != 15 {
669 t.Errorf("model[0]: %+v", m)
670 }
671 // context falls back to top_provider; no reasoning.
672 if m := models[1]; m.ContextWindow != 128000 || m.MaxOutput != 0 || m.Reasoning {
673 t.Errorf("model[1]: %+v", m)
674 }
675}
676
677func TestGPT56AndGPT6CatalogEntries(t *testing.T) {
678 cases := []struct {
679 provider string
680 id string
681 context int
682 priceInput float64
683 priceOut float64
684 cacheRead float64
685 cacheWrite float64
686 }{
687 {"openai", "gpt-5.6-luna", 500000, 1, 6, 0.1, 1.25},
688 {"openai", "gpt-5.6-sol", 500000, 5, 30, 0.5, 6.25},
689 {"openai", "gpt-5.6-terra", 500000, 2.5, 15, 0.25, 3.125},
690 {"openai", "gpt-6-astra", 500000, 10, 50, 1, 12.5},
691 {"openai-codex", "gpt-5.6-luna", 500000, 1, 6, 0.1, 1.25},
692 {"openai-codex", "gpt-5.6-sol", 500000, 5, 30, 0.5, 6.25},
693 {"openai-codex", "gpt-5.6-terra", 500000, 2.5, 15, 0.25, 3.125},
694 {"openai-codex", "gpt-6-astra", 500000, 10, 50, 1, 12.5},
695 {"azure-openai-responses", "gpt-5.6-luna", 1050000, 1, 6, 0.1, 1.25},
696 {"azure-openai-responses", "gpt-5.6-sol", 1050000, 5, 30, 0.5, 6.25},
697 {"azure-openai-responses", "gpt-5.6-terra", 1050000, 2.5, 15, 0.25, 3.125},
698 {"vercel-ai-gateway", "openai/gpt-5.6-luna", 1050000, 1, 6, 0.1, 1.25},
699 {"vercel-ai-gateway", "openai/gpt-5.6-sol", 1050000, 5, 30, 0.5, 6.25},
700 {"vercel-ai-gateway", "openai/gpt-5.6-terra", 1050000, 2.5, 15, 0.25, 3.125},
701 }
702 for _, tc := range cases {
703 m, err := FindModel(tc.provider, tc.id)
704 if err != nil {
705 t.Fatal(err)
706 }
707 if m.ContextWindow != tc.context || m.MaxOutput != 128000 || !m.Reasoning {
708 t.Fatalf("%s/%s limits: %+v", tc.provider, tc.id, m)
709 }
710 if m.PriceInput != tc.priceInput || m.PriceOutput != tc.priceOut || m.PriceCacheRead != tc.cacheRead || m.PriceCacheWrite != tc.cacheWrite {
711 t.Fatalf("%s/%s prices: %+v", tc.provider, tc.id, m)
712 }
713 if tc.provider == "openai-codex" && m.Speculative {
714 t.Fatalf("%s/%s is speculative", tc.provider, tc.id)
715 }
716 }
717}
718
719// TestOpenAIOmitsZeroMaxTokens guards against sending max_tokens: 0 for
720// discovered models that don't advertise an output cap (MaxOutput == 0).
721func TestOpenAIOmitsZeroMaxTokens(t *testing.T) {
722 SetLiveModels([]Model{
723 {Provider: "openrouter", ID: "vendor/no-cap", DisplayName: "No Cap"},
724 {Provider: "openrouter", ID: "vendor/reason-no-cap", DisplayName: "Reason No Cap", Reasoning: true},
725 {Provider: "openrouter", ID: "vendor/capped", DisplayName: "Capped", MaxOutput: 4096},
726 })
727 defer SetLiveModels(nil)
728
729 c := NewOpenAI("x", "").(*openaiClient)
730
731 got, err := c.buildRequest(Request{Model: "vendor/no-cap"})
732 if err != nil {
733 t.Fatal(err)
734 }
735 if got.MaxTokens != nil {
736 t.Errorf("expected max_tokens omitted, got %d", *got.MaxTokens)
737 }
738
739 got, err = c.buildRequest(Request{Model: "vendor/reason-no-cap"})
740 if err != nil {
741 t.Fatal(err)
742 }
743 if got.MaxCompletionTok != nil {
744 t.Errorf("expected max_completion_tokens omitted, got %d", *got.MaxCompletionTok)
745 }
746
747 got, err = c.buildRequest(Request{Model: "vendor/capped"})
748 if err != nil {
749 t.Fatal(err)
750 }
751 if got.MaxTokens == nil || *got.MaxTokens != 4096 {
752 t.Errorf("expected max_tokens 4096, got %v", got.MaxTokens)
753 }
754}