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: 50})
209 wantBelow := (50.0*1 + 10*2 + 50*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 above := ComputeCost(m, Usage{InputTokens: 51, OutputTokens: 10, CacheReadTokens: 50})
215 wantAbove := (51.0*3 + 10*4 + 50*0.2) / 1_000_000
216 if math.Abs(above-wantAbove) > 1e-12 {
217 t.Fatalf("above-tier cost=%v want=%v", above, wantAbove)
218 }
219}
220
221func TestAnthropicErrorStatus(t *testing.T) {
222 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
223 w.WriteHeader(http.StatusUnauthorized)
224 _, _ = w.Write([]byte(`{"error":{"type":"auth","message":"bad key"}}`))
225 }))
226 defer srv.Close()
227
228 c := NewAnthropic("x", srv.URL)
229 _, err := c.Stream(context.Background(), Request{Model: "claude-sonnet-4-5"})
230 if err == nil || !strings.Contains(err.Error(), "401") {
231 t.Fatalf("want 401 err, got %v", err)
232 }
233}
234
235func TestOpenAIErrorStatus(t *testing.T) {
236 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
237 w.WriteHeader(http.StatusBadRequest)
238 _, _ = w.Write([]byte(`{"error":{"message":"nope"}}`))
239 }))
240 defer srv.Close()
241
242 c := NewOpenAI("x", srv.URL)
243 _, err := c.Stream(context.Background(), Request{Model: "gpt-5"})
244 if err == nil || !strings.Contains(err.Error(), "400") {
245 t.Fatalf("want 400 err, got %v", err)
246 }
247}
248
249func TestOpenAIUserContentPreservesProjectedTextBlocks(t *testing.T) {
250 c := NewOpenAI("x", "").(*openaiClient)
251 wire, err := c.buildRequest(Request{
252 Model: "gpt-5",
253 Messages: []Message{{
254 Role: RoleUser,
255 Content: []Content{
256 TextBlock{Text: "[message time: 2026-08-08T12:34:56-07:00]"},
257 TextBlock{Text: "fail"},
258 },
259 }},
260 })
261 if err != nil {
262 t.Fatal(err)
263 }
264 blocks, ok := wire.Messages[0].Content.([]interface{})
265 if !ok {
266 t.Fatalf("user content = %T, want structured text blocks", wire.Messages[0].Content)
267 }
268 if len(blocks) != 2 {
269 t.Fatalf("user content blocks = %d, want 2", len(blocks))
270 }
271 if got := blocks[0].(oaiContentText).Text; got != "[message time: 2026-08-08T12:34:56-07:00]" {
272 t.Fatalf("timestamp block = %q", got)
273 }
274 if got := blocks[1].(oaiContentText).Text; got != "fail" {
275 t.Fatalf("user text block = %q", got)
276 }
277}
278
279func TestMiniMaxM3CatalogAndRequest(t *testing.T) {
280 for _, provider := range []string{"minimax", "minimax-cn"} {
281 m, err := FindModel(provider, "MiniMax-M3")
282 if err != nil {
283 t.Fatal(err)
284 }
285 if m.ContextWindow != 1000000 || m.MaxOutput != 131072 || !m.Reasoning || !m.AdaptiveThinkingCompat {
286 t.Errorf("%s metadata = %+v", provider, m)
287 }
288 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 {
289 t.Errorf("%s pricing = %+v", provider, m)
290 }
291 }
292
293 c := NewAnthropicCompat("minimax", "x", "https://api.minimax.io/anthropic").(*anthropicClient)
294 temp := float32(0.7)
295 wire, err := c.buildRequest(Request{
296 Model: "MiniMax-M3",
297 Reasoning: "high",
298 Temperature: &temp,
299 Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
300 })
301 if err != nil {
302 t.Fatal(err)
303 }
304 if wire.Thinking == nil || wire.Thinking.Type != "adaptive" || wire.Thinking.BudgetTokens != 0 {
305 t.Fatalf("want adaptive thinking without a budget, got %+v", wire.Thinking)
306 }
307 if wire.OutputConfig != nil {
308 t.Fatalf("MiniMax-M3 must not send output_config, got %+v", wire.OutputConfig)
309 }
310 if wire.Temperature == nil || *wire.Temperature != temp {
311 t.Fatalf("MiniMax-M3 should keep temperature, got %v", wire.Temperature)
312 }
313}
314
315func TestAnthropicAdaptiveThinking(t *testing.T) {
316 c := NewAnthropic("x", "").(*anthropicClient)
317 temp := float32(0.7)
318
319 // Opus 5 -> adaptive thinking, effort set, no budget, no temperature.
320 wire, err := c.buildRequest(Request{
321 Model: "claude-opus-5",
322 Reasoning: "high",
323 Temperature: &temp,
324 Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
325 })
326 if err != nil {
327 t.Fatal(err)
328 }
329 if wire.Thinking == nil || wire.Thinking.Type != "adaptive" {
330 t.Fatalf("want adaptive thinking, got %+v", wire.Thinking)
331 }
332 if wire.Thinking.BudgetTokens != 0 {
333 t.Fatalf("adaptive must not send budget_tokens, got %d", wire.Thinking.BudgetTokens)
334 }
335 if wire.OutputConfig == nil || wire.OutputConfig.Effort != "high" {
336 t.Fatalf("want effort=high, got %+v", wire.OutputConfig)
337 }
338 if wire.Temperature != nil {
339 t.Fatalf("adaptive must drop temperature, got %v", *wire.Temperature)
340 }
341
342 // maximum -> xhigh effort.
343 wire, err = c.buildRequest(Request{
344 Model: "claude-opus-5",
345 Reasoning: "maximum",
346 Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
347 })
348 if err != nil {
349 t.Fatal(err)
350 }
351 if wire.OutputConfig == nil || wire.OutputConfig.Effort != "xhigh" {
352 t.Fatalf("want effort=xhigh, got %+v", wire.OutputConfig)
353 }
354
355 // max is a separate native tier above xhigh on adaptive models.
356 wire, err = c.buildRequest(Request{
357 Model: "claude-opus-5",
358 Reasoning: "max",
359 Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
360 })
361 if err != nil {
362 t.Fatal(err)
363 }
364 if wire.OutputConfig == nil || wire.OutputConfig.Effort != "max" {
365 t.Fatalf("want effort=max, got %+v", wire.OutputConfig)
366 }
367
368 // Sonnet 5 -> adaptive thinking, effort set, no budget, no temperature.
369 wire, err = c.buildRequest(Request{
370 Model: "claude-sonnet-5",
371 Reasoning: "high",
372 Temperature: &temp,
373 Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
374 })
375 if err != nil {
376 t.Fatal(err)
377 }
378 if wire.Thinking == nil || wire.Thinking.Type != "adaptive" {
379 t.Fatalf("want adaptive thinking for Sonnet 5, got %+v", wire.Thinking)
380 }
381 if wire.Thinking.BudgetTokens != 0 {
382 t.Fatalf("Sonnet 5 adaptive must not send budget_tokens, got %d", wire.Thinking.BudgetTokens)
383 }
384 if wire.OutputConfig == nil || wire.OutputConfig.Effort != "high" {
385 t.Fatalf("want Sonnet 5 effort=high, got %+v", wire.OutputConfig)
386 }
387 if wire.Temperature != nil {
388 t.Fatalf("Sonnet 5 adaptive must drop temperature, got %v", *wire.Temperature)
389 }
390
391 // Opus 4.5 -> budget-based thinking, no output_config, temperature kept.
392 wire, err = c.buildRequest(Request{
393 Model: "claude-opus-4-5",
394 Reasoning: "high",
395 Temperature: &temp,
396 Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
397 })
398 if err != nil {
399 t.Fatal(err)
400 }
401 if wire.Thinking == nil || wire.Thinking.Type != "enabled" || wire.Thinking.BudgetTokens <= 0 {
402 t.Fatalf("want budget thinking, got %+v", wire.Thinking)
403 }
404 if wire.OutputConfig != nil {
405 t.Fatalf("budget models must not send output_config, got %+v", wire.OutputConfig)
406 }
407 if wire.Temperature == nil || *wire.Temperature != temp {
408 t.Fatalf("budget model should keep temperature, got %v", wire.Temperature)
409 }
410}
411
412func TestAnthropicBuildRequestStripsAssistantImages(t *testing.T) {
413 c := NewAnthropic("x", "").(*anthropicClient)
414 wire, err := c.buildRequest(Request{
415 Model: "claude-sonnet-4-5",
416 Messages: []Message{
417 {Role: RoleUser, Content: []Content{TextBlock{Text: "make an image"}}},
418 {Role: RoleAssistant, Content: []Content{
419 TextBlock{Text: "done"},
420 ImageBlock{MimeType: "image/png", Data: []byte("png")},
421 TextBlock{Text: "Saved image: `zut-gemini-image-x.png`"},
422 }},
423 {Role: RoleUser, Content: []Content{TextBlock{Text: "hello"}}},
424 },
425 })
426 if err != nil {
427 t.Fatal(err)
428 }
429 if len(wire.Messages) != 3 {
430 t.Fatalf("messages=%d", len(wire.Messages))
431 }
432 assistant := wire.Messages[1]
433 if assistant.Role != "assistant" {
434 t.Fatalf("role=%q", assistant.Role)
435 }
436 if len(assistant.Content) != 2 {
437 t.Fatalf("assistant content=%+v", assistant.Content)
438 }
439 for _, b := range assistant.Content {
440 if _, ok := b.(anthImageBlock); ok {
441 t.Fatalf("assistant image block was not stripped: %+v", assistant.Content)
442 }
443 }
444}
445
446func TestAnthropicStreamHappyPath(t *testing.T) {
447 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
448 w.Header().Set("content-type", "text/event-stream")
449 w.WriteHeader(http.StatusOK)
450 fl, _ := w.(http.Flusher)
451 write := func(s string) {
452 _, _ = w.Write([]byte(s))
453 if fl != nil {
454 fl.Flush()
455 }
456 }
457 write("event: message_start\ndata: {\"message\":{\"usage\":{\"input_tokens\":10,\"output_tokens\":0}}}\n\n")
458 write("event: content_block_start\ndata: {\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}\n\n")
459 write("event: content_block_delta\ndata: {\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"hi\"}}\n\n")
460 write("event: content_block_stop\ndata: {\"index\":0}\n\n")
461 write("event: message_delta\ndata: {\"delta\":{\"stop_reason\":\"end_turn\"},\"usage\":{\"output_tokens\":5}}\n\n")
462 write("event: message_stop\ndata: {}\n\n")
463 }))
464 defer srv.Close()
465
466 c := NewAnthropic("x", srv.URL)
467 evs, err := c.Stream(context.Background(), Request{Model: "claude-sonnet-4-5"})
468 if err != nil {
469 t.Fatal(err)
470 }
471 var gotText string
472 var done EventDone
473 for ev := range evs {
474 switch e := ev.(type) {
475 case EventTextDelta:
476 gotText += e.Delta
477 case EventDone:
478 done = e
479 }
480 }
481 if gotText != "hi" {
482 t.Fatalf("text=%q", gotText)
483 }
484 if done.Stop != StopEnd {
485 t.Fatalf("stop=%v", done.Stop)
486 }
487}
488
489func TestClaudeOpus5Catalog(t *testing.T) {
490 m, err := FindModel("anthropic", "claude-opus-5")
491 if err != nil {
492 t.Fatal(err)
493 }
494 if m.DisplayName != "Claude Opus 5" || m.ContextWindow != 1000000 || m.MaxOutput != 128000 || !m.Reasoning || !m.AdaptiveThinking || m.Speculative {
495 t.Fatalf("unexpected Opus 5 model: %+v", m)
496 }
497 if m.PriceInput != 5 || m.PriceOutput != 25 || m.PriceCacheRead != 0.5 || m.PriceCacheWrite != 6.25 {
498 t.Fatalf("unexpected Opus 5 pricing: %+v", m)
499 }
500}
501
502func TestClaudeSonnet5Catalog(t *testing.T) {
503 m, err := FindModel("anthropic", "claude-sonnet-5")
504 if err != nil {
505 t.Fatal(err)
506 }
507 if m.DisplayName != "Claude Sonnet 5" || m.ContextWindow != 1000000 || m.MaxOutput != 128000 || !m.Reasoning || !m.AdaptiveThinking {
508 t.Fatalf("unexpected Sonnet 5 model: %+v", m)
509 }
510 if m.PriceInput != 2 || m.PriceOutput != 10 || m.PriceCacheRead != 0.2 || m.PriceCacheWrite != 2.5 {
511 t.Fatalf("unexpected Sonnet 5 pricing: %+v", m)
512 }
513}
514
515func TestOpenAICompatAnthropicReasoningEffort(t *testing.T) {
516 c := NewOpenRouter("token", "").(*openaiClient)
517 wire, err := c.buildRequest(Request{
518 Model: "anthropic/claude-opus-4.8",
519 Reasoning: "maximum",
520 Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
521 })
522 if err != nil {
523 t.Fatal(err)
524 }
525 if wire.ReasoningEffort != "xhigh" {
526 t.Fatalf("want xhigh for adaptive Anthropic model over OpenAI-compatible wire, got %q", wire.ReasoningEffort)
527 }
528
529 wire, err = c.buildRequest(Request{
530 Model: "anthropic/claude-sonnet-5",
531 Reasoning: "maximum",
532 Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
533 })
534 if err != nil {
535 t.Fatal(err)
536 }
537 if wire.ReasoningEffort != "xhigh" {
538 t.Fatalf("want xhigh for Claude Sonnet 5 over OpenAI-compatible wire, got %q", wire.ReasoningEffort)
539 }
540
541 wire, err = c.buildRequest(Request{
542 Model: "gpt-5.1",
543 Reasoning: "maximum",
544 Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
545 })
546 if err != nil {
547 t.Fatal(err)
548 }
549 if wire.ReasoningEffort != "high" {
550 t.Fatalf("want generic OpenAI-compatible maximum clamped to high, got %q", wire.ReasoningEffort)
551 }
552}
553
554func TestOpenAIBuildRequestSkipsReasoningOnlyAssistantMessages(t *testing.T) {
555 c := NewKimi("token", "").(*openaiClient)
556 wire, err := c.buildRequest(Request{
557 Model: "kimi-for-coding",
558 Messages: []Message{
559 {Role: RoleUser, Content: []Content{TextBlock{Text: "first"}}},
560 {Role: RoleAssistant, Content: []Content{ReasoningBlock{Summary: "thinking only"}}},
561 {Role: RoleUser, Content: []Content{TextBlock{Text: "second"}}},
562 },
563 })
564 if err != nil {
565 t.Fatal(err)
566 }
567 for i, msg := range wire.Messages {
568 if msg.Role == "assistant" && msg.Content == nil && len(msg.ToolCalls) == 0 {
569 t.Fatalf("message %d is empty assistant: %+v", i, msg)
570 }
571 }
572 if got := len(wire.Messages); got != 2 {
573 t.Fatalf("messages=%d want 2 after skipping reasoning-only assistant", got)
574 }
575}
576
577func TestOpenAIStreamHappyPath(t *testing.T) {
578 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
579 w.Header().Set("content-type", "text/event-stream")
580 w.WriteHeader(http.StatusOK)
581 fl, _ := w.(http.Flusher)
582 write := func(s string) {
583 _, _ = w.Write([]byte(s))
584 if fl != nil {
585 fl.Flush()
586 }
587 }
588 write("data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"hel\"},\"finish_reason\":null}]}\n\n")
589 write("data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"lo\"},\"finish_reason\":\"stop\"}]}\n\n")
590 write("data: {\"choices\":[],\"usage\":{\"prompt_tokens\":8,\"completion_tokens\":2}}\n\n")
591 write("data: [DONE]\n\n")
592 }))
593 defer srv.Close()
594
595 c := NewOpenAI("x", srv.URL)
596 evs, err := c.Stream(context.Background(), Request{Model: "gpt-5"})
597 if err != nil {
598 t.Fatal(err)
599 }
600 var gotText string
601 var done EventDone
602 for ev := range evs {
603 switch e := ev.(type) {
604 case EventTextDelta:
605 gotText += e.Delta
606 case EventDone:
607 done = e
608 }
609 }
610 if gotText != "hello" {
611 t.Fatalf("text=%q", gotText)
612 }
613 if done.Stop != StopEnd {
614 t.Fatalf("stop=%v", done.Stop)
615 }
616}
617
618func TestDiscoverOpenRouter(t *testing.T) {
619 const body = `{"data":[
620 {"id":"x/full","pricing":{"prompt":"0.000003","completion":"0.000015"},
621 "context_length":200000,"top_provider":{"max_completion_tokens":64000},
622 "supported_parameters":["reasoning"]},
623 {"id":"x/fallback","top_provider":{"context_length":128000}},
624 {"id":""}]}`
625 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
626 _, _ = w.Write([]byte(body))
627 }))
628 defer srv.Close()
629
630 models, err := DiscoverOpenRouter(context.Background(), srv.URL)
631 if err != nil {
632 t.Fatal(err)
633 }
634 if len(models) != 2 { // empty id dropped
635 t.Fatalf("want 2 models, got %d", len(models))
636 }
637 // per-token USD -> per-1M; reasoning from supported_parameters.
638 if m := models[0]; m.Provider != "openrouter" || m.ContextWindow != 200000 ||
639 m.MaxOutput != 64000 || !m.Reasoning || m.PriceInput != 3 || m.PriceOutput != 15 {
640 t.Errorf("model[0]: %+v", m)
641 }
642 // context falls back to top_provider; no reasoning.
643 if m := models[1]; m.ContextWindow != 128000 || m.MaxOutput != 0 || m.Reasoning {
644 t.Errorf("model[1]: %+v", m)
645 }
646}
647
648func TestGPT56AndGPT6CatalogEntries(t *testing.T) {
649 cases := []struct {
650 provider string
651 id string
652 context int
653 priceInput float64
654 priceOut float64
655 cacheRead float64
656 cacheWrite float64
657 }{
658 {"openai", "gpt-5.6-luna", 500000, 1, 6, 0.1, 1.25},
659 {"openai", "gpt-5.6-sol", 500000, 5, 30, 0.5, 6.25},
660 {"openai", "gpt-5.6-terra", 500000, 2.5, 15, 0.25, 3.125},
661 {"openai", "gpt-6-astra", 500000, 10, 50, 1, 12.5},
662 {"openai-codex", "gpt-5.6-luna", 500000, 1, 6, 0.1, 1.25},
663 {"openai-codex", "gpt-5.6-sol", 500000, 5, 30, 0.5, 6.25},
664 {"openai-codex", "gpt-5.6-terra", 500000, 2.5, 15, 0.25, 3.125},
665 {"openai-codex", "gpt-6-astra", 500000, 10, 50, 1, 12.5},
666 {"azure-openai-responses", "gpt-5.6-luna", 1050000, 1, 6, 0.1, 1.25},
667 {"azure-openai-responses", "gpt-5.6-sol", 1050000, 5, 30, 0.5, 6.25},
668 {"azure-openai-responses", "gpt-5.6-terra", 1050000, 2.5, 15, 0.25, 3.125},
669 {"vercel-ai-gateway", "openai/gpt-5.6-luna", 1050000, 1, 6, 0.1, 1.25},
670 {"vercel-ai-gateway", "openai/gpt-5.6-sol", 1050000, 5, 30, 0.5, 6.25},
671 {"vercel-ai-gateway", "openai/gpt-5.6-terra", 1050000, 2.5, 15, 0.25, 3.125},
672 }
673 for _, tc := range cases {
674 m, err := FindModel(tc.provider, tc.id)
675 if err != nil {
676 t.Fatal(err)
677 }
678 if m.ContextWindow != tc.context || m.MaxOutput != 128000 || !m.Reasoning {
679 t.Fatalf("%s/%s limits: %+v", tc.provider, tc.id, m)
680 }
681 if m.PriceInput != tc.priceInput || m.PriceOutput != tc.priceOut || m.PriceCacheRead != tc.cacheRead || m.PriceCacheWrite != tc.cacheWrite {
682 t.Fatalf("%s/%s prices: %+v", tc.provider, tc.id, m)
683 }
684 if tc.provider == "openai-codex" && m.Speculative {
685 t.Fatalf("%s/%s is speculative", tc.provider, tc.id)
686 }
687 }
688}
689
690// TestOpenCodeGoCatalog pins entries added from the vendor's current Go
691// model lineup (https://opencode.ai/docs/go).
692func TestOpenCodeGoCatalog(t *testing.T) {
693 cases := []struct {
694 id string
695 context int
696 maxOut int
697 priceIn float64
698 priceOut float64
699 cacheRead float64
700 }{
701 {"gpt-5.6-luna", 1050000, 128000, 0.2, 1.2, 0.02},
702 {"grok-4.5", 500000, 500000, 2, 6, 0.3},
703 {"kimi-k3", 1048576, 131072, 3, 15, 0.3},
704 }
705 for _, tc := range cases {
706 m, err := FindModel("opencode-go", tc.id)
707 if err != nil {
708 t.Fatal(err)
709 }
710 if m.ContextWindow != tc.context || m.MaxOutput != tc.maxOut || !m.Reasoning {
711 t.Fatalf("opencode-go/%s limits: %+v", tc.id, m)
712 }
713 if m.PriceInput != tc.priceIn || m.PriceOutput != tc.priceOut || m.PriceCacheRead != tc.cacheRead {
714 t.Fatalf("opencode-go/%s prices: %+v", tc.id, m)
715 }
716 if m.BaseURL != "https://opencode.ai/zen/go/v1" {
717 t.Fatalf("opencode-go/%s baseURL: %s", tc.id, m.BaseURL)
718 }
719 }
720
721 luna, err := FindModel("opencode-go", "gpt-5.6-luna")
722 if err != nil {
723 t.Fatal(err)
724 }
725 if luna.API != APIResponses || luna.PriceCacheWrite != 0.25 {
726 t.Fatalf("opencode-go/gpt-5.6-luna API or cache-write price: %+v", luna)
727 }
728 if luna.PriceTierInputTokens != 272000 || luna.PriceInputAbove != 0.4 || luna.PriceOutputAbove != 1.8 || luna.PriceCacheReadAbove != 0.04 || luna.PriceCacheWriteAbove != 0.5 {
729 t.Fatalf("opencode-go/gpt-5.6-luna long-context prices: %+v", luna)
730 }
731}
732
733// TestOpenAIOmitsZeroMaxTokens guards against sending max_tokens: 0 for
734// discovered models that don't advertise an output cap (MaxOutput == 0).
735func TestOpenAIOmitsZeroMaxTokens(t *testing.T) {
736 SetLiveModels([]Model{
737 {Provider: "openrouter", ID: "vendor/no-cap", DisplayName: "No Cap"},
738 {Provider: "openrouter", ID: "vendor/reason-no-cap", DisplayName: "Reason No Cap", Reasoning: true},
739 {Provider: "openrouter", ID: "vendor/capped", DisplayName: "Capped", MaxOutput: 4096},
740 })
741 defer SetLiveModels(nil)
742
743 c := NewOpenAI("x", "").(*openaiClient)
744
745 got, err := c.buildRequest(Request{Model: "vendor/no-cap"})
746 if err != nil {
747 t.Fatal(err)
748 }
749 if got.MaxTokens != nil {
750 t.Errorf("expected max_tokens omitted, got %d", *got.MaxTokens)
751 }
752
753 got, err = c.buildRequest(Request{Model: "vendor/reason-no-cap"})
754 if err != nil {
755 t.Fatal(err)
756 }
757 if got.MaxCompletionTok != nil {
758 t.Errorf("expected max_completion_tokens omitted, got %d", *got.MaxCompletionTok)
759 }
760
761 got, err = c.buildRequest(Request{Model: "vendor/capped"})
762 if err != nil {
763 t.Fatal(err)
764 }
765 if got.MaxTokens == nil || *got.MaxTokens != 4096 {
766 t.Errorf("expected max_tokens 4096, got %v", got.MaxTokens)
767 }
768}