sonda/internal/storage/stats_test.go
tgiachi 08c83c948f feat: how the genres I listen to changed over time
- GET /stats/listening has genre_timeline: the share of the five biggest genres, of the smaller ones and of the songs with no genre, for each year, or for each month when all the listens are in one year, in the listener's time zone; years and months with no listen are empty
- web: a stacked column chart on the Statistics page, coloured in tints of the colour of this server so it follows every theme, with a legend; left out when there is only one column
- README: the new field
2026-11-15 18:17:33 +01:00

287 lines
11 KiB
Go

package storage
import (
"context"
"testing"
"time"
"github.com/uptrace/bun"
"git.kmalloc.org/squid/sonda/internal/listening"
"git.kmalloc.org/squid/sonda/internal/stats"
)
type statsWorld struct {
alice, bob int64
debaser, mind int64
song2, karma int64
repo *StatsRepo
listens *ListeningRepo
ctx context.Context
db *bun.DB
firstListenAt time.Time
jan, jun, next time.Time
}
// seedStats builds a small library: Debaser and Where Is My Mind (Pixies), Song 2 (Blur), and Karma Police
// (Radiohead) whose file is missing. Debaser also has an mp3.
func seedStats(t *testing.T, db *bun.DB) statsWorld {
t.Helper()
ctx := context.Background()
users := NewUserRepo(db)
alice, err := users.CreateUser(ctx, "alice", "h", false)
if err != nil {
t.Fatal(err)
}
bob, err := users.CreateUser(ctx, "bob", "h", false)
if err != nil {
t.Fatal(err)
}
lib, err := NewLibraryRepo(db).CreateLibrary(ctx, "music", "/music")
if err != nil {
t.Fatal(err)
}
w := statsWorld{alice: alice.ID, bob: bob.ID, repo: NewStatsRepo(db), listens: NewListeningRepo(db), ctx: ctx, db: db}
w.debaser = affinitySong(t, db, lib.ID, "a.flac", "Debaser", "Pixies")
w.mind = affinitySong(t, db, lib.ID, "b.flac", "Where Is My Mind", "Pixies")
w.song2 = affinitySong(t, db, lib.ID, "c.flac", "Song 2", "Blur")
w.karma = affinitySong(t, db, lib.ID, "d.flac", "Karma Police", "Radiohead")
exec := func(q string, args ...any) {
t.Helper()
if _, err := db.ExecContext(ctx, q, args...); err != nil {
t.Fatalf("%s: %v", q, err)
}
}
exec("UPDATE audio_files SET missing_at = 1 WHERE track_id = ?", w.karma)
exec("INSERT INTO audio_files (track_id, library_id, rel_path, format, size_bytes, mtime_unix) VALUES (?, ?, 'a.mp3', 'mp3', 50, 1)", w.debaser, lib.ID)
for id, year := range map[int64]int{w.debaser: 1989, w.mind: 1988, w.song2: 1994, w.karma: 1990} {
exec("UPDATE tracks SET year = ? WHERE id = ?", year, id)
}
for _, g := range []struct {
track int64
genre string
}{{w.debaser, "rock"}, {w.debaser, "alternative"}, {w.mind, "rock"}, {w.song2, "britpop"}, {w.karma, "rock"}} {
exec("INSERT INTO track_genres (track_id, genre) VALUES (?, ?)", g.track, g.genre)
}
at := func(y int, m time.Month, d int) time.Time { return time.Date(y, m, d, 12, 0, 0, 0, time.UTC) }
w.jan, w.jun, w.next = at(2025, 1, 10), at(2025, 6, 10), at(2026, 2, 1)
w.firstListenAt = w.jan
n := 0
rec := func(user, track int64, typ listening.EventType, when time.Time, played int64) {
t.Helper()
n++
if _, err := w.listens.Record(ctx, listening.Event{UserID: user, TrackID: track, Type: typ, OccurredAt: when, DurationPlayedMs: played, IdempotencyKey: "k" + string(rune('a'+n))}); err != nil {
t.Fatal(err)
}
}
rec(alice.ID, w.debaser, listening.Complete, w.jan, 200_000)
rec(alice.ID, w.debaser, listening.Complete, w.jun, 200_000)
rec(alice.ID, w.debaser, listening.Complete, w.next, 200_000)
rec(alice.ID, w.song2, listening.Complete, at(2025, 3, 3), 200_000)
rec(alice.ID, w.mind, listening.Play, at(2025, 4, 4), 30_000) // long enough to count
rec(alice.ID, w.karma, listening.Play, at(2025, 4, 5), 5_000) // too short
rec(alice.ID, w.debaser, listening.Skip, at(2025, 4, 6), 1_000)
rec(bob.ID, w.debaser, listening.Complete, at(2025, 5, 5), 200_000) // someone else
return w
}
func TestTheLibraryStatsCountWhatCanBePlayed(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
w := seedStats(t, db)
got, err := w.repo.Totals(w.ctx)
if err != nil {
t.Fatal(err)
}
want := stats.Totals{Artists: 2, Albums: 2, Songs: 3, DurationMs: 600_000, SizeBytes: 350}
if got != want {
t.Errorf("totals = %+v, want %+v (Karma Police has no file left, so Radiohead is not counted)", got, want)
}
formats, err := w.repo.Formats(w.ctx)
if err != nil || len(formats) != 2 {
t.Fatalf("formats = %+v, %v", formats, err)
}
by := map[string]stats.FormatCount{}
for _, f := range formats {
by[f.Format] = f
}
if by["flac"].Files != 3 || by["flac"].Bytes != 300 || by["mp3"].Files != 1 || by["mp3"].Bytes != 50 {
t.Errorf("formats = %+v", by)
}
genres, tagged, err := w.repo.Genres(w.ctx)
if err != nil || tagged != 3 {
t.Fatalf("genres = %+v, tagged %d, %v", genres, tagged, err)
}
g := map[string]stats.GenreWeight{}
for _, x := range genres {
g[x.Genre] = x
}
if len(g) != 3 || g["rock"].Songs != 2 || g["rock"].Weight != 1.5 || g["alternative"].Weight != 0.5 || g["britpop"].Weight != 1 {
t.Errorf("genres = %+v (the genre of the song with no file is not counted)", g)
}
years, err := w.repo.Years(w.ctx)
if err != nil {
t.Fatal(err)
}
ys := map[int]int{}
for _, y := range years {
ys[y.Year] = y.Songs
}
if len(ys) != 3 || ys[1988] != 1 || ys[1989] != 1 || ys[1994] != 1 {
t.Errorf("years = %+v", years)
}
})
}
func TestTheListeningStatsAreOfOnePersonAndOnlyWhatCounts(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
w := seedStats(t, db)
minMs := int64(20_000)
all := stats.Range{}
tot, err := w.repo.ListenTotals(w.ctx, w.alice, minMs, all)
if err != nil {
t.Fatal(err)
}
want := stats.ListenTotals{Listens: 5, PlayedMs: 830_000, Songs: 3, Artists: 2, First: w.firstListenAt.Unix()}
if tot != want {
t.Errorf("totals = %+v, want %+v", tot, want)
}
times, err := w.repo.ListenTimes(w.ctx, w.alice, minMs, all)
if err != nil || len(times) != 5 {
t.Fatalf("times = %v, %v", times, err)
}
artists, err := w.repo.TopArtists(w.ctx, w.alice, minMs, all, 10)
if err != nil || len(artists) != 2 || artists[0].Name != "Pixies" || artists[0].Listens != 4 || artists[1].Name != "Blur" || artists[1].Listens != 1 {
t.Errorf("artists = %+v, %v", artists, err)
}
if artists[0].ID == 0 {
t.Error("an artist has an id")
}
songs, err := w.repo.TopSongs(w.ctx, w.alice, minMs, all, 10)
if err != nil || len(songs) != 3 {
t.Fatalf("songs = %+v, %v", songs, err)
}
if songs[0].Name != "Debaser" || songs[0].Listens != 3 || len(songs[0].Artists) != 1 || songs[0].Artists[0] != "Pixies" || songs[0].ID != w.debaser {
t.Errorf("first song = %+v", songs[0])
}
if songs[1].Name != "Song 2" || songs[2].Name != "Where Is My Mind" { // a tie: by title
t.Errorf("tie = %q, %q", songs[1].Name, songs[2].Name)
}
if one, _ := w.repo.TopSongs(w.ctx, w.alice, minMs, all, 1); len(one) != 1 {
t.Errorf("limit 1 gave %d", len(one))
}
genres, tagged, err := w.repo.ListenGenres(w.ctx, w.alice, minMs, all)
if err != nil || tagged != 5 {
t.Fatalf("genres = %+v, tagged %d, %v", genres, tagged, err)
}
g := map[string]stats.GenreWeight{}
for _, x := range genres {
g[x.Genre] = x
}
if g["rock"].Songs != 4 || g["rock"].Weight != 2.5 || g["alternative"].Songs != 3 || g["alternative"].Weight != 1.5 || g["britpop"].Weight != 1 {
t.Errorf("listen genres = %+v", g)
}
})
}
func TestTheListeningStatsCanBeCutToAPeriod(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
w := seedStats(t, db)
year := stats.Range{From: time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC), Until: time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)}
tot, err := w.repo.ListenTotals(w.ctx, w.alice, 20_000, year)
if err != nil || tot.Listens != 4 || tot.Songs != 3 {
t.Errorf("2025 = %+v, %v, want 4 listens (not the one of 2026)", tot, err)
}
since := stats.Range{From: time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)}
tot, _ = w.repo.ListenTotals(w.ctx, w.alice, 20_000, since)
if tot.Listens != 1 || tot.First != w.next.Unix() {
t.Errorf("since 2026 = %+v", tot)
}
songs, _ := w.repo.TopSongs(w.ctx, w.alice, 20_000, year, 10)
if songs[0].Listens != 2 {
t.Errorf("Debaser in 2025 = %d, want 2", songs[0].Listens)
}
none := stats.Range{From: time.Date(2030, 1, 1, 0, 0, 0, 0, time.UTC)}
tot, err = w.repo.ListenTotals(w.ctx, w.alice, 20_000, none)
if err != nil || tot != (stats.ListenTotals{}) {
t.Errorf("a period with no listens = %+v, %v", tot, err)
}
if times, _ := w.repo.ListenTimes(w.ctx, w.alice, 20_000, none); len(times) != 0 {
t.Errorf("times = %v", times)
}
})
}
func TestTheStatsOfAPersonWithNoListensAreEmpty(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
w := seedStats(t, db)
carol, err := NewUserRepo(db).CreateUser(w.ctx, "carol", "h", false)
if err != nil {
t.Fatal(err)
}
tot, err := w.repo.ListenTotals(w.ctx, carol.ID, 20_000, stats.Range{})
if err != nil || tot != (stats.ListenTotals{}) {
t.Errorf("totals = %+v, %v", tot, err)
}
if a, err := w.repo.TopArtists(w.ctx, carol.ID, 20_000, stats.Range{}, 10); err != nil || len(a) != 0 {
t.Errorf("artists = %+v, %v", a, err)
}
if _, tagged, err := w.repo.ListenGenres(w.ctx, carol.ID, 20_000, stats.Range{}); err != nil || tagged != 0 {
t.Errorf("tagged = %d, %v", tagged, err)
}
})
}
func TestEachListenCarriesTheGenresOfItsSongAndTheyWeighOne(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
w := seedStats(t, db)
lib, _ := NewLibraryRepo(db).ListLibraries(w.ctx)
bare := affinitySong(t, db, lib[0].ID, "e.flac", "Untitled", "Nobody") // a song with no genre
if _, err := w.listens.Record(w.ctx, listening.Event{UserID: w.alice, TrackID: bare, Type: listening.Complete, OccurredAt: w.jun.Add(time.Hour), DurationPlayedMs: 100_000, IdempotencyKey: "bare"}); err != nil {
t.Fatal(err)
}
rows, err := w.repo.ListenGenreTimes(w.ctx, w.alice, 20_000, stats.Range{})
if err != nil {
t.Fatal(err)
}
// Debaser three times with two genres (6 rows), Where Is My Mind, Song 2 and the song with no genre (1 row each).
if len(rows) != 9 {
t.Fatalf("%d rows: %+v", len(rows), rows)
}
perListen := map[int64]float64{}
genres := map[string]float64{}
for _, r := range rows {
perListen[r.At] += r.Weight
genres[r.Genre] += r.Weight
}
if len(perListen) != 6 {
t.Errorf("%d listens, want 6", len(perListen))
}
for at, sum := range perListen {
if sum != 1 {
t.Errorf("the listen at %d weighs %v, want 1", at, sum)
}
}
if genres["rock"] != 2.5 || genres["alternative"] != 1.5 || genres["britpop"] != 1 || genres[""] != 1 {
t.Errorf("genres = %v (the empty one is the song with no genre)", genres)
}
year := stats.Range{From: time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC), Until: time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)}
if cut, _ := w.repo.ListenGenreTimes(w.ctx, w.alice, 20_000, year); len(cut) != 7 { // not the listen of 2026 (2 rows), nor bob's
t.Errorf("2025 has %d rows, want 7", len(cut))
}
bob, _ := w.repo.ListenGenreTimes(w.ctx, w.bob, 20_000, stats.Range{})
if len(bob) != 2 {
t.Errorf("bob has %d rows, want the 2 of one listen", len(bob))
}
})
}