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.
227 lines
8 KiB
Go
227 lines
8 KiB
Go
package taste
|
|
|
|
import (
|
|
"math"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.kmalloc.org/squid/sonda/internal/listening"
|
|
)
|
|
|
|
var (
|
|
now = time.Date(2026, 11, 11, 12, 0, 0, 0, time.UTC)
|
|
options = Options{MinListen: 20 * time.Second, HalfLife: 90 * 24 * time.Hour}
|
|
)
|
|
|
|
func near(a, b float64) bool { return math.Abs(a-b) < 1e-9 }
|
|
|
|
func TestEventValue(t *testing.T) {
|
|
min := 20 * time.Second
|
|
for _, c := range []struct {
|
|
name string
|
|
typ listening.EventType
|
|
played int64
|
|
want float64
|
|
}{
|
|
{"complete", listening.Complete, 200_000, 1},
|
|
{"complete played little", listening.Complete, 1_000, 1},
|
|
{"play that counts", listening.Play, 20_000, 0.5},
|
|
{"skip after the minimum still was a listen", listening.Skip, 45_000, 0.5},
|
|
{"skip before the minimum", listening.Skip, 4_000, -1},
|
|
{"skip with no time", listening.Skip, 0, -1},
|
|
{"short play says nothing", listening.Play, 5_000, 0},
|
|
} {
|
|
if got := EventValue(c.typ, c.played, min); got != c.want {
|
|
t.Errorf("%s: value = %v, want %v", c.name, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPreferenceGrowsWithListensAndStaysBelowOne(t *testing.T) {
|
|
events := []Event{
|
|
{ArtistID: 1, Type: listening.Complete, At: now},
|
|
{ArtistID: 1, Type: listening.Complete, At: now},
|
|
{ArtistID: 1, Type: listening.Complete, At: now},
|
|
}
|
|
got := Preferences(events, now, options)[1]
|
|
if want := math.Tanh(1); !near(got.Preference, want) { // three full listens: sum 3
|
|
t.Errorf("preference = %v, want %v", got.Preference, want)
|
|
}
|
|
if got.Listens != 3 {
|
|
t.Errorf("listens = %d, want 3", got.Listens)
|
|
}
|
|
for i := 0; i < 10; i++ {
|
|
events = append(events, Event{ArtistID: 1, Type: listening.Complete, At: now})
|
|
}
|
|
if p := Preferences(events, now, options)[1].Preference; p >= 1 || p < 0.99 {
|
|
t.Errorf("many listens: preference = %v, want just under 1", p)
|
|
}
|
|
}
|
|
|
|
func TestHeavilyPlayedArtistsKeepTheirOrder(t *testing.T) {
|
|
var events []Event
|
|
for i := 0; i < 500; i++ {
|
|
events = append(events, Event{ArtistID: 1, Type: listening.Complete, At: now.Add(-time.Hour)})
|
|
}
|
|
for i := 0; i < 60; i++ {
|
|
events = append(events, Event{ArtistID: 2, Type: listening.Complete, At: now})
|
|
}
|
|
got := Preferences(events, now, options)
|
|
if got[1].Score <= got[2].Score {
|
|
t.Errorf("500 listens score %v, 60 listens score %v: the one heard more must score more", got[1].Score, got[2].Score)
|
|
}
|
|
if got[1].Preference > 1 || got[1].Preference < 0.999 {
|
|
t.Errorf("preference = %v, want at most 1 and close to it", got[1].Preference)
|
|
}
|
|
}
|
|
|
|
func TestAnEventFromTheFutureIsNewAndNothingMore(t *testing.T) {
|
|
future := now.Add(365 * 24 * time.Hour)
|
|
got := Preferences([]Event{{ArtistID: 1, Type: listening.Complete, At: future}}, now, options)[1]
|
|
if !got.LastListened.Equal(now) {
|
|
t.Errorf("last listened = %v, want now (%v): a clock that is wrong cannot make a listen later than now", got.LastListened, now)
|
|
}
|
|
}
|
|
|
|
func TestOneSkipDoesNotSinkAnArtist(t *testing.T) {
|
|
got := Preferences([]Event{{ArtistID: 1, Type: listening.Skip, PlayedMs: 2_000, At: now}}, now, options)[1]
|
|
if want := math.Tanh(-1.0 / 3); !near(got.Preference, want) {
|
|
t.Errorf("preference = %v, want %v", got.Preference, want)
|
|
}
|
|
if got.Preference < -0.4 {
|
|
t.Errorf("one skip weighs too much: %v", got.Preference)
|
|
}
|
|
if got.Listens != 0 {
|
|
t.Errorf("a skip is not a listen: %d", got.Listens)
|
|
}
|
|
}
|
|
|
|
func TestOldEventsWeighLess(t *testing.T) {
|
|
events := []Event{{ArtistID: 1, Type: listening.Complete, At: now.Add(-90 * 24 * time.Hour)}}
|
|
if got := Preferences(events, now, options)[1].Preference; !near(got, math.Tanh(0.5/3)) {
|
|
t.Errorf("after one half-life: preference = %v, want %v", got, math.Tanh(0.5/3))
|
|
}
|
|
// An event in the future (a client with a fast clock) counts as new, not more than new.
|
|
future := []Event{{ArtistID: 1, Type: listening.Complete, At: now.Add(48 * time.Hour)}}
|
|
if got := Preferences(future, now, options)[1].Preference; !near(got, math.Tanh(1.0/3)) {
|
|
t.Errorf("future event: preference = %v, want %v", got, math.Tanh(1.0/3))
|
|
}
|
|
}
|
|
|
|
func TestPreferenceKeepsArtistsApartAndRemembersTheLastListen(t *testing.T) {
|
|
earlier := now.Add(-time.Hour)
|
|
events := []Event{
|
|
{ArtistID: 1, Type: listening.Complete, At: earlier},
|
|
{ArtistID: 1, Type: listening.Play, PlayedMs: 30_000, At: now},
|
|
{ArtistID: 2, Type: listening.Skip, PlayedMs: 1_000, At: now},
|
|
}
|
|
got := Preferences(events, now, options)
|
|
if len(got) != 2 {
|
|
t.Fatalf("got %d artists, want 2", len(got))
|
|
}
|
|
if got[1].Preference <= 0 || got[2].Preference >= 0 {
|
|
t.Errorf("preferences = %v and %v", got[1].Preference, got[2].Preference)
|
|
}
|
|
if !got[1].LastListened.Equal(now) {
|
|
t.Errorf("last listened = %v, want %v", got[1].LastListened, now)
|
|
}
|
|
if !got[2].LastListened.IsZero() {
|
|
t.Errorf("an artist that was only skipped has no last listen: %v", got[2].LastListened)
|
|
}
|
|
}
|
|
|
|
func TestPreferencesWithoutEventsIsEmpty(t *testing.T) {
|
|
if got := Preferences(nil, now, options); len(got) != 0 {
|
|
t.Errorf("got %v", got)
|
|
}
|
|
}
|
|
|
|
func TestTagProfileFollowsTheArtistsILike(t *testing.T) {
|
|
prefs := map[int64]ArtistPref{
|
|
1: {ArtistID: 1, Preference: 0.8},
|
|
2: {ArtistID: 2, Preference: 0.4},
|
|
3: {ArtistID: 3, Preference: -0.9}, // disliked: must not pull its tags in
|
|
}
|
|
tags := map[int64]map[string]float64{
|
|
1: {"rock": 1, "grunge": 0.5},
|
|
2: {"rock": 0.5, "pop": 1},
|
|
3: {"jazz": 1},
|
|
}
|
|
got := TagProfile(prefs, tags)
|
|
// rock 0.8*1 + 0.4*0.5 = 1.0 ; grunge 0.4 ; pop 0.4 ; jazz absent. Best first, scaled to 1.
|
|
if len(got) != 3 {
|
|
t.Fatalf("got %v", got)
|
|
}
|
|
if got[0].Tag != "rock" || !near(got[0].Weight, 1) {
|
|
t.Errorf("first = %+v, want rock at 1", got[0])
|
|
}
|
|
if !near(got[1].Weight, 0.4) || !near(got[2].Weight, 0.4) {
|
|
t.Errorf("weights = %v and %v, want 0.4 and 0.4", got[1].Weight, got[2].Weight)
|
|
}
|
|
if got[1].Tag != "grunge" || got[2].Tag != "pop" { // ties in name order
|
|
t.Errorf("tie order = %s, %s", got[1].Tag, got[2].Tag)
|
|
}
|
|
}
|
|
|
|
func TestTagProfileIsEmptyWithoutLikedArtists(t *testing.T) {
|
|
prefs := map[int64]ArtistPref{1: {ArtistID: 1, Preference: -0.5}, 2: {ArtistID: 2, Preference: 0}}
|
|
tags := map[int64]map[string]float64{1: {"rock": 1}, 2: {"rock": 1}}
|
|
if got := TagProfile(prefs, tags); len(got) != 0 {
|
|
t.Errorf("got %v", got)
|
|
}
|
|
}
|
|
|
|
func TestAHeartCountsLikeListening(t *testing.T) {
|
|
for name, c := range map[string]struct {
|
|
f Favorites
|
|
want float64
|
|
}{
|
|
"nothing": {Favorites{}, 0},
|
|
"the artist": {Favorites{Artist: true}, 3},
|
|
"two albums": {Favorites{Albums: 2}, 3},
|
|
"three songs": {Favorites{Tracks: 3}, 3},
|
|
"all three": {Favorites{Artist: true, Albums: 1, Tracks: 1}, 5.5},
|
|
"a great many songs": {Favorites{Artist: true, Albums: 40, Tracks: 4000}, MaxFavoriteValue},
|
|
} {
|
|
if got := FavoriteValue(c.f); math.Abs(got-c.want) > 1e-9 {
|
|
t.Errorf("%s: %v, want %v", name, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFavouritesMakeAnArtistLikedWithoutAnyListen(t *testing.T) {
|
|
prefs := Preferences(complete(1, 1), now, options)
|
|
prefs = WithFavorites(prefs, map[int64]Favorites{
|
|
1: {Tracks: 1}, // one listen and one song hearted
|
|
2: {Artist: true}, // never listened to
|
|
3: {Albums: 1},
|
|
})
|
|
if p := prefs[2]; p.Listens != 0 || !p.LastListened.IsZero() || p.Score != 3 || math.Abs(p.Preference-math.Tanh(1)) > 1e-9 {
|
|
t.Errorf("a favourite artist with no listens: %+v", p)
|
|
}
|
|
if p := prefs[1]; p.Listens != 1 || p.Score != 2 { // 1 listen + 1 heart
|
|
t.Errorf("an artist with both: %+v", p)
|
|
}
|
|
if p := prefs[3]; p.Score != 1.5 || p.ArtistID != 3 {
|
|
t.Errorf("an artist with a favourite album: %+v", p)
|
|
}
|
|
}
|
|
|
|
func TestAHeartOutweighsASkip(t *testing.T) {
|
|
prefs := Preferences([]Event{{ArtistID: 1, Type: listening.Skip, PlayedMs: 1000, At: now}}, now, options)
|
|
if prefs[1].Preference >= 0 {
|
|
t.Fatalf("one skip: %+v", prefs[1])
|
|
}
|
|
prefs = WithFavorites(prefs, map[int64]Favorites{1: {Artist: true}})
|
|
if prefs[1].Preference <= 0 {
|
|
t.Errorf("a skip and a heart: %+v", prefs[1])
|
|
}
|
|
}
|
|
|
|
func TestNoFavouritesChangeNothing(t *testing.T) {
|
|
before := Preferences(complete(1, 2), now, options)
|
|
after := WithFavorites(Preferences(complete(1, 2), now, options), nil)
|
|
if len(before) != len(after) || before[1] != after[1] {
|
|
t.Fatalf("%+v vs %+v", before, after)
|
|
}
|
|
}
|