v0.3.60 bfc7b11

provider_test.go RawHistoryFolder

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

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