fix tidal playlist metadata and retries

This commit is contained in:
2026-05-21 23:03:27 +02:00
parent 3bc965db77
commit fa39582849
4 changed files with 249 additions and 66 deletions

View File

@@ -6,6 +6,7 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"strconv"
"testing"
@@ -238,6 +239,83 @@ func TestGetMetadataTrackIgnoresLyricsEndpointFailure(t *testing.T) {
}
}
func TestAPIRequestRetriesTooManyRequests(t *testing.T) {
calls := 0
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v1/tracks/42" {
w.WriteHeader(http.StatusNotFound)
return
}
calls++
if calls == 1 {
w.Header().Set("Retry-After", "0")
w.WriteHeader(http.StatusTooManyRequests)
_, _ = w.Write([]byte("slow down"))
return
}
_ = json.NewEncoder(w).Encode(map[string]any{"id": 42, "title": "Song"})
}))
defer ts.Close()
cfgData := config.DefaultConfigData()
cfgData.Downloads.RequestsPerMinute = 0
cfgData.Tidal.AccessToken = "token"
cfgData.Tidal.CountryCode = "US"
c := New(&config.Config{File: cfgData, Session: cfgData})
c.baseURL = ts.URL + "/v1"
resp, status, err := c.apiRequest(context.Background(), "tracks/42", nil, c.baseURL)
if err != nil {
t.Fatalf("apiRequest() err = %v", err)
}
if status != http.StatusOK {
t.Fatalf("status = %d, want %d", status, http.StatusOK)
}
if calls != 2 {
t.Fatalf("calls = %d, want 2", calls)
}
if stringify(resp["title"]) != "Song" {
t.Fatalf("title = %q, want Song", stringify(resp["title"]))
}
}
func TestAPIPostRetriesTooManyRequests(t *testing.T) {
calls := 0
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/token" {
w.WriteHeader(http.StatusNotFound)
return
}
calls++
if calls == 1 {
w.Header().Set("Retry-After", "0")
w.WriteHeader(http.StatusTooManyRequests)
_, _ = w.Write([]byte("slow down"))
return
}
_ = json.NewEncoder(w).Encode(map[string]any{"access_token": "fresh-token"})
}))
defer ts.Close()
cfgData := config.DefaultConfigData()
cfgData.Downloads.RequestsPerMinute = 0
c := New(&config.Config{File: cfgData, Session: cfgData})
resp, status, err := c.apiPost(context.Background(), ts.URL+"/token", url.Values{"grant_type": []string{"refresh_token"}}, false)
if err != nil {
t.Fatalf("apiPost() err = %v", err)
}
if status != http.StatusOK {
t.Fatalf("status = %d, want %d", status, http.StatusOK)
}
if calls != 2 {
t.Fatalf("calls = %d, want 2", calls)
}
if stringify(resp["access_token"]) != "fresh-token" {
t.Fatalf("access_token = %q, want fresh-token", stringify(resp["access_token"]))
}
}
func TestGetDownloadablePrefersAtmosWhenEnabled(t *testing.T) {
var calls []string
allImmersive := true