-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcomplete_test.go
172 lines (123 loc) · 4.28 KB
/
complete_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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package anthrogo
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net/http"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/dleviminzi/anthrogo/mocks"
)
func TestComplete(t *testing.T) {
ctx := context.Background()
mockRes := &CompletionResponse{
Completion: "test",
StopReason: "test",
Model: "test",
}
resBodyBytes, _ := json.Marshal(mockRes)
resBodyReader := io.NopCloser(bytes.NewReader(resBodyBytes))
mockHTTPClient := new(mocks.MockHttpClient)
mockHTTPClient.On("Do", mock.Anything).Return(&http.Response{
StatusCode: 200,
Body: resBodyReader,
}, nil)
client, err := NewClient(WithApiKey("blah"))
require.NoError(t, err)
client.httpClient = mockHTTPClient
payload := CompletionPayload{
MaxTokensToSample: 10,
Model: "test",
Prompt: "test",
}
response, err := client.CompletionRequest(ctx, payload)
assert.NoError(t, err)
assert.NotNil(t, response)
assert.Equal(t, mockRes.Completion, response.Completion)
assert.Equal(t, mockRes.StopReason, response.StopReason)
assert.Equal(t, mockRes.Model, response.Model)
mockHTTPClient.AssertExpectations(t)
}
func TestComplete_HttpClientError(t *testing.T) {
ctx := context.Background()
mockHTTPClient := new(mocks.MockHttpClient)
mockHTTPClient.On("Do", mock.Anything).Return(&http.Response{}, errors.New("some http error"))
client, err := NewClient(WithApiKey("blah"))
require.NoError(t, err)
client.httpClient = mockHTTPClient
payload := CompletionPayload{}
response, err := client.CompletionRequest(ctx, payload)
assert.Empty(t, response)
assert.Error(t, err)
mockHTTPClient.AssertExpectations(t)
}
func TestComplete_Non200StatusCode(t *testing.T) {
ctx := context.Background()
mockHTTPClient := new(mocks.MockHttpClient)
mockHTTPClient.On("Do", mock.Anything).Return(&http.Response{
StatusCode: 400,
Body: io.NopCloser(bytes.NewReader([]byte(`{"error": {"type": "BadRequest", "message": "Bad request"}}`))),
}, nil)
client, err := NewClient(WithApiKey("blah"))
require.NoError(t, err)
client.httpClient = mockHTTPClient
payload := CompletionPayload{}
_, err = client.CompletionRequest(ctx, payload)
assert.NotNil(t, err)
mockHTTPClient.AssertExpectations(t)
}
func TestComplete_UnmarshalError(t *testing.T) {
ctx := context.Background()
mockHTTPClient := new(mocks.MockHttpClient)
mockHTTPClient.On("Do", mock.Anything).Return(&http.Response{
StatusCode: 200,
Body: io.NopCloser(bytes.NewReader([]byte(`{"completion": "test", "invalid": {}`))),
}, nil)
client, err := NewClient(WithApiKey("blah"))
require.NoError(t, err)
client.httpClient = mockHTTPClient
payload := CompletionPayload{}
response, err := client.CompletionRequest(ctx, payload)
assert.Empty(t, response)
assert.Error(t, err)
mockHTTPClient.AssertExpectations(t)
}
func TestCompleteStream(t *testing.T) {
ctx := context.Background()
expectedBody := "data: {\"completion\":\"testCompletion\",\"stop_reason\":\"testReason\",\"model\":\"testModel\",\"stop\":\"testStop\",\"log_id\":\"testLogId\"}\n\r\n"
mockResponse := &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(expectedBody)),
}
mockHTTPClient := new(mocks.MockHttpClient)
mockHTTPClient.On("Do", mock.Anything).Return(mockResponse, nil)
client, err := NewClient(WithApiKey("blah"))
require.NoError(t, err)
client.httpClient = mockHTTPClient
payload := CompletionPayload{}
response, err := client.StreamingCompletionRequest(ctx, payload)
defer response.Close()
assert.NoError(t, err)
_, _ = response.Decode()
event, err := response.Decode()
assert.NoError(t, err)
assert.Equal(t, "testCompletion", event.Data.Completion)
mockHTTPClient.AssertExpectations(t)
}
func TestCompleteStream_HttpClientError(t *testing.T) {
mockHTTPClient := new(mocks.MockHttpClient)
mockHTTPClient.On("Do", mock.Anything).Return(&http.Response{}, errors.New("some http error"))
client, err := NewClient(WithApiKey("blah"))
require.NoError(t, err)
client.httpClient = mockHTTPClient
payload := CompletionPayload{}
response, err := client.StreamingCompletionRequest(context.Background(), payload)
assert.Nil(t, response)
assert.Error(t, err)
mockHTTPClient.AssertExpectations(t)
}