GET /mixes lists the mixes a listener has, GET /mixes/{id}?variant= gives one with its
songs, and POST /mixes/{id}/playlist keeps one as a playlist of their own.
- Genre mixes: one for each genre with at least 30 songs by 4 artists, the genres the
listener plays most first. Spellings that differ by case or punctuation are one
genre (jazz+funk, jazz-funk); other and unknown are not genres.
- Made for the listener: Your favourites, Rediscover (liked artists not played for a
month; both need history) and New to you (songs never played).
- A mix has up to 50 songs, at most 3 by one artist, never two of an artist in a row,
picked by weighted chance (artist preference, never-played, less if played lately;
skipped lately and avoided artists are out). The seed is user, mix, day and variant,
so a mix is steady for a day and another is one variant away.
- Worked out when asked, from the listener's own history and the genre tags; nothing is
stored and nothing leaves the server.
internal/mixes holds the selection and the service, internal/storage/mixes.go the
queries (SQLite and PostgreSQL). Spec, contract test, README and web types follow;
the web app comes separately.
Closes #95
122 lines
3.2 KiB
Go
122 lines
3.2 KiB
Go
package mixes
|
|
|
|
import (
|
|
"slices"
|
|
"testing"
|
|
)
|
|
|
|
// pool makes artists artists with perArtist songs each, all of weight 1. Song ids are 1, 2, 3, ...
|
|
func pool(artists, perArtist int) []Candidate {
|
|
var out []Candidate
|
|
id := int64(0)
|
|
for a := 1; a <= artists; a++ {
|
|
for i := 0; i < perArtist; i++ {
|
|
id++
|
|
out = append(out, Candidate{TrackID: id, ArtistID: int64(a), Weight: 1})
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func artistOf(c []Candidate) map[int64]int64 {
|
|
m := map[int64]int64{}
|
|
for _, x := range c {
|
|
m[x.TrackID] = x.ArtistID
|
|
}
|
|
return m
|
|
}
|
|
|
|
func TestPickTakesAtMostSizeSongsFromThePoolWithoutRepeats(t *testing.T) {
|
|
c := pool(20, 10)
|
|
got := Pick(c, 50, 3, 1)
|
|
if len(got) != 50 {
|
|
t.Fatalf("got %d songs, want 50", len(got))
|
|
}
|
|
seen := map[int64]bool{}
|
|
for _, id := range got {
|
|
if seen[id] || id < 1 || id > 200 {
|
|
t.Fatalf("song %d is repeated or not in the pool", id)
|
|
}
|
|
seen[id] = true
|
|
}
|
|
}
|
|
|
|
func TestPickGivesEverythingWhenThePoolIsSmallAndNothingWhenItIsEmpty(t *testing.T) {
|
|
if got := Pick(pool(4, 3), 50, 3, 1); len(got) != 12 {
|
|
t.Errorf("a pool of 12 gave %d songs", len(got))
|
|
}
|
|
if got := Pick(nil, 50, 3, 1); len(got) != 0 {
|
|
t.Errorf("an empty pool gave %v", got)
|
|
}
|
|
}
|
|
|
|
func TestPickIsTheSameForTheSameSeedAndDifferentForAnother(t *testing.T) {
|
|
c := pool(30, 10)
|
|
a, b := Pick(c, 50, 3, 7), Pick(c, 50, 3, 7)
|
|
if !slices.Equal(a, b) {
|
|
t.Error("the same seed gave two different mixes")
|
|
}
|
|
if slices.Equal(a, Pick(c, 50, 3, 8)) {
|
|
t.Error("another seed gave the same mix")
|
|
}
|
|
}
|
|
|
|
func TestPickKeepsAnArtistToItsShareAndRelaxesItWhenThereAreFewArtists(t *testing.T) {
|
|
c := pool(20, 10)
|
|
by := artistOf(c)
|
|
for seed := uint64(0); seed < 20; seed++ {
|
|
count := map[int64]int{}
|
|
for _, id := range Pick(c, 50, 3, seed) {
|
|
count[by[id]]++
|
|
}
|
|
for artist, n := range count {
|
|
if n > 3 {
|
|
t.Fatalf("seed %d: artist %d has %d songs, want at most 3", seed, artist, n)
|
|
}
|
|
}
|
|
}
|
|
// Two artists cannot fill ten places with three songs each: the cap gives way to the size.
|
|
few := pool(2, 20)
|
|
if got := Pick(few, 10, 3, 1); len(got) != 10 {
|
|
t.Errorf("two artists gave %d songs, want 10", len(got))
|
|
}
|
|
}
|
|
|
|
func TestPickPrefersTheHeavierSongsAndNeverTakesOneWithNoWeight(t *testing.T) {
|
|
c := pool(25, 4)
|
|
c[0].Weight = 200 // song 1
|
|
c[1].Weight = 0 // song 2
|
|
c[2].Weight = -5 // song 3
|
|
heavy := 0
|
|
for seed := uint64(0); seed < 200; seed++ {
|
|
got := Pick(c, 10, 3, seed)
|
|
if slices.Contains(got, 2) || slices.Contains(got, 3) {
|
|
t.Fatalf("seed %d took a song with no weight: %v", seed, got)
|
|
}
|
|
if slices.Contains(got, 1) {
|
|
heavy++
|
|
}
|
|
}
|
|
if heavy < 190 {
|
|
t.Errorf("the heavy song was in %d of 200 mixes, want nearly all", heavy)
|
|
}
|
|
}
|
|
|
|
func TestPickLeavesNoTwoSongsOfTheSameArtistNextToEachOther(t *testing.T) {
|
|
c := pool(10, 8)
|
|
by := artistOf(c)
|
|
for seed := uint64(0); seed < 50; seed++ {
|
|
got := Pick(c, 30, 3, seed)
|
|
for i := 1; i < len(got); i++ {
|
|
if by[got[i]] == by[got[i-1]] {
|
|
t.Fatalf("seed %d: songs %d and %d are by the same artist and next to each other: %v", seed, got[i-1], got[i], got)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPickWithOneArtistStillGivesSongs(t *testing.T) {
|
|
if got := Pick(pool(1, 30), 10, 3, 1); len(got) != 10 {
|
|
t.Errorf("one artist gave %d songs, want 10", len(got))
|
|
}
|
|
}
|