|
1 | 1 | package chatgpt |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
4 | 7 | "testing" |
5 | 8 |
|
6 | | - "github.com/stretchr/testify/assert" |
7 | 9 | chatgpt_errors "github.com/ayush6624/go-chatgpt/utils" |
| 10 | + "github.com/stretchr/testify/assert" |
8 | 11 | ) |
9 | 12 |
|
10 | 13 | func TestValidate(t *testing.T) { |
@@ -56,9 +59,9 @@ func TestValidate(t *testing.T) { |
56 | 59 | { |
57 | 60 | name: "Invalid presence penalty", |
58 | 61 | request: &ChatCompletionRequest{ |
59 | | - Model: GPT35Turbo, |
60 | | - Messages: validRequest().Messages, |
61 | | - PresencePenalty: -3, |
| 62 | + Model: GPT35Turbo, |
| 63 | + Messages: validRequest().Messages, |
| 64 | + PresencePenalty: -3, |
62 | 65 | }, |
63 | 66 | expectedError: chatgpt_errors.ErrInvalidPresencePenalty, |
64 | 67 | }, |
@@ -92,3 +95,121 @@ func validRequest() *ChatCompletionRequest { |
92 | 95 | }, |
93 | 96 | } |
94 | 97 | } |
| 98 | + |
| 99 | +func newTestServerAndClient() (*httptest.Server, *Client) { |
| 100 | + // Create a new test HTTP server to handle requests |
| 101 | + testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 102 | + w.WriteHeader(http.StatusOK) |
| 103 | + w.Write([]byte(`{ "id": "chatcmpl-abcd", "object": "chat.completion", "created_at": 0, "choices": [ { "index": 0, "message": { "role": "assistant", "content": "\n\n Sample response" }, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 19, "completion_tokens": 47, "total_tokens": 66 }}`)) |
| 104 | + })) |
| 105 | + |
| 106 | + // Create a new client with the test server's URL and a mock API key |
| 107 | + return testServer, &Client{ |
| 108 | + client: http.DefaultClient, |
| 109 | + config: &Config{ |
| 110 | + BaseURL: testServer.URL, |
| 111 | + APIKey: "mock_api_key", |
| 112 | + OrganizationID: "mock_organization_id", |
| 113 | + }, |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func newTestClientWithInvalidResponse() (*httptest.Server, *Client) { |
| 118 | + // Create a new test HTTP server to handle requests |
| 119 | + testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 120 | + w.WriteHeader(http.StatusOK) |
| 121 | + w.Write([]byte(`{ fakejson }`)) |
| 122 | + })) |
| 123 | + |
| 124 | + // Create a new client with the test server's URL and a mock API key |
| 125 | + return testServer, &Client{ |
| 126 | + client: http.DefaultClient, |
| 127 | + config: &Config{ |
| 128 | + BaseURL: testServer.URL, |
| 129 | + APIKey: "mock_api_key", |
| 130 | + OrganizationID: "mock_organization_id", |
| 131 | + }, |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func newTestClientWithInvalidStatusCode() (*httptest.Server, *Client) { |
| 136 | + // Create a new test HTTP server to handle requests |
| 137 | + testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 138 | + w.WriteHeader(http.StatusInternalServerError) |
| 139 | + w.Write([]byte(`{ "error": "bad request" }`)) |
| 140 | + })) |
| 141 | + |
| 142 | + // Create a new client with the test server's URL and a mock API key |
| 143 | + return testServer, &Client{ |
| 144 | + client: http.DefaultClient, |
| 145 | + config: &Config{ |
| 146 | + BaseURL: testServer.URL, |
| 147 | + APIKey: "mock_api_key", |
| 148 | + OrganizationID: "mock_organization_id", |
| 149 | + }, |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +func TestSend(t *testing.T) { |
| 154 | + server, client := newTestServerAndClient() |
| 155 | + defer server.Close() |
| 156 | + |
| 157 | + _, err := client.Send(context.Background(), &ChatCompletionRequest{ |
| 158 | + Model: GPT35Turbo, |
| 159 | + Messages: []ChatMessage{ |
| 160 | + { |
| 161 | + Role: ChatGPTModelRoleUser, |
| 162 | + Content: "Hello", |
| 163 | + }, |
| 164 | + }, |
| 165 | + }) |
| 166 | + assert.NoError(t, err) |
| 167 | + |
| 168 | + _, err = client.Send(context.Background(), &ChatCompletionRequest{ |
| 169 | + Model: "invalid model", |
| 170 | + Messages: []ChatMessage{ |
| 171 | + { |
| 172 | + Role: ChatGPTModelRoleUser, |
| 173 | + Content: "Hello", |
| 174 | + }, |
| 175 | + }, |
| 176 | + }) |
| 177 | + assert.Error(t, err) |
| 178 | + |
| 179 | + server, client = newTestClientWithInvalidResponse() |
| 180 | + defer server.Close() |
| 181 | + |
| 182 | + _, err = client.Send(context.Background(), &ChatCompletionRequest{ |
| 183 | + Model: GPT35Turbo, |
| 184 | + Messages: []ChatMessage{ |
| 185 | + { |
| 186 | + Role: ChatGPTModelRoleUser, |
| 187 | + Content: "Hello", |
| 188 | + }, |
| 189 | + }, |
| 190 | + }) |
| 191 | + assert.Error(t, err) |
| 192 | + |
| 193 | + server, client = newTestClientWithInvalidStatusCode() |
| 194 | + defer server.Close() |
| 195 | + |
| 196 | + _, err = client.Send(context.Background(), &ChatCompletionRequest{ |
| 197 | + Model: GPT35Turbo, |
| 198 | + Messages: []ChatMessage{ |
| 199 | + { |
| 200 | + Role: ChatGPTModelRoleUser, |
| 201 | + Content: "Hello", |
| 202 | + }, |
| 203 | + }, |
| 204 | + }) |
| 205 | + assert.Error(t, err) |
| 206 | + |
| 207 | +} |
| 208 | + |
| 209 | +func TestSimpleSend(t *testing.T) { |
| 210 | + server, client := newTestServerAndClient() |
| 211 | + defer server.Close() |
| 212 | + |
| 213 | + _, err := client.SimpleSend(context.Background(), "Hello") |
| 214 | + assert.NoError(t, err) |
| 215 | +} |
0 commit comments