v0.1.68 9b4f4da

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 TestAnthropicStreamHappyPath(t *testing.T) {
82	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
83		w.Header().Set("content-type", "text/event-stream")
84		w.WriteHeader(http.StatusOK)
85		fl, _ := w.(http.Flusher)
86		write := func(s string) {
87			_, _ = w.Write([]byte(s))
88			if fl != nil {
89				fl.Flush()
90			}
91		}
92		write("event: message_start\ndata: {\"message\":{\"usage\":{\"input_tokens\":10,\"output_tokens\":0}}}\n\n")
93		write("event: content_block_start\ndata: {\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}\n\n")
94		write("event: content_block_delta\ndata: {\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"hi\"}}\n\n")
95		write("event: content_block_stop\ndata: {\"index\":0}\n\n")
96		write("event: message_delta\ndata: {\"delta\":{\"stop_reason\":\"end_turn\"},\"usage\":{\"output_tokens\":5}}\n\n")
97		write("event: message_stop\ndata: {}\n\n")
98	}))
99	defer srv.Close()
100
101	c := NewAnthropic("x", srv.URL)
102	evs, err := c.Stream(context.Background(), Request{Model: "claude-sonnet-4-5"})
103	if err != nil {
104		t.Fatal(err)
105	}
106	var gotText string
107	var done EventDone
108	for ev := range evs {
109		switch e := ev.(type) {
110		case EventTextDelta:
111			gotText += e.Delta
112		case EventDone:
113			done = e
114		}
115	}
116	if gotText != "hi" {
117		t.Fatalf("text=%q", gotText)
118	}
119	if done.Stop != StopEnd {
120		t.Fatalf("stop=%v", done.Stop)
121	}
122}
123
124func TestOpenAIBuildRequestSkipsReasoningOnlyAssistantMessages(t *testing.T) {
125	c := NewKimi("token", "").(*openaiClient)
126	wire, err := c.buildRequest(Request{
127		Model: "kimi-for-coding",
128		Messages: []Message{
129			{Role: RoleUser, Content: []Content{TextBlock{Text: "first"}}},
130			{Role: RoleAssistant, Content: []Content{ReasoningBlock{Summary: "thinking only"}}},
131			{Role: RoleUser, Content: []Content{TextBlock{Text: "second"}}},
132		},
133	})
134	if err != nil {
135		t.Fatal(err)
136	}
137	for i, msg := range wire.Messages {
138		if msg.Role == "assistant" && msg.Content == nil && len(msg.ToolCalls) == 0 {
139			t.Fatalf("message %d is empty assistant: %+v", i, msg)
140		}
141	}
142	if got := len(wire.Messages); got != 2 {
143		t.Fatalf("messages=%d want 2 after skipping reasoning-only assistant", got)
144	}
145}
146
147func TestOpenAIStreamHappyPath(t *testing.T) {
148	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
149		w.Header().Set("content-type", "text/event-stream")
150		w.WriteHeader(http.StatusOK)
151		fl, _ := w.(http.Flusher)
152		write := func(s string) {
153			_, _ = w.Write([]byte(s))
154			if fl != nil {
155				fl.Flush()
156			}
157		}
158		write("data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"hel\"},\"finish_reason\":null}]}\n\n")
159		write("data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"lo\"},\"finish_reason\":\"stop\"}]}\n\n")
160		write("data: {\"choices\":[],\"usage\":{\"prompt_tokens\":8,\"completion_tokens\":2}}\n\n")
161		write("data: [DONE]\n\n")
162	}))
163	defer srv.Close()
164
165	c := NewOpenAI("x", srv.URL)
166	evs, err := c.Stream(context.Background(), Request{Model: "gpt-5"})
167	if err != nil {
168		t.Fatal(err)
169	}
170	var gotText string
171	var done EventDone
172	for ev := range evs {
173		switch e := ev.(type) {
174		case EventTextDelta:
175			gotText += e.Delta
176		case EventDone:
177			done = e
178		}
179	}
180	if gotText != "hello" {
181		t.Fatalf("text=%q", gotText)
182	}
183	if done.Stop != StopEnd {
184		t.Fatalf("stop=%v", done.Stop)
185	}
186}

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