package qobuz import ( "context" "encoding/json" "net/http" "net/http/httptest" "testing" "streamrip-go/internal/config" ) func TestQualityMap(t *testing.T) { tests := []struct { in int want int }{ {1, 5}, {2, 6}, {3, 7}, {4, 27}, {0, 7}, {99, 7}, } for _, tt := range tests { got := qualityMap(tt.in) if got != tt.want { t.Fatalf("qualityMap(%d)=%d want %d", tt.in, got, tt.want) } } } func TestParseTrackMetadata(t *testing.T) { resp := map[string]any{ "id": "19512574", "title": "Dreams", "version": "Remastered", "track_number": float64(2), "media_number": float64(1), "parental_warning": false, "maximum_bit_depth": float64(24), "maximum_sampling_rate": float64(96), "performer": map[string]any{ "name": "Fleetwood Mac", }, "album": map[string]any{ "title": "Rumours", }, } m, err := ParseTrackMetadata(resp) if err != nil { t.Fatalf("ParseTrackMetadata() error = %v", err) } if m.ID != "19512574" || m.Title != "Dreams" || m.Album != "Rumours" || m.Artist != "Fleetwood Mac" { t.Fatalf("unexpected metadata: %+v", m) } if m.Quality != 3 { t.Fatalf("quality = %d, want 3", m.Quality) } } func TestGetPlaylistPagination(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { offset := r.URL.Query().Get("offset") if offset == "" { offset = "0" } resp := map[string]any{} switch offset { case "0": resp = map[string]any{ "tracks_count": 1200, "tracks": map[string]any{"items": makeItems(0, 500)}, } case "500": resp = map[string]any{"tracks": map[string]any{"items": makeItems(500, 1000)}} case "1000": resp = map[string]any{"tracks": map[string]any{"items": makeItems(1000, 1200)}} default: w.WriteHeader(http.StatusNotFound) _ = json.NewEncoder(w).Encode(map[string]any{"message": "not found"}) return } _ = json.NewEncoder(w).Encode(resp) })) defer ts.Close() c := newTestClient(t) c.loggedIn = true c.baseURL = ts.URL raw, err := c.GetMetadata(context.Background(), "playlist-id", "playlist") if err != nil { t.Fatalf("GetMetadata() error = %v", err) } tracks, ok := mapValue(raw["tracks"]) if !ok { t.Fatalf("tracks missing") } items, ok := tracks["items"].([]any) if !ok { t.Fatalf("items missing") } if len(items) != 1200 { t.Fatalf("len(items) = %d, want 1200", len(items)) } } func TestGetLabelPagination(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { offset := r.URL.Query().Get("offset") if offset == "" { offset = "0" } resp := map[string]any{} switch offset { case "0": resp = map[string]any{ "albums_count": 700, "albums": map[string]any{"items": makeItems(0, 500)}, } case "500": resp = map[string]any{"albums": map[string]any{"items": makeItems(500, 700)}} default: w.WriteHeader(http.StatusNotFound) _ = json.NewEncoder(w).Encode(map[string]any{"message": "not found"}) return } _ = json.NewEncoder(w).Encode(resp) })) defer ts.Close() c := newTestClient(t) c.loggedIn = true c.baseURL = ts.URL raw, err := c.GetMetadata(context.Background(), "label-id", "label") if err != nil { t.Fatalf("GetMetadata() error = %v", err) } albums, ok := mapValue(raw["albums"]) if !ok { t.Fatalf("albums missing") } items, ok := albums["items"].([]any) if !ok { t.Fatalf("items missing") } if len(items) != 700 { t.Fatalf("len(items) = %d, want 700", len(items)) } } func TestGetArtistPagination(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { offset := r.URL.Query().Get("offset") if offset == "" { offset = "0" } resp := map[string]any{} switch offset { case "0": resp = map[string]any{ "albums_count": 620, "albums": map[string]any{"items": makeItems(0, 500)}, } case "500": resp = map[string]any{"albums": map[string]any{"items": makeItems(500, 620)}} default: w.WriteHeader(http.StatusNotFound) _ = json.NewEncoder(w).Encode(map[string]any{"message": "not found"}) return } _ = json.NewEncoder(w).Encode(resp) })) defer ts.Close() c := newTestClient(t) c.loggedIn = true c.baseURL = ts.URL raw, err := c.GetMetadata(context.Background(), "artist-id", "artist") if err != nil { t.Fatalf("GetMetadata() error = %v", err) } albums, ok := mapValue(raw["albums"]) if !ok { t.Fatalf("albums missing") } items, ok := albums["items"].([]any) if !ok { t.Fatalf("items missing") } if len(items) != 620 { t.Fatalf("len(items) = %d, want 620", len(items)) } } func newTestClient(t *testing.T) *Client { t.Helper() d := config.DefaultConfigData() d.Qobuz.AppID = "12345" cfg := &config.Config{File: d, Session: d} return New(cfg) } func makeItems(start, end int) []map[string]any { items := make([]map[string]any, 0, end-start) for i := start; i < end; i++ { items = append(items, map[string]any{"id": i}) } return items }