112 lines
3.5 KiB
Go
112 lines
3.5 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"tts-server/internal/tts"
|
|
)
|
|
|
|
func testServer(t *testing.T) *server {
|
|
t.Helper()
|
|
root := t.TempDir()
|
|
for _, name := range []string{"model.onnx", "tokens.txt"} {
|
|
if err := os.WriteFile(filepath.Join(root, name), []byte(name), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
hash := sha256.Sum256([]byte("model.onnx"))
|
|
manifest, _ := json.Marshal(tts.Manifest{Version: "test", Default: "a", Voices: []tts.VoiceEntry{{Name: "a", Locale: "zh-CN", Model: "model.onnx", SHA256: hex.EncodeToString(hash[:])}}})
|
|
if err := os.WriteFile(filepath.Join(root, "manifest.json"), manifest, 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
repo, err := tts.LoadRepo(root)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cache, err := tts.NewCache(t.TempDir())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return &server{cfg: settings{maxRunes: 300, voicesDir: root}, repo: repo, cache: cache, eng: newEngine(1), queue: make(chan struct{}, 3), worker: make(chan struct{}, 1)}
|
|
}
|
|
func TestRejectUnauthorizedAndTrailingJSON(t *testing.T) {
|
|
s := testServer(t)
|
|
s.cfg.apiToken = "test-token"
|
|
request := httptest.NewRequest("POST", "/tts", strings.NewReader(`{"text":"hello"}`))
|
|
response := httptest.NewRecorder()
|
|
s.routes().ServeHTTP(response, request)
|
|
if response.Code != http.StatusUnauthorized {
|
|
t.Fatal(response.Code)
|
|
}
|
|
request = httptest.NewRequest("POST", "/tts", strings.NewReader(`{"text":"hello"} {}`))
|
|
request.Header.Set("Authorization", "Bearer test-token")
|
|
response = httptest.NewRecorder()
|
|
s.routes().ServeHTTP(response, request)
|
|
if response.Code != http.StatusBadRequest {
|
|
t.Fatal(response.Code)
|
|
}
|
|
}
|
|
func TestCancelledQueueReleasesSlot(t *testing.T) {
|
|
s := testServer(t)
|
|
s.worker <- struct{}{}
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
request := httptest.NewRequest("POST", "/tts", strings.NewReader(`{"text":"hello"}`)).WithContext(ctx)
|
|
response := httptest.NewRecorder()
|
|
s.routes().ServeHTTP(response, request)
|
|
if response.Code != http.StatusGatewayTimeout {
|
|
t.Fatal(response.Code)
|
|
}
|
|
if len(s.queue) != 0 {
|
|
t.Fatal("cancelled request leaked queue slot")
|
|
}
|
|
}
|
|
func TestCacheHitDoesNotWaitForInference(t *testing.T) {
|
|
s := testServer(t)
|
|
key := s.cache.Key("test", "a", 1, 1, "hello")
|
|
data := tts.EncodeWAV([]float32{0.1, 0.2}, 22050)
|
|
if err := s.cache.Put(key, data); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s.worker <- struct{}{}
|
|
request := httptest.NewRequest("POST", "/tts", strings.NewReader(`{"text":"hello"}`))
|
|
response := httptest.NewRecorder()
|
|
s.routes().ServeHTTP(response, request)
|
|
if response.Code != 200 || response.Header().Get("X-Tts-Cache") != "hit" || response.Body.Len() != len(data) {
|
|
t.Fatal("cache response invalid")
|
|
}
|
|
}
|
|
func TestNativeSynthesis(t *testing.T) {
|
|
dir := os.Getenv("TTS_TEST_VOICES")
|
|
if dir == "" {
|
|
t.Skip("set TTS_TEST_VOICES to run native model smoke test")
|
|
}
|
|
repo, err := tts.LoadRepo(dir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
engine := newEngine(2)
|
|
defer engine.close()
|
|
for _, pitch := range []float32{1, 0.5, 1.2, 2} {
|
|
samples, rate, err := engine.synthesize(context.Background(), repo.DefaultPack(), "您好。现在时间是九点整。", 0.9, pitch)
|
|
if err != nil || len(samples) == 0 || rate != 22050 {
|
|
t.Fatalf("pitch=%v samples=%d rate=%d err=%v", pitch, len(samples), rate, err)
|
|
}
|
|
var energy float64
|
|
for _, v := range samples {
|
|
energy += float64(v * v)
|
|
}
|
|
if energy < 0.01 {
|
|
t.Fatalf("silent output pitch=%v", pitch)
|
|
}
|
|
}
|
|
}
|