mirror of
https://git.sr.ht/~joren/streamrip-go
synced 2026-06-17 15:05:39 +02:00
harden deezer quality fallback and metadata handling
Improve Deezer full-quality mode behavior by returning explicit errors when yt-dlp mode fails with fallback disabled, parse structured API errors, and correctly map explicit_lyrics booleans into explicit tags.
This commit is contained in:
@@ -3,8 +3,10 @@ package deezer
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"streamrip-go/internal/config"
|
||||
@@ -64,3 +66,87 @@ func TestGetDownloadableUsesPreview(t *testing.T) {
|
||||
t.Fatalf("unexpected downloadable: %+v", d)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetMetadataSetsExplicitFromBool(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/track/9":
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||
"id": 9,
|
||||
"title": "X",
|
||||
"explicit_lyrics": true,
|
||||
"artist": map[string]any{"name": "Artist"},
|
||||
})
|
||||
default:
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
cfgData := config.DefaultConfigData()
|
||||
c := New(&config.Config{File: cfgData, Session: cfgData})
|
||||
c.loggedIn = true
|
||||
orig := baseURL
|
||||
baseURL = ts.URL
|
||||
defer func() { baseURL = orig }()
|
||||
|
||||
meta, err := c.GetMetadata(context.Background(), "9", "track")
|
||||
if err != nil {
|
||||
t.Fatalf("GetMetadata() error = %v", err)
|
||||
}
|
||||
if explicit, _ := meta["explicit"].(bool); !explicit {
|
||||
t.Fatalf("expected explicit=true, got %#v", meta["explicit"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchReturnsStructuredAPIError(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/search/track" {
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"error": map[string]any{"message": "invalid query"}})
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
cfgData := config.DefaultConfigData()
|
||||
c := New(&config.Config{File: cfgData, Session: cfgData})
|
||||
c.loggedIn = true
|
||||
orig := baseURL
|
||||
baseURL = ts.URL
|
||||
defer func() { baseURL = orig }()
|
||||
|
||||
_, err := c.Search(context.Background(), "track", "", 5)
|
||||
if err == nil || !strings.Contains(err.Error(), "invalid query") {
|
||||
t.Fatalf("expected structured deezer error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetDownloadableErrorsWhenFullQualityFailsAndFallbackDisabled(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/track/42" {
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"id": 42, "title": "X", "preview": "https://cdn.example/p.mp3"})
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
cfgData := config.DefaultConfigData()
|
||||
cfgData.Deezer.UseDeezloader = true
|
||||
cfgData.Deezer.LowerQualityIfNotAvailable = false
|
||||
c := New(&config.Config{File: cfgData, Session: cfgData})
|
||||
c.loggedIn = true
|
||||
c.bin = "definitely-not-a-real-yt-dlp-bin"
|
||||
c.run = func(context.Context, string, ...string) ([]byte, error) {
|
||||
return nil, fmt.Errorf("unexpected run call")
|
||||
}
|
||||
orig := baseURL
|
||||
baseURL = ts.URL
|
||||
defer func() { baseURL = orig }()
|
||||
|
||||
_, err := c.GetDownloadable(context.Background(), "42", 2)
|
||||
if err == nil || !strings.Contains(err.Error(), "full-quality mode failed") {
|
||||
t.Fatalf("expected full-quality failure error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user