From 4c33f8977b62136ed5335e57c8864b249a35d120 Mon Sep 17 00:00:00 2001 From: haguro <1076+haguro@users.noreply.github.com> Date: Sun, 18 Feb 2024 23:34:47 +1100 Subject: [PATCH] Add test for TextToSpeechStream --- elevenlabs_test.go | 63 ++++++++++++++++++++++++++++++++++++++++++ request_bodies_test.go | 5 ++-- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/elevenlabs_test.go b/elevenlabs_test.go index 3569857..ba29c31 100644 --- a/elevenlabs_test.go +++ b/elevenlabs_test.go @@ -1,6 +1,7 @@ package elevenlabs_test import ( + "bytes" "context" "encoding/json" "errors" @@ -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{ diff --git a/request_bodies_test.go b/request_bodies_test.go index 85b5af5..325b998 100644 --- a/request_bodies_test.go +++ b/request_bodies_test.go @@ -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": "",