forked from neosapience/cast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtts_test.go
More file actions
86 lines (75 loc) · 2.22 KB
/
tts_test.go
File metadata and controls
86 lines (75 loc) · 2.22 KB
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
package client
import (
"encoding/json"
"net/http"
"testing"
)
func TestTextToSpeech_SendsCorrectRequest(t *testing.T) {
c, _ := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v1/text-to-speech" {
t.Errorf("unexpected path: %s", r.URL.Path)
}
if r.Method != http.MethodPost {
t.Errorf("expected POST, got %s", r.Method)
}
var body TTSRequest
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
t.Fatalf("failed to decode request body: %v", err)
}
if body.VoiceID != "voice-1" {
t.Errorf("expected voice_id=voice-1, got %q", body.VoiceID)
}
if body.Text != "hello" {
t.Errorf("expected text=hello, got %q", body.Text)
}
w.Write([]byte("audio-bytes"))
})
audio, err := c.TextToSpeech(TTSRequest{VoiceID: "voice-1", Text: "hello", Model: "ssfm-v30"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if string(audio) != "audio-bytes" {
t.Errorf("unexpected audio response: %s", audio)
}
}
func TestTextToSpeech_ReturnsErrorOnAPIFailure(t *testing.T) {
c, _ := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{"message":"invalid voice_id"}`))
})
_, err := c.TextToSpeech(TTSRequest{VoiceID: "bad-id", Text: "hello"})
if err == nil {
t.Fatal("expected error, got nil")
}
}
func TestTextToSpeech_SendsOptionalFields(t *testing.T) {
seed := 42
volume := 150
c, _ := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
var body TTSRequest
json.NewDecoder(r.Body).Decode(&body)
if body.Prompt == nil {
t.Fatal("expected prompt to be set")
}
if body.Prompt.EmotionType != "smart" {
t.Errorf("expected emotion=smart, got %q", body.Prompt.EmotionType)
}
if body.Output == nil {
t.Fatal("expected output to be set")
}
if body.Output.Volume == nil || *body.Output.Volume != 150 {
t.Errorf("expected volume=150, got %v", body.Output.Volume)
}
if body.Seed == nil || *body.Seed != 42 {
t.Errorf("expected seed=42, got %v", body.Seed)
}
w.Write([]byte("ok"))
})
c.TextToSpeech(TTSRequest{
VoiceID: "v1",
Text: "hi",
Prompt: &TTSPrompt{EmotionType: "smart"},
Output: &TTSOutput{Volume: &volume},
Seed: &seed,
})
}