sonda/internal/storage/mixes_test.go
tgiachi 407b20334a feat: radio stations that Sonda makes by itself
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
2026-11-16 12:13:55 +01:00

429 lines
15 KiB
Go

package storage
import (
"context"
"slices"
"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/mixes"
)
// mixEnv is a library with songs of known genres and artists, and a user.
type mixEnv struct {
t *testing.T
db *bun.DB
repo *MixRepo
user int64
tracks map[string]int64
artist map[string]int64
}
// mixSong is one file: its title, artists, genres.
type mixSong struct {
title, artist string
genres []string
}
func newMixEnv(t *testing.T, db *bun.DB, songs []mixSong) *mixEnv {
t.Helper()
ctx := context.Background()
u, err := NewUserRepo(db).CreateUser(ctx, "a@example.org", "hash", false)
if err != nil {
t.Fatal(err)
}
lib := NewLibraryRepo(db)
l, err := lib.CreateLibrary(ctx, "m", "/lib/m")
if err != nil {
t.Fatal(err)
}
e := &mixEnv{t: t, db: db, repo: NewMixRepo(db), user: u.ID, tracks: map[string]int64{}, artist: map[string]int64{}}
for i, s := range songs {
f := pf("x/"+s.title+".flac", s.title, "Album "+s.artist, s.artist, i+1, 1, "flac", s.artist)
f.Tags.Genres = s.genres
if _, err := lib.SaveFile(ctx, l.ID, f, 0); err != nil {
t.Fatal(err)
}
var id int64
if err := db.QueryRow("SELECT id FROM tracks WHERE title = ?", s.title).Scan(&id); err != nil {
t.Fatal(err)
}
e.tracks[s.title] = id
}
rows, err := db.Query("SELECT id, name FROM artists")
if err != nil {
t.Fatal(err)
}
defer rows.Close()
for rows.Next() {
var id int64
var name string
if err := rows.Scan(&id, &name); err != nil {
t.Fatal(err)
}
e.artist[name] = id
}
return e
}
func mixBackends(t *testing.T, songs []mixSong, fn func(t *testing.T, e *mixEnv)) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { fn(t, newMixEnv(t, db, songs)) })
}
var mixSongs = []mixSong{
{"r1", "Queen", []string{"rock", "classic rock"}},
{"r2", "Queen", []string{"rock"}},
{"r3", "Kiss", []string{"rock"}},
{"m1", "Slayer", []string{"metal"}},
{"m2", "Slayer", []string{"metal"}},
{"j1", "Miles", []string{"jazz+funk"}},
{"n1", "Nobody", nil},
}
func TestMixRepoGenresCountSongsAndArtistsOfThePlayableOnes(t *testing.T) {
mixBackends(t, mixSongs, func(t *testing.T, e *mixEnv) {
got, err := e.repo.Genres(context.Background())
if err != nil {
t.Fatal(err)
}
by := map[string]mixes.Genre{}
for _, g := range got {
by[g.Name] = g
}
if g := by["rock"]; g.Tracks != 3 || g.Artists != 2 {
t.Errorf("rock = %+v, want 3 songs by 2 artists", g)
}
if g := by["metal"]; g.Tracks != 2 || g.Artists != 1 {
t.Errorf("metal = %+v", g)
}
if _, ok := by["jazz+funk"]; !ok {
t.Error("the spelling of the file is kept")
}
// A song whose files are gone is not counted.
if _, err := e.db.Exec("UPDATE audio_files SET missing_at = 1 WHERE track_id = ?", e.tracks["r3"]); err != nil {
t.Fatal(err)
}
got, _ = e.repo.Genres(context.Background())
for _, g := range got {
if g.Name == "rock" && (g.Tracks != 2 || g.Artists != 1) {
t.Errorf("rock after a song went = %+v", g)
}
}
})
}
func TestMixRepoPoolByGenreOrByArtistGivesPlayableSongsWithTheirArtist(t *testing.T) {
mixBackends(t, mixSongs, func(t *testing.T, e *mixEnv) {
ctx := context.Background()
ids := func(refs []mixes.TrackRef) []int64 {
out := make([]int64, len(refs))
for i, r := range refs {
out[i] = r.TrackID
}
slices.Sort(out)
return out
}
rock, err := e.repo.Pool(ctx, mixes.Filter{Genres: []string{"rock"}})
if err != nil {
t.Fatal(err)
}
want := []int64{e.tracks["r1"], e.tracks["r2"], e.tracks["r3"]}
slices.Sort(want)
if !slices.Equal(ids(rock), want) {
t.Errorf("rock pool = %v, want %v", ids(rock), want)
}
for _, r := range rock {
if r.TrackID == e.tracks["r3"] && r.ArtistID != e.artist["Kiss"] {
t.Errorf("artist of r3 = %d, want Kiss", r.ArtistID)
}
}
both, _ := e.repo.Pool(ctx, mixes.Filter{Genres: []string{"metal"}, ArtistIDs: []int64{e.artist["Miles"]}})
if len(both) != 3 {
t.Errorf("metal or Miles = %d songs, want 3", len(both))
}
if none, _ := e.repo.Pool(ctx, mixes.Filter{}); len(none) != 0 {
t.Errorf("an empty filter gave %d songs", len(none))
}
if _, err := e.db.Exec("UPDATE audio_files SET missing_at = 1 WHERE track_id = ?", e.tracks["r1"]); err != nil {
t.Fatal(err)
}
if again, _ := e.repo.Pool(ctx, mixes.Filter{Genres: []string{"rock"}}); len(again) != 2 {
t.Errorf("a song with no file is in the pool: %d songs", len(again))
}
})
}
func TestMixRepoGenreArtistsAreTheOnesWithMostSongs(t *testing.T) {
mixBackends(t, mixSongs, func(t *testing.T, e *mixEnv) {
got, err := e.repo.GenreArtists(context.Background(), []string{"rock", "metal"}, 2)
if err != nil || len(got) != 2 {
t.Fatalf("artists = %v err %v", got, err)
}
if got[0] != "Queen" && got[0] != "Slayer" { // both have two songs
t.Errorf("first = %q", got[0])
}
all, _ := e.repo.GenreArtists(context.Background(), []string{"rock"}, 10)
if !slices.Equal(all, []string{"Queen", "Kiss"}) {
t.Errorf("rock artists = %v, want Queen (2 songs) then Kiss", all)
}
})
}
func TestMixRepoGenreArtistsLeaveOutPlaceholderNames(t *testing.T) {
songs := append([]mixSong{}, mixSongs...)
songs = append(songs, mixSong{"u1", "Unknown Artist", []string{"rock"}}, mixSong{"u2", "Unknown Artist", []string{"rock"}},
mixSong{"u3", "Unknown Artist", []string{"rock"}}, mixSong{"v1", "Various Artists", []string{"rock"}})
mixBackends(t, songs, func(t *testing.T, e *mixEnv) {
got, err := e.repo.GenreArtists(context.Background(), []string{"rock"}, 10)
if err != nil || !slices.Equal(got, []string{"Queen", "Kiss"}) {
t.Errorf("rock artists = %v err %v: the names that stand for nobody are not artists to show", got, err)
}
})
}
func TestMixRepoArtistGenresCountTheSongsOfEachGenre(t *testing.T) {
mixBackends(t, mixSongs, func(t *testing.T, e *mixEnv) {
got, err := e.repo.ArtistGenres(context.Background(), []int64{e.artist["Queen"], e.artist["Miles"]})
if err != nil {
t.Fatal(err)
}
if g := got[e.artist["Queen"]]; g["rock"] != 2 || g["classic rock"] != 1 {
t.Errorf("Queen = %v", g)
}
if g := got[e.artist["Miles"]]; g["jazz+funk"] != 1 {
t.Errorf("Miles = %v", g)
}
})
}
func TestMixRepoHistoryCountsListensAndSkipsLikeTheTasteModel(t *testing.T) {
mixBackends(t, mixSongs, func(t *testing.T, e *mixEnv) {
ctx := context.Background()
rec := listening.NewService(NewListeningRepo(e.db), 20*time.Second)
t0 := time.Now().Add(-48 * time.Hour).Truncate(time.Second)
ev := func(title string, typ listening.EventType, playedMs int64, at time.Time, key string) {
t.Helper()
if _, err := rec.Record(ctx, listening.Event{UserID: e.user, TrackID: e.tracks[title], Type: typ, DurationPlayedMs: playedMs, OccurredAt: at, IdempotencyKey: key}); err != nil {
t.Fatalf("record %s: %v", key, err)
}
}
ev("r1", listening.Complete, 59000, t0, "a")
ev("r1", listening.Play, 30000, t0.Add(time.Hour), "b") // 30 s: a listen
ev("r1", listening.Play, 5000, t0.Add(2*time.Hour), "c") // 5 s: nothing
ev("r2", listening.Skip, 4000, t0, "d") // skipped before enough: a skip
ev("r3", listening.Skip, 40000, t0, "e") // skipped after 40 s: a listen, not a dislike
h, err := e.repo.History(ctx, e.user, 20*time.Second)
if err != nil {
t.Fatal(err)
}
r1 := h[e.tracks["r1"]]
if r1.Plays != 2 || !r1.LastPlayed.Equal(t0.Add(time.Hour)) || !r1.SkippedAt.IsZero() {
t.Errorf("r1 = %+v", r1)
}
if r2 := h[e.tracks["r2"]]; r2.Plays != 0 || !r2.SkippedAt.Equal(t0) {
t.Errorf("r2 = %+v", r2)
}
if r3 := h[e.tracks["r3"]]; r3.Plays != 1 || !r3.SkippedAt.IsZero() {
t.Errorf("r3 = %+v", r3)
}
if _, ok := h[e.tracks["m1"]]; ok {
t.Error("a song with no event is in the history")
}
if other, _ := e.repo.History(ctx, e.user+99, 20*time.Second); len(other) != 0 {
t.Errorf("another user has history: %v", other)
}
})
}
func TestMixRepoTracksComeInTheOrderAskedWithoutTheOnesThatCannotBePlayed(t *testing.T) {
mixBackends(t, mixSongs, func(t *testing.T, e *mixEnv) {
ctx := context.Background()
ask := []int64{e.tracks["m2"], e.tracks["r1"], 999999, e.tracks["j1"], e.tracks["r1"]}
got, err := e.repo.Tracks(ctx, ask)
if err != nil {
t.Fatal(err)
}
var titles []string
for _, tr := range got {
titles = append(titles, tr.Title)
}
if !slices.Equal(titles, []string{"m2", "r1", "j1", "r1"}) {
t.Errorf("titles = %v: the order asked, the unknown one left out, a repeat kept", titles)
}
if got[0].Album == nil || len(got[0].Artists) != 1 || got[0].Format != "flac" {
t.Errorf("details = %+v", got[0])
}
if _, err := e.db.Exec("UPDATE audio_files SET missing_at = 1 WHERE track_id = ?", e.tracks["m2"]); err != nil {
t.Fatal(err)
}
got, _ = e.repo.Tracks(ctx, ask)
if len(got) != 3 || got[0].Title != "r1" {
t.Errorf("a song with no file was not left out: %d songs", len(got))
}
if none, err := e.repo.Tracks(ctx, nil); err != nil || len(none) != 0 {
t.Errorf("nothing asked: %v %v", none, err)
}
})
}
// cover gives an album a cover record with the status.
func (e *mixEnv) cover(album int64, status string) {
e.t.Helper()
if _, err := e.db.Exec("DELETE FROM images WHERE kind = 'album' AND owner_id = ?", album); err != nil { // the scan may have made one
e.t.Fatal(err)
}
if _, err := e.db.Exec("INSERT INTO images (kind, owner_id, status, checked_at) VALUES ('album', ?, ?, 1)", album, status); err != nil {
e.t.Fatal(err)
}
}
func (e *mixEnv) albumOf(title string) int64 {
e.t.Helper()
var id int64
if err := e.db.QueryRow("SELECT album_id FROM tracks WHERE id = ?", e.tracks[title]).Scan(&id); err != nil {
e.t.Fatal(err)
}
return id
}
func TestMixRepoCoversAreAlbumsWithACoverByDifferentArtistsWithMostSongsFirst(t *testing.T) {
songs := []mixSong{
{"r1", "Queen", []string{"rock"}}, {"r2", "Queen", []string{"rock"}}, {"r3", "Queen", []string{"rock"}},
{"k1", "Kiss", []string{"rock"}}, {"k2", "Kiss", []string{"rock"}},
{"a1", "ABBA", []string{"rock"}},
{"m1", "Slayer", []string{"metal"}}, {"m2", "Slayer", []string{"metal"}},
{"p1", "Pooh", []string{"pop"}},
}
mixBackends(t, songs, func(t *testing.T, e *mixEnv) {
ctx := context.Background()
queen, kiss, abba, slayer := e.albumOf("r1"), e.albumOf("k1"), e.albumOf("a1"), e.albumOf("m1")
// A second album of Queen, with one song, and a cover too: the artist is not shown twice.
var second int64
if err := e.db.QueryRow("INSERT INTO albums (title, artist_id, title_key, search_key) SELECT 'Second', artist_id, 'second', 'second' FROM albums WHERE id = ? RETURNING id", queen).Scan(&second); err != nil {
t.Fatal(err)
}
if _, err := e.db.Exec("UPDATE tracks SET album_id = ? WHERE id = ?", second, e.tracks["r3"]); err != nil {
t.Fatal(err)
}
e.cover(queen, "ok")
e.cover(second, "ok")
e.cover(kiss, "ok")
e.cover(abba, "none") // no cover
e.cover(slayer, "ok") // another genre
got, err := e.repo.Covers(ctx, mixes.Filter{Genres: []string{"rock"}}, 4)
if err != nil {
t.Fatal(err)
}
if want := []int64{queen, kiss}; !slices.Equal(got, want) {
t.Errorf("rock = %v, want %v: Queen once (the album with most songs), Kiss, and not the album with no cover", got, want)
}
if got, _ := e.repo.Covers(ctx, mixes.Filter{Genres: []string{"rock"}}, 1); !slices.Equal(got, []int64{queen}) {
t.Errorf("limit 1 = %v", got)
}
// By artist, and by genre or artist together.
got, _ = e.repo.Covers(ctx, mixes.Filter{ArtistIDs: []int64{e.artist["Slayer"]}}, 4)
if !slices.Equal(got, []int64{slayer}) {
t.Errorf("artist = %v", got)
}
got, _ = e.repo.Covers(ctx, mixes.Filter{Genres: []string{"metal"}, ArtistIDs: []int64{e.artist["Kiss"]}}, 4)
if len(got) != 2 {
t.Errorf("metal or Kiss = %v, want two albums", got)
}
// Nothing asked for, nothing given.
if got, _ := e.repo.Covers(ctx, mixes.Filter{}, 4); len(got) != 0 {
t.Errorf("no filter = %v", got)
}
// An album whose songs are all gone is not offered.
if _, err := e.db.Exec("UPDATE audio_files SET missing_at = 1 WHERE track_id IN (?, ?)", e.tracks["k1"], e.tracks["k2"]); err != nil {
t.Fatal(err)
}
got, _ = e.repo.Covers(ctx, mixes.Filter{Genres: []string{"rock"}}, 4)
if !slices.Equal(got, []int64{queen}) {
t.Errorf("after Kiss's files went = %v", got)
}
})
}
func TestHeartsAreTheSongsHeartedAndTheSongsOfHeartedAlbums(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
s := seedCatalog(t, db)
fav := NewFavoritesRepo(db)
uid, other := seedUser(t, db, "a@example.org"), seedUser(t, db, "b@example.org")
var ok int64
if err := db.QueryRowContext(ctx, "SELECT id FROM albums WHERE title = 'OK Computer'").Scan(&ok); err != nil {
t.Fatal(err)
}
at := time.Unix(1_700_000_000, 0)
_ = fav.Add(ctx, uid, favorites.Track, s.track["Airbag"], at)
_ = fav.Add(ctx, uid, favorites.Track, s.track["Army of Me"], at)
_ = fav.Add(ctx, uid, favorites.Album, ok, at)
_ = fav.Add(ctx, other, favorites.Track, s.track["So What"], at)
got, err := NewMixRepo(db).Hearts(ctx, uid)
if err != nil {
t.Fatal(err)
}
if h := got[s.track["Airbag"]]; !h.Track || !h.Album { // hearted, and on a hearted album
t.Errorf("Airbag = %+v", h)
}
if h := got[s.track["Karma Police"]]; h.Track || !h.Album {
t.Errorf("Karma Police = %+v", h)
}
if h := got[s.track["Army of Me"]]; !h.Track || h.Album {
t.Errorf("Army of Me = %+v", h)
}
if _, ok := got[s.track["So What"]]; ok {
t.Error("another person's heart counted")
}
})
}
func TestTheRadioKnowsTheDecadesAndTakesEverythingOrTheSongsOfSomeYears(t *testing.T) {
mixBackends(t, mixSongs, func(t *testing.T, e *mixEnv) {
ctx := context.Background()
years := map[string]int{"r1": 1975, "r2": 1979, "r3": 1991}
for title, y := range years {
if _, err := e.db.ExecContext(ctx, "UPDATE tracks SET year = ? WHERE id = ?", y, e.tracks[title]); err != nil {
t.Fatal(err)
}
}
// A song gone (no file to play) does not count.
decades, err := e.repo.Decades(ctx)
if err != nil {
t.Fatal(err)
}
got := map[int]mixes.Decade{}
for _, d := range decades {
got[d.Start] = d
}
if len(got) != 2 || got[1970].Tracks != 2 || got[1970].Artists != 1 || got[1990].Tracks != 1 {
t.Fatalf("decades %+v", decades)
}
all, err := e.repo.Pool(ctx, mixes.Filter{All: true})
if err != nil || len(all) != len(mixSongs) {
t.Fatalf("everything = %d songs, want %d (%v)", len(all), len(mixSongs), err)
}
seventies, err := e.repo.Pool(ctx, mixes.Filter{MinYear: 1970, MaxYear: 1979})
if err != nil || len(seventies) != 2 {
t.Fatalf("the seventies = %v %v", seventies, err)
}
// Years narrow what else was asked for.
rock70, err := e.repo.Pool(ctx, mixes.Filter{Genres: []string{"rock"}, MinYear: 1970, MaxYear: 1979})
if err != nil || len(rock70) != 2 {
t.Fatalf("rock of the seventies = %v %v", rock70, err)
}
if none, _ := e.repo.Pool(ctx, mixes.Filter{MinYear: 1950, MaxYear: 1959}); len(none) != 0 {
t.Fatalf("the fifties = %v", none)
}
if nothing, _ := e.repo.Pool(ctx, mixes.Filter{}); len(nothing) != 0 {
t.Fatalf("a filter that asks for nothing gave %v", nothing)
}
})
}