-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenrouter_client_test.go
More file actions
108 lines (97 loc) · 3.78 KB
/
Copy pathopenrouter_client_test.go
File metadata and controls
108 lines (97 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"context"
"io"
"net/http"
"strings"
"testing"
)
func TestOpenRouterClientListModels(t *testing.T) {
client := newOpenRouterClient(&http.Client{
Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
if req.URL.Path != "/api/v1/models" {
t.Fatalf("path = %q, want /api/v1/models", req.URL.Path)
}
if got := req.Header.Get("Authorization"); got != "Bearer sk-or-test" {
t.Fatalf("Authorization header = %q, want Bearer sk-or-test", got)
}
body := `{"data":[{"id":"anthropic/claude-3.5-sonnet","name":"Claude 3.5 Sonnet","context_length":200000}]}`
return &http.Response{
StatusCode: http.StatusOK,
Status: "200 OK",
Body: io.NopCloser(strings.NewReader(body)),
Header: http.Header{},
}, nil
}),
}, "sk-or-test")
models, err := client.ListModels(context.Background())
if err != nil {
t.Fatalf("ListModels returned error: %v", err)
}
if len(models) != 1 || models[0].ID != "anthropic/claude-3.5-sonnet" || models[0].ContextLen != 200000 {
t.Fatalf("models = %+v, want one anthropic/claude-3.5-sonnet entry with context 200000", models)
}
}
func TestOpenRouterClientCompleteChat(t *testing.T) {
client := newOpenRouterClient(&http.Client{
Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
body := `{"model":"anthropic/claude-3.5-sonnet","choices":[{"message":{"content":"Hello"},"finish_reason":"stop"}],"usage":{"completion_tokens":4}}`
return &http.Response{
StatusCode: http.StatusOK,
Status: "200 OK",
Body: io.NopCloser(strings.NewReader(body)),
Header: http.Header{},
}, nil
}),
}, "sk-or-test")
result, err := client.CompleteChat(context.Background(), ChatRequest{Model: "anthropic/claude-3.5-sonnet"})
if err != nil {
t.Fatalf("CompleteChat returned error: %v", err)
}
if result.Content != "Hello" || result.Reason != "stop" || result.EvalTokens != 4 {
t.Fatalf("result = %+v, want Content=Hello Reason=stop EvalTokens=4", result)
}
}
func TestOpenRouterClientMissingAPIKey(t *testing.T) {
client := newOpenRouterClient(&http.Client{
Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
t.Fatal("request should not be sent without an API key")
return nil, nil
}),
}, "")
if _, err := client.CompleteChat(context.Background(), ChatRequest{Model: "anthropic/claude-3.5-sonnet"}); err == nil {
t.Fatal("expected an error when the API key is empty")
}
}
func TestOpenRouterClientMapsUnauthorized(t *testing.T) {
client := newOpenRouterClient(&http.Client{
Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusUnauthorized,
Status: "401 Unauthorized",
Body: io.NopCloser(strings.NewReader(`{"error":{"message":"invalid key"}}`)),
Header: http.Header{},
}, nil
}),
}, "sk-or-bad")
_, err := client.CompleteChat(context.Background(), ChatRequest{Model: "anthropic/claude-3.5-sonnet"})
if err == nil || !strings.Contains(err.Error(), "authentication failed") {
t.Fatalf("err = %v, want an authentication-failed error", err)
}
}
func TestOpenRouterClientMapsRateLimit(t *testing.T) {
client := newOpenRouterClient(&http.Client{
Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusTooManyRequests,
Status: "429 Too Many Requests",
Body: io.NopCloser(strings.NewReader(`{"error":{"message":"rate limit exceeded"}}`)),
Header: http.Header{},
}, nil
}),
}, "sk-or-test")
_, err := client.CompleteChat(context.Background(), ChatRequest{Model: "anthropic/claude-3.5-sonnet"})
if err == nil || !strings.Contains(err.Error(), "rate limited") {
t.Fatalf("err = %v, want a rate-limited error", err)
}
}