feat: 增加在线语音 SDK 和音色管理后台,完善引擎适配
This commit is contained in:
1 parent
1856de97bc
commit
a532ba4e0f
71 files changed
+3189
-472
No files matched your search
@@ -0,0 +1,110 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
"tts-server/internal/tts"
|
||||
)
|
||||
|
||||
func TestAdminSeparateAuthentication(t *testing.T) {
|
||||
s := testServer(t)
|
||||
s.cfg.adminToken = "admin-secret"
|
||||
s.cfg.apiToken = "synthesis-secret"
|
||||
for _, token := range []string{"", "synthesis-secret"} {
|
||||
r := httptest.NewRequest("GET", "/admin/api/voices", nil)
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
w := httptest.NewRecorder()
|
||||
s.routes().ServeHTTP(w, r)
|
||||
if w.Code != 401 {
|
||||
t.Fatalf("synthesis token granted admin access: %d", w.Code)
|
||||
}
|
||||
}
|
||||
r := httptest.NewRequest("GET", "/admin/api/voices", nil)
|
||||
r.Header.Set("Authorization", "Bearer admin-secret")
|
||||
w := httptest.NewRecorder()
|
||||
s.routes().ServeHTTP(w, r)
|
||||
if w.Code != 200 {
|
||||
t.Fatal(w.Code)
|
||||
}
|
||||
}
|
||||
func TestAdminDefaultAndPreview(t *testing.T) {
|
||||
s := testServer(t)
|
||||
s.cfg.adminToken = "admin-secret"
|
||||
s.cfg.apiToken = "synthesis-secret"
|
||||
r := httptest.NewRequest("POST", "/admin/api/default", bytes.NewBufferString(`{"voice":"a"}`))
|
||||
r.Header.Set("Authorization", "Bearer admin-secret")
|
||||
w := httptest.NewRecorder()
|
||||
s.routes().ServeHTTP(w, r)
|
||||
if w.Code != 200 {
|
||||
t.Fatal(w.Code, w.Body.String())
|
||||
}
|
||||
repo := s.repository()
|
||||
key := s.cache.Key(repo.Version, "a", 1, 1, "hello")
|
||||
if err := s.cache.Put(key, tts.EncodeWAV([]float32{0.1, 0.2}, 22050)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r = httptest.NewRequest("POST", "/admin/api/preview", bytes.NewBufferString(`{"text":"hello"}`))
|
||||
r.Header.Set("Authorization", "Bearer admin-secret")
|
||||
w = httptest.NewRecorder()
|
||||
s.routes().ServeHTTP(w, r)
|
||||
if w.Code != 200 || w.Header().Get("Content-Type") != "audio/wav" {
|
||||
t.Fatal(w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
type pausedUpload struct {
|
||||
started chan struct{}
|
||||
release chan struct{}
|
||||
once sync.Once
|
||||
}
|
||||
|
||||
func (p *pausedUpload) Read(b []byte) (int, error) {
|
||||
p.once.Do(func() { close(p.started) })
|
||||
<-p.release
|
||||
return 0, io.EOF
|
||||
}
|
||||
func (p *pausedUpload) Close() error { return nil }
|
||||
func TestUploadDoesNotBlockHealth(t *testing.T) {
|
||||
s := testServer(t)
|
||||
s.cfg.adminToken = "admin-secret"
|
||||
body := &pausedUpload{started: make(chan struct{}), release: make(chan struct{})}
|
||||
r := httptest.NewRequest("POST", "/admin/api/import", body)
|
||||
r.Header.Set("Authorization", "Bearer admin-secret")
|
||||
done := make(chan struct{})
|
||||
go func() { defer close(done); s.routes().ServeHTTP(httptest.NewRecorder(), r) }()
|
||||
<-body.started
|
||||
health := make(chan int, 1)
|
||||
go func() {
|
||||
w := httptest.NewRecorder()
|
||||
s.routes().ServeHTTP(w, httptest.NewRequest("GET", "/healthz", nil))
|
||||
health <- w.Code
|
||||
}()
|
||||
select {
|
||||
case code := <-health:
|
||||
if code != http.StatusOK {
|
||||
t.Error(code)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Error("upload blocked health")
|
||||
}
|
||||
close(body.release)
|
||||
<-done
|
||||
}
|
||||
func TestAdminPageServedWithoutSecrets(t *testing.T) {
|
||||
s := testServer(t)
|
||||
w := httptest.NewRecorder()
|
||||
s.routes().ServeHTTP(w, httptest.NewRequest("GET", "/admin/", nil))
|
||||
if w.Code != 200 || w.Header().Get("Content-Security-Policy") == "" {
|
||||
t.Fatal(w.Code)
|
||||
}
|
||||
var parsed any
|
||||
if json.Unmarshal(w.Body.Bytes(), &parsed) == nil {
|
||||
t.Fatal("expected HTML")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user