A new model (internal/affinity) splits my listening into sittings (plays less than 30 minutes apart) and links the artists that are often played in the same sitting. It is built from my listening events and from the plays of artists I have no music of that an import kept, on my server, with no provider, and is built again when plays are added (migration 0032 keeps it as a cache). GET /me/suggestions gives two lists, each artist with the artists it goes with: the ones I have and did not play for two months, and the ones I played at least three times and have no music of. It needs discover_from_listening. POST /me/suggestions/wishlist puts an artist I played on my wishlist by name. Discover has a "From your listening" tab for both lists.
151 lines
5.1 KiB
Go
151 lines
5.1 KiB
Go
package affinity
|
|
|
|
import (
|
|
"math"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
var t0 = time.Date(2023, 3, 1, 9, 0, 0, 0, time.UTC)
|
|
|
|
// at builds a play of an artist, minutes after t0.
|
|
func at(artist string, minutes int) Play {
|
|
return Play{Key: artist, Name: artist, At: t0.Add(time.Duration(minutes) * time.Minute)}
|
|
}
|
|
|
|
// sittings are plays of the given artists in order, one minute apart, starting a day after the day before.
|
|
func sitting(day int, artists ...string) []Play {
|
|
var out []Play
|
|
for i, a := range artists {
|
|
out = append(out, at(a, day*24*60+i))
|
|
}
|
|
return out
|
|
}
|
|
|
|
func link(m Model, a, b string) (float64, bool) {
|
|
for _, l := range m.Links {
|
|
if l.A == a && l.B == b {
|
|
return l.Score, true
|
|
}
|
|
}
|
|
return 0, false
|
|
}
|
|
|
|
func TestArtistsPlayedInTheSameSittingAreLinked(t *testing.T) {
|
|
var plays []Play
|
|
for day := 0; day < 3; day++ {
|
|
plays = append(plays, sitting(day, "blur", "oasis")...)
|
|
}
|
|
m := Build(plays, t0.AddDate(0, 0, 10), DefaultOptions())
|
|
score, ok := link(m, "blur", "oasis")
|
|
if !ok || math.Abs(score-1) > 1e-9 {
|
|
t.Fatalf("link = %v, %v, want 1 (always together)", score, ok)
|
|
}
|
|
if len(m.Links) != 1 || m.Sessions != 3 || m.Plays != 6 {
|
|
t.Errorf("model = %d links, %d sittings, %d plays, want 1, 3, 6", len(m.Links), m.Sessions, m.Plays)
|
|
}
|
|
}
|
|
|
|
func TestTheLinkIsTheCosineOfTheSittingsOfTheTwo(t *testing.T) {
|
|
var plays []Play
|
|
plays = append(plays, sitting(0, "a", "b")...)
|
|
plays = append(plays, sitting(1, "a", "b")...)
|
|
plays = append(plays, sitting(2, "a")...)
|
|
plays = append(plays, sitting(3, "a")...)
|
|
plays = append(plays, sitting(4, "a", "c")...) // a is in five sittings, b in two, together twice
|
|
m := Build(plays, t0.AddDate(0, 0, 10), DefaultOptions())
|
|
want := 2 / math.Sqrt(5*2)
|
|
if score, ok := link(m, "a", "b"); !ok || math.Abs(score-want) > 1e-9 {
|
|
t.Errorf("a-b = %v, %v, want %v", score, ok, want)
|
|
}
|
|
if _, ok := link(m, "a", "c"); ok {
|
|
t.Error("a and c were together once: not enough to be linked")
|
|
}
|
|
}
|
|
|
|
func TestAPauseLongerThanTheGapStartsAnotherSitting(t *testing.T) {
|
|
o := DefaultOptions()
|
|
gap := int(o.Gap / time.Minute)
|
|
together := []Play{at("a", 0), at("b", gap)} // exactly the gap: still one sitting
|
|
apart := []Play{at("a", 1000), at("b", 1000+gap+1)} // one minute more: two
|
|
m := Build(append(together, apart...), t0.AddDate(0, 0, 10), Options{Gap: o.Gap, MaxArtists: o.MaxArtists, MinTogether: 1, SeedHalfLife: o.SeedHalfLife})
|
|
if m.Sessions != 3 {
|
|
t.Fatalf("%d sittings, want 3", m.Sessions)
|
|
}
|
|
if score, ok := link(m, "a", "b"); !ok || math.Abs(score-1/math.Sqrt(2*2)) > 1e-9 {
|
|
t.Errorf("a-b = %v, %v, want together in one of the two sittings each is in", score, ok)
|
|
}
|
|
}
|
|
|
|
func TestAnArtistRepeatedInASittingCountsOnce(t *testing.T) {
|
|
var plays []Play
|
|
for day := 0; day < 2; day++ {
|
|
plays = append(plays, sitting(day, "a", "a", "a", "b")...)
|
|
}
|
|
m := Build(plays, t0.AddDate(0, 0, 10), DefaultOptions())
|
|
if score, ok := link(m, "a", "b"); !ok || math.Abs(score-1) > 1e-9 {
|
|
t.Errorf("a-b = %v, %v, want 1", score, ok)
|
|
}
|
|
for _, a := range m.Artists {
|
|
if a.Key == "a" && (a.Plays != 6 || a.Sessions != 2) {
|
|
t.Errorf("a = %+v, want 6 plays in 2 sittings", a)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAMarathonOfManyArtistsTeachesNothing(t *testing.T) {
|
|
o := DefaultOptions()
|
|
o.MaxArtists = 3
|
|
var names []string
|
|
for _, n := range []string{"a", "b", "c", "d"} {
|
|
names = append(names, n)
|
|
}
|
|
var plays []Play
|
|
for day := 0; day < 3; day++ {
|
|
plays = append(plays, sitting(day, names...)...)
|
|
}
|
|
m := Build(plays, t0.AddDate(0, 0, 10), o)
|
|
if len(m.Links) != 0 {
|
|
t.Errorf("%d links from sittings of more artists than the limit, want none", len(m.Links))
|
|
}
|
|
if m.Plays != 12 {
|
|
t.Errorf("%d plays, want all 12 still counted", m.Plays)
|
|
}
|
|
}
|
|
|
|
func TestRecentPlaysWeighMoreAndTheFutureIsNow(t *testing.T) {
|
|
now := t0.AddDate(3, 0, 0)
|
|
o := DefaultOptions()
|
|
plays := []Play{
|
|
{Key: "old", Name: "Old", At: now.AddDate(0, 0, -int(o.SeedHalfLife/(24*time.Hour)))}, // one half-life ago
|
|
{Key: "new", Name: "New", At: now},
|
|
{Key: "future", Name: "Future", At: now.Add(48 * time.Hour)},
|
|
}
|
|
m := Build(plays, now, o)
|
|
w := map[string]float64{}
|
|
for _, a := range m.Artists {
|
|
w[a.Key] = a.Weight
|
|
}
|
|
if math.Abs(w["old"]-0.5) > 0.01 || math.Abs(w["new"]-1) > 1e-9 || math.Abs(w["future"]-1) > 1e-9 {
|
|
t.Errorf("weights = %v, want old 0.5, new 1, future 1", w)
|
|
}
|
|
}
|
|
|
|
func TestAnArtistKnowsItsNameItsLastPlayAndItsOrderIsStable(t *testing.T) {
|
|
plays := []Play{{Key: "b", Name: "Björk", At: t0}, {Key: "a", Name: "Air", At: t0.Add(time.Hour)}, {Key: "b", Name: "BJORK", At: t0.Add(48 * time.Hour)}, {Key: "", Name: "nobody", At: t0}}
|
|
m := Build(plays, t0.AddDate(0, 0, 10), DefaultOptions())
|
|
if len(m.Artists) != 2 || m.Artists[0].Key != "a" || m.Artists[1].Key != "b" {
|
|
t.Fatalf("artists = %+v, want a then b, and none without a key", m.Artists)
|
|
}
|
|
b := m.Artists[1]
|
|
if b.Name != "Björk" || b.Plays != 2 || !b.Last.Equal(t0.Add(48*time.Hour)) {
|
|
t.Errorf("b = %+v, want the first name seen, 2 plays and the last time", b)
|
|
}
|
|
}
|
|
|
|
func TestNoPlaysMakeAnEmptyModel(t *testing.T) {
|
|
m := Build(nil, t0, DefaultOptions())
|
|
if len(m.Artists) != 0 || len(m.Links) != 0 || m.Plays != 0 || m.Sessions != 0 {
|
|
t.Errorf("model = %+v, want empty", m)
|
|
}
|
|
}
|