candBy scans the id into a UUID, so a title starting with 'o' failed to parse before the test ran. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
102 lines
3.5 KiB
Go
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("e%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], "e00000000000"; 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)
|
|
}
|