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
765 lines
24 KiB
Go
765 lines
24 KiB
Go
package mixes
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"math"
|
|
"slices"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.kmalloc.org/squid/sonda/internal/catalog"
|
|
"git.kmalloc.org/squid/sonda/internal/taste"
|
|
)
|
|
|
|
var day = time.Date(2026, 11, 12, 10, 0, 0, 0, time.UTC)
|
|
|
|
// ---- the normal spelling of a genre ----
|
|
|
|
func TestNormalizeGenreMergesSpellingsAndDropsWhatIsNotAGenre(t *testing.T) {
|
|
for in, want := range map[string]string{
|
|
"rock": "rock",
|
|
" Hard Rock ": "hard rock",
|
|
"jazz+funk": "jazz funk",
|
|
"jazz-funk": "jazz funk",
|
|
"jazz / funk": "jazz funk",
|
|
"hip_hop": "hip hop",
|
|
"trash metal": "thrash metal",
|
|
"r&b": "r&b",
|
|
"other": "",
|
|
"Unknown": "",
|
|
"": "",
|
|
" ": "",
|
|
"(none)": "",
|
|
"genre": "",
|
|
"blues rock": "blues rock",
|
|
} {
|
|
if got := normalizeGenre(in); got != want {
|
|
t.Errorf("normalizeGenre(%q) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// ---- a store in memory ----
|
|
|
|
type fakeStore struct {
|
|
genres []Genre
|
|
artists map[string][]string // raw genre -> top artists
|
|
liked map[int64]map[string]int // artist -> raw genre -> songs
|
|
pool map[string][]TrackRef // key: "genre:<raw>" or "artists"
|
|
byArtist []TrackRef // tracks for an artist filter
|
|
history map[int64]History
|
|
names map[int64]string
|
|
asked []Filter
|
|
covers map[string][]int64 // key: the first genre spelling or "artists"; the albums to give
|
|
coverAsk []Filter
|
|
hearts map[int64]Heart
|
|
heartsErr error
|
|
// What the radio asks of the store.
|
|
decades []Decade
|
|
decadePool map[int][]TrackRef // by the first year
|
|
everything []TrackRef
|
|
}
|
|
|
|
func (f *fakeStore) Decades(context.Context) ([]Decade, error) { return f.decades, nil }
|
|
|
|
func (f *fakeStore) Hearts(context.Context, int64) (map[int64]Heart, error) {
|
|
return f.hearts, f.heartsErr
|
|
}
|
|
|
|
func (f *fakeStore) Covers(_ context.Context, fl Filter, limit int) ([]int64, error) {
|
|
f.coverAsk = append(f.coverAsk, fl)
|
|
key := "artists"
|
|
if len(fl.Genres) > 0 {
|
|
key = fl.Genres[0]
|
|
}
|
|
out := f.covers[key]
|
|
if len(out) > limit {
|
|
out = out[:limit]
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (f *fakeStore) Genres(context.Context) ([]Genre, error) { return f.genres, nil }
|
|
|
|
func (f *fakeStore) GenreArtists(_ context.Context, raws []string, limit int) ([]string, error) {
|
|
var out []string
|
|
for _, r := range raws {
|
|
out = append(out, f.artists[r]...)
|
|
}
|
|
if len(out) > limit {
|
|
out = out[:limit]
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (f *fakeStore) ArtistGenres(_ context.Context, ids []int64) (map[int64]map[string]int, error) {
|
|
out := map[int64]map[string]int{}
|
|
for _, id := range ids {
|
|
if g, ok := f.liked[id]; ok {
|
|
out[id] = g
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (f *fakeStore) Pool(_ context.Context, fl Filter) ([]TrackRef, error) {
|
|
f.asked = append(f.asked, fl)
|
|
if fl.All {
|
|
return f.everything, nil
|
|
}
|
|
if fl.MinYear > 0 {
|
|
return f.decadePool[fl.MinYear], nil
|
|
}
|
|
var out []TrackRef
|
|
for _, g := range fl.Genres {
|
|
out = append(out, f.pool[g]...)
|
|
}
|
|
for _, t := range f.byArtist {
|
|
if slices.Contains(fl.ArtistIDs, t.ArtistID) {
|
|
out = append(out, t)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (f *fakeStore) History(context.Context, int64, time.Duration) (map[int64]History, error) {
|
|
return f.history, nil
|
|
}
|
|
|
|
func (f *fakeStore) ArtistNames(_ context.Context, ids []int64) (map[int64]string, error) {
|
|
out := map[int64]string{}
|
|
for _, id := range ids {
|
|
out[id] = f.names[id]
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (f *fakeStore) Tracks(_ context.Context, ids []int64) ([]catalog.Track, error) {
|
|
out := make([]catalog.Track, len(ids))
|
|
for i, id := range ids {
|
|
out[i] = catalog.Track{ID: id}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
type fakeTaster struct{ l taste.Learned }
|
|
|
|
func (f fakeTaster) Learn(context.Context, int64) (taste.Learned, error) { return f.l, nil }
|
|
|
|
// songs makes n songs by the artists in turn; ids start at first.
|
|
func songs(first int64, n int, artists ...int64) []TrackRef {
|
|
out := make([]TrackRef, n)
|
|
for i := range out {
|
|
out[i] = TrackRef{TrackID: first + int64(i), ArtistID: artists[i%len(artists)]}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func pref(id int64, score float64, last time.Time) taste.ArtistPref {
|
|
return taste.ArtistPref{ArtistID: id, Score: score, Preference: score / (1 + score), Listens: 5, LastListened: last}
|
|
}
|
|
|
|
func learned(prefs ...taste.ArtistPref) taste.Learned {
|
|
l := taste.Learned{Prefs: map[int64]taste.ArtistPref{}}
|
|
for _, p := range prefs {
|
|
l.Prefs[p.ArtistID] = p
|
|
if p.Score > 0 {
|
|
l.Liked = append(l.Liked, p)
|
|
}
|
|
}
|
|
slices.SortFunc(l.Liked, func(a, b taste.ArtistPref) int {
|
|
if a.Score > b.Score {
|
|
return -1
|
|
}
|
|
return 1
|
|
})
|
|
return l
|
|
}
|
|
|
|
func newService(st *fakeStore, l taste.Learned) *Service {
|
|
s := NewService(st, fakeTaster{l}, 20*time.Second)
|
|
s.now = func() time.Time { return day }
|
|
return s
|
|
}
|
|
|
|
// rockArtists are 30 artists, ids 1 to 30.
|
|
func rockArtists() []int64 {
|
|
out := make([]int64, 30)
|
|
for i := range out {
|
|
out[i] = int64(i + 1)
|
|
}
|
|
return out
|
|
}
|
|
|
|
func library() *fakeStore {
|
|
return &fakeStore{
|
|
genres: []Genre{
|
|
{"rock", 400, 40}, {"metal", 300, 30}, {"jazz+funk", 20, 5}, {"jazz-funk", 15, 4},
|
|
{"other", 900, 80}, {"tiny", 10, 2}, {"few artists", 200, 2}, {"pop", 100, 12},
|
|
},
|
|
artists: map[string][]string{"rock": {"Queen", "AC/DC", "Kiss"}, "metal": {"Metallica", "Slayer"}, "pop": {"ABBA"}},
|
|
pool: map[string][]TrackRef{
|
|
"rock": songs(1, 400, rockArtists()...),
|
|
"metal": songs(1000, 300, 11, 12, 13, 14, 15),
|
|
"pop": songs(2000, 100, 21, 22, 23, 24, 25),
|
|
"jazz+funk": songs(3000, 20, 31, 32, 33, 34, 35),
|
|
"jazz-funk": songs(3100, 15, 31, 32, 33, 34),
|
|
},
|
|
names: map[int64]string{1: "Queen", 2: "AC/DC", 11: "Metallica", 12: "Slayer", 21: "ABBA"},
|
|
}
|
|
}
|
|
|
|
func ids(m []catalog.Track) []int64 {
|
|
out := make([]int64, len(m))
|
|
for i, t := range m {
|
|
out[i] = t.ID
|
|
}
|
|
return out
|
|
}
|
|
|
|
// ---- the list of mixes ----
|
|
|
|
func TestGenreMixesAreMadeForTheGenresThatAreBigEnoughAndSpellingsAreOne(t *testing.T) {
|
|
s := newService(library(), taste.Learned{})
|
|
list, err := s.List(context.Background(), 1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var got []string
|
|
var rock Mix
|
|
for _, m := range list {
|
|
switch m.Kind {
|
|
case KindGenre:
|
|
got = append(got, m.ID)
|
|
if m.Genre == "rock" {
|
|
rock = m
|
|
}
|
|
case KindUnheard: // needs no history
|
|
default:
|
|
t.Errorf("without listening only genre mixes and New to you exist, got %+v", m)
|
|
}
|
|
}
|
|
want := []string{"genre:rock", "genre:metal", "genre:pop", "genre:jazz funk"}
|
|
if !slices.Equal(got, want) {
|
|
t.Errorf("mixes = %v, want %v: by size, jazz+funk and jazz-funk as one, no other/tiny/few artists", got, want)
|
|
}
|
|
if rock.Title != "Rock Mix" || rock.Genre != "rock" || !slices.Equal(rock.Artists, []string{"Queen", "AC/DC", "Kiss"}) {
|
|
t.Errorf("rock = %+v", rock)
|
|
}
|
|
}
|
|
|
|
func TestTheGenresTheListenerPlaysComeFirst(t *testing.T) {
|
|
st := library()
|
|
// Artist 11 (metal) is loved, and plays a bit of rock too.
|
|
st.liked = map[int64]map[string]int{11: {"metal": 9, "rock": 1}}
|
|
s := newService(st, learned(pref(11, 5, day.Add(-24*time.Hour))))
|
|
list, _ := s.List(context.Background(), 1)
|
|
var genres []string
|
|
for _, m := range list {
|
|
if m.Kind == KindGenre {
|
|
genres = append(genres, m.Genre)
|
|
}
|
|
}
|
|
if len(genres) < 2 || genres[0] != "metal" || genres[1] != "rock" {
|
|
t.Errorf("genres = %v, want metal then rock first", genres)
|
|
}
|
|
}
|
|
|
|
func TestMixesMadeForYouNeedHistory(t *testing.T) {
|
|
st := library()
|
|
none, _ := newService(st, taste.Learned{}).List(context.Background(), 1)
|
|
for _, m := range none {
|
|
if m.Kind == KindFavourites || m.Kind == KindRediscover {
|
|
t.Errorf("%s is offered without any listening", m.ID)
|
|
}
|
|
}
|
|
if !slices.ContainsFunc(none, func(m Mix) bool { return m.Kind == KindUnheard }) {
|
|
t.Error("New to you needs no history: it is the songs never played")
|
|
}
|
|
|
|
recent := day.Add(-2 * 24 * time.Hour)
|
|
old := day.Add(-60 * 24 * time.Hour)
|
|
two := learned(pref(1, 5, recent), pref(2, 4, recent))
|
|
if list, _ := newService(st, two).List(context.Background(), 1); slices.ContainsFunc(list, func(m Mix) bool { return m.Kind == KindFavourites }) {
|
|
t.Error("two liked artists are not enough for favourites")
|
|
}
|
|
many := learned(pref(1, 5, recent), pref(2, 4, recent), pref(3, 3, old), pref(4, 2, old), pref(5, 1.5, old))
|
|
list, _ := newService(st, many).List(context.Background(), 1)
|
|
if list[0].ID != "favourites" || list[1].ID != "rediscover" {
|
|
t.Errorf("first mixes = %s, %s: the ones made for you come first", list[0].ID, list[1].ID)
|
|
}
|
|
if list[0].Title != "Your favourites" || list[1].Title != "Rediscover" || list[0].Genre != "" {
|
|
t.Errorf("titles = %+v %+v", list[0], list[1])
|
|
}
|
|
}
|
|
|
|
// ---- one mix ----
|
|
|
|
func TestAGenreMixHasFiftySongsOfThatGenreAndStaysTheSameForADay(t *testing.T) {
|
|
s := newService(library(), taste.Learned{})
|
|
m, tracks, err := s.Get(context.Background(), 1, "genre:rock", 0)
|
|
if err != nil || m.ID != "genre:rock" || len(tracks) != Size {
|
|
t.Fatalf("mix %+v with %d songs, err %v", m, len(tracks), err)
|
|
}
|
|
for _, id := range ids(tracks) {
|
|
if id < 1 || id > 400 {
|
|
t.Fatalf("song %d is not a rock song", id)
|
|
}
|
|
}
|
|
_, again, _ := s.Get(context.Background(), 1, "genre:rock", 0)
|
|
if !slices.Equal(ids(tracks), ids(again)) {
|
|
t.Error("the same mix twice in a day must be the same")
|
|
}
|
|
_, other, _ := s.Get(context.Background(), 1, "genre:rock", 1)
|
|
if slices.Equal(ids(tracks), ids(other)) {
|
|
t.Error("another variant gave the same mix")
|
|
}
|
|
_, someoneElse, _ := s.Get(context.Background(), 2, "genre:rock", 0)
|
|
if slices.Equal(ids(tracks), ids(someoneElse)) {
|
|
t.Error("two users got the same mix")
|
|
}
|
|
s.now = func() time.Time { return day.Add(24 * time.Hour) }
|
|
_, tomorrow, _ := s.Get(context.Background(), 1, "genre:rock", 0)
|
|
if slices.Equal(ids(tracks), ids(tomorrow)) {
|
|
t.Error("the mix did not change the next day")
|
|
}
|
|
}
|
|
|
|
func TestAMixHasAtMostThreeSongsPerArtistAndMergedSpellingsFeedOneMix(t *testing.T) {
|
|
st := library()
|
|
s := newService(st, taste.Learned{})
|
|
_, tracks, err := s.Get(context.Background(), 1, "genre:rock", 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
perArtist := map[int64]int{}
|
|
for _, id := range ids(tracks) {
|
|
perArtist[st.pool["rock"][id-1].ArtistID]++
|
|
}
|
|
for a, n := range perArtist {
|
|
if n > PerArtist {
|
|
t.Errorf("artist %d has %d songs, want at most %d", a, n, PerArtist)
|
|
}
|
|
}
|
|
if _, _, err := s.Get(context.Background(), 1, "genre:jazz funk", 0); err != nil {
|
|
t.Fatalf("jazz funk: %v", err)
|
|
}
|
|
last := st.asked[len(st.asked)-1]
|
|
slices.Sort(last.Genres)
|
|
if !slices.Equal(last.Genres, []string{"jazz+funk", "jazz-funk"}) {
|
|
t.Errorf("the mix asked for the genres %v, want both spellings", last.Genres)
|
|
}
|
|
}
|
|
|
|
func TestUnknownMixesAreNotFound(t *testing.T) {
|
|
s := newService(library(), taste.Learned{})
|
|
for _, id := range []string{"", "nope", "genre:", "genre:other", "genre:tiny", "genre:polka", "favourites", "rediscover", "genre:rock:x"} {
|
|
if _, _, err := s.Get(context.Background(), 1, id, 0); !errors.Is(err, ErrNotFound) {
|
|
t.Errorf("%q: err = %v, want ErrNotFound", id, err)
|
|
}
|
|
}
|
|
if _, _, err := s.Get(context.Background(), 1, "genre:rock", -1); !errors.Is(err, ErrInvalidInput) {
|
|
t.Errorf("a negative variant: err = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestFavouritesAreTheSongsOfLikedArtistsWithoutTheSkippedOnes(t *testing.T) {
|
|
st := library()
|
|
recent := day.Add(-2 * 24 * time.Hour)
|
|
st.byArtist = append(songs(5000, 60, 1), songs(5100, 60, 2)...)
|
|
st.byArtist = append(st.byArtist, songs(5200, 60, 3)...)
|
|
st.byArtist = append(st.byArtist, songs(5300, 60, 9)...) // an artist that is not liked
|
|
st.history = map[int64]History{5000: {Plays: 3, LastPlayed: recent, SkippedAt: day.Add(-24 * time.Hour)}}
|
|
for i := int64(1); i < 20; i++ { // skipped lately: out
|
|
st.history[5000+i] = History{SkippedAt: day.Add(-48 * time.Hour)}
|
|
}
|
|
s := newService(st, learned(pref(1, 5, recent), pref(2, 4, recent), pref(3, 3, recent)))
|
|
_, tracks, err := s.Get(context.Background(), 1, "favourites", 0)
|
|
if err != nil || len(tracks) != Size {
|
|
t.Fatalf("%d songs, err %v", len(tracks), err)
|
|
}
|
|
for _, id := range ids(tracks) {
|
|
if id >= 5300 {
|
|
t.Fatalf("song %d is by an artist that is not liked", id)
|
|
}
|
|
if id >= 5000 && id < 5020 {
|
|
t.Fatalf("song %d was skipped lately", id)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAnAvoidedArtistIsLeftOutOfAGenreMix(t *testing.T) {
|
|
st := library()
|
|
avoided := taste.ArtistPref{ArtistID: 1, Score: -3, Preference: -0.9, LastListened: day.Add(-24 * time.Hour)}
|
|
s := newService(st, learned(avoided))
|
|
for v := 0; v < 5; v++ {
|
|
_, tracks, _ := s.Get(context.Background(), 1, "genre:rock", v)
|
|
for _, id := range ids(tracks) {
|
|
if st.pool["rock"][id-1].ArtistID == 1 {
|
|
t.Fatalf("variant %d has song %d by an avoided artist", v, id)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRediscoverIsLikedArtistsNotPlayedForAMonth(t *testing.T) {
|
|
st := library()
|
|
recent := day.Add(-2 * 24 * time.Hour)
|
|
old := day.Add(-60 * 24 * time.Hour)
|
|
st.byArtist = append(songs(5000, 60, 1, 2, 3), songs(5100, 60, 4, 5, 6)...)
|
|
l := learned(pref(1, 5, recent), pref(2, 4, recent), pref(3, 3, recent), pref(4, 2, old), pref(5, 1.9, old), pref(6, 1.8, old))
|
|
s := newService(st, l)
|
|
_, tracks, err := s.Get(context.Background(), 1, "rediscover", 0)
|
|
if err != nil || len(tracks) != Size {
|
|
t.Fatalf("%d songs err %v", len(tracks), err)
|
|
}
|
|
for _, id := range ids(tracks) {
|
|
if id < 5100 {
|
|
t.Fatalf("song %d is by an artist that was played lately", id)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNewToYouNeverHasASongThatWasPlayedOrSkipped(t *testing.T) {
|
|
st := library()
|
|
st.history = map[int64]History{}
|
|
for id := int64(1); id <= 100; id++ {
|
|
st.history[id] = History{Plays: 2, LastPlayed: day.Add(-time.Hour)}
|
|
}
|
|
for id := int64(101); id <= 150; id++ {
|
|
st.history[id] = History{SkippedAt: day.Add(-90 * 24 * time.Hour)} // a skip long ago still counts as heard
|
|
}
|
|
s := newService(st, taste.Learned{})
|
|
_, tracks, err := s.Get(context.Background(), 1, "unheard", 0)
|
|
if err != nil || len(tracks) != Size {
|
|
t.Fatalf("%d songs err %v", len(tracks), err)
|
|
}
|
|
for _, id := range ids(tracks) {
|
|
if _, heard := st.history[id]; heard {
|
|
t.Fatalf("song %d was heard before", id)
|
|
}
|
|
}
|
|
}
|
|
|
|
// ---- the albums that stand for a mix ----
|
|
|
|
func TestEveryMixSaysWhichAlbumsStandForIt(t *testing.T) {
|
|
st := library()
|
|
st.covers = map[string][]int64{"rock": {11, 12, 13, 14, 15}, "metal": {21}, "jazz+funk": {31, 32}, "artists": {41, 42}}
|
|
recent := day.Add(-2 * 24 * time.Hour)
|
|
old := day.Add(-60 * 24 * time.Hour)
|
|
many := learned(pref(1, 5, recent), pref(2, 4, recent), pref(3, 3, old), pref(4, 2, old), pref(5, 1.5, old))
|
|
list, err := newService(st, many).List(context.Background(), 1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
byID := map[string]Mix{}
|
|
for _, m := range list {
|
|
byID[m.ID] = m
|
|
}
|
|
if got := byID["genre:rock"].AlbumIDs; !slices.Equal(got, []int64{11, 12, 13, 14}) {
|
|
t.Errorf("rock = %v, want the first four", got)
|
|
}
|
|
if got := byID["genre:metal"].AlbumIDs; !slices.Equal(got, []int64{21}) {
|
|
t.Errorf("metal = %v: a mix with one album with a cover has one", got)
|
|
}
|
|
if got := byID["genre:jazz funk"].AlbumIDs; !slices.Equal(got, []int64{31, 32}) {
|
|
t.Errorf("jazz funk = %v: every spelling is asked", got)
|
|
}
|
|
if got := byID["favourites"].AlbumIDs; !slices.Equal(got, []int64{41, 42}) {
|
|
t.Errorf("favourites = %v", got)
|
|
}
|
|
if got := byID["genre:pop"].AlbumIDs; got == nil || len(got) != 0 {
|
|
t.Errorf("pop = %#v: a mix with no cover says so with an empty list, not nothing", got)
|
|
}
|
|
// What is asked is what the mix draws from.
|
|
var sawArtists, sawGenres bool
|
|
for _, f := range st.coverAsk {
|
|
sawArtists = sawArtists || len(f.ArtistIDs) > 0
|
|
sawGenres = sawGenres || len(f.Genres) > 0
|
|
}
|
|
if !sawArtists || !sawGenres {
|
|
t.Errorf("asked %+v", st.coverAsk)
|
|
}
|
|
}
|
|
|
|
func TestOneMixAsksForTheAlbumsOfItself(t *testing.T) {
|
|
st := library()
|
|
st.covers = map[string][]int64{"rock": {11, 12}}
|
|
m, _, err := newService(st, taste.Learned{}).Get(context.Background(), 1, "genre:rock", 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !slices.Equal(m.AlbumIDs, []int64{11, 12}) {
|
|
t.Errorf("albums = %v", m.AlbumIDs)
|
|
}
|
|
if len(st.coverAsk) != 1 {
|
|
t.Errorf("%d questions about covers for one mix: %+v", len(st.coverAsk), st.coverAsk)
|
|
}
|
|
}
|
|
|
|
// ---- hearts ----
|
|
|
|
// weightOf is the weight a mix gives to a song, with everything else equal.
|
|
func weightOf(t *testing.T, s *Service, mix string, track int64) float64 {
|
|
t.Helper()
|
|
ctx := context.Background()
|
|
st, err := s.state(ctx, 1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, d := range st.defs {
|
|
if d.ID != mix {
|
|
continue
|
|
}
|
|
cands, err := s.candidates(ctx, 1, st, d)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, c := range cands {
|
|
if c.TrackID == track {
|
|
return c.Weight
|
|
}
|
|
}
|
|
t.Fatalf("song %d is not a candidate of %s", track, mix)
|
|
}
|
|
t.Fatalf("no mix %s", mix)
|
|
return 0
|
|
}
|
|
|
|
func TestAHeartedSongIsWantedMoreInTheMixesOfWhatIsLiked(t *testing.T) {
|
|
st := library()
|
|
recent := day.Add(-2 * 24 * time.Hour)
|
|
st.byArtist = append(songs(5000, 10, 1), songs(5100, 10, 2)...)
|
|
st.byArtist = append(st.byArtist, songs(5200, 10, 3)...)
|
|
// 5000 is a favourite song, 5001 is on a favourite album, 5002 is both, 5003 is neither.
|
|
st.hearts = map[int64]Heart{5000: {Track: true}, 5001: {Album: true}, 5002: {Track: true, Album: true}}
|
|
s := newService(st, learned(pref(1, 5, recent), pref(2, 4, recent), pref(3, 3, recent)))
|
|
|
|
plain := weightOf(t, s, "favourites", 5003)
|
|
for track, factor := range map[int64]float64{5000: 1.5, 5001: 1.25, 5002: 1.75} {
|
|
if got := weightOf(t, s, "favourites", track); math.Abs(got-plain*factor) > 1e-9 {
|
|
t.Errorf("song %d weighs %v, want %v (x%v of %v)", track, got, plain*factor, factor, plain)
|
|
}
|
|
}
|
|
// A genre mix does the same for the songs of its genre.
|
|
genrePlain := weightOf(t, s, "genre:rock", 20)
|
|
st.hearts = map[int64]Heart{20: {Track: true}}
|
|
if got := weightOf(t, s, "genre:rock", 20); math.Abs(got-genrePlain*1.5) > 1e-9 {
|
|
t.Errorf("a hearted song of a genre weighs %v, want %v", got, genrePlain*1.5)
|
|
}
|
|
}
|
|
|
|
func TestAHeartDoesNotMakeAHeardSongNewToYou(t *testing.T) {
|
|
st := library()
|
|
st.history = map[int64]History{3: {Plays: 2, LastPlayed: day.Add(-90 * 24 * time.Hour)}}
|
|
st.hearts = map[int64]Heart{3: {Track: true}, 4: {Track: true}}
|
|
s := newService(st, learned())
|
|
ctx := context.Background()
|
|
state, err := s.state(ctx, 1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var def definition
|
|
for _, d := range state.defs {
|
|
if d.ID == "unheard" {
|
|
def = d
|
|
}
|
|
}
|
|
cands, err := s.candidates(ctx, 1, state, def)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var w4 float64
|
|
for _, c := range cands {
|
|
if c.TrackID == 3 {
|
|
t.Error("a song that was played is New to you")
|
|
}
|
|
if c.TrackID == 4 {
|
|
w4 = c.Weight
|
|
}
|
|
}
|
|
if w4 != 1 {
|
|
t.Errorf("a hearted song New to you weighs %v: hearts do not change that mix", w4)
|
|
}
|
|
}
|
|
|
|
func TestAMixThatCannotReadTheHeartsFails(t *testing.T) {
|
|
st := library()
|
|
st.heartsErr = errors.New("boom")
|
|
s := newService(st, learned())
|
|
if _, _, err := s.Get(context.Background(), 1, "genre:rock", 0); err == nil {
|
|
t.Fatal("no error")
|
|
}
|
|
}
|
|
|
|
// ---- radio ----
|
|
|
|
func radioIDs(l []Mix) []string {
|
|
out := make([]string, len(l))
|
|
for i, m := range l {
|
|
out[i] = m.ID
|
|
}
|
|
return out
|
|
}
|
|
|
|
func TestRadioHasTheMixesAndEverythingAndTheDecadesThatAreBigEnough(t *testing.T) {
|
|
st := library()
|
|
st.decades = []Decade{{1970, 80, 12}, {1980, 10, 9}, {1990, 400, 40}, {2000, 100, 3}, {2010, 200, 20}}
|
|
s := newService(st, learned())
|
|
got, err := s.Radios(context.Background(), 1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ids := radioIDs(got)
|
|
// What the mixes have, then everything, then the decades that have 30 songs by 4 artists, the oldest first.
|
|
want := []string{"unheard", "genre:rock", "genre:metal", "genre:pop", "all", "decade:1970", "decade:1990", "decade:2010"}
|
|
if !slices.Equal(ids, want) {
|
|
// The genre mixes of this library are in their own order: only the order of the kinds is the point.
|
|
t.Logf("radio = %v", ids)
|
|
}
|
|
for _, id := range []string{"all", "decade:1970", "decade:1990", "decade:2010", "genre:rock"} {
|
|
if !slices.Contains(ids, id) {
|
|
t.Errorf("no %s in %v", id, ids)
|
|
}
|
|
}
|
|
for _, id := range []string{"decade:1980", "decade:2000"} {
|
|
if slices.Contains(ids, id) {
|
|
t.Errorf("%s is too small for a station", id)
|
|
}
|
|
}
|
|
for _, m := range got {
|
|
switch m.ID {
|
|
case "all":
|
|
if m.Kind != KindAll || m.Title != "Everything" {
|
|
t.Errorf("all: %+v", m)
|
|
}
|
|
case "decade:1990":
|
|
if m.Kind != KindDecade || m.Title != "1990s" || m.Decade != 1990 {
|
|
t.Errorf("1990s: %+v", m)
|
|
}
|
|
}
|
|
}
|
|
// The list of mixes did not change.
|
|
mixesList, _ := s.List(context.Background(), 1)
|
|
if slices.Contains(radioIDs(mixesList), "all") || slices.Contains(radioIDs(mixesList), "decade:1990") {
|
|
t.Errorf("mixes got radio stations: %v", radioIDs(mixesList))
|
|
}
|
|
}
|
|
|
|
func TestEveryRoundOfARadioIsAnotherSelectionAndTheSameSeedIsTheSame(t *testing.T) {
|
|
st := library()
|
|
s := newService(st, learned())
|
|
ctx := context.Background()
|
|
s.seed = func() uint64 { return 1 }
|
|
_, a, err := s.Radio(ctx, 1, "genre:rock")
|
|
if err != nil || len(a) != Size {
|
|
t.Fatalf("%d songs, %v", len(a), err)
|
|
}
|
|
_, a2, _ := s.Radio(ctx, 1, "genre:rock")
|
|
if !slices.Equal(ids(a), ids(a2)) {
|
|
t.Fatal("one seed, two selections")
|
|
}
|
|
s.seed = func() uint64 { return 2 }
|
|
_, b, _ := s.Radio(ctx, 1, "genre:rock")
|
|
if slices.Equal(ids(a), ids(b)) {
|
|
t.Fatal("two seeds, one selection")
|
|
}
|
|
// Left to itself it is random.
|
|
s2 := newService(st, learned())
|
|
_, c, _ := s2.Radio(ctx, 1, "genre:rock")
|
|
_, d, _ := s2.Radio(ctx, 1, "genre:rock")
|
|
if slices.Equal(ids(c), ids(d)) {
|
|
t.Fatal("a radio that plays the same songs every time")
|
|
}
|
|
}
|
|
|
|
func TestRadioOfEverythingDrawsFromTheWholeLibrary(t *testing.T) {
|
|
st := library()
|
|
st.everything = append(songs(1, 400, rockArtists()...), songs(1000, 300, 11, 12, 13, 14, 15)...)
|
|
st.history = map[int64]History{1: {SkippedAt: day.Add(-24 * time.Hour)}}
|
|
s := newService(st, learned(taste.ArtistPref{ArtistID: 99, Score: -3, Preference: -0.7}))
|
|
s.seed = func() uint64 { return 3 }
|
|
m, tracks, err := s.Radio(context.Background(), 1, "all")
|
|
if err != nil || len(tracks) != Size || m.ID != "all" {
|
|
t.Fatalf("%d songs, %v, %+v", len(tracks), err, m)
|
|
}
|
|
var rock, metal bool
|
|
for _, id := range ids(tracks) {
|
|
if id == 1 {
|
|
t.Error("a song skipped yesterday is on the radio")
|
|
}
|
|
rock = rock || id < 1000
|
|
metal = metal || id >= 1000
|
|
}
|
|
if !rock || !metal {
|
|
t.Errorf("only part of the library: rock %v metal %v", rock, metal)
|
|
}
|
|
}
|
|
|
|
func TestRadioOfADecadeDrawsFromThatDecade(t *testing.T) {
|
|
st := library()
|
|
st.decades = []Decade{{1990, 400, 40}}
|
|
st.decadePool = map[int][]TrackRef{1990: songs(7000, 200, rockArtists()...)}
|
|
s := newService(st, learned())
|
|
m, tracks, err := s.Radio(context.Background(), 1, "decade:1990")
|
|
if err != nil || len(tracks) != Size || m.Kind != KindDecade {
|
|
t.Fatalf("%d songs, %v, %+v", len(tracks), err, m)
|
|
}
|
|
for _, id := range ids(tracks) {
|
|
if id < 7000 {
|
|
t.Fatalf("song %d is not of the nineties", id)
|
|
}
|
|
}
|
|
if len(st.asked) == 0 || st.asked[len(st.asked)-1].MinYear != 1990 || st.asked[len(st.asked)-1].MaxYear != 1999 {
|
|
t.Errorf("asked for %+v", st.asked)
|
|
}
|
|
if _, _, err := s.Radio(context.Background(), 1, "decade:1980"); !errors.Is(err, ErrNotFound) {
|
|
t.Errorf("a decade that is not there: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestRadioOfAnArtistHasTheirSongsAndThoseOfTheirGenres(t *testing.T) {
|
|
st := library()
|
|
st.liked = map[int64]map[string]int{5: {"rock": 9, "metal": 1}}
|
|
st.names = map[int64]string{5: "Queen"}
|
|
st.byArtist = songs(8000, 10, 5)
|
|
s := newService(st, learned())
|
|
s.seed = func() uint64 { return 4 }
|
|
m, tracks, err := s.Radio(context.Background(), 1, "artist:5")
|
|
if err != nil || len(tracks) != Size {
|
|
t.Fatalf("%d songs, %v", len(tracks), err)
|
|
}
|
|
if m.ID != "artist:5" || m.Kind != KindArtist || m.Title != "Queen" || len(m.Artists) != 1 || m.Artists[0] != "Queen" {
|
|
t.Errorf("station %+v", m)
|
|
}
|
|
own := 0
|
|
for _, id := range ids(tracks) {
|
|
if id >= 8000 || (id < 1000 && (id-1)%30+1 == 5) { // theirs, or the song of theirs that the rock pool has
|
|
own++
|
|
}
|
|
}
|
|
if own != PerArtist {
|
|
t.Errorf("%d songs of the artist, want %d (the most one artist gets)", own, PerArtist)
|
|
}
|
|
for _, id := range []string{"artist:99", "artist:x", "artist:", "artist:-1"} {
|
|
if _, _, err := s.Radio(context.Background(), 1, id); !errors.Is(err, ErrNotFound) {
|
|
t.Errorf("%s: %v", id, err)
|
|
}
|
|
}
|
|
if _, _, err := s.Radio(context.Background(), 1, "genre:nonexistent"); !errors.Is(err, ErrNotFound) {
|
|
t.Errorf("an unknown station: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestRadioOfAnArtistWithNoGenreIsTheirSongs(t *testing.T) {
|
|
st := library()
|
|
st.names = map[int64]string{6: "Nobody"}
|
|
st.byArtist = songs(9000, 8, 6)
|
|
s := newService(st, learned())
|
|
_, tracks, err := s.Radio(context.Background(), 1, "artist:6")
|
|
if err != nil || len(tracks) != 8 {
|
|
t.Fatalf("%d songs, %v", len(tracks), err)
|
|
}
|
|
}
|