v0.2.82 444a7ea

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

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