Files
minstrel/internal/recommendation/spread_test.go
T
bvandeusenandClaude Opus 5.5 27e3725e54
release / web (push) Successful in 1m17s
release / go (push) Failing after 1m42s
release / govulncheck (push) Successful in 17s
release / integration (push) Failing after 3m57s
release / android (push) Successful in 4m23s
release / Build signed APK (push) Successful in 4m50s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Skipped
release / Publish the release image to GHCR (dispatch only) (push) Skipped
release / Verify release artifacts (tag releases only) (push) Skipped
fix(recommendation): radio and shuffle keep one artist's songs apart (#8216)
Radio returned its picks in score order, and a seed's own artist matches
it best, so a DROELOE radio opened with four DROELOE songs in a row. The
artist cap bounded how many songs an artist got, not where they played.

SpreadArtists reorders picks so an artist doesn't play again within
three songs where another choice exists. Each slot takes the best
remaining pick outside that gap; when none is, the artist heard longest
ago goes next, so two artists alternate. Which songs are picked doesn't
change. Radio counts the seed's artist as just played; the library
shuffle spreads from a blank start.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 06:48:04 -05:00

102 lines
3.5 KiB
Go

package recommendation
import (
"fmt"
"slices"
"testing"
"github.com/jackc/pgx/v5/pgtype"
)
// tooClose lists each pick whose artist already played within artistGap songs
// before it, counting lastPlayed as the songs heard just before the picks.
func tooClose(picks []Candidate, lastPlayed ...pgtype.UUID) []string {
var heard []pgtype.UUID
heard = append(heard, lastPlayed...)
var out []string
for _, c := range picks {
a := c.Track.ArtistID
from := max(0, len(heard)-artistGap)
if a.Valid && slices.Contains(heard[from:], a) {
out = append(out, c.Track.Title)
}
heard = append(heard, a)
}
return out
}
func artistOf(t *testing.T, artist string) pgtype.UUID {
t.Helper()
var id pgtype.UUID
if err := id.Scan("00000000-0000-0000-0001-" + artist); err != nil {
t.Fatal(err)
}
return id
}
// A radio seeded from one artist: that artist's songs match best, so in score
// order they come first, back to back, right after the seed. Spread, the seed's
// artist waits artistGap songs and then never plays twice within the gap, and
// the radio holds exactly the same songs.
func TestSpreadArtists_TheSeedArtistDoesNotOpenTheRadioBackToBack(t *testing.T) {
const seed = "00000000000d"
var picks []Candidate
for i := 0; i < 4; i++ {
picks = append(picks, candBy(t, fmt.Sprintf("d%011d", i), seed, "000000000001", ScoringInputs{}))
}
// Twelve others: room for the seed artist's four songs each artistGap
// apart after the seed, as a 50-song radio capped at 6 per artist has.
for i := 0; i < 12; i++ {
picks = append(picks, candBy(t, fmt.Sprintf("o%011d", i), fmt.Sprintf("%012d", 100+i), fmt.Sprintf("%012d", 100+i), ScoringInputs{}))
}
seedArtist := artistOf(t, seed)
// The check below has to be able to fail: score order itself breaks it.
if len(tooClose(picks, seedArtist)) == 0 {
t.Fatal("score order already keeps the seed artist apart, so this fixture proves nothing")
}
spread := SpreadArtists(picks, seedArtist)
if bad := tooClose(spread, seedArtist); len(bad) > 0 {
t.Errorf("%v play within %d songs of the same artist: %v", bad, artistGap, titles(spread))
}
if got, want := titles(spread)[0], "o00000000000"; got != want {
t.Errorf("first pick %s, want %s: the best pick by another artist", got, want)
}
if !sameSongs(spread, picks) {
t.Errorf("spreading changed which songs were picked: %v from %v", titles(spread), titles(picks))
}
}
// Two artists can't keep artistGap apart, so they alternate rather than
// clumping.
func TestSpreadArtists_TwoArtistsAlternate(t *testing.T) {
var picks []Candidate
for i := 0; i < 3; i++ {
picks = append(picks, candBy(t, fmt.Sprintf("a%011d", i), "00000000000a", "000000000001", ScoringInputs{}))
}
for i := 0; i < 3; i++ {
picks = append(picks, candBy(t, fmt.Sprintf("b%011d", i), "00000000000b", "000000000002", ScoringInputs{}))
}
got := titles(SpreadArtists(picks))
want := []string{"a00000000000", "b00000000000", "a00000000001", "b00000000001", "a00000000002", "b00000000002"}
if !slices.Equal(got, want) {
t.Errorf("got %v, want %v", got, want)
}
}
// Picks with no known artist are never held back, so their order stands.
func TestSpreadArtists_UnknownArtistsKeepTheirOrder(t *testing.T) {
picks := []Candidate{cand("000000000001", ScoringInputs{}), cand("000000000002", ScoringInputs{}), cand("000000000003", ScoringInputs{})}
if got := titles(SpreadArtists(picks)); !slices.Equal(got, titles(picks)) {
t.Errorf("got %v, want the order unchanged", got)
}
}
func sameSongs(a, b []Candidate) bool {
x, y := titles(a), titles(b)
slices.Sort(x)
slices.Sort(y)
return slices.Equal(x, y)
}