feat(multimodal): 视觉分层路由(多模态检测→主/视觉/OCR 工具)+ 语音 STT→TTS 降级链(异步)
This commit is contained in:
1 parent
02aae2bbb4
commit
0a936a3202
19 files changed
+2121
No files matched your search
@@ -0,0 +1,81 @@
|
||||
package voice
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHTTPSTT(t *testing.T) {
|
||||
t.Parallel()
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var body map[string]any
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
t.Fatalf("decode: %v", err)
|
||||
}
|
||||
if r.Header.Get("Authorization") != "Bearer sk" {
|
||||
t.Errorf("auth = %q, want Bearer sk", r.Header.Get("Authorization"))
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"text":"hello world"}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
stt := NewHTTPSTT(srv.URL, "sk")
|
||||
text, err := stt.STT(context.Background(), Audio{Format: "wav", Data: []byte("raw")})
|
||||
if err != nil {
|
||||
t.Fatalf("STT error: %v", err)
|
||||
}
|
||||
if text != "hello world" {
|
||||
t.Errorf("text = %q, want hello world", text)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHTTPSTT_NotConfigured(t *testing.T) {
|
||||
t.Parallel()
|
||||
stt := NewHTTPSTT("", "")
|
||||
if _, err := stt.STT(context.Background(), Audio{}); err != ErrNotConfigured {
|
||||
t.Fatalf("err = %v, want ErrNotConfigured", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHTTPTTS(t *testing.T) {
|
||||
t.Parallel()
|
||||
audioBytes := []byte{1, 2, 3}
|
||||
b64 := base64.StdEncoding.EncodeToString(audioBytes)
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var body map[string]any
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
t.Fatalf("decode: %v", err)
|
||||
}
|
||||
if body["text"] != "hi" {
|
||||
t.Errorf("text = %v, want hi", body["text"])
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"audio":"` + b64 + `","format":"wav"}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
tts := NewHTTPTTS(srv.URL, "")
|
||||
audio, err := tts.TTS(context.Background(), "hi")
|
||||
if err != nil {
|
||||
t.Fatalf("TTS error: %v", err)
|
||||
}
|
||||
if audio.Format != "wav" || hex.EncodeToString(audio.Data) != hex.EncodeToString(audioBytes) {
|
||||
t.Errorf("audio = %+v, want wav/010203", audio)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHTTPTTS_EmptyText(t *testing.T) {
|
||||
t.Parallel()
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
|
||||
defer srv.Close()
|
||||
tts := NewHTTPTTS(srv.URL, "")
|
||||
if _, err := tts.TTS(context.Background(), " "); err != ErrEmptyText {
|
||||
t.Fatalf("err = %v, want ErrEmptyText", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user