package recommendation import ( "math" "math/rand" "testing" "time" ) func defaultWeights() ScoringWeights { return ScoringWeights{ BaseWeight: 1.0, LikeBoost: 2.0, RecencyWeight: 1.0, SkipPenalty: 1.0, JitterMagnitude: 0.1, } } func fixedRNG(v float64) func() float64 { return func() float64 { return v } } // Score is base + like boost + recency + context + similarity − skip // penalty, plus jitter, which a fixed RNG of 0.5 zeroes. With the default // weights a never-played, unliked track scores 2.0 (base 1.0, recency 1.0, // since never played counts as long ago), and each case moves one term. func TestScore(t *testing.T) { now := time.Now().UTC() daysAgo := func(d int) *time.Time { p := now.Add(-time.Duration(d) * 24 * time.Hour); return &p } withContext := func(w ScoringWeights) ScoringWeights { w.ContextWeight = 2.0; return w } withSimilarity := func(w ScoringWeights) ScoringWeights { w.SimilarityWeight = 2.0; return w } for _, tc := range []struct { name string in ScoringInputs weights func(ScoringWeights) ScoringWeights want float64 }{ {"never played", ScoringInputs{}, nil, 2.0}, {"liked", ScoringInputs{IsGeneralLiked: true}, nil, 4.0}, {"played 15 days ago, recency halved", ScoringInputs{LastPlayedAt: daysAgo(15)}, nil, 1.5}, {"played 60 days ago, recency capped at 1", ScoringInputs{LastPlayedAt: daysAgo(60)}, nil, 2.0}, {"half the plays skipped", ScoringInputs{PlayCount: 4, SkipCount: 2}, nil, 1.5}, {"full context match at weight 2", ScoringInputs{ContextualMatchScore: 1.0}, withContext, 4.0}, {"half context match at weight 2", ScoringInputs{ContextualMatchScore: 0.5}, withContext, 3.0}, {"no context match adds nothing", ScoringInputs{}, withContext, 2.0}, {"full similarity at weight 2", ScoringInputs{SimilarityScore: 1.0}, withSimilarity, 4.0}, {"half similarity at weight 2", ScoringInputs{SimilarityScore: 0.5}, withSimilarity, 3.0}, {"no similarity adds nothing", ScoringInputs{}, withSimilarity, 2.0}, } { t.Run(tc.name, func(t *testing.T) { w := defaultWeights() if tc.weights != nil { w = tc.weights(w) } if got := Score(tc.in, w, now, fixedRNG(0.5)); math.Abs(got-tc.want) > 1e-9 { t.Errorf("score = %v, want %v", got, tc.want) } }) } } func TestScore_JitterBounds(t *testing.T) { r := rand.New(rand.NewSource(42)) w := defaultWeights() now := time.Now().UTC() mid := Score(ScoringInputs{}, w, now, fixedRNG(0.5)) // jitter contribution = 0 for i := 0; i < 1000; i++ { got := Score(ScoringInputs{}, w, now, r.Float64) if got < mid-w.JitterMagnitude-1e-9 || got > mid+w.JitterMagnitude+1e-9 { t.Fatalf("score %v outside jitter band [%v, %v]", got, mid-w.JitterMagnitude, mid+w.JitterMagnitude) } } } func TestRecencyDecay_Clamping(t *testing.T) { now := time.Now().UTC() cases := []struct { ageDays float64 want float64 }{ {0, 0.0}, {15, 0.5}, {30, 1.0}, {45, 1.0}, } for _, c := range cases { t.Run("", func(t *testing.T) { past := now.Add(-time.Duration(c.ageDays * 24 * float64(time.Hour))) got := recencyDecay(&past, now) if math.Abs(got-c.want) > 1e-9 { t.Errorf("ageDays=%v decay=%v want %v", c.ageDays, got, c.want) } }) } }