Files
minstrel/internal/recommendation/score_test.go
T
bvandeusenandClaude Opus 5.5 a2dae7ec8c
release / go (push) Successful in 1m44s
release / web (push) Successful in 1m16s
release / govulncheck (push) Successful in 18s
release / integration (push) Successful in 2m45s
release / android (push) Successful in 4m29s
release / Build signed APK (push) Successful in 4m40s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Publish the release image to GHCR (dispatch only) (push) Skipped
release / Build + push container image (push) Successful in 14s
release / Verify release artifacts (tag releases only) (push) Skipped
test(playlists, recommendation): system-mix builds stop drawing covers no test reads; merge repeated cases (#8297)
Timings from one run of the integration lane with -v (39c5b1ed, reverted
here) put the system-mix builds at 1.4-3.8s each, most of the playlists
package's 35s. A build draws a JPEG collage per mix; under -race that is
most of its time, and no build test looked at a cover. The build tests now
pass no data folder, which skips drawing, except SufficientActivity, which
keeps drawing and now checks every mix got a cover: nothing tested that the
build wires covers before.

Merged:
- playlists service: 22 tests become 5 scenario tests (access, bad input,
  partial update, track-list edits, cover-file cleanup), one database
  reset each.
- ARebuildKeepsEachMixsID into DailyNonceDeterminism (both built twice in
  one day); the two You might like builds into one.
- capCandidatesByAlbumAndArtist, Score, Similarity, ContextualMatchScore
  and the session-vector seed rule: one table each.
- LoadCandidates' six contextual-like database tests: the max and seed
  rules are ContextualMatchScore's unit table; the database test keeps what
  only it can see, a like reaching its track and a withdrawn like ignored.

Dropped: Score determinism with a fixed RNG (a pure function), Score's
cold-start case (the same input as the base case), and recencyDecay(nil)
and skipRatio's two cases, which Score's table covers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-02 13:35:36 -05:00

98 lines
3.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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)
}
})
}
}