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.
81 lines
2.2 KiB
81 lines
2.2 KiB
package server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestGenerateMusicRefsUsesMusicGenerationsEndpoint(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", "application/json")
|
|
_, _ = w.Write([]byte(`{
|
|
"audio_url": "https://example.com/music-output.mp3",
|
|
"status": 2,
|
|
"extra_info": {
|
|
"music_duration": 34,
|
|
"music_sample_rate": 44100,
|
|
"music_channel": 2,
|
|
"bitrate": 256000,
|
|
"music_size": 1098543
|
|
}
|
|
}`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
client := &newAPIClient{
|
|
baseURL: strings.TrimRight(srv.URL, "/"),
|
|
httpClient: srv.Client(),
|
|
}
|
|
|
|
refs, usage, err := client.generateMusicRefs(context.Background(), "sk-test", "music-2.6", map[string]any{
|
|
"prompt": "lofi rainy night piano",
|
|
"is_instrumental": true,
|
|
"output_format": "url",
|
|
"audio_setting": map[string]any{
|
|
"format": "mp3",
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("generateMusicRefs: %v", err)
|
|
}
|
|
if gotPath != "/v1/music/generations" {
|
|
t.Fatalf("unexpected path: %s", gotPath)
|
|
}
|
|
if gotBody["model"] != "music-2.6" {
|
|
t.Fatalf("unexpected model: %#v", gotBody["model"])
|
|
}
|
|
if gotBody["is_instrumental"] != true {
|
|
t.Fatalf("unexpected is_instrumental: %#v", gotBody["is_instrumental"])
|
|
}
|
|
if len(refs) != 1 || refs[0].URL != "https://example.com/music-output.mp3" || refs[0].ContentType != "audio/mpeg" {
|
|
t.Fatalf("unexpected refs: %#v", refs)
|
|
}
|
|
if usage["music_duration"] != float64(34) {
|
|
t.Fatalf("unexpected usage: %#v", usage)
|
|
}
|
|
}
|
|
|
|
func TestDecodeMusicGenerationResponseAcceptsNumericErrorCode(t *testing.T) {
|
|
_, _, err := decodeMusicGenerationResponse(http.StatusBadRequest, "application/json", []byte(`{
|
|
"error": {
|
|
"message": "minimax music error: 1004 - auth failed",
|
|
"type": "bad_response",
|
|
"param": "",
|
|
"code": 1004
|
|
}
|
|
}`), "music-2.6")
|
|
if err == nil || !strings.Contains(err.Error(), "auth failed") {
|
|
t.Fatalf("expected decoded upstream error, got %v", err)
|
|
}
|
|
}
|
|
|