v0.15.0 6b178f8

provider_test.go RawHistoryFolder

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

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 476

↑↓ moveenter openesc close