-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathclient_test.go
77 lines (65 loc) · 2.09 KB
/
client_test.go
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
package deepseek_test
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/cohesion-org/deepseek-go"
)
func TestCreateChatCompletion_NilRequest(t *testing.T) {
client, err := deepseek.NewClientWithOptions("token")
if err != nil {
t.Fatal(err)
}
_, err = client.CreateChatCompletion(context.Background(), nil)
if err == nil {
t.Fatal("expected error for nil request")
}
if err.Error() != "request cannot be nil" {
t.Errorf("expected error 'request cannot be nil', got %q", err.Error())
}
}
func TestCreateChatCompletion_ErrorHandling(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{"error": {"message": "invalid request"}}`))
}))
defer testServer.Close()
client, err := deepseek.NewClientWithOptions("token", deepseek.WithBaseURL(testServer.URL+"/"))
if err != nil {
t.Fatal(err)
}
_, err = client.CreateChatCompletion(context.Background(), &deepseek.ChatCompletionRequest{})
if err == nil {
t.Fatal("expected error, got nil")
}
apiErr, ok := err.(*deepseek.APIError)
if !ok {
t.Fatalf("expected APIError, got %T", err)
}
if apiErr.Message != "Bad request" {
t.Errorf("expected error message 'Bad request', got %s", apiErr.Message)
}
}
func TestCreateChatCompletionStream_ErrorHandling(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{"error": {"message": "stream error"}}`))
}))
defer testServer.Close()
client, err := deepseek.NewClientWithOptions("token", deepseek.WithBaseURL(testServer.URL+"/"))
if err != nil {
t.Fatal(err)
}
_, err = client.CreateChatCompletionStream(context.Background(), &deepseek.StreamChatCompletionRequest{})
if err == nil {
t.Fatal("expected error, got nil")
}
apiErr, ok := err.(*deepseek.APIError)
if !ok {
t.Fatalf("expected APIError, got %T", err)
}
if apiErr.Message != "Bad request" {
t.Errorf("expected error message 'Bad request', got %s", apiErr.Message)
}
}