main 4509ec7

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 TestUsageCacheHitRatioIncludesAllPromptTokens(t *testing.T) {
15	u := Usage{InputTokens: 84_000, CacheReadTokens: 123_000, CacheWriteTokens: 3_000, CacheMeasuredPromptTokens: 210_000, CacheMeasuredReadTokens: 123_000}
16	if got := u.PromptTokens(); got != 210_000 {
17		t.Fatalf("prompt tokens = %d, want 210000", got)
18	}
19	ratio, ok := u.CacheHitRatio()
20	if !ok || math.Abs(ratio-123.0/210.0) > 1e-9 {
21		t.Fatalf("cache hit ratio = %v, %v, want %v, true", ratio, ok, 123.0/210.0)
22	}
23}
24
25func TestUsageCacheHitRatioDistinguishesKnownZeroFromHistoricalUnknown(t *testing.T) {
26	knownMiss := Usage{InputTokens: 84_000, CacheMeasuredPromptTokens: 84_000}
27	if ratio, ok := knownMiss.CacheHitRatio(); !ok || ratio != 0 {
28		t.Fatalf("known cache miss ratio = %v, %v, want 0, true", ratio, ok)
29	}
30
31	historicalUnknown := Usage{InputTokens: 84_000}
32	if ratio, ok := historicalUnknown.CacheHitRatio(); ok || ratio != 0 {
33		t.Fatalf("historical cache ratio = %v, %v, want 0, false", ratio, ok)
34	}
35}
36
37func TestUsageAddPreservesOnlyMeasuredCacheDenominators(t *testing.T) {
38	measured := Usage{InputTokens: 60, CacheReadTokens: 40, CacheMeasuredPromptTokens: 100, CacheMeasuredReadTokens: 40}
39	unknown := Usage{InputTokens: 200, CacheReadTokens: 100}
40
41	got := measured.Add(unknown)
42	if got.CacheMeasuredPromptTokens != 100 {
43		t.Fatalf("measured cache denominator = %d, want 100", got.CacheMeasuredPromptTokens)
44	}
45	if ratio, ok := got.CacheHitRatio(); !ok || math.Abs(ratio-0.4) > 1e-9 {
46		t.Fatalf("cache hit ratio = %v, %v, want 0.4, true", ratio, ok)
47	}
48}
49
50func TestSSEParse(t *testing.T) {
51	r := strings.NewReader("event: foo\ndata: {\"a\":1}\n\ndata: hello\ndata: world\n\n")
52	ch := make(chan sseEvent, 4)
53	go readSSE(r, ch)
54
55	e := <-ch
56	if e.Event != "foo" || e.Data != `{"a":1}` {
57		t.Fatalf("event 1: %+v", e)
58	}
59	e = <-ch
60	if e.Event != "" || e.Data != "hello\nworld" {
61		t.Fatalf("event 2: %+v", e)
62	}
63	if _, ok := <-ch; ok {
64		t.Fatalf("channel not closed")
65	}
66}
67
68type errorAfterReader struct {
69	data *strings.Reader
70	err  error
71}
72
73func (r *errorAfterReader) Read(p []byte) (int, error) {
74	if r.data.Len() > 0 {
75		return r.data.Read(p)
76	}
77	return 0, r.err
78}
79
80func TestSSEReadErrorIsDeliveredBeforeChannelClose(t *testing.T) {
81	want := io.ErrUnexpectedEOF
82	ch := make(chan sseEvent, 2)
83	go readSSE(&errorAfterReader{
84		data: strings.NewReader("data: hello\n\n"),
85		err:  want,
86	}, ch)
87
88	if event := <-ch; event.Data != "hello" {
89		t.Fatalf("event = %+v, want payload before read error", event)
90	}
91	terminal, ok := <-ch
92	if !ok {
93		t.Fatal("channel closed without scanner error")
94	}
95	if !errors.Is(terminal.Err, want) {
96		t.Fatalf("scanner error = %v, want %v", terminal.Err, want)
97	}
98	if _, ok := <-ch; ok {
99		t.Fatal("channel remained open after scanner error")
100	}
101}
102
103func TestStreamsReportSSEReadFailures(t *testing.T) {
104	want := io.ErrUnexpectedEOF
105	response := func() *http.Response {
106		return &http.Response{Body: io.NopCloser(&errorAfterReader{
107			data: strings.NewReader(""),
108			err:  want,
109		})}
110	}
111	cases := []struct {
112		name string
113		run  func(chan<- Event)
114	}{
115		{
116			name: "anthropic",
117			run: func(out chan<- Event) {
118				(&anthropicClient{}).runStream(context.Background(), response(), Request{Model: "test"}, out)
119			},
120		},
121		{
122			name: "openai",
123			run: func(out chan<- Event) {
124				(&openaiClient{}).runStream(context.Background(), response(), Request{Model: "test"}, out)
125			},
126		},
127		{
128			name: "gemini",
129			run: func(out chan<- Event) {
130				(&geminiClient{}).runStream(context.Background(), response(), Request{Model: "test"}, out)
131			},
132		},
133		{
134			name: "openai codex",
135			run: func(out chan<- Event) {
136				(&codexClient{}).runStream(context.Background(), response(), Request{Model: "test"}, out)
137			},
138		},
139	}
140	for _, tc := range cases {
141		t.Run(tc.name, func(t *testing.T) {
142			out := make(chan Event, 4)
143			go tc.run(out)
144
145			var done EventDone
146			foundDone := false
147			for event := range out {
148				if e, ok := event.(EventDone); ok {
149					done = e
150					foundDone = true
151				}
152			}
153			if !foundDone {
154				t.Fatal("stream ended without EventDone")
155			}
156			if done.Stop != StopError {
157				t.Fatalf("stop = %s, want %s", done.Stop, StopError)
158			}
159			if !errors.Is(done.Err, want) {
160				t.Fatalf("stream error = %v, want %v", done.Err, want)
161			}
162		})
163	}
164}
165
166func TestModelCatalog(t *testing.T) {
167	if len(Catalog) == 0 {
168		t.Fatal("empty catalog")
169	}
170	if _, err := FindModel("anthropic", "claude-sonnet-4-5"); err != nil {
171		t.Fatal(err)
172	}
173	if _, err := FindModel("openai", "gpt-5"); err != nil {
174		t.Fatal(err)
175	}
176	if _, err := FindModel("", "nope"); err == nil {
177		t.Fatal("expected error")
178	}
179}
180
181func TestVertexGeminiFlashCatalogLimits(t *testing.T) {
182	for _, id := range []string{"gemini-3.1-flash-lite", "gemini-3.5-flash"} {
183		m, err := FindModel("google-vertex", id)
184		if err != nil {
185			t.Fatal(err)
186		}
187		if m.ContextWindow != 1048576 || m.MaxOutput != 65535 || !m.Reasoning {
188			t.Errorf("%s metadata = %+v", id, m)
189		}
190	}
191}
192
193func TestComputeCost(t *testing.T) {
194	m, _ := FindModel("anthropic", "claude-sonnet-4-5")
195	cost := ComputeCost(m, Usage{InputTokens: 1_000_000, OutputTokens: 1_000_000})
196	want := m.PriceInput + m.PriceOutput
197	if cost != want {
198		t.Fatalf("cost=%v want=%v", cost, want)
199	}
200}
201
202func TestComputeCostInputTier(t *testing.T) {
203	m := Model{
204		PriceInput: 1, PriceOutput: 2, PriceCacheRead: 0.1,
205		PriceTierInputTokens: 100,
206		PriceInputAbove:      3, PriceOutputAbove: 4, PriceCacheReadAbove: 0.2,
207	}
208	below := ComputeCost(m, Usage{InputTokens: 50, OutputTokens: 10, CacheReadTokens: 49})
209	wantBelow := (50.0*1 + 10*2 + 49*0.1) / 1_000_000
210	if math.Abs(below-wantBelow) > 1e-12 {
211		t.Fatalf("below-tier cost=%v want=%v", below, wantBelow)
212	}
213
214	atThreshold := ComputeCost(m, Usage{InputTokens: 50, OutputTokens: 10, CacheReadTokens: 50})
215	wantAtThreshold := (50.0*3 + 10*4 + 50*0.2) / 1_000_000
216	if math.Abs(atThreshold-wantAtThreshold) > 1e-12 {
217		t.Fatalf("at-tier cost=%v want=%v", atThreshold, wantAtThreshold)
218	}
219}
220
221func TestComputeCostMultipleInputTiers(t *testing.T) {
222	m := Model{
223		PriceInput: 1,
224		PriceTiers: []ModelPriceTier{
225			{InputTokens: 500000, PriceInput: 5},
226			{InputTokens: 272000, PriceInput: 3},
227		},
228	}
229	for _, tc := range []struct {
230		name  string
231		input int
232		price float64
233	}{
234		{"below_first", 271999, 1},
235		{"at_first", 272000, 3},
236		{"between", 499999, 3},
237		{"at_second", 500000, 5},
238		{"above_second", 600000, 5},
239	} {
240		t.Run(tc.name, func(t *testing.T) {
241			got := ComputeCost(m, Usage{InputTokens: tc.input})
242			want := float64(tc.input) * tc.price / 1_000_000
243			if math.Abs(got-want) > 1e-12 {
244				t.Fatalf("cost = %.12f, want %.12f", got, want)
245			}
246		})
247	}
248}
249
250func TestAnthropicErrorStatus(t *testing.T) {
251	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
252		w.WriteHeader(http.StatusUnauthorized)
253		_, _ = w.Write([]byte(`{"error":{"type":"auth","message":"bad key"}}`))
254	}))
255	defer srv.Close()
256
257	c := NewAnthropic("x", srv.URL)
258	_, err := c.Stream(context.Background(), Request{Model: "claude-sonnet-4-5"})
259	if err == nil || !strings.Contains(err.Error(), "401") {
260		t.Fatalf("want 401 err, got %v", err)
261	}
262}
263
264func TestOpenAIErrorStatus(t *testing.T) {
265	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
266		w.WriteHeader(http.StatusBadRequest)
267		_, _ = w.Write([]byte(`{"error":{"message":"nope"}}`))
268	}))
269	defer srv.Close()
270
271	c := NewOpenAI("x", srv.URL)
272	_, err := c.Stream(context.Background(), Request{Model: "gpt-5"})
273	if err == nil || !strings.Contains(err.Error(), "400") {
274		t.Fatalf("want 400 err, got %v", err)
275	}
276}
277
278func TestOpenAIUserContentPreservesProjectedTextBlocks(t *testing.T) {
279	c := NewOpenAI("x", "").(*openaiClient)
280	wire, err := c.buildRequest(Request{
281		Model: "gpt-5",
282		Messages: []Message{{
283			Role: RoleUser,
284			Content: []Content{
285				TextBlock{Text: "[message time: 2026-08-08T12:34:56-07:00]"},
286				TextBlock{Text: "fail"},
287			},
288		}},
289	})
290	if err != nil {
291		t.Fatal(err)
292	}
293	blocks, ok := wire.Messages[0].Content.([]interface{})
294	if !ok {
295		t.Fatalf("user content = %T, want structured text blocks", wire.Messages[0].Content)
296	}
297	if len(blocks) != 2 {
298		t.Fatalf("user content blocks = %d, want 2", len(blocks))
299	}
300	if got := blocks[0].(oaiContentText).Text; got != "[message time: 2026-08-08T12:34:56-07:00]" {
301		t.Fatalf("timestamp block = %q", got)
302	}
303	if got := blocks[1].(oaiContentText).Text; got != "fail" {
304		t.Fatalf("user text block = %q", got)
305	}
306}
307
308func TestMiniMaxM3CatalogAndRequest(t *testing.T) {
309	for _, provider := range []string{"minimax", "minimax-cn"} {
310		m, err := FindModel(provider, "MiniMax-M3")
311		if err != nil {
312			t.Fatal(err)
313		}
314		if m.ContextWindow != 1000000 || m.MaxOutput != 131072 || !m.Reasoning || !m.AdaptiveThinkingCompat {
315			t.Errorf("%s metadata = %+v", provider, m)
316		}
317		if m.PriceInput != 0.3 || m.PriceOutput != 1.2 || m.PriceCacheRead != 0.06 || m.PriceTierInputTokens != 512000 || m.PriceInputAbove != 0.6 || m.PriceOutputAbove != 2.4 || m.PriceCacheReadAbove != 0.12 {
318			t.Errorf("%s pricing = %+v", provider, m)
319		}
320	}
321
322	c := NewAnthropicCompat("minimax", "x", "https://api.minimax.io/anthropic").(*anthropicClient)
323	temp := float32(0.7)
324	wire, err := c.buildRequest(Request{
325		Model:       "MiniMax-M3",
326		Reasoning:   "high",
327		Temperature: &temp,
328		Messages:    []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
329	})
330	if err != nil {
331		t.Fatal(err)
332	}
333	if wire.Thinking == nil || wire.Thinking.Type != "adaptive" || wire.Thinking.BudgetTokens != 0 {
334		t.Fatalf("want adaptive thinking without a budget, got %+v", wire.Thinking)
335	}
336	if wire.OutputConfig != nil {
337		t.Fatalf("MiniMax-M3 must not send output_config, got %+v", wire.OutputConfig)
338	}
339	if wire.Temperature == nil || *wire.Temperature != temp {
340		t.Fatalf("MiniMax-M3 should keep temperature, got %v", wire.Temperature)
341	}
342}
343
344func TestAnthropicAdaptiveThinking(t *testing.T) {
345	c := NewAnthropic("x", "").(*anthropicClient)
346	temp := float32(0.7)
347
348	// Opus 5 -> adaptive thinking, effort set, no budget, no temperature.
349	wire, err := c.buildRequest(Request{
350		Model:       "claude-opus-5",
351		Reasoning:   "high",
352		Temperature: &temp,
353		Messages:    []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
354	})
355	if err != nil {
356		t.Fatal(err)
357	}
358	if wire.Thinking == nil || wire.Thinking.Type != "adaptive" {
359		t.Fatalf("want adaptive thinking, got %+v", wire.Thinking)
360	}
361	if wire.Thinking.BudgetTokens != 0 {
362		t.Fatalf("adaptive must not send budget_tokens, got %d", wire.Thinking.BudgetTokens)
363	}
364	if wire.OutputConfig == nil || wire.OutputConfig.Effort != "high" {
365		t.Fatalf("want effort=high, got %+v", wire.OutputConfig)
366	}
367	if wire.Temperature != nil {
368		t.Fatalf("adaptive must drop temperature, got %v", *wire.Temperature)
369	}
370
371	// maximum -> xhigh effort.
372	wire, err = c.buildRequest(Request{
373		Model:     "claude-opus-5",
374		Reasoning: "maximum",
375		Messages:  []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
376	})
377	if err != nil {
378		t.Fatal(err)
379	}
380	if wire.OutputConfig == nil || wire.OutputConfig.Effort != "xhigh" {
381		t.Fatalf("want effort=xhigh, got %+v", wire.OutputConfig)
382	}
383
384	// max is a separate native tier above xhigh on adaptive models.
385	wire, err = c.buildRequest(Request{
386		Model:     "claude-opus-5",
387		Reasoning: "max",
388		Messages:  []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
389	})
390	if err != nil {
391		t.Fatal(err)
392	}
393	if wire.OutputConfig == nil || wire.OutputConfig.Effort != "max" {
394		t.Fatalf("want effort=max, got %+v", wire.OutputConfig)
395	}
396
397	// Sonnet 5 -> adaptive thinking, effort set, no budget, no temperature.
398	wire, err = c.buildRequest(Request{
399		Model:       "claude-sonnet-5",
400		Reasoning:   "high",
401		Temperature: &temp,
402		Messages:    []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
403	})
404	if err != nil {
405		t.Fatal(err)
406	}
407	if wire.Thinking == nil || wire.Thinking.Type != "adaptive" {
408		t.Fatalf("want adaptive thinking for Sonnet 5, got %+v", wire.Thinking)
409	}
410	if wire.Thinking.BudgetTokens != 0 {
411		t.Fatalf("Sonnet 5 adaptive must not send budget_tokens, got %d", wire.Thinking.BudgetTokens)
412	}
413	if wire.OutputConfig == nil || wire.OutputConfig.Effort != "high" {
414		t.Fatalf("want Sonnet 5 effort=high, got %+v", wire.OutputConfig)
415	}
416	if wire.Temperature != nil {
417		t.Fatalf("Sonnet 5 adaptive must drop temperature, got %v", *wire.Temperature)
418	}
419
420	// Opus 4.5 -> budget-based thinking, no output_config, temperature kept.
421	wire, err = c.buildRequest(Request{
422		Model:       "claude-opus-4-5",
423		Reasoning:   "high",
424		Temperature: &temp,
425		Messages:    []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
426	})
427	if err != nil {
428		t.Fatal(err)
429	}
430	if wire.Thinking == nil || wire.Thinking.Type != "enabled" || wire.Thinking.BudgetTokens <= 0 {
431		t.Fatalf("want budget thinking, got %+v", wire.Thinking)
432	}
433	if wire.OutputConfig != nil {
434		t.Fatalf("budget models must not send output_config, got %+v", wire.OutputConfig)
435	}
436	if wire.Temperature == nil || *wire.Temperature != temp {
437		t.Fatalf("budget model should keep temperature, got %v", wire.Temperature)
438	}
439}
440
441func TestAnthropicBuildRequestStripsAssistantImages(t *testing.T) {
442	c := NewAnthropic("x", "").(*anthropicClient)
443	wire, err := c.buildRequest(Request{
444		Model: "claude-sonnet-4-5",
445		Messages: []Message{
446			{Role: RoleUser, Content: []Content{TextBlock{Text: "make an image"}}},
447			{Role: RoleAssistant, Content: []Content{
448				TextBlock{Text: "done"},
449				ImageBlock{MimeType: "image/png", Data: []byte("png")},
450				TextBlock{Text: "Saved image: `zut-gemini-image-x.png`"},
451			}},
452			{Role: RoleUser, Content: []Content{TextBlock{Text: "hello"}}},
453		},
454	})
455	if err != nil {
456		t.Fatal(err)
457	}
458	if len(wire.Messages) != 3 {
459		t.Fatalf("messages=%d", len(wire.Messages))
460	}
461	assistant := wire.Messages[1]
462	if assistant.Role != "assistant" {
463		t.Fatalf("role=%q", assistant.Role)
464	}
465	if len(assistant.Content) != 2 {
466		t.Fatalf("assistant content=%+v", assistant.Content)
467	}
468	for _, b := range assistant.Content {
469		if _, ok := b.(anthImageBlock); ok {
470			t.Fatalf("assistant image block was not stripped: %+v", assistant.Content)
471		}
472	}
473}
474
475func TestRepairOrphanedToolResultsDeduplicates(t *testing.T) {
476	text := func(s string) TextBlock { return TextBlock{Text: s} }
477	result := func(id, s string) ToolResultBlock {
478		return ToolResultBlock{CallID: id, Content: []Content{text(s)}}
479	}
480	msgs := []Message{
481		{Role: RoleAssistant, Content: []Content{
482			ToolCallBlock{ID: "tool-1", Name: "read"},
483			ToolCallBlock{ID: "tool-2", Name: "read"},
484		}},
485		{Role: RoleTool, Content: []Content{result("tool-1", "first"), text("note"), result("tool-1", "dup-same-msg")}},
486		{Role: RoleTool, Content: []Content{result("tool-1", "dup-across-msg")}},
487		{Role: RoleTool, Content: []Content{result("tool-2", "second")}},
488		{Role: RoleTool, Content: []Content{result("orphan", "orphan")}},
489	}
490	got := RepairOrphanedToolResults(msgs)
491	if len(got) != 3 {
492		t.Fatalf("messages=%d, want 3 after dropping duplicates and orphans", len(got))
493	}
494	first := got[1].Content
495	if len(first) != 2 {
496		t.Fatalf("first result message blocks=%d, want result+note", len(first))
497	}
498	tr, ok := first[0].(ToolResultBlock)
499	if !ok || tr.CallID != "tool-1" {
500		t.Fatalf("first block=%T %+v, want tool-1 result", first[0], first[0])
501	}
502	if txt, ok := tr.Content[0].(TextBlock); !ok || txt.Text != "first" {
503		t.Fatalf("first result content=%+v, want first", tr.Content)
504	}
505	if txt, ok := first[1].(TextBlock); !ok || txt.Text != "note" {
506		t.Fatalf("mixed content=%+v, want preserved note", first[1])
507	}
508	last, ok := got[2].Content[0].(ToolResultBlock)
509	if !ok || last.CallID != "tool-2" {
510		t.Fatalf("last block=%T %+v, want tool-2 result", got[2].Content[0], got[2].Content[0])
511	}
512}
513
514func TestAnthropicStreamHappyPath(t *testing.T) {
515	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
516		w.Header().Set("content-type", "text/event-stream")
517		w.WriteHeader(http.StatusOK)
518		fl, _ := w.(http.Flusher)
519		write := func(s string) {
520			_, _ = w.Write([]byte(s))
521			if fl != nil {
522				fl.Flush()
523			}
524		}
525		write("event: message_start\ndata: {\"message\":{\"usage\":{\"input_tokens\":10,\"output_tokens\":0}}}\n\n")
526		write("event: content_block_start\ndata: {\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}\n\n")
527		write("event: content_block_delta\ndata: {\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"hi\"}}\n\n")
528		write("event: content_block_stop\ndata: {\"index\":0}\n\n")
529		write("event: message_delta\ndata: {\"delta\":{\"stop_reason\":\"end_turn\"},\"usage\":{\"output_tokens\":5}}\n\n")
530		write("event: message_stop\ndata: {}\n\n")
531	}))
532	defer srv.Close()
533
534	c := NewAnthropic("x", srv.URL)
535	evs, err := c.Stream(context.Background(), Request{Model: "claude-sonnet-4-5"})
536	if err != nil {
537		t.Fatal(err)
538	}
539	var gotText string
540	var done EventDone
541	for ev := range evs {
542		switch e := ev.(type) {
543		case EventTextDelta:
544			gotText += e.Delta
545		case EventDone:
546			done = e
547		}
548	}
549	if gotText != "hi" {
550		t.Fatalf("text=%q", gotText)
551	}
552	if done.Stop != StopEnd {
553		t.Fatalf("stop=%v", done.Stop)
554	}
555}
556
557func anthropicStreamUsage(t *testing.T, messageStart string) Usage {
558	t.Helper()
559	return anthropicStreamUsageWithDelta(t, messageStart, `{"delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":5}}`)
560}
561
562func anthropicStreamUsageWithDelta(t *testing.T, messageStart, messageDelta string) Usage {
563	t.Helper()
564	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
565		w.Header().Set("content-type", "text/event-stream")
566		w.WriteHeader(http.StatusOK)
567		_, _ = w.Write([]byte("event: message_start\ndata: " + messageStart + "\n\n" +
568			"event: message_delta\ndata: " + messageDelta + "\n\n" +
569			"event: message_stop\ndata: {}\n\n"))
570	}))
571	defer srv.Close()
572
573	evs, err := NewAnthropic("x", srv.URL).Stream(context.Background(), Request{Model: "claude-sonnet-4-5"})
574	if err != nil {
575		t.Fatal(err)
576	}
577	var usage Usage
578	for ev := range evs {
579		if e, ok := ev.(EventUsage); ok {
580			usage = e.Usage
581		}
582	}
583	return usage
584}
585
586func TestAnthropicStreamReportsCacheHitRatio(t *testing.T) {
587	usage := anthropicStreamUsage(t, `{"message":{"usage":{"input_tokens":20,"output_tokens":0,"cache_read_input_tokens":900,"cache_creation_input_tokens":80}}}`)
588	if usage.CacheReadTokens != 900 || usage.CacheWriteTokens != 80 {
589		t.Fatalf("cache tokens = R%d W%d, want R900 W80", usage.CacheReadTokens, usage.CacheWriteTokens)
590	}
591	ratio, ok := usage.CacheHitRatio()
592	if !ok || math.Abs(ratio-0.9) > 1e-9 {
593		t.Fatalf("cache hit ratio = %v, %v, want 0.9, true", ratio, ok)
594	}
595}
596
597func TestAnthropicStreamKnownCacheMissReportsZeroRatio(t *testing.T) {
598	usage := anthropicStreamUsage(t, `{"message":{"usage":{"input_tokens":100,"output_tokens":0,"cache_read_input_tokens":0,"cache_creation_input_tokens":0}}}`)
599	if ratio, ok := usage.CacheHitRatio(); !ok || ratio != 0 {
600		t.Fatalf("cache hit ratio = %v, %v, want 0, true", ratio, ok)
601	}
602}
603
604func TestAnthropicStreamCacheFieldsInDeltaOnly(t *testing.T) {
605	start := `{"message":{"usage":{"input_tokens":100,"output_tokens":0}}}`
606	for _, tc := range []struct {
607		name  string
608		delta string
609		want  float64
610	}{
611		{"known miss", `{"delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":5,"cache_read_input_tokens":0,"cache_creation_input_tokens":0}}`, 0},
612		{"hit", `{"delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":5,"cache_read_input_tokens":300,"cache_creation_input_tokens":0}}`, 0.75},
613	} {
614		t.Run(tc.name, func(t *testing.T) {
615			usage := anthropicStreamUsageWithDelta(t, start, tc.delta)
616			if ratio, ok := usage.CacheHitRatio(); !ok || math.Abs(ratio-tc.want) > 1e-9 {
617				t.Fatalf("cache hit ratio = %v, %v, want %v, true", ratio, ok, tc.want)
618			}
619		})
620	}
621}
622
623func TestAnthropicStreamWithoutCacheFieldsLeavesRatioUnknown(t *testing.T) {
624	usage := anthropicStreamUsage(t, `{"message":{"usage":{"input_tokens":100,"output_tokens":0}}}`)
625	if ratio, ok := usage.CacheHitRatio(); ok {
626		t.Fatalf("cache hit ratio = %v, true, want unknown", ratio)
627	}
628}
629
630func TestClaudeOpus5Catalog(t *testing.T) {
631	m, err := FindModel("anthropic", "claude-opus-5")
632	if err != nil {
633		t.Fatal(err)
634	}
635	if m.DisplayName != "Claude Opus 5" || m.ContextWindow != 1000000 || m.MaxOutput != 128000 || !m.Reasoning || !m.AdaptiveThinking || m.Speculative {
636		t.Fatalf("unexpected Opus 5 model: %+v", m)
637	}
638	if m.PriceInput != 5 || m.PriceOutput != 25 || m.PriceCacheRead != 0.5 || m.PriceCacheWrite != 6.25 {
639		t.Fatalf("unexpected Opus 5 pricing: %+v", m)
640	}
641}
642
643func TestClaudeOpus55Catalog(t *testing.T) {
644	m, err := FindModel("anthropic", "claude-opus-5-5")
645	if err != nil {
646		t.Fatal(err)
647	}
648	if m.DisplayName != "Claude Opus 5.5" || m.ContextWindow != 500000 || m.MaxOutput != 128000 || !m.Reasoning || !m.AdaptiveThinking || m.Speculative {
649		t.Fatalf("unexpected Opus 5.5 model: %+v", m)
650	}
651	if m.PriceInput != 4 || m.PriceOutput != 20 || m.PriceCacheRead != 0.2 || m.PriceCacheWrite != 5 {
652		t.Fatalf("unexpected Opus 5.5 pricing: %+v", m)
653	}
654}
655
656func TestClaudeFable51Catalog(t *testing.T) {
657	m, err := FindModel("anthropic", "claude-fable-5-1")
658	if err != nil {
659		t.Fatal(err)
660	}
661	if m.DisplayName != "Claude Fable 5.1" || m.ContextWindow != 500000 || m.MaxOutput != 128000 || !m.Reasoning || !m.AdaptiveThinking || m.Speculative {
662		t.Fatalf("unexpected Fable 5.1 model: %+v", m)
663	}
664	if m.PriceInput != 10 || m.PriceOutput != 50 || m.PriceCacheRead != 0.25 || m.PriceCacheWrite != 12.5 {
665		t.Fatalf("unexpected Fable 5.1 pricing: %+v", m)
666	}
667}
668
669func TestClaudeSonnet55Catalog(t *testing.T) {
670	m, err := FindModel("anthropic", "claude-sonnet-5-5")
671	if err != nil {
672		t.Fatal(err)
673	}
674	if m.DisplayName != "Claude Sonnet 5.5" || m.ContextWindow != 1000000 || m.MaxOutput != 128000 || !m.Reasoning || !m.AdaptiveThinking || m.Speculative {
675		t.Fatalf("unexpected Sonnet 5.5 model: %+v", m)
676	}
677	if m.PriceInput != 2 || m.PriceOutput != 10 || m.PriceCacheRead != 0.2 || m.PriceCacheWrite != 2.5 {
678		t.Fatalf("unexpected Sonnet 5.5 pricing: %+v", m)
679	}
680}
681
682func TestClaudeSonnet5Catalog(t *testing.T) {
683	m, err := FindModel("anthropic", "claude-sonnet-5")
684	if err != nil {
685		t.Fatal(err)
686	}
687	if m.DisplayName != "Claude Sonnet 5" || m.ContextWindow != 1000000 || m.MaxOutput != 128000 || !m.Reasoning || !m.AdaptiveThinking {
688		t.Fatalf("unexpected Sonnet 5 model: %+v", m)
689	}
690	if m.PriceInput != 2 || m.PriceOutput != 10 || m.PriceCacheRead != 0.2 || m.PriceCacheWrite != 2.5 {
691		t.Fatalf("unexpected Sonnet 5 pricing: %+v", m)
692	}
693}
694
695func TestOpenAICompatAnthropicReasoningEffort(t *testing.T) {
696	c := NewOpenRouter("token", "").(*openaiClient)
697	wire, err := c.buildRequest(Request{
698		Model:     "anthropic/claude-opus-4.8",
699		Reasoning: "maximum",
700		Messages:  []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
701	})
702	if err != nil {
703		t.Fatal(err)
704	}
705	if wire.ReasoningEffort != "xhigh" {
706		t.Fatalf("want xhigh for adaptive Anthropic model over OpenAI-compatible wire, got %q", wire.ReasoningEffort)
707	}
708
709	wire, err = c.buildRequest(Request{
710		Model:     "anthropic/claude-sonnet-5",
711		Reasoning: "maximum",
712		Messages:  []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
713	})
714	if err != nil {
715		t.Fatal(err)
716	}
717	if wire.ReasoningEffort != "xhigh" {
718		t.Fatalf("want xhigh for Claude Sonnet 5 over OpenAI-compatible wire, got %q", wire.ReasoningEffort)
719	}
720
721	wire, err = c.buildRequest(Request{
722		Model:     "gpt-5.1",
723		Reasoning: "maximum",
724		Messages:  []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
725	})
726	if err != nil {
727		t.Fatal(err)
728	}
729	if wire.ReasoningEffort != "high" {
730		t.Fatalf("want generic OpenAI-compatible maximum clamped to high, got %q", wire.ReasoningEffort)
731	}
732}
733
734func TestOpenAIBuildRequestSkipsReasoningOnlyAssistantMessages(t *testing.T) {
735	c := NewKimi("token", "").(*openaiClient)
736	wire, err := c.buildRequest(Request{
737		Model: "kimi-for-coding",
738		Messages: []Message{
739			{Role: RoleUser, Content: []Content{TextBlock{Text: "first"}}},
740			{Role: RoleAssistant, Content: []Content{ReasoningBlock{Summary: "thinking only"}}},
741			{Role: RoleUser, Content: []Content{TextBlock{Text: "second"}}},
742		},
743	})
744	if err != nil {
745		t.Fatal(err)
746	}
747	for i, msg := range wire.Messages {
748		if msg.Role == "assistant" && msg.Content == nil && len(msg.ToolCalls) == 0 {
749			t.Fatalf("message %d is empty assistant: %+v", i, msg)
750		}
751	}
752	if got := len(wire.Messages); got != 2 {
753		t.Fatalf("messages=%d want 2 after skipping reasoning-only assistant", got)
754	}
755}
756
757func TestOpenAIStreamHappyPath(t *testing.T) {
758	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
759		w.Header().Set("content-type", "text/event-stream")
760		w.WriteHeader(http.StatusOK)
761		fl, _ := w.(http.Flusher)
762		write := func(s string) {
763			_, _ = w.Write([]byte(s))
764			if fl != nil {
765				fl.Flush()
766			}
767		}
768		write("data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"hel\"},\"finish_reason\":null}]}\n\n")
769		write("data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"lo\"},\"finish_reason\":\"stop\"}]}\n\n")
770		write("data: {\"choices\":[],\"usage\":{\"prompt_tokens\":8,\"completion_tokens\":2}}\n\n")
771		write("data: [DONE]\n\n")
772	}))
773	defer srv.Close()
774
775	c := NewOpenAI("x", srv.URL)
776	evs, err := c.Stream(context.Background(), Request{Model: "gpt-5"})
777	if err != nil {
778		t.Fatal(err)
779	}
780	var gotText string
781	var done EventDone
782	for ev := range evs {
783		switch e := ev.(type) {
784		case EventTextDelta:
785			gotText += e.Delta
786		case EventDone:
787			done = e
788		}
789	}
790	if gotText != "hello" {
791		t.Fatalf("text=%q", gotText)
792	}
793	if done.Stop != StopEnd {
794		t.Fatalf("stop=%v", done.Stop)
795	}
796}
797
798func TestDiscoverOpenRouter(t *testing.T) {
799	const body = `{"data":[
800		{"id":"x/full","pricing":{"prompt":"0.000003","completion":"0.000015"},
801		 "context_length":200000,"top_provider":{"max_completion_tokens":64000},
802		 "supported_parameters":["reasoning"]},
803		{"id":"x/fallback","top_provider":{"context_length":128000}},
804		{"id":""}]}`
805	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
806		_, _ = w.Write([]byte(body))
807	}))
808	defer srv.Close()
809
810	models, err := DiscoverOpenRouter(context.Background(), srv.URL)
811	if err != nil {
812		t.Fatal(err)
813	}
814	if len(models) != 2 { // empty id dropped
815		t.Fatalf("want 2 models, got %d", len(models))
816	}
817	// per-token USD -> per-1M; reasoning from supported_parameters.
818	if m := models[0]; m.Provider != "openrouter" || m.ContextWindow != 200000 ||
819		m.MaxOutput != 64000 || !m.Reasoning || m.PriceInput != 3 || m.PriceOutput != 15 {
820		t.Errorf("model[0]: %+v", m)
821	}
822	// context falls back to top_provider; no reasoning.
823	if m := models[1]; m.ContextWindow != 128000 || m.MaxOutput != 0 || m.Reasoning {
824		t.Errorf("model[1]: %+v", m)
825	}
826}
827
828func TestGPT56AndGPT6CatalogEntries(t *testing.T) {
829	cases := []struct {
830		provider   string
831		id         string
832		context    int
833		priceInput float64
834		priceOut   float64
835		cacheRead  float64
836		cacheWrite float64
837	}{
838		{"openai", "gpt-5.6-luna", 500000, 1, 6, 0.1, 1.25},
839		{"openai", "gpt-5.6-sol", 500000, 5, 30, 0.5, 6.25},
840		{"openai", "gpt-5.6-terra", 500000, 2.5, 15, 0.25, 3.125},
841		{"openai", "gpt-6-astra", 500000, 10, 50, 1, 12.5},
842		{"openai", "gpt-6.1-sol", 500000, 2, 10, 0.1, 2.5},
843		{"openai-codex", "gpt-5.6-luna", 500000, 1, 6, 0.1, 1.25},
844		{"openai-codex", "gpt-5.6-sol", 500000, 5, 30, 0.5, 6.25},
845		{"openai-codex", "gpt-5.6-terra", 500000, 2.5, 15, 0.25, 3.125},
846		{"openai-codex", "gpt-6-astra", 500000, 10, 50, 1, 12.5},
847		{"openai-codex", "gpt-6-sol", 500000, 2, 10, 0.2, 2.5},
848		{"openai-codex", "gpt-6.1-sol", 500000, 2, 10, 0.1, 2.5},
849		{"openai-codex", "gpt-6-luna", 500000, 0.1, 0.5, 0.01, 0.125},
850		{"azure-openai-responses", "gpt-5.6-luna", 1050000, 1, 6, 0.1, 1.25},
851		{"azure-openai-responses", "gpt-5.6-sol", 1050000, 5, 30, 0.5, 6.25},
852		{"azure-openai-responses", "gpt-5.6-terra", 1050000, 2.5, 15, 0.25, 3.125},
853		{"vercel-ai-gateway", "openai/gpt-5.6-luna", 1050000, 1, 6, 0.1, 1.25},
854		{"vercel-ai-gateway", "openai/gpt-5.6-sol", 1050000, 5, 30, 0.5, 6.25},
855		{"vercel-ai-gateway", "openai/gpt-5.6-terra", 1050000, 2.5, 15, 0.25, 3.125},
856	}
857	for _, tc := range cases {
858		m, err := FindModel(tc.provider, tc.id)
859		if err != nil {
860			t.Fatal(err)
861		}
862		if m.ContextWindow != tc.context || m.MaxOutput != 128000 || !m.Reasoning {
863			t.Fatalf("%s/%s limits: %+v", tc.provider, tc.id, m)
864		}
865		if m.PriceInput != tc.priceInput || m.PriceOutput != tc.priceOut || m.PriceCacheRead != tc.cacheRead || m.PriceCacheWrite != tc.cacheWrite {
866			t.Fatalf("%s/%s prices: %+v", tc.provider, tc.id, m)
867		}
868		if tc.provider == "openai-codex" && m.Speculative {
869			t.Fatalf("%s/%s is speculative", tc.provider, tc.id)
870		}
871	}
872}
873
874// TestOpenAIOmitsZeroMaxTokens guards against sending max_tokens: 0 for
875// discovered models that don't advertise an output cap (MaxOutput == 0).
876func TestOpenAIOmitsZeroMaxTokens(t *testing.T) {
877	SetLiveModels([]Model{
878		{Provider: "openrouter", ID: "vendor/no-cap", DisplayName: "No Cap"},
879		{Provider: "openrouter", ID: "vendor/reason-no-cap", DisplayName: "Reason No Cap", Reasoning: true},
880		{Provider: "openrouter", ID: "vendor/capped", DisplayName: "Capped", MaxOutput: 4096},
881	})
882	defer SetLiveModels(nil)
883
884	c := NewOpenAI("x", "").(*openaiClient)
885
886	got, err := c.buildRequest(Request{Model: "vendor/no-cap"})
887	if err != nil {
888		t.Fatal(err)
889	}
890	if got.MaxTokens != nil {
891		t.Errorf("expected max_tokens omitted, got %d", *got.MaxTokens)
892	}
893
894	got, err = c.buildRequest(Request{Model: "vendor/reason-no-cap"})
895	if err != nil {
896		t.Fatal(err)
897	}
898	if got.MaxCompletionTok != nil {
899		t.Errorf("expected max_completion_tokens omitted, got %d", *got.MaxCompletionTok)
900	}
901
902	got, err = c.buildRequest(Request{Model: "vendor/capped"})
903	if err != nil {
904		t.Fatal(err)
905	}
906	if got.MaxTokens == nil || *got.MaxTokens != 4096 {
907		t.Errorf("expected max_tokens 4096, got %v", got.MaxTokens)
908	}
909}

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