- Check the discover_from_listening setting before reading the listening history, so a refused request does no work and a database failure cannot turn it into a 500. - Cosine adds up in tag order: the sum in map order changed in the last digit between runs, and equal artists could swap places between requests. - A seed that could not be read is no longer listed as a seed. - README and spec: Discover exists now; "disabled" means something had to be asked and Last.fm is off; the stored lookups are shared, so another local user can see from retrieved_at that an artist was looked up, but not who. - Tests: the history is not read for a refused request, a stable cosine, tags fetched only for the pool and four at a time, a cancelled request stops, MusicBrainz is never asked, the setting is per user.
167 lines
5.4 KiB
Go
167 lines
5.4 KiB
Go
package discover
|
|
|
|
import (
|
|
"math"
|
|
"testing"
|
|
)
|
|
|
|
func near(a, b float64) bool { return math.Abs(a-b) < 1e-9 }
|
|
|
|
func ptr(v float64) *float64 { return &v }
|
|
|
|
func TestAffinityOfOneSeedIsItsWeightTimesTheMatch(t *testing.T) {
|
|
got := Affinity([]Link{{Seed: 1, Weight: 0.8, Match: 0.5}})
|
|
if !near(got, 0.4) {
|
|
t.Errorf("affinity = %v, want 0.4", got)
|
|
}
|
|
}
|
|
|
|
func TestSeveralSeedsReinforceEachOtherAndStayBelowOne(t *testing.T) {
|
|
one := Affinity([]Link{{Weight: 0.8, Match: 0.5}})
|
|
two := Affinity([]Link{{Weight: 0.8, Match: 0.5}, {Weight: 0.6, Match: 0.5}})
|
|
// 1 - (1-0.4)(1-0.3)
|
|
if !near(two, 1-0.6*0.7) || two <= one {
|
|
t.Errorf("two seeds = %v (one seed %v), want %v", two, one, 1-0.6*0.7)
|
|
}
|
|
many := make([]Link, 100)
|
|
for i := range many {
|
|
many[i] = Link{Weight: 1, Match: 0.9}
|
|
}
|
|
if got := Affinity(many); got > 1 || got < 0.999 {
|
|
t.Errorf("a hundred strong seeds = %v, want just under 1", got)
|
|
}
|
|
}
|
|
|
|
func TestAffinityIgnoresNonsense(t *testing.T) {
|
|
if got := Affinity(nil); got != 0 {
|
|
t.Errorf("no links = %v", got)
|
|
}
|
|
// Out of range numbers are pulled into 0..1: a bad provider number cannot make a score above 1 or below 0.
|
|
got := Affinity([]Link{{Weight: 2, Match: 3}, {Weight: -1, Match: 1}, {Weight: 1, Match: math.NaN()}})
|
|
if got < 0 || got > 1 || math.IsNaN(got) {
|
|
t.Errorf("affinity = %v, want a number from 0 to 1", got)
|
|
}
|
|
if !near(got, 1) { // the first link is clamped to 1*1
|
|
t.Errorf("affinity = %v, want 1", got)
|
|
}
|
|
}
|
|
|
|
func TestCosine(t *testing.T) {
|
|
a := map[string]float64{"rock": 1, "grunge": 1}
|
|
if got, ok := Cosine(a, a); !ok || !near(got, 1) {
|
|
t.Errorf("same tags = %v %v, want 1", got, ok)
|
|
}
|
|
if got, ok := Cosine(a, map[string]float64{"jazz": 1}); !ok || got != 0 {
|
|
t.Errorf("nothing in common = %v %v, want 0", got, ok)
|
|
}
|
|
if got, ok := Cosine(map[string]float64{"rock": 1}, map[string]float64{"rock": 1, "pop": 1}); !ok || !near(got, 1/math.Sqrt2) {
|
|
t.Errorf("half in common = %v %v, want %v", got, ok, 1/math.Sqrt2)
|
|
}
|
|
}
|
|
|
|
func TestCosineSaysWhenThereIsNothingToCompare(t *testing.T) {
|
|
for name, pair := range map[string][2]map[string]float64{
|
|
"empty left": {nil, {"rock": 1}},
|
|
"empty right": {{"rock": 1}, {}},
|
|
"zero weight": {{"rock": 0}, {"rock": 1}},
|
|
} {
|
|
if got, ok := Cosine(pair[0], pair[1]); ok || got != 0 {
|
|
t.Errorf("%s: %v %v, want no answer", name, got, ok)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPreferenceScore(t *testing.T) {
|
|
for _, tt := range []struct {
|
|
pref float64
|
|
known bool
|
|
want float64
|
|
}{{0.8, true, 0.9}, {-1, true, 0}, {0, true, 0.5}, {0.8, false, 0.5}} {
|
|
if got := PreferenceScore(tt.pref, tt.known); !near(got, tt.want) {
|
|
t.Errorf("PreferenceScore(%v, %v) = %v, want %v", tt.pref, tt.known, got, tt.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDiscoveryBonusFallsWithEveryListen(t *testing.T) {
|
|
if DiscoveryBonus(0) != 1 || !near(DiscoveryBonus(1), 0.5) || !near(DiscoveryBonus(3), 0.25) {
|
|
t.Errorf("bonus = %v %v %v", DiscoveryBonus(0), DiscoveryBonus(1), DiscoveryBonus(3))
|
|
}
|
|
if DiscoveryBonus(-5) != 1 {
|
|
t.Errorf("a negative count = %v, want 1", DiscoveryBonus(-5))
|
|
}
|
|
}
|
|
|
|
func TestCombineWithEverythingKnown(t *testing.T) {
|
|
w := Weights{Affinity: 0.40, Genre: 0.25, Preference: 0.25, Discovery: 0.10}
|
|
got := Combine(Components{A: 1, G: ptr(0), P: 0.5, D: 1}, w)
|
|
if want := 0.40 + 0 + 0.125 + 0.10; !near(got, want) {
|
|
t.Errorf("score = %v, want %v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestCombineDropsWhatIsMissingAndRescalesTheRest(t *testing.T) {
|
|
w := Weights{Affinity: 0.40, Genre: 0.25, Preference: 0.25, Discovery: 0.10}
|
|
got := Combine(Components{A: 1, G: nil, P: 1, D: 1}, w)
|
|
// Without G everything is 1, so the score must be 1, not 0.75.
|
|
if !near(got, 1) {
|
|
t.Errorf("score = %v, want 1", got)
|
|
}
|
|
got = Combine(Components{A: 0.5, G: nil, P: 0.5, D: 0.5}, w)
|
|
if !near(got, 0.5) {
|
|
t.Errorf("score = %v, want 0.5", got)
|
|
}
|
|
}
|
|
|
|
func TestCombineStaysInRangeWhateverComesIn(t *testing.T) {
|
|
w := Weights{Affinity: 0.40, Genre: 0.25, Preference: 0.25, Discovery: 0.10}
|
|
got := Combine(Components{A: 7, G: ptr(-3), P: math.NaN(), D: 2}, w)
|
|
if got < 0 || got > 1 || math.IsNaN(got) {
|
|
t.Errorf("score = %v, want a number from 0 to 1", got)
|
|
}
|
|
}
|
|
|
|
func TestCombineWithNoWeightsIsZero(t *testing.T) {
|
|
if got := Combine(Components{A: 1, P: 1, D: 1}, Weights{}); got != 0 {
|
|
t.Errorf("score = %v, want 0", got)
|
|
}
|
|
// Only G has weight, and G is missing: nothing is left to score with.
|
|
if got := Combine(Components{A: 1, P: 1, D: 1}, Weights{Genre: 1}); got != 0 {
|
|
t.Errorf("score = %v, want 0", got)
|
|
}
|
|
}
|
|
|
|
func TestWeightsValidation(t *testing.T) {
|
|
good := Weights{Affinity: 0.4, Genre: 0.25, Preference: 0.25, Discovery: 0.1}
|
|
if err := good.Validate(); err != nil {
|
|
t.Errorf("default-like weights refused: %v", err)
|
|
}
|
|
for name, w := range map[string]Weights{
|
|
"all zero": {},
|
|
"negative": {Affinity: -0.1, Genre: 1},
|
|
"nan": {Affinity: math.NaN(), Genre: 1},
|
|
"too big": {Affinity: 11, Genre: 1},
|
|
} {
|
|
if err := w.Validate(); err == nil {
|
|
t.Errorf("%s: accepted", name)
|
|
}
|
|
}
|
|
if d := DefaultWeights(); d != good {
|
|
t.Errorf("default weights = %+v, want %+v", d, good)
|
|
}
|
|
}
|
|
|
|
func TestCosineGivesTheSameNumberEveryTime(t *testing.T) {
|
|
a, b := map[string]float64{}, map[string]float64{}
|
|
for i := 0; i < 60; i++ {
|
|
tag := string(rune('a'+i%26)) + string(rune('a'+i/26))
|
|
a[tag] = 1 / float64(i+3)
|
|
b[tag] = 1 / float64(2*i+7)
|
|
}
|
|
first, _ := Cosine(a, b)
|
|
for i := 0; i < 300; i++ {
|
|
if got, _ := Cosine(a, b); got != first {
|
|
t.Fatalf("run %d: %v, first was %v: the sums depend on map order, and equal artists can swap places", i, got, first)
|
|
}
|
|
}
|
|
}
|