-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
62 lines (52 loc) · 1.47 KB
/
main_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
package chatgpt_test
import (
"encoding/json"
"net/http"
"testing"
"github.com/garydevenay/go-chatgpt-client"
"github.com/jarcoal/httpmock"
)
func TestSendMessage(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
client := chatgpt.NewClient("test-api-key")
mockResponse := chatgpt.ChatResponse{
ID: "testid",
Model: "gpt-3.5-turbo",
Choices: []struct {
Message struct {
Role string `json:"role"`
Content string `json:"content"`
} `json:"message"`
FinishReason string `json:"finish_reason"`
Index int `json:"index"`
}{
{
Message: struct {
Role string `json:"role"`
Content string `json:"content"`
}{
Role: "assistant",
Content: "The capital of France is Paris.",
},
FinishReason: "stop",
Index: 0,
},
},
}
mockResponseBody, _ := json.Marshal(mockResponse)
httpmock.RegisterResponder("POST", "https://api.openai.com/v1/chat/completions",
httpmock.NewStringResponder(http.StatusOK, string(mockResponseBody)))
messages := []chatgpt.Message{
{Role: "system", Content: "You are a helpful assistant."},
{Role: "user", Content: "What is the capital of France?"},
}
response, err := client.SendMessage("gpt-3.5-turbo", messages)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
expectedResponse := "The capital of France is Paris."
if response != expectedResponse {
t.Errorf("Expected response: %s, got: %s", expectedResponse, response)
}
}