v0.3.88 ed7e2b4

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-opus-5-5"})
109	if err == nil {
110		t.Fatal("expected test server error")
111	}
112	if want := "claude-cli/2.1.280"; 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 TestRepairOrphanedToolResultsPreservesRepeatedToolCallPairs(t *testing.T) {
299	msgs := []Message{
300		{Role: RoleAssistant, Content: []Content{ToolCallBlock{ID: "reused", Name: "read"}}},
301		{Role: RoleTool, Content: []Content{ToolResultBlock{CallID: "reused", Content: []Content{TextBlock{Text: "first"}}}}},
302		{Role: RoleAssistant, Content: []Content{ToolCallBlock{ID: "reused", Name: "read"}}},
303		{Role: RoleTool, Content: []Content{ToolResultBlock{CallID: "reused", Content: []Content{TextBlock{Text: "second"}}}}},
304	}
305
306	got := RepairOrphanedToolResults(msgs)
307	if len(got) != len(msgs) {
308		t.Fatalf("messages=%d, want %d valid repeated-ID pairs", len(got), len(msgs))
309	}
310	for _, index := range []int{1, 3} {
311		if len(got[index].Content) != 1 {
312			t.Fatalf("message %d content=%d, want 1 tool result", index, len(got[index].Content))
313		}
314		if _, ok := got[index].Content[0].(ToolResultBlock); !ok {
315			t.Fatalf("message %d content type=%T, want ToolResultBlock", index, got[index].Content[0])
316		}
317	}
318}
319
320func TestAnthropicBuildRequestDeduplicatesToolResults(t *testing.T) {
321	c := NewAnthropic("x", "").(*anthropicClient)
322	wire, err := c.buildRequest(Request{
323		Model: "claude-sonnet-4-5",
324		Messages: []Message{
325			{Role: RoleUser, Content: []Content{TextBlock{Text: "read the file"}}},
326			{Role: RoleAssistant, Content: []Content{
327				ToolCallBlock{ID: "tool-1", Name: "read"},
328			}},
329			{Role: RoleTool, Content: []Content{
330				ToolResultBlock{CallID: "tool-1", Content: []Content{TextBlock{Text: "first result"}}},
331			}},
332			{Role: RoleTool, Content: []Content{
333				ToolResultBlock{CallID: "tool-1", Content: []Content{TextBlock{Text: "duplicate result"}}},
334			}},
335		},
336	})
337	if err != nil {
338		t.Fatal(err)
339	}
340	if len(wire.Messages) != 3 {
341		t.Fatalf("messages=%d, want 3 after dropping duplicate result message", len(wire.Messages))
342	}
343	blocks := wire.Messages[2].Content
344	if len(blocks) != 1 {
345		t.Fatalf("tool result blocks=%d, want 1", len(blocks))
346	}
347	result, ok := blocks[0].(anthToolResultBlock)
348	if !ok {
349		t.Fatalf("tool result block type=%T", blocks[0])
350	}
351	if string(result.Content) != `"first result"` {
352		t.Fatalf("tool result content=%s", result.Content)
353	}
354}
355
356func TestAnthropicBuildRequestPreservesRepeatedToolCallPairs(t *testing.T) {
357	c := NewAnthropic("x", "").(*anthropicClient)
358	wire, err := c.buildRequest(Request{
359		Model: "claude-sonnet-4-5",
360		Messages: []Message{
361			{Role: RoleAssistant, Content: []Content{ToolCallBlock{ID: "reused", Name: "read"}}},
362			{Role: RoleTool, Content: []Content{ToolResultBlock{CallID: "reused", Content: []Content{TextBlock{Text: "first"}}}}},
363			{Role: RoleAssistant, Content: []Content{ToolCallBlock{ID: "reused", Name: "read"}}},
364			{Role: RoleTool, Content: []Content{ToolResultBlock{CallID: "reused", Content: []Content{TextBlock{Text: "second"}}}}},
365		},
366	})
367	if err != nil {
368		t.Fatal(err)
369	}
370	if len(wire.Messages) != 4 {
371		t.Fatalf("messages=%d, want both repeated-ID tool result pairs", len(wire.Messages))
372	}
373	for _, index := range []int{1, 3} {
374		if len(wire.Messages[index].Content) != 1 {
375			t.Fatalf("message %d content=%d, want 1 tool result", index, len(wire.Messages[index].Content))
376		}
377		if _, ok := wire.Messages[index].Content[0].(anthToolResultBlock); !ok {
378			t.Fatalf("message %d content type=%T, want anthToolResultBlock", index, wire.Messages[index].Content[0])
379		}
380	}
381}
382
383func TestAnthropicStreamHappyPath(t *testing.T) {
384	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
385		w.Header().Set("content-type", "text/event-stream")
386		w.WriteHeader(http.StatusOK)
387		fl, _ := w.(http.Flusher)
388		write := func(s string) {
389			_, _ = w.Write([]byte(s))
390			if fl != nil {
391				fl.Flush()
392			}
393		}
394		write("event: message_start\ndata: {\"message\":{\"usage\":{\"input_tokens\":10,\"output_tokens\":0}}}\n\n")
395		write("event: content_block_start\ndata: {\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}\n\n")
396		write("event: content_block_delta\ndata: {\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"hi\"}}\n\n")
397		write("event: content_block_stop\ndata: {\"index\":0}\n\n")
398		write("event: message_delta\ndata: {\"delta\":{\"stop_reason\":\"end_turn\"},\"usage\":{\"output_tokens\":5}}\n\n")
399		write("event: message_stop\ndata: {}\n\n")
400	}))
401	defer srv.Close()
402
403	c := NewAnthropic("x", srv.URL)
404	evs, err := c.Stream(context.Background(), Request{Model: "claude-sonnet-4-5"})
405	if err != nil {
406		t.Fatal(err)
407	}
408	var gotText string
409	var done EventDone
410	for ev := range evs {
411		switch e := ev.(type) {
412		case EventTextDelta:
413			gotText += e.Delta
414		case EventDone:
415			done = e
416		}
417	}
418	if gotText != "hi" {
419		t.Fatalf("text=%q", gotText)
420	}
421	if done.Stop != StopEnd {
422		t.Fatalf("stop=%v", done.Stop)
423	}
424}
425
426func TestClaudeOpus5Catalog(t *testing.T) {
427	m, err := FindModel("anthropic", "claude-opus-5")
428	if err != nil {
429		t.Fatal(err)
430	}
431	if m.DisplayName != "Claude Opus 5" || m.ContextWindow != 1000000 || m.MaxOutput != 128000 || !m.Reasoning || !m.AdaptiveThinking || m.Speculative {
432		t.Fatalf("unexpected Opus 5 model: %+v", m)
433	}
434	if m.PriceInput != 5 || m.PriceOutput != 25 || m.PriceCacheRead != 0.5 || m.PriceCacheWrite != 6.25 {
435		t.Fatalf("unexpected Opus 5 pricing: %+v", m)
436	}
437}
438
439func TestClaudeSonnet5Catalog(t *testing.T) {
440	m, err := FindModel("anthropic", "claude-sonnet-5")
441	if err != nil {
442		t.Fatal(err)
443	}
444	if m.DisplayName != "Claude Sonnet 5" || m.ContextWindow != 1000000 || m.MaxOutput != 128000 || !m.Reasoning || !m.AdaptiveThinking {
445		t.Fatalf("unexpected Sonnet 5 model: %+v", m)
446	}
447	if m.PriceInput != 2 || m.PriceOutput != 10 || m.PriceCacheRead != 0.2 || m.PriceCacheWrite != 2.5 {
448		t.Fatalf("unexpected Sonnet 5 pricing: %+v", m)
449	}
450}
451
452func TestClaudeFable5Catalog(t *testing.T) {
453	m, err := FindModel("anthropic", "claude-fable-5")
454	if err != nil {
455		t.Fatal(err)
456	}
457	if m.DisplayName != "Claude Fable 5" || m.ContextWindow != 1000000 || m.MaxOutput != 128000 || !m.Reasoning || !m.AdaptiveThinking || m.Speculative {
458		t.Fatalf("unexpected Fable 5 model: %+v", m)
459	}
460	if m.PriceInput != 10 || m.PriceOutput != 50 || m.PriceCacheRead != 1 || m.PriceCacheWrite != 12.5 {
461		t.Fatalf("unexpected Fable 5 pricing: %+v", m)
462	}
463}
464
465func TestClaudeFable51Catalog(t *testing.T) {
466	m, err := FindModel("anthropic", "claude-fable-5-1")
467	if err != nil {
468		t.Fatal(err)
469	}
470	if m.DisplayName != "Claude Fable 5.1" || m.ContextWindow != 1000000 || m.MaxOutput != 128000 || !m.Reasoning || !m.AdaptiveThinking || m.Speculative {
471		t.Fatalf("unexpected Fable 5.1 model: %+v", m)
472	}
473	if m.PriceInput != 10 || m.PriceOutput != 50 || m.PriceCacheRead != 0.25 || m.PriceCacheWrite != 12.5 {
474		t.Fatalf("unexpected Fable 5.1 pricing: %+v", m)
475	}
476
477	wire, err := NewAnthropic("x", "").(*anthropicClient).buildRequest(Request{
478		Model:     m.ID,
479		Reasoning: "max",
480		Messages:  []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
481	})
482	if err != nil {
483		t.Fatal(err)
484	}
485	if wire.Thinking == nil || wire.Thinking.Type != "adaptive" || wire.Thinking.BudgetTokens != 0 {
486		t.Fatalf("unexpected Fable 5.1 thinking config: %+v", wire.Thinking)
487	}
488	if wire.OutputConfig == nil || wire.OutputConfig.Effort != "max" {
489		t.Fatalf("unexpected Fable 5.1 output config: %+v", wire.OutputConfig)
490	}
491}
492
493func TestGondolaSeedCatalog(t *testing.T) {
494	kimi, err := FindModel("gondola", "kimi-k3")
495	if err != nil {
496		t.Fatal(err)
497	}
498	if kimi.ContextWindow != 1000000 || kimi.MaxOutput != 131072 || !kimi.Reasoning || kimi.AdaptiveThinking {
499		t.Fatalf("unexpected Kimi K3 model: %+v", kimi)
500	}
501	if kimi.PriceInput != 1.1475 || kimi.PriceOutput != 5.7375 || kimi.PriceCacheRead != 0.11475 {
502		t.Fatalf("unexpected Kimi K3 pricing: %+v", kimi)
503	}
504
505	opus, err := FindModel("gondola", "claude-opus-5")
506	if err != nil {
507		t.Fatal(err)
508	}
509	if !opus.AdaptiveThinking || opus.PriceInput != 1.836 || opus.PriceOutput != 9.18 || opus.PriceCacheRead != 0.1836 || opus.PriceCacheWrite != 2.295 {
510		t.Fatalf("unexpected Claude Opus 5 model: %+v", opus)
511	}
512}
513
514func TestYoloAutoSeedCatalog(t *testing.T) {
515	// The free-plan default must resolve without a live refresh.
516	flash, err := FindModel("yolo-auto", "qwen3.8-flash")
517	if err != nil {
518		t.Fatal(err)
519	}
520	if flash.ContextWindow != yoloAutoContextWindow || flash.MaxOutput != yoloAutoMaxOutput ||
521		!flash.Reasoning || flash.BaseURL != yoloAutoDefaultBaseURL {
522		t.Fatalf("unexpected Qwen model: %+v", flash)
523	}
524	yolo, err := FindModel("yolo-auto", "yolo")
525	if err != nil {
526		t.Fatal(err)
527	}
528	if yolo.ContextWindow != yoloAutoContextWindow || yolo.MaxOutput != yoloAutoMaxOutput ||
529		!yolo.Reasoning || yolo.BaseURL != yoloAutoDefaultBaseURL {
530		t.Fatalf("unexpected Yolo model: %+v", yolo)
531	}
532
533	small, err := FindModel("yolo-auto", "yolo-small")
534	if err != nil {
535		t.Fatal(err)
536	}
537	if small.BaseURL != yoloAutoDefaultBaseURL || small.Reasoning {
538		t.Fatalf("unexpected Yolo Small model: %+v", small)
539	}
540}
541
542func TestOpenAICompatAnthropicReasoningEffort(t *testing.T) {
543	c := NewOpenRouter("token", "").(*openaiClient)
544	wire, err := c.buildRequest(Request{
545		Model:     "anthropic/claude-opus-4.8",
546		Reasoning: "maximum",
547		Messages:  []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
548	})
549	if err != nil {
550		t.Fatal(err)
551	}
552	if wire.ReasoningEffort != "xhigh" {
553		t.Fatalf("want xhigh for adaptive Anthropic model over OpenAI-compatible wire, got %q", wire.ReasoningEffort)
554	}
555
556	wire, err = c.buildRequest(Request{
557		Model:     "anthropic/claude-sonnet-5",
558		Reasoning: "maximum",
559		Messages:  []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
560	})
561	if err != nil {
562		t.Fatal(err)
563	}
564	if wire.ReasoningEffort != "xhigh" {
565		t.Fatalf("want xhigh for Claude Sonnet 5 over OpenAI-compatible wire, got %q", wire.ReasoningEffort)
566	}
567
568	wire, err = c.buildRequest(Request{
569		Model:     "gpt-5.1",
570		Reasoning: "maximum",
571		Messages:  []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
572	})
573	if err != nil {
574		t.Fatal(err)
575	}
576	if wire.ReasoningEffort != "high" {
577		t.Fatalf("want generic OpenAI-compatible maximum clamped to high, got %q", wire.ReasoningEffort)
578	}
579}
580
581func TestOpenAIBuildRequestSkipsReasoningOnlyAssistantMessages(t *testing.T) {
582	c := NewKimi("token", "").(*openaiClient)
583	wire, err := c.buildRequest(Request{
584		Model: "kimi-for-coding",
585		Messages: []Message{
586			{Role: RoleUser, Content: []Content{TextBlock{Text: "first"}}},
587			{Role: RoleAssistant, Content: []Content{ReasoningBlock{Summary: "thinking only"}}},
588			{Role: RoleUser, Content: []Content{TextBlock{Text: "second"}}},
589		},
590	})
591	if err != nil {
592		t.Fatal(err)
593	}
594	for i, msg := range wire.Messages {
595		if msg.Role == "assistant" && msg.Content == nil && len(msg.ToolCalls) == 0 {
596			t.Fatalf("message %d is empty assistant: %+v", i, msg)
597		}
598	}
599	if got := len(wire.Messages); got != 2 {
600		t.Fatalf("messages=%d want 2 after skipping reasoning-only assistant", got)
601	}
602}
603
604func TestOpenAIStreamHappyPath(t *testing.T) {
605	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
606		w.Header().Set("content-type", "text/event-stream")
607		w.WriteHeader(http.StatusOK)
608		fl, _ := w.(http.Flusher)
609		write := func(s string) {
610			_, _ = w.Write([]byte(s))
611			if fl != nil {
612				fl.Flush()
613			}
614		}
615		write("data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"hel\"},\"finish_reason\":null}]}\n\n")
616		write("data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"lo\"},\"finish_reason\":\"stop\"}]}\n\n")
617		write("data: {\"choices\":[],\"usage\":{\"prompt_tokens\":8,\"completion_tokens\":2}}\n\n")
618		write("data: [DONE]\n\n")
619	}))
620	defer srv.Close()
621
622	c := NewOpenAI("x", srv.URL)
623	evs, err := c.Stream(context.Background(), Request{Model: "gpt-5"})
624	if err != nil {
625		t.Fatal(err)
626	}
627	var gotText string
628	var done EventDone
629	for ev := range evs {
630		switch e := ev.(type) {
631		case EventTextDelta:
632			gotText += e.Delta
633		case EventDone:
634			done = e
635		}
636	}
637	if gotText != "hello" {
638		t.Fatalf("text=%q", gotText)
639	}
640	if done.Stop != StopEnd {
641		t.Fatalf("stop=%v", done.Stop)
642	}
643}
644
645func TestDiscoverOpenRouter(t *testing.T) {
646	const body = `{"data":[
647		{"id":"x/full","pricing":{"prompt":"0.000003","completion":"0.000015"},
648		 "context_length":200000,"top_provider":{"max_completion_tokens":64000},
649		 "supported_parameters":["reasoning"]},
650		{"id":"x/fallback","top_provider":{"context_length":128000}},
651		{"id":""}]}`
652	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
653		_, _ = w.Write([]byte(body))
654	}))
655	defer srv.Close()
656
657	models, err := DiscoverOpenRouter(context.Background(), srv.URL)
658	if err != nil {
659		t.Fatal(err)
660	}
661	if len(models) != 2 { // empty id dropped
662		t.Fatalf("want 2 models, got %d", len(models))
663	}
664	// per-token USD -> per-1M; reasoning from supported_parameters.
665	if m := models[0]; m.Provider != "openrouter" || m.ContextWindow != 200000 ||
666		m.MaxOutput != 64000 || !m.Reasoning || m.PriceInput != 3 || m.PriceOutput != 15 {
667		t.Errorf("model[0]: %+v", m)
668	}
669	// context falls back to top_provider; no reasoning.
670	if m := models[1]; m.ContextWindow != 128000 || m.MaxOutput != 0 || m.Reasoning {
671		t.Errorf("model[1]: %+v", m)
672	}
673}
674
675func TestGPT56CatalogEntries(t *testing.T) {
676	cases := []struct {
677		provider   string
678		id         string
679		context    int
680		priceInput float64
681		priceOut   float64
682		cacheRead  float64
683		cacheWrite float64
684	}{
685		{"openai", "gpt-5.6-luna", 272000, 1, 6, 0.1, 1.25},
686		{"openai", "gpt-5.6-sol", 272000, 5, 30, 0.5, 6.25},
687		{"openai", "gpt-5.6-terra", 272000, 2.5, 15, 0.25, 3.125},
688		{"openai-codex", "gpt-5.6-luna", 272000, 1, 6, 0.1, 1.25},
689		{"openai-codex", "gpt-5.6-sol", 272000, 5, 30, 0.5, 6.25},
690		{"openai-codex", "gpt-5.6-terra", 272000, 2.5, 15, 0.25, 3.125},
691		{"azure-openai-responses", "gpt-5.6-luna", 1050000, 1, 6, 0.1, 1.25},
692		{"azure-openai-responses", "gpt-5.6-sol", 1050000, 5, 30, 0.5, 6.25},
693		{"azure-openai-responses", "gpt-5.6-terra", 1050000, 2.5, 15, 0.25, 3.125},
694		{"vercel-ai-gateway", "openai/gpt-5.6-luna", 1050000, 1, 6, 0.1, 1.25},
695		{"vercel-ai-gateway", "openai/gpt-5.6-sol", 1050000, 5, 30, 0.5, 6.25},
696		{"vercel-ai-gateway", "openai/gpt-5.6-terra", 1050000, 2.5, 15, 0.25, 3.125},
697	}
698	for _, tc := range cases {
699		m, err := FindModel(tc.provider, tc.id)
700		if err != nil {
701			t.Fatal(err)
702		}
703		if m.ContextWindow != tc.context || m.MaxOutput != 128000 || !m.Reasoning {
704			t.Fatalf("%s/%s limits: %+v", tc.provider, tc.id, m)
705		}
706		if m.PriceInput != tc.priceInput || m.PriceOutput != tc.priceOut || m.PriceCacheRead != tc.cacheRead || m.PriceCacheWrite != tc.cacheWrite {
707			t.Fatalf("%s/%s prices: %+v", tc.provider, tc.id, m)
708		}
709	}
710}
711
712// TestOpenCodeGoCatalog pins entries added from the vendor's current Go
713// model lineup (https://opencode.ai/docs/go).
714func TestOpenCodeGoCatalog(t *testing.T) {
715	cases := []struct {
716		id        string
717		context   int
718		maxOut    int
719		priceIn   float64
720		priceOut  float64
721		cacheRead float64
722	}{
723		{"gpt-5.6-luna", 1050000, 128000, 0.2, 1.2, 0.02},
724		{"grok-4.5", 500000, 500000, 2, 6, 0.3},
725		{"kimi-k3", 1048576, 131072, 3, 15, 0.3},
726	}
727	for _, tc := range cases {
728		m, err := FindModel("opencode-go", tc.id)
729		if err != nil {
730			t.Fatal(err)
731		}
732		if m.ContextWindow != tc.context || m.MaxOutput != tc.maxOut || !m.Reasoning {
733			t.Fatalf("opencode-go/%s limits: %+v", tc.id, m)
734		}
735		if m.PriceInput != tc.priceIn || m.PriceOutput != tc.priceOut || m.PriceCacheRead != tc.cacheRead {
736			t.Fatalf("opencode-go/%s prices: %+v", tc.id, m)
737		}
738		if m.BaseURL != "https://opencode.ai/zen/go/v1" {
739			t.Fatalf("opencode-go/%s baseURL: %s", tc.id, m.BaseURL)
740		}
741	}
742
743	luna, err := FindModel("opencode-go", "gpt-5.6-luna")
744	if err != nil {
745		t.Fatal(err)
746	}
747	if luna.API != APIResponses || luna.PriceCacheWrite != 0.25 {
748		t.Fatalf("opencode-go/gpt-5.6-luna API or cache-write price: %+v", luna)
749	}
750	if luna.PriceTierInputTokens != 272000 || luna.PriceInputAbove != 0.4 || luna.PriceOutputAbove != 1.8 || luna.PriceCacheReadAbove != 0.04 || luna.PriceCacheWriteAbove != 0.5 {
751		t.Fatalf("opencode-go/gpt-5.6-luna long-context prices: %+v", luna)
752	}
753}
754
755// TestOpenAIOmitsZeroMaxTokens guards against sending max_tokens: 0 for
756// discovered models that don't advertise an output cap (MaxOutput == 0).
757func TestOpenAIOmitsZeroMaxTokens(t *testing.T) {
758	SetLiveModels([]Model{
759		{Provider: "openrouter", ID: "vendor/no-cap", DisplayName: "No Cap"},
760		{Provider: "openrouter", ID: "vendor/reason-no-cap", DisplayName: "Reason No Cap", Reasoning: true},
761		{Provider: "openrouter", ID: "vendor/capped", DisplayName: "Capped", MaxOutput: 4096},
762	})
763	defer SetLiveModels(nil)
764
765	c := NewOpenAI("x", "").(*openaiClient)
766
767	got, err := c.buildRequest(Request{Model: "vendor/no-cap"})
768	if err != nil {
769		t.Fatal(err)
770	}
771	if got.MaxTokens != nil {
772		t.Errorf("expected max_tokens omitted, got %d", *got.MaxTokens)
773	}
774
775	got, err = c.buildRequest(Request{Model: "vendor/reason-no-cap"})
776	if err != nil {
777		t.Fatal(err)
778	}
779	if got.MaxCompletionTok != nil {
780		t.Errorf("expected max_completion_tokens omitted, got %d", *got.MaxCompletionTok)
781	}
782
783	got, err = c.buildRequest(Request{Model: "vendor/capped"})
784	if err != nil {
785		t.Fatal(err)
786	}
787	if got.MaxTokens == nil || *got.MaxTokens != 4096 {
788		t.Errorf("expected max_tokens 4096, got %v", got.MaxTokens)
789	}
790}

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 477

↑↓ moveenter openesc close