Files
minstrel/internal/recommendation/sessionvector_test.go
T
bvandeusenandClaude Opus 5.5 6ef96be290
release / web (push) Successful in 1m30s
release / go (push) Successful in 1m55s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 2m43s
release / android (push) Successful in 4m31s
release / Build signed APK (push) Successful in 5m0s
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 13s
release / Verify release artifacts (tag releases only) (push) Skipped
api tests hash passwords at bcrypt's lowest cost; one genre splitter (#8330, #8331)
#8330: every password the api handlers store goes through hashPassword at
bcrypt.DefaultCost, most of a second per hash under -race, so the password
tests took about 1.4s each, roughly 15s of the package. The cost is now a
package variable the tests' TestMain lowers to MinCost, after refusing a
production default below bcrypt.DefaultCost.

#8331: recommendation and taste each had a splitGenres and subsonic's
hasGenre split inline, the same rule three times. internal/genretag.Split
is the one copy, a stdlib-only leaf, and the two TestSplitGenres tables
are one.

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

164 lines
4.8 KiB
Go

package recommendation
import (
"encoding/json"
"testing"
"github.com/jackc/pgx/v5/pgtype"
"github.com/Fabled-Sword/minstrel/internal/db/dbq"
)
func track(artistID pgtype.UUID, genre string) dbq.Track {
t := dbq.Track{
ArtistID: artistID,
}
if genre != "" {
g := genre
t.Genre = &g
}
return t
}
func uuid(s string) pgtype.UUID {
var u pgtype.UUID
_ = u.Scan(s)
return u
}
// A session is a seed (too short to describe) until it holds three tracks.
// Artists are deduplicated and genres counted, and each track's id is kept.
func TestBuildSessionVector_SeedUntilThreeTracks(t *testing.T) {
a1 := uuid("11111111-1111-1111-1111-111111111111")
rock := track(a1, "rock")
rock.ID = uuid("22222222-2222-2222-2222-222222222222")
for _, tc := range []struct {
tracks []dbq.Track
seed bool
rockTags int
}{
{nil, true, 0},
{[]dbq.Track{rock}, true, 1},
{[]dbq.Track{rock, track(a1, "jazz")}, true, 1},
{[]dbq.Track{rock, rock, rock}, false, 3},
} {
v := BuildSessionVector(tc.tracks)
n := len(tc.tracks)
if v.Seed != tc.seed {
t.Errorf("%d tracks: Seed = %v, want %v", n, v.Seed, tc.seed)
}
if n > 0 && len(v.Artists) != 1 {
t.Errorf("%d tracks by one artist: Artists = %v, want one", n, v.Artists)
}
if v.Tags["rock"] != tc.rockTags || len(v.RecentTrackIDs) != n {
t.Errorf("%d tracks: rock = %d, recent ids = %d; want %d and %d", n, v.Tags["rock"], len(v.RecentTrackIDs), tc.rockTags, n)
}
}
}
func TestBuildSessionVector_DistinctArtistsAndGenres(t *testing.T) {
a1 := uuid("11111111-1111-1111-1111-111111111111")
a2 := uuid("22222222-2222-2222-2222-222222222222")
a3 := uuid("33333333-3333-3333-3333-333333333333")
v := BuildSessionVector([]dbq.Track{
track(a1, "rock"),
track(a2, "jazz"),
track(a3, "rock"),
track(a1, "experimental"),
})
// 4 tracks → not seed.
if v.Seed {
t.Errorf("Seed=true, want false")
}
// Distinct artists: 3 (a1 dedup'd).
if len(v.Artists) != 3 {
t.Errorf("Artists len = %d, want 3", len(v.Artists))
}
// Tag counts.
if v.Tags["rock"] != 2 || v.Tags["jazz"] != 1 || v.Tags["experimental"] != 1 {
t.Errorf("Tags = %+v", v.Tags)
}
if len(v.RecentTrackIDs) != 4 {
t.Errorf("RecentTrackIDs len = %d", len(v.RecentTrackIDs))
}
}
func TestBuildSessionVector_NilGenre_NotIndexed(t *testing.T) {
a1 := uuid("11111111-1111-1111-1111-111111111111")
v := BuildSessionVector([]dbq.Track{
track(a1, ""), // empty genre via the helper (nil pointer)
track(a1, "rock"),
track(a1, ""),
})
if v.Tags["rock"] != 1 {
t.Errorf("Tags['rock'] = %d, want 1", v.Tags["rock"])
}
if _, exists := v.Tags[""]; exists {
t.Errorf("Tags has empty-string entry: %+v", v.Tags)
}
}
func TestBuildSessionVector_MultiGenreSplitsOnSemicolon(t *testing.T) {
a1 := uuid("11111111-1111-1111-1111-111111111111")
v := BuildSessionVector([]dbq.Track{
track(a1, "Indie Pop; Pop; Alternative Pop"),
track(a1, "Pop"),
track(a1, "Boom Bap; Hip Hop; Lo-Fi"),
})
// Pop appears once on track 1 and once on track 2 → count 2.
if v.Tags["Pop"] != 2 {
t.Errorf("Tags['Pop'] = %d, want 2 (one per matching track)", v.Tags["Pop"])
}
// Indie Pop, Alternative Pop only on track 1.
if v.Tags["Indie Pop"] != 1 || v.Tags["Alternative Pop"] != 1 {
t.Errorf("Indie/Alt Pop counts wrong: %+v", v.Tags)
}
// Track 3's genres.
if v.Tags["Boom Bap"] != 1 || v.Tags["Hip Hop"] != 1 || v.Tags["Lo-Fi"] != 1 {
t.Errorf("track-3 genre counts wrong: %+v", v.Tags)
}
}
func TestBuildSessionVector_NoSeparatorStaysOpaque(t *testing.T) {
// Concatenated strings without separators (broken tag-editor output)
// can't be parsed; they stay as one tag and only match identical strings.
a1 := uuid("11111111-1111-1111-1111-111111111111")
v := BuildSessionVector([]dbq.Track{
track(a1, "ElectronicComplextroGlitch Hop"),
track(a1, "ElectronicComplextroGlitch Hop"),
})
if v.Tags["ElectronicComplextroGlitch Hop"] != 2 {
t.Errorf("opaque tag count = %d, want 2", v.Tags["ElectronicComplextroGlitch Hop"])
}
if len(v.Tags) != 1 {
t.Errorf("len(Tags) = %d, want 1", len(v.Tags))
}
}
func TestBuildSessionVector_JSONRoundTrip(t *testing.T) {
a1 := uuid("11111111-1111-1111-1111-111111111111")
v := BuildSessionVector([]dbq.Track{
track(a1, "rock"), track(a1, "jazz"), track(a1, "rock"),
})
data, err := json.Marshal(v)
if err != nil {
t.Fatalf("marshal: %v", err)
}
var got SessionVector
if err := json.Unmarshal(data, &got); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if got.Seed != v.Seed {
t.Errorf("Seed: %v != %v", got.Seed, v.Seed)
}
if got.Tags["rock"] != v.Tags["rock"] {
t.Errorf("Tags: %+v != %+v", got.Tags, v.Tags)
}
if len(got.Artists) != len(v.Artists) {
t.Errorf("Artists: len %d != %d", len(got.Artists), len(v.Artists))
}
if len(got.RecentTrackIDs) != len(v.RecentTrackIDs) {
t.Errorf("RecentTrackIDs: len %d != %d", len(got.RecentTrackIDs), len(v.RecentTrackIDs))
}
}