Skip to content

Commit

Permalink
Add test for TextToSpeechStream
Browse files Browse the repository at this point in the history
  • Loading branch information
haguro committed Feb 18, 2024
1 parent 18a2991 commit 4c33f89
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
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

0 comments on commit 4c33f89

Please sign in to comment.