forked from neosapience/cast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroot_test.go
More file actions
454 lines (418 loc) · 11.8 KB
/
root_test.go
File metadata and controls
454 lines (418 loc) · 11.8 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package cmd
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"github.com/spf13/viper"
)
func TestBuildTTSRequest_EmotionPreset(t *testing.T) {
cases := []struct {
name string
model string
preset string
intensity string
wantEmotionType string
wantEmotionPreset string
}{
{
name: "preset on ssfm-v21",
model: "ssfm-v21",
preset: "happy",
wantEmotionType: "preset",
wantEmotionPreset: "happy",
},
{
name: "preset on ssfm-v30",
model: "ssfm-v30",
preset: "whisper",
wantEmotionType: "preset",
wantEmotionPreset: "whisper",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
resetFlags()
rootCmd.Flags().Set("emotion", "preset")
rootCmd.Flags().Set("emotion-preset", tc.preset)
rootCmd.Flags().Set("model", tc.model)
req, err := buildTTSRequest(rootCmd, "hello")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if req.Prompt == nil {
t.Fatal("expected prompt to be set")
}
if req.Prompt.EmotionType != tc.wantEmotionType {
t.Errorf("emotion_type: want %q, got %q", tc.wantEmotionType, req.Prompt.EmotionType)
}
if req.Prompt.EmotionPreset != tc.wantEmotionPreset {
t.Errorf("emotion_preset: want %q, got %q", tc.wantEmotionPreset, req.Prompt.EmotionPreset)
}
})
}
}
func TestBuildTTSRequest_EmotionIntensity(t *testing.T) {
resetFlags()
rootCmd.Flags().Set("emotion", "preset")
rootCmd.Flags().Set("emotion-preset", "happy")
rootCmd.Flags().Set("emotion-intensity", "1.5")
rootCmd.Flags().Set("model", defaultModel)
req, err := buildTTSRequest(rootCmd, "hello")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if req.Prompt == nil || req.Prompt.EmotionIntensity == nil {
t.Fatal("expected emotion_intensity to be set")
}
if *req.Prompt.EmotionIntensity != 1.5 {
t.Errorf("emotion_intensity: want 1.5, got %g", *req.Prompt.EmotionIntensity)
}
}
func TestBuildTTSRequest_AudioParams(t *testing.T) {
cases := []struct {
name string
volume string
pitch string
tempo string
format string
wantVolume *int
wantPitch *int
wantTempo *float64
wantFormat string
}{
{
name: "volume and pitch set",
volume: "150", pitch: "6",
wantVolume: intPtr(150), wantPitch: intPtr(6),
},
{
name: "tempo set",
tempo: "1.5",
wantTempo: float64Ptr(1.5),
},
{
name: "format mp3",
format: "mp3",
wantFormat: "mp3",
},
{
name: "negative pitch",
pitch: "-6",
wantPitch: intPtr(-6),
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
resetFlags()
if tc.volume != "" {
rootCmd.Flags().Set("volume", tc.volume)
}
if tc.pitch != "" {
rootCmd.Flags().Set("pitch", tc.pitch)
}
if tc.tempo != "" {
rootCmd.Flags().Set("tempo", tc.tempo)
}
if tc.format != "" {
viper.Set("format", tc.format)
}
req, err := buildTTSRequest(rootCmd, "hello")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if tc.wantVolume != nil {
if req.Output == nil || req.Output.Volume == nil || *req.Output.Volume != *tc.wantVolume {
t.Errorf("volume: want %v, got %v", tc.wantVolume, outputField(req.Output, "volume"))
}
}
if tc.wantPitch != nil {
if req.Output == nil || req.Output.AudioPitch == nil || *req.Output.AudioPitch != *tc.wantPitch {
t.Errorf("pitch: want %v, got %v", tc.wantPitch, outputField(req.Output, "pitch"))
}
}
if tc.wantTempo != nil {
if req.Output == nil || req.Output.AudioTempo == nil || *req.Output.AudioTempo != *tc.wantTempo {
t.Errorf("tempo: want %v, got %v", tc.wantTempo, outputField(req.Output, "tempo"))
}
}
if tc.wantFormat != "" {
if req.Output == nil || req.Output.AudioFormat != tc.wantFormat {
t.Errorf("format: want %q, got %q", tc.wantFormat, outputField(req.Output, "format"))
}
}
viper.Set("format", "")
})
}
}
func TestBuildTTSRequest_Seed(t *testing.T) {
resetFlags()
rootCmd.Flags().Set("seed", "42")
req, err := buildTTSRequest(rootCmd, "hello")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if req.Seed == nil || *req.Seed != 42 {
t.Errorf("seed: want 42, got %v", req.Seed)
}
}
func TestBuildTTSRequest_Language(t *testing.T) {
resetFlags()
viper.Set("language", "eng")
defer viper.Set("language", "")
req, err := buildTTSRequest(rootCmd, "hello")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if req.Language != "eng" {
t.Errorf("language: want %q, got %q", "eng", req.Language)
}
}
func TestBuildTTSRequest_SmartEmotionContext(t *testing.T) {
resetFlags()
rootCmd.Flags().Set("emotion", "smart")
rootCmd.Flags().Set("model", "ssfm-v30")
rootCmd.Flags().Set("prev-text", "오늘 복권에 당첨됐어요!")
rootCmd.Flags().Set("next-text", "믿기지가 않네요.")
req, err := buildTTSRequest(rootCmd, "정말요?")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if req.Prompt == nil {
t.Fatal("expected prompt to be set")
}
if req.Prompt.EmotionType != "smart" {
t.Errorf("emotion_type: want %q, got %q", "smart", req.Prompt.EmotionType)
}
if req.Prompt.PreviousText != "오늘 복권에 당첨됐어요!" {
t.Errorf("previous_text: want %q, got %q", "오늘 복권에 당첨됐어요!", req.Prompt.PreviousText)
}
if req.Prompt.NextText != "믿기지가 않네요." {
t.Errorf("next_text: want %q, got %q", "믿기지가 않네요.", req.Prompt.NextText)
}
}
func TestBuildTTSRequest_OutputNilWhenDefaults(t *testing.T) {
resetFlags()
req, err := buildTTSRequest(rootCmd, "hello")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if req.Output != nil {
t.Errorf("expected output to be nil when no audio params set, got %+v", req.Output)
}
if req.Prompt != nil {
t.Errorf("expected prompt to be nil when no emotion set, got %+v", req.Prompt)
}
if req.Seed != nil {
t.Errorf("expected seed to be nil when not set, got %v", req.Seed)
}
}
func TestRootCmd_OutFileSaved(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var body map[string]interface{}
json.NewDecoder(r.Body).Decode(&body)
w.Write([]byte("fake-audio-bytes"))
}))
defer srv.Close()
outFile := filepath.Join(t.TempDir(), "out.mp3")
resetFlags()
rootCmd.SetArgs([]string{"hello", "--base-url", srv.URL, "--out", outFile})
if err := rootCmd.Execute(); err != nil {
t.Fatalf("unexpected error: %v", err)
}
data, err := os.ReadFile(outFile)
if err != nil {
t.Fatalf("output file not found: %v", err)
}
if string(data) != "fake-audio-bytes" {
t.Errorf("file content: want %q, got %q", "fake-audio-bytes", string(data))
}
}
func TestRootCmd_OutFileFormatInferredFromExtension(t *testing.T) {
var capturedFormat string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var body struct {
Output *struct {
AudioFormat string `json:"audio_format"`
} `json:"output"`
}
json.NewDecoder(r.Body).Decode(&body)
if body.Output != nil {
capturedFormat = body.Output.AudioFormat
}
w.Write([]byte("audio"))
}))
defer srv.Close()
for _, ext := range []string{"mp3", "wav"} {
t.Run(ext, func(t *testing.T) {
capturedFormat = ""
outFile := filepath.Join(t.TempDir(), "out."+ext)
resetFlags()
rootCmd.SetArgs([]string{"hello", "--base-url", srv.URL, "--out", outFile})
if err := rootCmd.Execute(); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if capturedFormat != ext {
t.Errorf("format inferred from .%s: want %q, got %q", ext, ext, capturedFormat)
}
})
}
}
// helpers
func intPtr(v int) *int { return &v }
func float64Ptr(v float64) *float64 { return &v }
func outputField(out interface{}, field string) string {
if out == nil {
return "<nil output>"
}
return "<set>"
}
// resetFlags resets cobra flag values to their defaults between tests.
// Cobra's pflag state is global, so values persist across Execute() calls.
func resetFlags() {
f := rootCmd.Flags()
if flag := f.Lookup("volume"); flag != nil {
flag.Value.Set("-1")
flag.Changed = false
}
if flag := f.Lookup("pitch"); flag != nil {
flag.Value.Set("0")
flag.Changed = false
}
if flag := f.Lookup("tempo"); flag != nil {
flag.Value.Set("-1")
flag.Changed = false
}
f.Set("emotion", "")
f.Set("emotion-preset", "")
if flag := f.Lookup("emotion-intensity"); flag != nil {
flag.Value.Set("-1")
flag.Changed = false
}
f.Set("prev-text", "")
f.Set("next-text", "")
f.Set("model", defaultModel)
if flag := f.Lookup("seed"); flag != nil {
flag.Value.Set("-1")
flag.Changed = false
}
f.Set("out", "")
f.Set("format", "")
f.Set("language", "")
viper.Set("format", "")
viper.Set("language", "")
}
func TestRootCmd_TextValidation(t *testing.T) {
cases := []struct {
name string
text string
errMsg string
}{
{"empty text", "", "text must be between 1 and 2000 characters"},
{"text too long", strings.Repeat("a", 2001), "text must be between 1 and 2000 characters"},
{"korean text too long", strings.Repeat("가", 2001), "text must be between 1 and 2000 characters"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
resetFlags()
rootCmd.SetArgs([]string{tc.text})
err := rootCmd.Execute()
if err == nil {
t.Fatal("expected error, got nil")
}
if !strings.Contains(err.Error(), tc.errMsg) {
t.Errorf("want error %q, got %q", tc.errMsg, err.Error())
}
})
}
}
func TestRootCmd_AudioParamValidation(t *testing.T) {
cases := []struct {
name string
args []string
errMsg string
}{
{
name: "volume too high",
args: []string{"hello", "--volume", "201"},
errMsg: "volume must be between",
},
{
name: "pitch too high",
args: []string{"hello", "--pitch", "13"},
errMsg: "pitch must be between",
},
{
name: "tempo too high",
args: []string{"hello", "--tempo", "2.5"},
errMsg: "tempo must be between",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
resetFlags()
rootCmd.SetArgs(tc.args)
err := rootCmd.Execute()
if err == nil {
t.Fatalf("expected error, got nil")
}
if !strings.Contains(err.Error(), tc.errMsg) {
t.Errorf("want error containing %q, got %q", tc.errMsg, err.Error())
}
})
}
}
func TestRootCmd_EmotionValidation(t *testing.T) {
cases := []struct {
name string
args []string
errMsg string
}{
{
name: "smart requires ssfm-v30",
args: []string{"hello", "--emotion", "smart", "--model", "ssfm-v21"},
errMsg: "emotion 'smart' is only supported with ssfm-v30",
},
{
name: "prev-text without smart emotion",
args: []string{"hello", "--prev-text", "context"},
errMsg: "--prev-text and --next-text require --emotion smart",
},
{
name: "next-text without smart emotion",
args: []string{"hello", "--next-text", "context", "--emotion", "preset"},
errMsg: "--prev-text and --next-text require --emotion smart",
},
{
name: "emotion-preset without emotion preset",
args: []string{"hello", "--emotion-preset", "happy"},
errMsg: "--emotion-preset and --emotion-intensity require --emotion preset",
},
{
name: "emotion-intensity without emotion preset",
args: []string{"hello", "--emotion-intensity", "1.5"},
errMsg: "--emotion-preset and --emotion-intensity require --emotion preset",
},
{
name: "unknown emotion type",
args: []string{"hello", "--emotion", "invalid"},
errMsg: "unknown emotion type",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
resetFlags()
rootCmd.SetArgs(tc.args)
err := rootCmd.Execute()
if err == nil {
t.Fatal("expected error, got nil")
}
if !strings.Contains(err.Error(), tc.errMsg) {
t.Errorf("want error containing %q, got %q", tc.errMsg, err.Error())
}
})
}
}