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.
216 lines
8.2 KiB
Go
216 lines
8.2 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"math"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/uptrace/bun"
|
|
|
|
"git.kmalloc.org/squid/sonda/internal/favorites"
|
|
"git.kmalloc.org/squid/sonda/internal/listening"
|
|
"git.kmalloc.org/squid/sonda/internal/taste"
|
|
)
|
|
|
|
func trackByTitle(t *testing.T, db *bun.DB, title string) int64 {
|
|
t.Helper()
|
|
var id int64
|
|
if err := db.QueryRow("SELECT id FROM tracks WHERE title = ?", title).Scan(&id); err != nil {
|
|
t.Fatalf("track %q: %v", title, err)
|
|
}
|
|
return id
|
|
}
|
|
|
|
func TestTasteArtistEvents(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
s := seedCatalog(t, db)
|
|
ctx := context.Background()
|
|
users := NewUserRepo(db)
|
|
alice, _ := users.CreateUser(ctx, "alice", "h", false)
|
|
bob, _ := users.CreateUser(ctx, "bob", "h", false)
|
|
repo := NewListeningRepo(db)
|
|
record := func(user int64, track, key string, typ listening.EventType, ms int64, when time.Time) {
|
|
t.Helper()
|
|
if _, err := repo.Record(ctx, listening.Event{UserID: user, TrackID: s.track[track], Type: typ,
|
|
OccurredAt: when, DurationPlayedMs: ms, IdempotencyKey: key}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
old, recent := time.Unix(1_000_000, 0), time.Unix(1_700_000_000, 0)
|
|
record(alice.ID, "Airbag", "a1", listening.Complete, 200_000, recent)
|
|
record(alice.ID, "Airbag", "a2", listening.Skip, 3_000, old) // before the cut: not read
|
|
record(alice.ID, "Army of Me", "a3", listening.Play, 30_000, recent)
|
|
record(bob.ID, "Airbag", "b1", listening.Complete, 1, recent) // somebody else's
|
|
// Two artists credited on one track: both are credited with the event.
|
|
if _, err := db.Exec("INSERT INTO track_artists (track_id, artist_id, role) VALUES (?, ?, 'main')",
|
|
s.track["Song A"], artistID(t, db, "Artist B")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
record(alice.ID, "Song A", "a4", listening.Complete, 100_000, recent)
|
|
|
|
got, err := NewTasteRepo(db).ArtistEvents(ctx, alice.ID, time.Unix(1_600_000_000, 0))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
by := map[int64][]taste.Event{}
|
|
for _, e := range got {
|
|
by[e.ArtistID] = append(by[e.ArtistID], e)
|
|
}
|
|
radiohead, bjork := artistID(t, db, "Radiohead"), artistID(t, db, "Björk")
|
|
if len(by[radiohead]) != 1 || by[radiohead][0].Type != listening.Complete || by[radiohead][0].PlayedMs != 200_000 ||
|
|
!by[radiohead][0].At.Equal(recent) {
|
|
t.Errorf("Radiohead events = %+v", by[radiohead])
|
|
}
|
|
if len(by[bjork]) != 1 || by[bjork][0].Type != listening.Play {
|
|
t.Errorf("Björk events = %+v", by[bjork])
|
|
}
|
|
if len(by[artistID(t, db, "Artist A")]) != 1 {
|
|
t.Errorf("the artist credited on Song A has %d events, want 1", len(by[artistID(t, db, "Artist A")]))
|
|
}
|
|
if len(by[artistID(t, db, "Artist B")]) != 1 || by[artistID(t, db, "Artist B")][0].PlayedMs != 100_000 {
|
|
t.Errorf("the second artist credited on Song A has %+v, want the same event", by[artistID(t, db, "Artist B")])
|
|
}
|
|
if len(got) != 4 {
|
|
t.Errorf("got %d events, want 4: %+v", len(got), got)
|
|
}
|
|
|
|
// A track whose files are gone still counts: the history is the listener's own.
|
|
record(alice.ID, "Ghost", "a5", listening.Complete, 100_000, recent)
|
|
got, _ = NewTasteRepo(db).ArtistEvents(ctx, alice.ID, time.Unix(1_600_000_000, 0))
|
|
if len(got) != 5 {
|
|
t.Errorf("with a track that has no file: %d events, want 5", len(got))
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestTasteArtistNames(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
seedCatalog(t, db)
|
|
repo := NewTasteRepo(db)
|
|
radiohead := artistID(t, db, "Radiohead")
|
|
got, err := repo.ArtistNames(context.Background(), []int64{radiohead, 99999})
|
|
if err != nil || len(got) != 1 || got[radiohead] != "Radiohead" {
|
|
t.Errorf("names = %v err=%v", got, err)
|
|
}
|
|
if got, err := repo.ArtistNames(context.Background(), nil); err != nil || len(got) != 0 {
|
|
t.Errorf("no ids: %v err=%v", got, err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestTasteArtistTags(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
s := seedCatalog(t, db)
|
|
ctx := context.Background()
|
|
radiohead, bjork := artistID(t, db, "Radiohead"), artistID(t, db, "Björk")
|
|
|
|
// Genres of the files: Radiohead has 4 tracks. Ghost's file is gone, so its
|
|
// genre says nothing about the music that is there: 3 rock and 2 art rock.
|
|
for _, g := range []struct{ track, genre string }{
|
|
{"Airbag", "rock"}, {"Paranoid Android", "rock"}, {"Karma Police", "rock"}, {"Ghost", "rock"},
|
|
{"Airbag", "art rock"}, {"Karma Police", "art rock"},
|
|
} {
|
|
if _, err := db.Exec("INSERT INTO track_genres (track_id, genre) VALUES (?, ?)", s.track[g.track], g.genre); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if _, err := db.Exec("INSERT INTO track_genres (track_id, genre) VALUES (?, 'trip hop')", s.track["Ghost"]); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Provider tags: Last.fm is 0-100, MusicBrainz is votes.
|
|
for _, row := range []struct {
|
|
artist int64
|
|
provider string
|
|
tag string
|
|
weight float64
|
|
}{
|
|
{radiohead, "lastfm", "alternative", 100},
|
|
{radiohead, "lastfm", "rock", 40}, // the files say 1: the larger weight wins
|
|
{radiohead, "musicbrainz", "electronic", 4},
|
|
{radiohead, "musicbrainz", "alternative", 8}, // 8 of 8 votes: 1
|
|
{bjork, "lastfm", "icelandic", 50},
|
|
} {
|
|
if _, err := db.Exec("INSERT INTO artist_tags (artist_id, provider, tag, weight) VALUES (?, ?, ?, ?)",
|
|
row.artist, row.provider, row.tag, row.weight); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
got, err := NewTasteRepo(db).ArtistTags(ctx, []int64{radiohead, bjork, artistID(t, db, "Miles Davis")})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := map[string]float64{"rock": 1, "art rock": 2.0 / 3, "alternative": 1, "electronic": 0.5}
|
|
if len(got[radiohead]) != len(want) {
|
|
t.Errorf("Radiohead tags = %v, want %v", got[radiohead], want)
|
|
}
|
|
for tag, w := range want {
|
|
if math.Abs(got[radiohead][tag]-w) > 1e-9 {
|
|
t.Errorf("Radiohead %q = %v, want %v", tag, got[radiohead][tag], w)
|
|
}
|
|
}
|
|
if got[bjork]["icelandic"] != 0.5 || len(got[bjork]) != 1 {
|
|
t.Errorf("Björk tags = %v", got[bjork])
|
|
}
|
|
if len(got[artistID(t, db, "Miles Davis")]) != 0 {
|
|
t.Errorf("an artist with no tags has %v", got[artistID(t, db, "Miles Davis")])
|
|
}
|
|
if got, err := NewTasteRepo(db).ArtistTags(ctx, nil); err != nil || len(got) != 0 {
|
|
t.Errorf("no ids: %v err=%v", got, err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestTheHeartsOfAListenerAreCountedPerArtist(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
s := seedCatalog(t, db)
|
|
repo := NewTasteRepo(db)
|
|
fav := NewFavoritesRepo(db)
|
|
uid, other := seedUser(t, db, "a@example.org"), seedUser(t, db, "b@example.org")
|
|
id := func(table, col, v string) int64 {
|
|
var n int64
|
|
if err := db.QueryRowContext(ctx, "SELECT id FROM "+table+" WHERE "+col+" = ?", v).Scan(&n); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return n
|
|
}
|
|
radiohead, bjork, miles := id("artists", "name", "Radiohead"), id("artists", "name", "Björk"), id("artists", "name", "Miles Davis")
|
|
at := time.Unix(1_700_000_000, 0)
|
|
add := func(user int64, kind favorites.Kind, item int64) {
|
|
t.Helper()
|
|
if err := fav.Add(ctx, user, kind, item, at); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
add(uid, favorites.Artist, radiohead)
|
|
add(uid, favorites.Album, id("albums", "title", "OK Computer")) // credited to Radiohead
|
|
add(uid, favorites.Track, s.track["Airbag"]) // Radiohead
|
|
add(uid, favorites.Track, s.track["Karma Police"]) // Radiohead
|
|
add(uid, favorites.Track, s.track["Army of Me"]) // Björk
|
|
add(uid, favorites.Album, id("albums", "title", "Various Hits"))
|
|
add(other, favorites.Artist, miles) // somebody else's
|
|
|
|
got, err := repo.ArtistFavorites(ctx, uid)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if r := got[radiohead]; !r.Artist || r.Albums != 1 || r.Tracks != 2 {
|
|
t.Errorf("Radiohead = %+v", r)
|
|
}
|
|
if b := got[bjork]; b.Artist || b.Albums != 0 || b.Tracks != 1 {
|
|
t.Errorf("Björk = %+v", b)
|
|
}
|
|
if _, ok := got[miles]; ok {
|
|
t.Error("another person's heart counted")
|
|
}
|
|
// The album of two other artists is credited to its album artist, Various Artists.
|
|
if va := got[id("artists", "name", "Various Artists")]; va.Albums != 1 {
|
|
t.Errorf("Various Artists = %+v", va)
|
|
}
|
|
if none, err := repo.ArtistFavorites(ctx, other+100); err != nil || len(none) != 0 {
|
|
t.Errorf("a person with no hearts: %v %v", none, err)
|
|
}
|
|
})
|
|
}
|