-
Notifications
You must be signed in to change notification settings - Fork 1
/
whisper_test.go
99 lines (88 loc) · 2.38 KB
/
whisper_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
package openai
import (
"errors"
"io"
"net/http"
"testing"
"github.com/Simplou/goxios"
)
// MockWhisperHTTPClient is a mock implementation of HTTPClient for testing purposes.
type MockWhisperHTTPClient struct{}
func (c *MockWhisperHTTPClient) Get(url string, opts *goxios.RequestOpts) (*http.Response, error) {
return &http.Response{}, nil
}
func (c *MockWhisperHTTPClient) Post(url string, opts *goxios.RequestOpts) (*http.Response, error) {
json := goxios.JSON{
"text": "Hello.",
}
b, err := json.Marshal()
if err != nil {
return nil, err
}
resp := &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(ioReader(b)),
}
return resp, nil
}
// MockFailingWhisperHTTPClient is a mock implementation of HTTPClient for testing purposes.
// This implementation always returns an error.
type MockFailingWhisperHTTPClient struct{}
func (c *MockFailingWhisperHTTPClient) Get(url string, opts *goxios.RequestOpts) (*http.Response, error) {
return &http.Response{}, nil
}
func (c *MockFailingWhisperHTTPClient) Post(url string, opts *goxios.RequestOpts) (*http.Response, error) {
json := goxios.JSON{}
b, err := json.Marshal()
if err != nil {
return nil, err
}
resp := &http.Response{
StatusCode: http.StatusBadRequest,
Body: io.NopCloser(ioReader(b)),
}
return resp, errors.New("mock HTTP client always fails")
}
func TestTranscription(t *testing.T) {
client := &OpenAIClientMock{
MockClient: MockClient{
baseUrl: "https://fake.api.openai.com/v1",
},
}
testCases := []struct {
name string
httpClient HTTPClient
expectedErr bool
expectedMessage string
}{
{
name: "Successful request",
httpClient: new(MockWhisperHTTPClient),
expectedErr: false,
},
{
name: "Failed request",
httpClient: new(MockFailingWhisperHTTPClient),
expectedErr: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
body := &TranscriptionsRequestBody{
Model: DefaultTranscriptionModel,
Filename: "hello.mp3",
}
body.AudioFilePath = "./temp/" + body.Filename
resp, err := Transcription(client, tc.httpClient, body)
if tc.expectedErr && err == nil {
t.Errorf("Expected an error, but got nil")
}
if !tc.expectedErr && err != nil {
t.Errorf("Expected no error, but got: %v", err)
}
if resp != nil && resp.Text != "Hello." {
t.Fail()
}
})
}
}