sonda/internal/api/http/radio_test.go
tgiachi ed32aac228 feat: the radio stations are in the apps only when the person asks
A new setting, radio_in_apps, off until the person turns it on (Settings, Apps and
devices, or PATCH /me/settings). With it off, the list of playlists of an OpenSubsonic
app is only the playlists of the person, and a station asked for by id is not found.
With it on, the stations come as playlists called Radio · Rock.

- migration 0036: user_settings.radio_in_apps
- usersettings, the settings API and the OpenAPI spec have the new field; the PATCH
  now keeps each setting apart (naming one no longer sets the other)
- Settings has a switch
2026-11-16 12:33:09 +01:00

149 lines
4.9 KiB
Go

package httpapi
import (
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"slices"
"strings"
"testing"
)
func (m *mixAPI) radio(t *testing.T, id string) resp {
t.Helper()
return do(t, m.h, "GET", "/api/v1/radio/"+url.PathEscape(id), m.alice, nil)
}
func TestRadioNeedsASignedInUser(t *testing.T) {
m := newMixAPI(t)
for _, path := range []string{"/api/v1/radio", "/api/v1/radio/all"} {
if r := do(t, m.h, "GET", path, "", nil); r.Code != http.StatusUnauthorized {
t.Errorf("%s: %d", path, r.Code)
}
}
}
func TestTheRadioHasStationsThatNobodyMade(t *testing.T) {
m := newMixAPI(t)
r := do(t, m.h, "GET", "/api/v1/radio", m.alice, nil)
if r.Code != http.StatusOK {
t.Fatalf("%d %v", r.Code, r.Body)
}
var ids []string
for _, x := range r.Body["items"].([]any) {
s := x.(map[string]any)
sameKeys(t, "station", s, "id", "kind", "title", "genre", "decade", "artists", "album_ids")
ids = append(ids, s["id"].(string))
}
for _, want := range []string{"all", "genre:rock", "genre:hard rock", "unheard"} {
if !slices.Contains(ids, want) {
t.Errorf("no %s in %v", want, ids)
}
}
}
func TestEveryRoundOfAStationIsOtherSongs(t *testing.T) {
m := newMixAPI(t)
a := do(t, m.h, "GET", "/api/v1/radio/all", m.alice, nil)
if a.Code != http.StatusOK || a.Body["id"] != "all" || a.Body["kind"] != "all" {
t.Fatalf("%d %v", a.Code, a.Body)
}
first := trackIDs(t, a)
if len(first) != 50 {
t.Fatalf("%d songs", len(first))
}
if _, has := a.Body["variant"]; has {
t.Fatal("a radio has no variant")
}
same := 0
for i := 0; i < 3; i++ {
if slices.Equal(first, trackIDs(t, do(t, m.h, "GET", "/api/v1/radio/all", m.alice, nil))) {
same++
}
}
if same == 3 {
t.Fatal("a radio that plays the same songs every time")
}
if r := do(t, m.h, "GET", "/api/v1/radio/"+url.PathEscape("genre:hard rock"), m.alice, nil); r.Code != http.StatusOK || len(trackIDs(t, r)) != 36 { // all it has
t.Fatalf("hard rock: %d %d songs", r.Code, len(trackIDs(t, r)))
}
}
func TestTheRadioOfAnArtist(t *testing.T) {
m := newMixAPI(t)
var id int64
if err := m.db.QueryRow("SELECT id FROM artists WHERE name = 'rock artist 0'").Scan(&id); err != nil {
t.Fatal(err)
}
r := do(t, m.h, "GET", "/api/v1/radio/artist:"+itoa(id), m.alice, nil)
if r.Code != http.StatusOK || r.Body["kind"] != "artist" || r.Body["title"] != "rock artist 0" {
t.Fatalf("%d %v", r.Code, r.Body)
}
if len(trackIDs(t, r)) != 40 { // the 40 rock songs: the genre of the artist
t.Fatalf("%d songs", len(trackIDs(t, r)))
}
}
func TestAStationThatIsNotThereIsNotFound(t *testing.T) {
m := newMixAPI(t)
for _, id := range []string{"nope", "genre:polka", "artist:999999", "artist:x", "decade:1850"} {
if r := do(t, m.h, "GET", "/api/v1/radio/"+url.PathEscape(id), m.alice, nil); r.Code != http.StatusNotFound || errCode(r) != "station_not_found" {
t.Errorf("%s: %d %v", id, r.Code, r.Body)
}
}
}
func TestAnAppSeesTheStationsAsPlaylistsWithOtherSongsEveryTime(t *testing.T) {
m := newMixAPI(t)
secret := do(t, m.h, "POST", "/api/v1/me/devices", m.alice, map[string]any{"name": "Feishin"}).Body["secret"].(string)
rest := func(path string, q url.Values) map[string]any {
q.Set("apiKey", secret)
q.Set("f", "json")
return restCall(t, m.h, path, q)
}
// The stations are not in the apps until the person asks.
before, _ := rest("getPlaylists", url.Values{})["playlists"].(map[string]any)["playlist"].([]any) // none at all: the key is absent
for _, p := range before {
if strings.HasPrefix(p.(map[string]any)["id"].(string), "rd-") {
t.Fatalf("a station in the apps before it was asked for: %v", p)
}
}
if r := do(t, m.h, "PATCH", "/api/v1/me/settings", m.alice, map[string]any{"radio_in_apps": true}); r.Code != http.StatusOK {
t.Fatalf("setting: %d %v", r.Code, r.Body)
}
var found bool
for _, p := range rest("getPlaylists", url.Values{})["playlists"].(map[string]any)["playlist"].([]any) {
if p.(map[string]any)["id"] == "rd-all" {
found = p.(map[string]any)["name"] == "Radio · Everything"
}
}
if !found {
t.Fatal("no Radio · Everything in the playlists")
}
songs := func() []string {
var out []string
for _, e := range rest("getPlaylist", url.Values{"id": {"rd-all"}})["playlist"].(map[string]any)["entry"].([]any) {
out = append(out, e.(map[string]any)["id"].(string))
}
return out
}
a, b := songs(), songs()
if len(a) != 50 || slices.Equal(a, b) {
t.Fatalf("%d songs, and the same twice: %v", len(a), slices.Equal(a, b))
}
}
// restCall asks /rest and returns what is inside subsonic-response, failing when the call failed.
func restCall(t *testing.T, h http.Handler, path string, q url.Values) map[string]any {
t.Helper()
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest("GET", "/rest/"+path+"?"+q.Encode(), nil))
var out struct {
R map[string]any `json:"subsonic-response"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil || out.R["status"] != "ok" {
t.Fatalf("%s: %v %s", path, err, rec.Body.String())
}
return out.R
}