-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathresponseHandler_test.go
185 lines (169 loc) · 4.31 KB
/
responseHandler_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
173
174
175
176
177
178
179
180
181
182
183
184
185
package deepseek_test
import (
"bytes"
"errors"
"io"
"net/http"
"testing"
"github.com/cohesion-org/deepseek-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestHandleChatCompletionResponse(t *testing.T) {
validResponse := deepseek.ChatCompletionResponse{
ID: "chat-123",
Object: "chat.completion",
Created: 1677858242,
Model: "deepseek-chat",
Choices: []deepseek.Choice{
{
Index: 0,
Message: deepseek.Message{
Role: "assistant",
Content: "Hello! How can I help you today?",
},
FinishReason: "stop",
},
},
Usage: deepseek.Usage{
PromptTokens: 10,
CompletionTokens: 20,
TotalTokens: 30,
},
}
tests := []struct {
name string
response *http.Response
want *deepseek.ChatCompletionResponse
wantErr string
errContains string
}{
{
name: "valid response",
response: &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(`{
"id": "chat-123",
"object": "chat.completion",
"created": 1677858242,
"model": "deepseek-chat",
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! How can I help you today?"
},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 20,
"total_tokens": 30
}
}`))),
},
want: &validResponse,
},
{
name: "invalid JSON",
response: &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(`{"invalid": `))),
},
wantErr: "failed to parse response JSON",
errContains: "unexpected end of JSON input",
},
{
name: "empty body",
response: &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte{})),
},
wantErr: "failed to parse response JSON",
errContains: "empty response body",
},
{
name: "read error",
response: &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(&errorReader{
err: errors.New("read error"),
}),
},
wantErr: "failed to read response body",
errContains: "read error",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer tt.response.Body.Close()
resp, err := deepseek.HandleChatCompletionResponse(tt.response)
if tt.wantErr != "" {
require.Error(t, err)
assert.Contains(t, err.Error(), tt.wantErr)
if tt.errContains != "" {
assert.Contains(t, err.Error(), tt.errContains)
}
return
}
require.NoError(t, err)
assert.Equal(t, tt.want, resp)
})
}
}
type errorReader struct {
err error
}
func (r *errorReader) Read(p []byte) (int, error) {
return 0, r.err
}
func TestResponseStructureValidation(t *testing.T) {
t.Run("missing required fields", func(t *testing.T) {
resp := &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(`{
"created": 1677858242,
"choices": [{}]
}`))),
}
defer resp.Body.Close()
_, err := deepseek.HandleChatCompletionResponse(resp)
require.Error(t, err)
assert.Contains(t, err.Error(), "invalid response")
})
t.Run("unexpected field types", func(t *testing.T) {
resp := &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(`{
"id": 123,
"object": "chat.completion",
"created": "invalid",
"model": "deepseek-chat",
"choices": [{"index": "zero"}],
"usage": {"prompt_tokens": "ten"}
}`))),
}
defer resp.Body.Close()
_, err := deepseek.HandleChatCompletionResponse(resp)
require.Error(t, err)
assert.Contains(t, err.Error(), "failed to parse response JSON")
})
}
func TestErrorWrapping(t *testing.T) {
t.Run("read error wrapping", func(t *testing.T) {
resp := &http.Response{
Body: io.NopCloser(&errorReader{err: errors.New("custom error")}),
}
_, err := deepseek.HandleChatCompletionResponse(resp)
require.Error(t, err)
assert.ErrorContains(t, err, "failed to read response body")
})
t.Run("json error wrapping", func(t *testing.T) {
resp := &http.Response{
Body: io.NopCloser(bytes.NewReader([]byte(`{"invalid": `))),
}
_, err := deepseek.HandleChatCompletionResponse(resp)
require.Error(t, err)
assert.ErrorContains(t, err, "failed to parse response JSON")
})
}