You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.7 KiB
61 lines
1.7 KiB
package server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestGenerateSpeechRefsUsesAudioSpeechEndpoint(t *testing.T) {
|
|
var gotPath string
|
|
var gotBody map[string]any
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
gotPath = r.URL.Path
|
|
if err := json.NewDecoder(r.Body).Decode(&gotBody); err != nil {
|
|
t.Fatalf("decode request body: %v", err)
|
|
}
|
|
w.Header().Set("Content-Type", "audio/mpeg")
|
|
_, _ = w.Write([]byte("fake-mp3"))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
client := &newAPIClient{
|
|
baseURL: strings.TrimRight(srv.URL, "/"),
|
|
httpClient: srv.Client(),
|
|
}
|
|
|
|
refs, _, err := client.generateSpeechRefs(context.Background(), "sk-test", "speech-2.8-hd", map[string]any{
|
|
"prompt": "今天想去上海走一走。",
|
|
"format": "mp3",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("generateSpeechRefs: %v", err)
|
|
}
|
|
if gotPath != "/v1/audio/speech" {
|
|
t.Fatalf("unexpected path: %s", gotPath)
|
|
}
|
|
if gotBody["model"] != "speech-2.8-hd" || gotBody["input"] != "今天想去上海走一走。" || gotBody["response_format"] != "mp3" {
|
|
t.Fatalf("unexpected body: %#v", gotBody)
|
|
}
|
|
if len(refs) != 1 || refs[0].Kind != "data_url" || refs[0].ContentType != "audio/mpeg" {
|
|
t.Fatalf("unexpected refs: %#v", refs)
|
|
}
|
|
}
|
|
|
|
func TestDecodeSpeechGenerationResponseAcceptsNumericErrorCode(t *testing.T) {
|
|
_, _, err := decodeSpeechGenerationResponse(http.StatusBadRequest, "application/json", "mp3", []byte(`{
|
|
"error": {
|
|
"message": "minimax TTS error: 1004 - auth failed",
|
|
"type": "bad_response",
|
|
"param": "",
|
|
"code": 1004
|
|
}
|
|
}`), "speech-2.8-hd")
|
|
if err == nil || !strings.Contains(err.Error(), "auth failed") {
|
|
t.Fatalf("expected decoded upstream error, got %v", err)
|
|
}
|
|
}
|
|
|