A heart adds to what the listening says of an artist: a favourite artist counts as much as three complete listens, a favourite album 1.5 for its album artist and a favourite song 1 for each of its main artists, at most 6 for one artist, without fading with time. An artist hearted but never played is liked, with no listens. That feeds Discover, the mixes and the smart playlists that use the taste, and GET /me/taste. In the mixes of what the listener likes (genre mixes, Your favourites, Rediscover) a hearted song is wanted 1.5 times as much and a song on a hearted album 1.25 times. New to you is not changed.
583 lines
18 KiB
Go
583 lines
18 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
|
|
}
|
|
|
|
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)
|
|
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")
|
|
}
|
|
}
|