Files
minstrel/internal/recommendation/similarity_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

80 lines
3.5 KiB
Go

package recommendation
import (
"math"
"testing"
)
func approxEq(a, b float64) bool { return math.Abs(a-b) < 1e-9 }
// Similarity is the set overlap (Jaccard) of tags weighted 0.7 plus that of
// artists weighted 0.3. Tag counts collapse to a set, an axis empty on either
// side contributes nothing, empty vectors give 0 rather than NaN, and a seed
// vector (too short a session to say anything) matches nothing.
func TestSimilarity(t *testing.T) {
v := SessionVector{Artists: []string{"a1", "a2"}, Tags: map[string]int{"rock": 2, "indie": 1}}
for _, tc := range []struct {
name string
a, b SessionVector
want float64
}{
{"identical", v, v, 1.0},
{"disjoint",
SessionVector{Artists: []string{"a1"}, Tags: map[string]int{"rock": 1}},
SessionVector{Artists: []string{"a2"}, Tags: map[string]int{"jazz": 1}}, 0},
{"tags only shared",
SessionVector{Artists: []string{"a1"}, Tags: map[string]int{"rock": 1}},
SessionVector{Artists: []string{"a2"}, Tags: map[string]int{"rock": 5}}, 0.7},
{"artists only shared",
SessionVector{Artists: []string{"a1"}, Tags: map[string]int{"rock": 1}},
SessionVector{Artists: []string{"a1"}, Tags: map[string]int{"jazz": 1}}, 0.3},
{"partial tag overlap",
SessionVector{Artists: []string{"a1"}, Tags: map[string]int{"rock": 1, "indie": 1}},
SessionVector{Artists: []string{"a1"}, Tags: map[string]int{"rock": 1, "jazz": 1}}, 0.7*(1.0/3.0) + 0.3},
{"counts collapse to a set",
SessionVector{Artists: []string{"a1"}, Tags: map[string]int{"rock": 2, "indie": 1}},
SessionVector{Artists: []string{"a1"}, Tags: map[string]int{"rock": 5, "indie": 3}}, 1.0},
{"each side has a different axis",
SessionVector{Tags: map[string]int{"rock": 1}}, SessionVector{Artists: []string{"a1"}}, 0},
{"both empty", SessionVector{}, SessionVector{}, 0},
{"a seed on the right", v, SessionVector{Seed: true, Artists: v.Artists, Tags: v.Tags}, 0},
{"a seed on the left", SessionVector{Seed: true, Artists: v.Artists, Tags: v.Tags}, v, 0},
} {
t.Run(tc.name, func(t *testing.T) {
got := Similarity(tc.a, tc.b, DefaultSimilarityWeights)
if math.IsNaN(got) || !approxEq(got, tc.want) {
t.Errorf("Similarity = %v, want %v", got, tc.want)
}
})
}
}
// A track's contextual match is its best similarity to the sessions in which
// it was liked, ignoring likes made in a seed session; a current seed session
// matches nothing.
func TestContextualMatchScore(t *testing.T) {
current := SessionVector{Artists: []string{"a1"}, Tags: map[string]int{"rock": 1}}
full := SessionVector{Artists: []string{"a1"}, Tags: map[string]int{"rock": 1}}
tagsOnly := SessionVector{Artists: []string{"a2"}, Tags: map[string]int{"rock": 1}}
none := SessionVector{Artists: []string{"a99"}, Tags: map[string]int{"jazz": 1}}
seedFull := SessionVector{Seed: true, Artists: full.Artists, Tags: full.Tags}
for _, tc := range []struct {
name string
current SessionVector
likes []SessionVector
want float64
}{
{"no likes", current, nil, 0},
{"the best like counts", current, []SessionVector{full, tagsOnly, none}, 1.0},
{"likes in seed sessions are ignored", current, []SessionVector{seedFull, seedFull}, 0},
{"seed likes are dropped before the max", current, []SessionVector{seedFull, tagsOnly}, 0.7},
{"a seed session matches nothing", SessionVector{Seed: true}, []SessionVector{full}, 0},
} {
t.Run(tc.name, func(t *testing.T) {
if got := ContextualMatchScore(tc.current, tc.likes, DefaultSimilarityWeights); !approxEq(got, tc.want) {
t.Errorf("ContextualMatchScore = %v, want %v", got, tc.want)
}
})
}
}