ScuttleBot

scuttlebot / internal / llm / openai_test.go
Blame History Raw 68 lines
1
package llm
2
3
import (
4
"context"
5
"encoding/json"
6
"net/http"
7
"net/http/httptest"
8
"testing"
9
)
10
11
func TestOpenAISummarizeRetriesWithMaxCompletionTokens(t *testing.T) {
12
t.Helper()
13
14
requests := 0
15
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
16
requests++
17
18
var body map[string]any
19
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
20
t.Fatalf("decode request: %v", err)
21
}
22
23
switch requests {
24
case 1:
25
if _, ok := body["max_tokens"]; !ok {
26
t.Fatalf("first request missing max_tokens: %#v", body)
27
}
28
w.WriteHeader(http.StatusBadRequest)
29
_, _ = w.Write([]byte(`{"error":{"message":"Unsupported parameter: 'max_tokens' is not supported with this model. Use 'max_completion_tokens' instead.","param":"max_tokens"}}`))
30
case 2:
31
if _, ok := body["max_completion_tokens"]; !ok {
32
t.Fatalf("second request missing max_completion_tokens: %#v", body)
33
}
34
if _, ok := body["max_tokens"]; ok {
35
t.Fatalf("second request still included max_tokens: %#v", body)
36
}
37
_, _ = w.Write([]byte(`{"choices":[{"message":{"content":"relay smoke test succeeded"}}]}`))
38
default:
39
t.Fatalf("unexpected extra request %d", requests)
40
}
41
}))
42
defer srv.Close()
43
44
p := newOpenAIProvider("test-key", srv.URL, "gpt-5.4-mini", srv.Client())
45
got, err := p.Summarize(context.Background(), "test prompt")
46
if err != nil {
47
t.Fatalf("Summarize returned error: %v", err)
48
}
49
if got != "relay smoke test succeeded" {
50
t.Fatalf("Summarize = %q, want %q", got, "relay smoke test succeeded")
51
}
52
if requests != 2 {
53
t.Fatalf("request count = %d, want 2", requests)
54
}
55
}
56
57
func TestShouldRetryWithMaxCompletionTokens(t *testing.T) {
58
t.Helper()
59
60
body := []byte(`{"error":{"message":"Unsupported parameter: 'max_tokens' is not supported with this model. Use 'max_completion_tokens' instead.","param":"max_tokens"}}`)
61
if !shouldRetryWithMaxCompletionTokens(http.StatusBadRequest, body) {
62
t.Fatalf("expected retry to be enabled")
63
}
64
if shouldRetryWithMaxCompletionTokens(http.StatusUnauthorized, body) {
65
t.Fatalf("unexpected retry on unauthorized response")
66
}
67
}
68

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button