paginate tidal artist album fetches across all pages

This commit is contained in:
2026-04-21 17:33:25 +02:00
parent 654bed17e2
commit 6dbf6a222d
2 changed files with 101 additions and 21 deletions

View File

@@ -6,6 +6,7 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"streamrip-go/internal/config"
@@ -98,3 +99,64 @@ func TestBestHLSVariantURLFallsBackToMaster(t *testing.T) {
t.Fatalf("url = %q, want %q", got, master)
}
}
func TestGetMetadataArtistPaginatesAlbums(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/v1/sessions":
_ = json.NewEncoder(w).Encode(map[string]any{"countryCode": "US", "userId": 123})
case "/v1/artists/9":
_ = json.NewEncoder(w).Encode(map[string]any{"id": 9, "name": "Artist X"})
case "/v1/artists/9/albums":
offset, _ := strconv.Atoi(r.URL.Query().Get("offset"))
filter := r.URL.Query().Get("filter")
if filter == "" {
if offset == 0 {
items := make([]any, 0, 100)
for i := 0; i < 100; i++ {
items = append(items, map[string]any{"id": i + 1})
}
_ = json.NewEncoder(w).Encode(map[string]any{"items": items})
return
}
if offset == 100 {
_ = json.NewEncoder(w).Encode(map[string]any{"items": []any{map[string]any{"id": 101}}})
return
}
_ = json.NewEncoder(w).Encode(map[string]any{"items": []any{}})
return
}
if filter == "EPSANDSINGLES" {
if offset == 0 {
_ = json.NewEncoder(w).Encode(map[string]any{"items": []any{map[string]any{"id": 101}, map[string]any{"id": 102}}})
return
}
_ = json.NewEncoder(w).Encode(map[string]any{"items": []any{}})
return
}
w.WriteHeader(http.StatusBadRequest)
default:
w.WriteHeader(http.StatusNotFound)
}
}))
defer ts.Close()
cfgData := config.DefaultConfigData()
cfgData.Tidal.AccessToken = "token"
cfgData.Tidal.CountryCode = "US"
c := New(&config.Config{File: cfgData, Session: cfgData})
c.baseURL = ts.URL + "/v1"
if err := c.Login(context.Background()); err != nil {
t.Fatalf("login err = %v", err)
}
meta, err := c.GetMetadata(context.Background(), "9", "artist")
if err != nil {
t.Fatalf("GetMetadata() err = %v", err)
}
albumsObj, _ := meta["albums"].(map[string]any)
items, _ := albumsObj["items"].([]map[string]any)
if len(items) != 102 {
t.Fatalf("albums len = %d, want 102", len(items))
}
}