40384cc05e
Milestone #160 Opt 2 (era half). A third taste facet alongside artists + genre tags: signed weights over decade buckets ("1990s") derived from albums.release_date, rebuilt daily and scored into the taste match. - Migration 0045: taste_profile_eras table (mirrors taste_profile_tags) + taste_tuning.era_scale column (DEFAULT 0.5). - Build side (internal/taste): Config.EraScale ([0,1] damper, mirrors EnrichedTagScale), accumulate folds each play/like's decade at base*EraScale, persist atomic-replaces the era rows. - Scorer (internal/recommendation): TasteProfile gains an era term (own tanh scale + additive 0.15 share so it never weakens the existing artist/tag signal when a track is undated); candidate queries return album release_date; decadeOf mirrors the builder helper. - Tuning lab: era_scale threaded through recsettings + admin API + web card (auto-renders the new row) + Go/web tests. Mood facet deferred to #1534 (partial enrichment coverage + needs candidate-side enriched-tag loading). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
265 lines
8.4 KiB
Go
265 lines
8.4 KiB
Go
package recsettings
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"log/slog"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db"
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
|
)
|
|
|
|
func newPool(t *testing.T) *pgxpool.Pool {
|
|
t.Helper()
|
|
if testing.Short() {
|
|
t.Skip("skipping integration test in -short mode")
|
|
}
|
|
dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL")
|
|
if dsn == "" {
|
|
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
|
|
}
|
|
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
|
if err := db.Migrate(dsn, logger); err != nil {
|
|
t.Fatalf("migrate: %v", err)
|
|
}
|
|
pool, err := pgxpool.New(context.Background(), dsn)
|
|
if err != nil {
|
|
t.Fatalf("pool: %v", err)
|
|
}
|
|
t.Cleanup(pool.Close)
|
|
dbtest.ResetDB(t, pool)
|
|
return pool
|
|
}
|
|
|
|
func newService(t *testing.T, pool *pgxpool.Pool) *Service {
|
|
t.Helper()
|
|
s, err := New(context.Background(), pool, slog.New(slog.NewTextHandler(io.Discard, nil)))
|
|
if err != nil {
|
|
t.Fatalf("recsettings.New: %v", err)
|
|
}
|
|
return s
|
|
}
|
|
|
|
func auditRows(t *testing.T, pool *pgxpool.Pool) []struct {
|
|
Scope, Action string
|
|
Changes []fieldChange
|
|
} {
|
|
t.Helper()
|
|
rows, err := pool.Query(context.Background(),
|
|
`SELECT scope, action, changes FROM recommendation_tuning_audit ORDER BY id`)
|
|
if err != nil {
|
|
t.Fatalf("query audit: %v", err)
|
|
}
|
|
defer rows.Close()
|
|
var out []struct {
|
|
Scope, Action string
|
|
Changes []fieldChange
|
|
}
|
|
for rows.Next() {
|
|
var scope, action string
|
|
var raw []byte
|
|
if err := rows.Scan(&scope, &action, &raw); err != nil {
|
|
t.Fatalf("scan audit: %v", err)
|
|
}
|
|
var changes []fieldChange
|
|
if err := json.Unmarshal(raw, &changes); err != nil {
|
|
t.Fatalf("unmarshal audit changes: %v", err)
|
|
}
|
|
out = append(out, struct {
|
|
Scope, Action string
|
|
Changes []fieldChange
|
|
}{scope, action, changes})
|
|
}
|
|
return out
|
|
}
|
|
|
|
func TestNew_SeedsShippedDefaults(t *testing.T) {
|
|
pool := newPool(t)
|
|
s := newService(t, pool)
|
|
if got := s.Weights(ScopeRadio); got != ShippedRadioWeights() {
|
|
t.Errorf("radio weights = %+v, want shipped defaults", got)
|
|
}
|
|
if got := s.Weights(ScopeDailyMix); got != ShippedDailyMixWeights() {
|
|
t.Errorf("daily_mix weights = %+v, want shipped defaults", got)
|
|
}
|
|
if got := s.Taste(); got != ShippedTasteTuning() {
|
|
t.Errorf("taste tuning = %+v, want shipped defaults", got)
|
|
}
|
|
// Seeding must not write audit rows — nothing changed.
|
|
if rows := auditRows(t, pool); len(rows) != 0 {
|
|
t.Errorf("boot reconcile wrote %d audit rows, want 0", len(rows))
|
|
}
|
|
}
|
|
|
|
func TestUpdateProfile_PersistsAndAudits(t *testing.T) {
|
|
pool := newPool(t)
|
|
s := newService(t, pool)
|
|
if err := s.UpdateProfile(context.Background(), ScopeRadio,
|
|
map[string]float64{"taste_weight": 2.5, "skip_penalty": 3.0}); err != nil {
|
|
t.Fatalf("UpdateProfile: %v", err)
|
|
}
|
|
got := s.Weights(ScopeRadio)
|
|
if got.TasteWeight != 2.5 || got.SkipPenalty != 3.0 {
|
|
t.Errorf("weights = %+v, want taste 2.5 / skip 3.0", got)
|
|
}
|
|
// The other profile is untouched.
|
|
if s.Weights(ScopeDailyMix) != ShippedDailyMixWeights() {
|
|
t.Error("daily_mix must be unaffected by a radio update")
|
|
}
|
|
// Values survive a fresh boot (persisted, not just cached), and the
|
|
// reconcile's ON CONFLICT DO NOTHING doesn't clobber tuned rows.
|
|
s2 := newService(t, pool)
|
|
if got := s2.Weights(ScopeRadio); got.TasteWeight != 2.5 {
|
|
t.Errorf("rebooted taste_weight = %v, want 2.5", got.TasteWeight)
|
|
}
|
|
rows := auditRows(t, pool)
|
|
if len(rows) != 1 {
|
|
t.Fatalf("audit rows = %d, want 1", len(rows))
|
|
}
|
|
if rows[0].Scope != ScopeRadio || rows[0].Action != "update" || len(rows[0].Changes) != 2 {
|
|
t.Errorf("audit row = %+v, want radio/update with 2 changes", rows[0])
|
|
}
|
|
// Changes are field-sorted: skip_penalty before taste_weight.
|
|
if rows[0].Changes[0].Field != "skip_penalty" || rows[0].Changes[0].New != 3.0 {
|
|
t.Errorf("changes[0] = %+v, want skip_penalty → 3.0", rows[0].Changes[0])
|
|
}
|
|
}
|
|
|
|
func TestUpdateProfile_Validation(t *testing.T) {
|
|
pool := newPool(t)
|
|
s := newService(t, pool)
|
|
cases := []struct {
|
|
name string
|
|
scope string
|
|
patch map[string]float64
|
|
want error
|
|
}{
|
|
{"unknown scope", "banana", map[string]float64{"taste_weight": 1}, ErrUnknownScope},
|
|
{"unknown field", ScopeRadio, map[string]float64{"vibes": 1}, ErrUnknownField},
|
|
{"over bound", ScopeRadio, map[string]float64{"taste_weight": 11}, ErrOutOfRange},
|
|
{"negative jitter", ScopeRadio, map[string]float64{"jitter_magnitude": -0.1}, ErrOutOfRange},
|
|
}
|
|
for _, c := range cases {
|
|
if err := s.UpdateProfile(context.Background(), c.scope, c.patch); !errors.Is(err, c.want) {
|
|
t.Errorf("%s: err = %v, want %v", c.name, err, c.want)
|
|
}
|
|
}
|
|
if s.Weights(ScopeRadio) != ShippedRadioWeights() {
|
|
t.Error("rejected patches must not partially apply")
|
|
}
|
|
if rows := auditRows(t, pool); len(rows) != 0 {
|
|
t.Errorf("rejected patches wrote %d audit rows, want 0", len(rows))
|
|
}
|
|
}
|
|
|
|
func TestUpdateTaste_CurveOrderingEnforced(t *testing.T) {
|
|
pool := newPool(t)
|
|
s := newService(t, pool)
|
|
// Moving neutral above full must reject on the PATCHED result.
|
|
err := s.UpdateTaste(context.Background(), map[string]float64{"engagement_neutral": 0.95})
|
|
if !errors.Is(err, ErrOutOfRange) {
|
|
t.Fatalf("err = %v, want ErrOutOfRange (curve ordering)", err)
|
|
}
|
|
// A coherent multi-point move is fine.
|
|
if err := s.UpdateTaste(context.Background(), map[string]float64{
|
|
"engagement_neutral": 0.40, "engagement_full": 0.95, "half_life_days": 30,
|
|
}); err != nil {
|
|
t.Fatalf("UpdateTaste: %v", err)
|
|
}
|
|
cfg := s.TasteConfig()
|
|
if cfg.HalfLifeDays != 30 || cfg.Engagement.NeutralCompletion != 0.40 {
|
|
t.Errorf("taste config = %+v, want tuned values", cfg)
|
|
}
|
|
// WindowDays scales with the half-life at the shipped ratio.
|
|
if cfg.WindowDays != 30*3.6 {
|
|
t.Errorf("WindowDays = %v, want %v", cfg.WindowDays, 30*3.6)
|
|
}
|
|
}
|
|
|
|
func TestUpdateTaste_EnrichedTagScale(t *testing.T) {
|
|
pool := newPool(t)
|
|
s := newService(t, pool)
|
|
// In-range update persists into cache + the assembled taste config.
|
|
if err := s.UpdateTaste(context.Background(),
|
|
map[string]float64{"enriched_tag_scale": 0.8}); err != nil {
|
|
t.Fatalf("UpdateTaste: %v", err)
|
|
}
|
|
if got := s.Taste().EnrichedTagScale; got != 0.8 {
|
|
t.Errorf("Taste().EnrichedTagScale = %v, want 0.8", got)
|
|
}
|
|
if got := s.TasteConfig().EnrichedTagScale; got != 0.8 {
|
|
t.Errorf("TasteConfig().EnrichedTagScale = %v, want 0.8", got)
|
|
}
|
|
// Out of [0,1] rejects.
|
|
if err := s.UpdateTaste(context.Background(),
|
|
map[string]float64{"enriched_tag_scale": 1.5}); !errors.Is(err, ErrOutOfRange) {
|
|
t.Errorf("err = %v, want ErrOutOfRange for 1.5", err)
|
|
}
|
|
}
|
|
|
|
func TestUpdateTaste_EraScale(t *testing.T) {
|
|
pool := newPool(t)
|
|
s := newService(t, pool)
|
|
// In-range update persists into cache + the assembled taste config.
|
|
if err := s.UpdateTaste(context.Background(),
|
|
map[string]float64{"era_scale": 0.8}); err != nil {
|
|
t.Fatalf("UpdateTaste: %v", err)
|
|
}
|
|
if got := s.Taste().EraScale; got != 0.8 {
|
|
t.Errorf("Taste().EraScale = %v, want 0.8", got)
|
|
}
|
|
if got := s.TasteConfig().EraScale; got != 0.8 {
|
|
t.Errorf("TasteConfig().EraScale = %v, want 0.8", got)
|
|
}
|
|
// Out of [0,1] rejects.
|
|
if err := s.UpdateTaste(context.Background(),
|
|
map[string]float64{"era_scale": 1.5}); !errors.Is(err, ErrOutOfRange) {
|
|
t.Errorf("err = %v, want ErrOutOfRange for 1.5", err)
|
|
}
|
|
}
|
|
|
|
func TestReset_RestoresShippedAndAudits(t *testing.T) {
|
|
pool := newPool(t)
|
|
s := newService(t, pool)
|
|
// Reset with nothing changed is a no-op (no audit row).
|
|
if err := s.Reset(context.Background(), ScopeDailyMix); err != nil {
|
|
t.Fatalf("no-op reset: %v", err)
|
|
}
|
|
if rows := auditRows(t, pool); len(rows) != 0 {
|
|
t.Fatalf("no-op reset wrote audit rows")
|
|
}
|
|
if err := s.UpdateProfile(context.Background(), ScopeDailyMix,
|
|
map[string]float64{"similarity_weight": 4}); err != nil {
|
|
t.Fatalf("UpdateProfile: %v", err)
|
|
}
|
|
if err := s.Reset(context.Background(), ScopeDailyMix); err != nil {
|
|
t.Fatalf("Reset: %v", err)
|
|
}
|
|
if s.Weights(ScopeDailyMix) != ShippedDailyMixWeights() {
|
|
t.Error("reset must restore shipped defaults")
|
|
}
|
|
rows := auditRows(t, pool)
|
|
if len(rows) != 2 || rows[1].Action != "reset" {
|
|
t.Fatalf("audit rows = %+v, want update then reset", rows)
|
|
}
|
|
}
|
|
|
|
func TestUpdate_NoOpWritesNoAudit(t *testing.T) {
|
|
pool := newPool(t)
|
|
s := newService(t, pool)
|
|
shipped := ShippedRadioWeights()
|
|
if err := s.UpdateProfile(context.Background(), ScopeRadio,
|
|
map[string]float64{"taste_weight": shipped.TasteWeight}); err != nil {
|
|
t.Fatalf("UpdateProfile: %v", err)
|
|
}
|
|
if rows := auditRows(t, pool); len(rows) != 0 {
|
|
t.Errorf("no-op update wrote %d audit rows, want 0", len(rows))
|
|
}
|
|
}
|