v0.3.7 ee6f014

provider_test.go RawHistoryFolder

1package provider
2
3import (
4	"context"
5	"net/http"
6	"net/http/httptest"
7	"strings"
8	"testing"
9)
10
11func TestSSEParse(t *testing.T) {
12	r := strings.NewReader("event: foo\ndata: {\"a\":1}\n\ndata: hello\ndata: world\n\n")
13	ch := make(chan sseEvent, 4)
14	go readSSE(r, ch)
15
16	e := <-ch
17	if e.Event != "foo" || e.Data != `{"a":1}` {
18		t.Fatalf("event 1: %+v", e)
19	}
20	e = <-ch
21	if e.Event != "" || e.Data != "hello\nworld" {
22		t.Fatalf("event 2: %+v", e)
23	}
24	if _, ok := <-ch; ok {
25		t.Fatalf("channel not closed")
26	}
27}
28
29func TestModelCatalog(t *testing.T) {
30	if len(Catalog) == 0 {
31		t.Fatal("empty catalog")
32	}
33	if _, err := FindModel("anthropic", "claude-sonnet-4-5"); err != nil {
34		t.Fatal(err)
35	}
36	if _, err := FindModel("openai", "gpt-5"); err != nil {
37		t.Fatal(err)
38	}
39	if _, err := FindModel("", "nope"); err == nil {
40		t.Fatal("expected error")
41	}
42}
43
44func TestVertexGeminiFlashCatalogLimits(t *testing.T) {
45	for _, id := range []string{"gemini-3.1-flash-lite", "gemini-3.5-flash"} {
46		m, err := FindModel("google-vertex", id)
47		if err != nil {
48			t.Fatal(err)
49		}
50		if m.ContextWindow != 1048576 || m.MaxOutput != 65535 || !m.Reasoning {
51			t.Errorf("%s metadata = %+v", id, m)
52		}
53	}
54}
55
56func TestComputeCost(t *testing.T) {
57	m, _ := FindModel("anthropic", "claude-sonnet-4-5")
58	cost := ComputeCost(m, Usage{InputTokens: 1_000_000, OutputTokens: 1_000_000})
59	want := m.PriceInput + m.PriceOutput
60	if cost != want {
61		t.Fatalf("cost=%v want=%v", cost, want)
62	}
63}
64
65func TestAnthropicErrorStatus(t *testing.T) {
66	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
67		w.WriteHeader(http.StatusUnauthorized)
68		_, _ = w.Write([]byte(`{"error":{"type":"auth","message":"bad key"}}`))
69	}))
70	defer srv.Close()
71
72	c := NewAnthropic("x", srv.URL)
73	_, err := c.Stream(context.Background(), Request{Model: "claude-sonnet-4-5"})
74	if err == nil || !strings.Contains(err.Error(), "401") {
75		t.Fatalf("want 401 err, got %v", err)
76	}
77}
78
79func TestOpenAIErrorStatus(t *testing.T) {
80	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
81		w.WriteHeader(http.StatusBadRequest)
82		_, _ = w.Write([]byte(`{"error":{"message":"nope"}}`))
83	}))
84	defer srv.Close()
85
86	c := NewOpenAI("x", srv.URL)
87	_, err := c.Stream(context.Background(), Request{Model: "gpt-5"})
88	if err == nil || !strings.Contains(err.Error(), "400") {
89		t.Fatalf("want 400 err, got %v", err)
90	}
91}
92
93func TestAnthropicAdaptiveThinking(t *testing.T) {
94	c := NewAnthropic("x", "").(*anthropicClient)
95	temp := float32(0.7)
96
97	// Opus 5 -> adaptive thinking, effort set, no budget, no temperature.
98	wire, err := c.buildRequest(Request{
99		Model:       "claude-opus-5",
100		Reasoning:   "high",
101		Temperature: &temp,
102		Messages:    []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
103	})
104	if err != nil {
105		t.Fatal(err)
106	}
107	if wire.Thinking == nil || wire.Thinking.Type != "adaptive" {
108		t.Fatalf("want adaptive thinking, got %+v", wire.Thinking)
109	}
110	if wire.Thinking.BudgetTokens != 0 {
111		t.Fatalf("adaptive must not send budget_tokens, got %d", wire.Thinking.BudgetTokens)
112	}
113	if wire.OutputConfig == nil || wire.OutputConfig.Effort != "high" {
114		t.Fatalf("want effort=high, got %+v", wire.OutputConfig)
115	}
116	if wire.Temperature != nil {
117		t.Fatalf("adaptive must drop temperature, got %v", *wire.Temperature)
118	}
119
120	// maximum -> xhigh effort.
121	wire, err = c.buildRequest(Request{
122		Model:     "claude-opus-5",
123		Reasoning: "maximum",
124		Messages:  []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
125	})
126	if err != nil {
127		t.Fatal(err)
128	}
129	if wire.OutputConfig == nil || wire.OutputConfig.Effort != "xhigh" {
130		t.Fatalf("want effort=xhigh, got %+v", wire.OutputConfig)
131	}
132
133	// max is a separate native tier above xhigh on adaptive models.
134	wire, err = c.buildRequest(Request{
135		Model:     "claude-opus-5",
136		Reasoning: "max",
137		Messages:  []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
138	})
139	if err != nil {
140		t.Fatal(err)
141	}
142	if wire.OutputConfig == nil || wire.OutputConfig.Effort != "max" {
143		t.Fatalf("want effort=max, got %+v", wire.OutputConfig)
144	}
145
146	// Sonnet 5 -> adaptive thinking, effort set, no budget, no temperature.
147	wire, err = c.buildRequest(Request{
148		Model:       "claude-sonnet-5",
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" {
157		t.Fatalf("want adaptive thinking for Sonnet 5, got %+v", wire.Thinking)
158	}
159	if wire.Thinking.BudgetTokens != 0 {
160		t.Fatalf("Sonnet 5 adaptive must not send budget_tokens, got %d", wire.Thinking.BudgetTokens)
161	}
162	if wire.OutputConfig == nil || wire.OutputConfig.Effort != "high" {
163		t.Fatalf("want Sonnet 5 effort=high, got %+v", wire.OutputConfig)
164	}
165	if wire.Temperature != nil {
166		t.Fatalf("Sonnet 5 adaptive must drop temperature, got %v", *wire.Temperature)
167	}
168
169	// Opus 4.5 -> budget-based thinking, no output_config, temperature kept.
170	wire, err = c.buildRequest(Request{
171		Model:       "claude-opus-4-5",
172		Reasoning:   "high",
173		Temperature: &temp,
174		Messages:    []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
175	})
176	if err != nil {
177		t.Fatal(err)
178	}
179	if wire.Thinking == nil || wire.Thinking.Type != "enabled" || wire.Thinking.BudgetTokens <= 0 {
180		t.Fatalf("want budget thinking, got %+v", wire.Thinking)
181	}
182	if wire.OutputConfig != nil {
183		t.Fatalf("budget models must not send output_config, got %+v", wire.OutputConfig)
184	}
185	if wire.Temperature == nil || *wire.Temperature != temp {
186		t.Fatalf("budget model should keep temperature, got %v", wire.Temperature)
187	}
188}
189
190func TestAnthropicBuildRequestStripsAssistantImages(t *testing.T) {
191	c := NewAnthropic("x", "").(*anthropicClient)
192	wire, err := c.buildRequest(Request{
193		Model: "claude-sonnet-4-5",
194		Messages: []Message{
195			{Role: RoleUser, Content: []Content{TextBlock{Text: "make an image"}}},
196			{Role: RoleAssistant, Content: []Content{
197				TextBlock{Text: "done"},
198				ImageBlock{MimeType: "image/png", Data: []byte("png")},
199				TextBlock{Text: "Saved image: `zot-gemini-image-x.png`"},
200			}},
201			{Role: RoleUser, Content: []Content{TextBlock{Text: "hello"}}},
202		},
203	})
204	if err != nil {
205		t.Fatal(err)
206	}
207	if len(wire.Messages) != 3 {
208		t.Fatalf("messages=%d", len(wire.Messages))
209	}
210	assistant := wire.Messages[1]
211	if assistant.Role != "assistant" {
212		t.Fatalf("role=%q", assistant.Role)
213	}
214	if len(assistant.Content) != 2 {
215		t.Fatalf("assistant content=%+v", assistant.Content)
216	}
217	for _, b := range assistant.Content {
218		if _, ok := b.(anthImageBlock); ok {
219			t.Fatalf("assistant image block was not stripped: %+v", assistant.Content)
220		}
221	}
222}
223
224func TestAnthropicStreamHappyPath(t *testing.T) {
225	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
226		w.Header().Set("content-type", "text/event-stream")
227		w.WriteHeader(http.StatusOK)
228		fl, _ := w.(http.Flusher)
229		write := func(s string) {
230			_, _ = w.Write([]byte(s))
231			if fl != nil {
232				fl.Flush()
233			}
234		}
235		write("event: message_start\ndata: {\"message\":{\"usage\":{\"input_tokens\":10,\"output_tokens\":0}}}\n\n")
236		write("event: content_block_start\ndata: {\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}\n\n")
237		write("event: content_block_delta\ndata: {\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"hi\"}}\n\n")
238		write("event: content_block_stop\ndata: {\"index\":0}\n\n")
239		write("event: message_delta\ndata: {\"delta\":{\"stop_reason\":\"end_turn\"},\"usage\":{\"output_tokens\":5}}\n\n")
240		write("event: message_stop\ndata: {}\n\n")
241	}))
242	defer srv.Close()
243
244	c := NewAnthropic("x", srv.URL)
245	evs, err := c.Stream(context.Background(), Request{Model: "claude-sonnet-4-5"})
246	if err != nil {
247		t.Fatal(err)
248	}
249	var gotText string
250	var done EventDone
251	for ev := range evs {
252		switch e := ev.(type) {
253		case EventTextDelta:
254			gotText += e.Delta
255		case EventDone:
256			done = e
257		}
258	}
259	if gotText != "hi" {
260		t.Fatalf("text=%q", gotText)
261	}
262	if done.Stop != StopEnd {
263		t.Fatalf("stop=%v", done.Stop)
264	}
265}
266
267func TestClaudeOpus5Catalog(t *testing.T) {
268	m, err := FindModel("anthropic", "claude-opus-5")
269	if err != nil {
270		t.Fatal(err)
271	}
272	if m.DisplayName != "Claude Opus 5" || m.ContextWindow != 1000000 || m.MaxOutput != 128000 || !m.Reasoning || !m.AdaptiveThinking || m.Speculative {
273		t.Fatalf("unexpected Opus 5 model: %+v", m)
274	}
275	if m.PriceInput != 5 || m.PriceOutput != 25 || m.PriceCacheRead != 0.5 || m.PriceCacheWrite != 6.25 {
276		t.Fatalf("unexpected Opus 5 pricing: %+v", m)
277	}
278}
279
280func TestClaudeSonnet5Catalog(t *testing.T) {
281	m, err := FindModel("anthropic", "claude-sonnet-5")
282	if err != nil {
283		t.Fatal(err)
284	}
285	if m.DisplayName != "Claude Sonnet 5" || m.ContextWindow != 1000000 || m.MaxOutput != 128000 || !m.Reasoning || !m.AdaptiveThinking {
286		t.Fatalf("unexpected Sonnet 5 model: %+v", m)
287	}
288	if m.PriceInput != 2 || m.PriceOutput != 10 || m.PriceCacheRead != 0.2 || m.PriceCacheWrite != 2.5 {
289		t.Fatalf("unexpected Sonnet 5 pricing: %+v", m)
290	}
291}
292
293func TestOpenAICompatAnthropicReasoningEffort(t *testing.T) {
294	c := NewOpenRouter("token", "").(*openaiClient)
295	wire, err := c.buildRequest(Request{
296		Model:     "anthropic/claude-opus-4.8",
297		Reasoning: "maximum",
298		Messages:  []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
299	})
300	if err != nil {
301		t.Fatal(err)
302	}
303	if wire.ReasoningEffort != "xhigh" {
304		t.Fatalf("want xhigh for adaptive Anthropic model over OpenAI-compatible wire, got %q", wire.ReasoningEffort)
305	}
306
307	wire, err = c.buildRequest(Request{
308		Model:     "anthropic/claude-sonnet-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.ReasoningEffort != "xhigh" {
316		t.Fatalf("want xhigh for Claude Sonnet 5 over OpenAI-compatible wire, got %q", wire.ReasoningEffort)
317	}
318
319	wire, err = c.buildRequest(Request{
320		Model:     "gpt-5.1",
321		Reasoning: "maximum",
322		Messages:  []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
323	})
324	if err != nil {
325		t.Fatal(err)
326	}
327	if wire.ReasoningEffort != "high" {
328		t.Fatalf("want generic OpenAI-compatible maximum clamped to high, got %q", wire.ReasoningEffort)
329	}
330}
331
332func TestOpenAIBuildRequestSkipsReasoningOnlyAssistantMessages(t *testing.T) {
333	c := NewKimi("token", "").(*openaiClient)
334	wire, err := c.buildRequest(Request{
335		Model: "kimi-for-coding",
336		Messages: []Message{
337			{Role: RoleUser, Content: []Content{TextBlock{Text: "first"}}},
338			{Role: RoleAssistant, Content: []Content{ReasoningBlock{Summary: "thinking only"}}},
339			{Role: RoleUser, Content: []Content{TextBlock{Text: "second"}}},
340		},
341	})
342	if err != nil {
343		t.Fatal(err)
344	}
345	for i, msg := range wire.Messages {
346		if msg.Role == "assistant" && msg.Content == nil && len(msg.ToolCalls) == 0 {
347			t.Fatalf("message %d is empty assistant: %+v", i, msg)
348		}
349	}
350	if got := len(wire.Messages); got != 2 {
351		t.Fatalf("messages=%d want 2 after skipping reasoning-only assistant", got)
352	}
353}
354
355func TestOpenAIStreamHappyPath(t *testing.T) {
356	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
357		w.Header().Set("content-type", "text/event-stream")
358		w.WriteHeader(http.StatusOK)
359		fl, _ := w.(http.Flusher)
360		write := func(s string) {
361			_, _ = w.Write([]byte(s))
362			if fl != nil {
363				fl.Flush()
364			}
365		}
366		write("data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"hel\"},\"finish_reason\":null}]}\n\n")
367		write("data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"lo\"},\"finish_reason\":\"stop\"}]}\n\n")
368		write("data: {\"choices\":[],\"usage\":{\"prompt_tokens\":8,\"completion_tokens\":2}}\n\n")
369		write("data: [DONE]\n\n")
370	}))
371	defer srv.Close()
372
373	c := NewOpenAI("x", srv.URL)
374	evs, err := c.Stream(context.Background(), Request{Model: "gpt-5"})
375	if err != nil {
376		t.Fatal(err)
377	}
378	var gotText string
379	var done EventDone
380	for ev := range evs {
381		switch e := ev.(type) {
382		case EventTextDelta:
383			gotText += e.Delta
384		case EventDone:
385			done = e
386		}
387	}
388	if gotText != "hello" {
389		t.Fatalf("text=%q", gotText)
390	}
391	if done.Stop != StopEnd {
392		t.Fatalf("stop=%v", done.Stop)
393	}
394}
395
396func TestDiscoverOpenRouter(t *testing.T) {
397	const body = `{"data":[
398		{"id":"x/full","pricing":{"prompt":"0.000003","completion":"0.000015"},
399		 "context_length":200000,"top_provider":{"max_completion_tokens":64000},
400		 "supported_parameters":["reasoning"]},
401		{"id":"x/fallback","top_provider":{"context_length":128000}},
402		{"id":""}]}`
403	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
404		_, _ = w.Write([]byte(body))
405	}))
406	defer srv.Close()
407
408	models, err := DiscoverOpenRouter(context.Background(), srv.URL)
409	if err != nil {
410		t.Fatal(err)
411	}
412	if len(models) != 2 { // empty id dropped
413		t.Fatalf("want 2 models, got %d", len(models))
414	}
415	// per-token USD -> per-1M; reasoning from supported_parameters.
416	if m := models[0]; m.Provider != "openrouter" || m.ContextWindow != 200000 ||
417		m.MaxOutput != 64000 || !m.Reasoning || m.PriceInput != 3 || m.PriceOutput != 15 {
418		t.Errorf("model[0]: %+v", m)
419	}
420	// context falls back to top_provider; no reasoning.
421	if m := models[1]; m.ContextWindow != 128000 || m.MaxOutput != 0 || m.Reasoning {
422		t.Errorf("model[1]: %+v", m)
423	}
424}
425
426func TestGPT56CatalogEntries(t *testing.T) {
427	cases := []struct {
428		provider   string
429		id         string
430		context    int
431		priceInput float64
432		priceOut   float64
433		cacheRead  float64
434		cacheWrite float64
435	}{
436		{"openai", "gpt-5.6-luna", 272000, 1, 6, 0.1, 1.25},
437		{"openai", "gpt-5.6-sol", 272000, 5, 30, 0.5, 6.25},
438		{"openai", "gpt-5.6-terra", 272000, 2.5, 15, 0.25, 3.125},
439		{"openai-codex", "gpt-5.6-luna", 372000, 1, 6, 0.1, 1.25},
440		{"openai-codex", "gpt-5.6-sol", 372000, 5, 30, 0.5, 6.25},
441		{"openai-codex", "gpt-5.6-terra", 372000, 2.5, 15, 0.25, 3.125},
442		{"azure-openai-responses", "gpt-5.6-luna", 1050000, 1, 6, 0.1, 1.25},
443		{"azure-openai-responses", "gpt-5.6-sol", 1050000, 5, 30, 0.5, 6.25},
444		{"azure-openai-responses", "gpt-5.6-terra", 1050000, 2.5, 15, 0.25, 3.125},
445		{"vercel-ai-gateway", "openai/gpt-5.6-luna", 1050000, 1, 6, 0.1, 1.25},
446		{"vercel-ai-gateway", "openai/gpt-5.6-sol", 1050000, 5, 30, 0.5, 6.25},
447		{"vercel-ai-gateway", "openai/gpt-5.6-terra", 1050000, 2.5, 15, 0.25, 3.125},
448	}
449	for _, tc := range cases {
450		m, err := FindModel(tc.provider, tc.id)
451		if err != nil {
452			t.Fatal(err)
453		}
454		if m.ContextWindow != tc.context || m.MaxOutput != 128000 || !m.Reasoning {
455			t.Fatalf("%s/%s limits: %+v", tc.provider, tc.id, m)
456		}
457		if m.PriceInput != tc.priceInput || m.PriceOutput != tc.priceOut || m.PriceCacheRead != tc.cacheRead || m.PriceCacheWrite != tc.cacheWrite {
458			t.Fatalf("%s/%s prices: %+v", tc.provider, tc.id, m)
459		}
460	}
461}
462
463// TestOpenCodeGoCatalog pins the Grok 4.5 and Kimi K3 entries added to
464// the OpenCode Go provider. Both are listed in the vendor's current Go
465// model lineup (https://opencode.ai/docs/go) and were missing from the
466// baked-in catalog.
467func TestOpenCodeGoCatalog(t *testing.T) {
468	cases := []struct {
469		id        string
470		context   int
471		maxOut    int
472		priceIn   float64
473		priceOut  float64
474		cacheRead float64
475	}{
476		{"grok-4.5", 500000, 500000, 2, 6, 0.3},
477		{"kimi-k3", 1048576, 131072, 3, 15, 0.3},
478	}
479	for _, tc := range cases {
480		m, err := FindModel("opencode-go", tc.id)
481		if err != nil {
482			t.Fatal(err)
483		}
484		if m.ContextWindow != tc.context || m.MaxOutput != tc.maxOut || !m.Reasoning {
485			t.Fatalf("opencode-go/%s limits: %+v", tc.id, m)
486		}
487		if m.PriceInput != tc.priceIn || m.PriceOutput != tc.priceOut || m.PriceCacheRead != tc.cacheRead {
488			t.Fatalf("opencode-go/%s prices: %+v", tc.id, m)
489		}
490		if m.BaseURL != "https://opencode.ai/zen/go/v1" {
491			t.Fatalf("opencode-go/%s baseURL: %s", tc.id, m.BaseURL)
492		}
493	}
494}
495
496// TestOpenAIOmitsZeroMaxTokens guards against sending max_tokens: 0 for
497// discovered models that don't advertise an output cap (MaxOutput == 0).
498func TestOpenAIOmitsZeroMaxTokens(t *testing.T) {
499	SetLiveModels([]Model{
500		{Provider: "openrouter", ID: "vendor/no-cap", DisplayName: "No Cap"},
501		{Provider: "openrouter", ID: "vendor/reason-no-cap", DisplayName: "Reason No Cap", Reasoning: true},
502		{Provider: "openrouter", ID: "vendor/capped", DisplayName: "Capped", MaxOutput: 4096},
503	})
504	defer SetLiveModels(nil)
505
506	c := NewOpenAI("x", "").(*openaiClient)
507
508	got, err := c.buildRequest(Request{Model: "vendor/no-cap"})
509	if err != nil {
510		t.Fatal(err)
511	}
512	if got.MaxTokens != nil {
513		t.Errorf("expected max_tokens omitted, got %d", *got.MaxTokens)
514	}
515
516	got, err = c.buildRequest(Request{Model: "vendor/reason-no-cap"})
517	if err != nil {
518		t.Fatal(err)
519	}
520	if got.MaxCompletionTok != nil {
521		t.Errorf("expected max_completion_tokens omitted, got %d", *got.MaxCompletionTok)
522	}
523
524	got, err = c.buildRequest(Request{Model: "vendor/capped"})
525	if err != nil {
526		t.Fatal(err)
527	}
528	if got.MaxTokens == nil || *got.MaxTokens != 4096 {
529		t.Errorf("expected max_tokens 4096, got %v", got.MaxTokens)
530	}
531}

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