sonda/internal/api/http/radio_test.go
tgiachi 407b20334a feat: radio stations that Sonda makes by itself
Stations made from the music, random and without end, with nothing to set up: the
mixes the listener has (genres, favourites, rediscover, new to you), everything,
one for each decade that has 30 songs by 4 artists, and the radio of an artist (their
songs, four times as likely, and the songs of the two genres most of theirs have).
Every time a station is asked for it picks other songs, with the same weights as the
mixes (taste, hearts, nothing skipped lately).

- mixes.Service.Radios and Radio; Filter has All and years, the store has Decades
- GET /radio and GET /radio/{id}, in the OpenAPI spec
- OpenSubsonic: each station is a playlist (rd-<id>, "Radio · Rock") that has other
  songs every time it is opened, for apps that cannot play a radio that never ends
2026-11-16 12:13:55 +01:00

138 lines
4.4 KiB
Go

package httpapi
import (
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"slices"
"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)
}
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
}