Files
streamrip-go/internal/provider/tidal/client_test.go
Joren b2688ce949 add CLI parity flags and expand provider support
This brings the Go CLI closer to upstream behavior with global flag handling and clearer resolve failures, while adding Tidal video downloads plus initial Deezer and SoundCloud no-account flows for broader end-to-end coverage.
2026-04-20 00:56:10 +02:00

101 lines
3.0 KiB
Go

package tidal
import (
"context"
"encoding/base64"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"streamrip-go/internal/config"
)
func TestLoginMissingToken(t *testing.T) {
cfgData := config.DefaultConfigData()
cfgData.Tidal.AccessToken = ""
c := New(&config.Config{File: cfgData, Session: cfgData})
err := c.Login(context.Background())
if err == nil {
t.Fatalf("expected error")
}
}
func TestSearch(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/search/albums":
_ = json.NewEncoder(w).Encode(map[string]any{"items": []any{map[string]any{"id": 1, "title": "x"}}})
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)
}
pages, err := c.Search(context.Background(), "album", "x", 10)
if err != nil {
t.Fatalf("search err = %v", err)
}
if len(pages) != 1 {
t.Fatalf("pages = %d", len(pages))
}
}
func TestGetVideoDownloadable(t *testing.T) {
var server *httptest.Server
server = 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/videos/42/playbackinfopostpaywall":
manifest := map[string]any{"urls": []string{server.URL + "/master.m3u8"}}
b, _ := json.Marshal(manifest)
_ = json.NewEncoder(w).Encode(map[string]any{"manifest": base64.StdEncoding.EncodeToString(b)})
case "/master.m3u8":
_, _ = w.Write([]byte("#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=1000,CODECS=\"avc1.42E01E,mp4a.40.2\",RESOLUTION=640x360\nlow/stream.m3u8\n#EXT-X-STREAM-INF:BANDWIDTH=2000,CODECS=\"avc1.4D401F,mp4a.40.2\",RESOLUTION=1280x720\nhi/stream.m3u8\n"))
default:
w.WriteHeader(http.StatusNotFound)
}
}))
defer server.Close()
cfgData := config.DefaultConfigData()
cfgData.Tidal.AccessToken = "token"
cfgData.Tidal.CountryCode = "US"
c := New(&config.Config{File: cfgData, Session: cfgData})
c.baseURL = server.URL + "/v1"
if err := c.Login(context.Background()); err != nil {
t.Fatalf("login err = %v", err)
}
d, err := c.GetVideoDownloadable(context.Background(), "42")
if err != nil {
t.Fatalf("GetVideoDownloadable() err = %v", err)
}
if d.Extension != "mp4" {
t.Fatalf("extension = %q, want mp4", d.Extension)
}
if d.URL != server.URL+"/hi/stream.m3u8" {
t.Fatalf("url = %q, want %q", d.URL, server.URL+"/hi/stream.m3u8")
}
}
func TestBestHLSVariantURLFallsBackToMaster(t *testing.T) {
master := "https://example.com/master.m3u8"
got := bestHLSVariantURL(master, "#EXTM3U\n#comment")
if got != master {
t.Fatalf("url = %q, want %q", got, master)
}
}