mirror of
https://git.sr.ht/~joren/streamrip-go
synced 2026-06-17 15:05:39 +02:00
53 lines
1.3 KiB
Go
53 lines
1.3 KiB
Go
package tidal
|
|
|
|
import (
|
|
"context"
|
|
"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))
|
|
}
|
|
}
|