Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for TextToSpeechStream #18

Merged
merged 1 commit into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions elevenlabs_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package elevenlabs_test

import (
"bytes"
"context"
"encoding/json"
"errors"
Expand Down Expand Up @@ -297,6 +298,68 @@ func TestTextToSpeech(t *testing.T) {
}
}

func TestTextToSpeechStream(t *testing.T) {
testCases := []struct {
name string
excludeAPIKey bool
queries []elevenlabs.QueryFunc
expQueryString string
testRequestBody any
expResponseBody []byte
expectedRespStatus int
}{
{
name: "No API key and no queries",
excludeAPIKey: true,
testRequestBody: elevenlabs.TextToSpeechRequest{
ModelID: "model1",
Text: "Test text",
},
expResponseBody: testRespBodies["TestTextToSpeechStream"],
expectedRespStatus: http.StatusOK,
},
{
name: "With API key and no queries",
excludeAPIKey: false,
testRequestBody: elevenlabs.TextToSpeechRequest{
ModelID: "model1",
Text: "Test text",
},
expResponseBody: testRespBodies["TestTextToSpeechStream"],
expectedRespStatus: http.StatusOK,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
requestAPIKey := mockAPIKey
if tc.excludeAPIKey {
requestAPIKey = ""
}
server := testServer(t, testServerConfig{
keyOptional: tc.excludeAPIKey,
expectedMethod: http.MethodPost,
expectedContentType: contentTypeJSON,
expectedQueryStr: tc.expQueryString,
statusCode: tc.expectedRespStatus,
responseBody: tc.expResponseBody,
})
defer server.Close()

client := elevenlabs.NewMockClient(context.Background(), server.URL, requestAPIKey, mockTimeout)
w := bytes.Buffer{}
err := client.TextToSpeechStream(&w, "voiceID", tc.testRequestBody.(elevenlabs.TextToSpeechRequest), tc.queries...)
if err != nil {
t.Errorf("Expected no errors, got error: %q", err)
}
respBody := w.Bytes()

if string(respBody) != string(tc.expResponseBody) {
t.Errorf("Expected response %q, got %q", string(tc.expResponseBody), string(respBody))
}
})
}
}

func TestGetModels(t *testing.T) {
respBody := testRespBodies["TestGetModels"]
server := testServer(t, testServerConfig{
Expand Down
5 changes: 3 additions & 2 deletions request_bodies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,9 @@ var testRespBodies = map[string][]byte{
]
}
}`),
"TestGetSampleAudio": []byte("testaudiobytes"),
"TestTextToSpeech": []byte("testaudiobytes"),
"TestGetSampleAudio": []byte("testaudiobytes"),
"TestTextToSpeech": []byte("testaudiobytes"),
"TestTextToSpeechStream": []byte("testaudiobytes"),
"TestGetHistory-NoMore": []byte(`{
"history":[],
"last_history_item_id": "",
Expand Down
Loading