-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaudio_speech_test.go
99 lines (83 loc) · 2.72 KB
/
audio_speech_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 coze
import (
"context"
"io"
"net/http"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAudioSpeech(t *testing.T) {
// Test Create method
t.Run("Create speech success", func(t *testing.T) {
mockTransport := &mockTransport{
roundTripFunc: func(req *http.Request) (*http.Response, error) {
// Verify request method and path
assert.Equal(t, http.MethodPost, req.Method)
assert.Equal(t, "/v1/audio/speech", req.URL.Path)
// Return mock response with audio data
resp := &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{},
Body: io.NopCloser(strings.NewReader("mock audio data")),
}
resp.Header.Set(logIDHeader, "test_log_id")
return resp, nil
},
}
core := newCore(&http.Client{Transport: mockTransport}, ComBaseURL)
speech := newSpeech(core)
resp, err := speech.Create(context.Background(), &CreateAudioSpeechReq{
Input: "Hello, world!",
VoiceID: "voice1",
ResponseFormat: AudioFormatMP3.Ptr(),
Speed: ptr[float32](1.0),
})
require.NoError(t, err)
assert.Equal(t, "test_log_id", resp.HTTPResponse.LogID())
// Read and verify response body
data, err := io.ReadAll(resp.Data)
require.NoError(t, err)
assert.Equal(t, "mock audio data", string(data))
resp.Data.Close()
})
// Test Create method with error
t.Run("Create speech with error", func(t *testing.T) {
mockTransport := &mockTransport{
roundTripFunc: func(req *http.Request) (*http.Response, error) {
// Return error response
return mockResponse(http.StatusBadRequest, &baseResponse{})
},
}
core := newCore(&http.Client{Transport: mockTransport}, ComBaseURL)
speech := newSpeech(core)
resp, err := speech.Create(context.Background(), &CreateAudioSpeechReq{
Input: "Hello, world!",
VoiceID: "invalid_voice",
ResponseFormat: AudioFormatMP3.Ptr(),
Speed: ptr[float32](1.0),
})
require.Error(t, err)
assert.Nil(t, resp)
})
// Test Create method with invalid speed
t.Run("Create speech with invalid speed", func(t *testing.T) {
mockTransport := &mockTransport{
roundTripFunc: func(req *http.Request) (*http.Response, error) {
// Return error response for invalid speed
return mockResponse(http.StatusBadRequest, &baseResponse{})
},
}
core := newCore(&http.Client{Transport: mockTransport}, ComBaseURL)
speech := newSpeech(core)
resp, err := speech.Create(context.Background(), &CreateAudioSpeechReq{
Input: "Hello, world!",
VoiceID: "voice1",
ResponseFormat: AudioFormatMP3.Ptr(),
Speed: ptr[float32](-1.0), // Invalid speed
})
require.Error(t, err)
assert.Nil(t, resp)
})
}