Migrating hosting from Codeberg to git.kmalloc.org. Renames the module path in go.mod, every internal import, and the OCI image source label in the Dockerfile. Build and full test suite verified green after the rename.
302 lines
9.4 KiB
Go
302 lines
9.4 KiB
Go
package affinity
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"sort"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.kmalloc.org/squid/sonda/internal/feedback"
|
|
"git.kmalloc.org/squid/sonda/internal/usersettings"
|
|
)
|
|
|
|
var now = time.Date(2026, 11, 15, 12, 0, 0, 0, time.UTC)
|
|
|
|
// memStore keeps the model in memory and counts how often it is built.
|
|
type memStore struct {
|
|
plays []Play
|
|
model Model
|
|
built Built
|
|
has bool
|
|
reads int
|
|
}
|
|
|
|
func (m *memStore) Plays(context.Context, int64, time.Duration) ([]Play, error) {
|
|
m.reads++
|
|
return m.plays, nil
|
|
}
|
|
func (m *memStore) PlayCount(context.Context, int64, time.Duration) (int, error) {
|
|
return len(m.plays), nil
|
|
}
|
|
func (m *memStore) Save(_ context.Context, _ int64, at time.Time, model Model) error {
|
|
m.model, m.has = model, true
|
|
m.built = Built{BuiltAt: at, Plays: model.Plays, Sessions: model.Sessions, Artists: len(model.Artists), Links: len(model.Links)}
|
|
return nil
|
|
}
|
|
func (m *memStore) Build(context.Context, int64) (Built, bool, error) { return m.built, m.has, nil }
|
|
func (m *memStore) Seeds(_ context.Context, _ int64, limit int) ([]Artist, error) {
|
|
out := append([]Artist(nil), m.model.Artists...)
|
|
sort.Slice(out, func(i, j int) bool {
|
|
if out[i].Weight != out[j].Weight {
|
|
return out[i].Weight > out[j].Weight
|
|
}
|
|
return out[i].Key < out[j].Key
|
|
})
|
|
return out[:min(limit, len(out))], nil
|
|
}
|
|
func (m *memStore) Artists(_ context.Context, _ int64, keys []string) (map[string]Artist, error) {
|
|
out := map[string]Artist{}
|
|
for _, a := range m.model.Artists {
|
|
for _, k := range keys {
|
|
if a.Key == k {
|
|
out[k] = a
|
|
}
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
func (m *memStore) Neighbours(_ context.Context, _ int64, keys []string) ([]Link, error) {
|
|
var out []Link
|
|
for _, l := range m.model.Links {
|
|
for _, k := range keys {
|
|
if l.A == k || l.B == k {
|
|
out = append(out, l)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
type fakeLibrary map[string]LibraryArtist
|
|
|
|
func (f fakeLibrary) LibraryArtists(_ context.Context, keys []string) (map[string]LibraryArtist, error) {
|
|
out := map[string]LibraryArtist{}
|
|
for _, k := range keys {
|
|
if a, ok := f[k]; ok {
|
|
out[k] = a
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
type fakeMarks map[int64]feedback.Kind
|
|
|
|
func (f fakeMarks) Marks(context.Context, int64) (map[int64]feedback.Kind, error) { return f, nil }
|
|
|
|
type fakeWishes struct {
|
|
names []string
|
|
id int64
|
|
}
|
|
|
|
func (f *fakeWishes) Wishlist(_ context.Context, _ int64, name string, _ time.Time) (int64, error) {
|
|
f.names = append(f.names, name)
|
|
return f.id, nil
|
|
}
|
|
|
|
type fakeSettings struct{ on bool }
|
|
|
|
func (f fakeSettings) Get(context.Context, int64) (usersettings.Settings, error) {
|
|
return usersettings.Settings{DiscoverFromListening: f.on}, nil
|
|
}
|
|
|
|
// ago is a moment this many days before now, and the sitting of artists played one after the other from there.
|
|
func ago(days int, artists ...string) []Play {
|
|
var out []Play
|
|
for i, a := range artists {
|
|
out = append(out, Play{Key: a, Name: a, At: now.AddDate(0, 0, -days).Add(time.Duration(i) * time.Minute)})
|
|
}
|
|
return out
|
|
}
|
|
|
|
type rig struct {
|
|
svc *Service
|
|
store *memStore
|
|
wishes *fakeWishes
|
|
marks fakeMarks
|
|
}
|
|
|
|
// newRig plays out this history: three old sittings of radiohead, pixies and canali; two recent ones of radiohead and
|
|
// placebo; one play of an artist met once. pixies and placebo are in the library, canali and oneoff are not.
|
|
func newRig(on bool) rig {
|
|
var plays []Play
|
|
for _, d := range []int{200, 199, 198} {
|
|
plays = append(plays, ago(d, "radiohead", "pixies", "canali")...)
|
|
}
|
|
for _, d := range []int{10, 9} {
|
|
plays = append(plays, ago(d, "radiohead", "placebo")...)
|
|
}
|
|
plays = append(plays, ago(150, "oneoff")...)
|
|
store := &memStore{plays: plays}
|
|
lib := fakeLibrary{
|
|
"radiohead": {ID: 1, Name: "Radiohead", Playable: true},
|
|
"pixies": {ID: 2, Name: "Pixies", Playable: true},
|
|
"placebo": {ID: 3, Name: "Placebo", Playable: true},
|
|
}
|
|
wishes := &fakeWishes{id: 77}
|
|
marks := fakeMarks{}
|
|
svc := NewService(store, lib, marks, wishes, fakeSettings{on}, 20*time.Second)
|
|
svc.now = func() time.Time { return now }
|
|
return rig{svc, store, wishes, marks}
|
|
}
|
|
|
|
func names(in []Suggestion) []string {
|
|
out := []string{}
|
|
for _, s := range in {
|
|
out = append(out, s.Name)
|
|
}
|
|
return out
|
|
}
|
|
|
|
func equal(a, b []string) bool {
|
|
if len(a) != len(b) {
|
|
return false
|
|
}
|
|
for i := range a {
|
|
if a[i] != b[i] {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func TestSuggestionsNeedTheListenerToAllowListening(t *testing.T) {
|
|
r := newRig(false)
|
|
if _, err := r.svc.Suggest(context.Background(), 1, 0); !errors.Is(err, ErrNotAllowed) {
|
|
t.Fatalf("err = %v, want ErrNotAllowed", err)
|
|
}
|
|
if r.store.reads != 0 {
|
|
t.Error("a refused request read the plays")
|
|
}
|
|
if _, err := r.svc.AddToWishlist(context.Background(), 1, "canali"); !errors.Is(err, ErrNotAllowed) {
|
|
t.Errorf("wishlist err = %v, want ErrNotAllowed", err)
|
|
}
|
|
}
|
|
|
|
func TestTheLibraryListHasTheArtistsThatGoWithWhatIsPlayedAndWereNotPlayedLately(t *testing.T) {
|
|
r := newRig(true)
|
|
got, err := r.svc.Suggest(context.Background(), 1, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// placebo goes with radiohead too, but it was played ten days ago; radiohead itself was too.
|
|
if !equal(names(got.Library), []string{"Pixies"}) {
|
|
t.Fatalf("library = %v, want only Pixies", names(got.Library))
|
|
}
|
|
p := got.Library[0]
|
|
if p.ArtistID != 2 || p.Score != 1 || p.Plays != 3 || !p.LastPlayed.Equal(now.AddDate(0, 0, -198).Add(time.Minute)) {
|
|
t.Errorf("Pixies = %+v", p)
|
|
}
|
|
if len(p.Because) == 0 || p.Because[0].Name == "Pixies" {
|
|
t.Errorf("because = %+v, want the artists it goes with", p.Because)
|
|
}
|
|
}
|
|
|
|
func TestTheMissingListHasTheArtistsPlayedThatAreNotInTheLibrary(t *testing.T) {
|
|
r := newRig(true)
|
|
got, err := r.svc.Suggest(context.Background(), 1, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// oneoff was played once: not worth getting. Radiohead, Pixies and Placebo are in the library.
|
|
if !equal(names(got.Missing), []string{"canali"}) {
|
|
t.Fatalf("missing = %v, want only canali", names(got.Missing))
|
|
}
|
|
c := got.Missing[0]
|
|
if c.ArtistID != 0 || c.Wishlisted || c.Plays != 3 || c.Score <= 0 || c.Score > 1 {
|
|
t.Errorf("canali = %+v", c)
|
|
}
|
|
}
|
|
|
|
func TestAMissingArtistThatIsOnTheWishlistSaysSoAndOneNotWantedIsLeftOut(t *testing.T) {
|
|
r := newRig(true)
|
|
lib := fakeLibrary{
|
|
"radiohead": {ID: 1, Name: "Radiohead", Playable: true},
|
|
"pixies": {ID: 2, Name: "Pixies", Playable: true},
|
|
"canali": {ID: 9, Name: "Giorgio Canali", Playable: false}, // a row without music
|
|
}
|
|
r.svc.library = lib
|
|
r.marks[9] = feedback.Wishlist
|
|
got, _ := r.svc.Suggest(context.Background(), 1, 0)
|
|
if len(got.Missing) != 1 || got.Missing[0].ArtistID != 9 || !got.Missing[0].Wishlisted || got.Missing[0].Name != "Giorgio Canali" {
|
|
t.Fatalf("missing = %+v, want the row 9 on the wishlist", got.Missing)
|
|
}
|
|
r.marks[9] = feedback.Dismissed
|
|
r.marks[2] = feedback.Dismissed
|
|
got, _ = r.svc.Suggest(context.Background(), 1, 0)
|
|
if len(got.Missing) != 0 || len(got.Library) != 0 {
|
|
t.Errorf("after dismissing: library %v, missing %v, want nothing", names(got.Library), names(got.Missing))
|
|
}
|
|
}
|
|
|
|
func TestTheModelIsBuiltOnceAndAgainWhenPlaysAreAdded(t *testing.T) {
|
|
r := newRig(true)
|
|
ctx := context.Background()
|
|
for i := 0; i < 3; i++ {
|
|
if _, err := r.svc.Suggest(ctx, 1, 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if r.store.reads != 1 {
|
|
t.Fatalf("plays read %d times for three requests, want 1", r.store.reads)
|
|
}
|
|
|
|
// A few plays, soon after: the model is left alone.
|
|
r.store.plays = append(r.store.plays, ago(0, "radiohead")...)
|
|
r.svc.now = func() time.Time { return now.Add(time.Minute) }
|
|
_, _ = r.svc.Suggest(ctx, 1, 0)
|
|
if r.store.reads != 1 {
|
|
t.Errorf("read %d times after one new play a minute later, want 1", r.store.reads)
|
|
}
|
|
// The same, but the model is old now.
|
|
r.svc.now = func() time.Time { return now.Add(time.Hour) }
|
|
_, _ = r.svc.Suggest(ctx, 1, 0)
|
|
if r.store.reads != 2 {
|
|
t.Errorf("read %d times after an hour, want 2", r.store.reads)
|
|
}
|
|
// An import adds many at once: at once.
|
|
for i := 0; i < bigChange; i++ {
|
|
r.store.plays = append(r.store.plays, ago(300+i, "radiohead", "pixies")...)
|
|
}
|
|
_, _ = r.svc.Suggest(ctx, 1, 0)
|
|
if r.store.reads != 3 {
|
|
t.Errorf("read %d times after a big import, want 3", r.store.reads)
|
|
}
|
|
}
|
|
|
|
func TestTheLimitIsAppliedAndBadOnesRefused(t *testing.T) {
|
|
r := newRig(true)
|
|
if _, err := r.svc.Suggest(context.Background(), 1, -1); !errors.Is(err, ErrInvalidLimit) {
|
|
t.Errorf("err = %v, want ErrInvalidLimit", err)
|
|
}
|
|
got, err := r.svc.Suggest(context.Background(), 1, 1)
|
|
if err != nil || len(got.Library) > 1 || len(got.Missing) > 1 {
|
|
t.Errorf("limit 1 = %v, %v", got, err)
|
|
}
|
|
}
|
|
|
|
func TestNoPlaysGiveEmptyListsAndNotNull(t *testing.T) {
|
|
r := newRig(true)
|
|
r.store.plays = nil
|
|
got, err := r.svc.Suggest(context.Background(), 1, 0)
|
|
if err != nil || got.Library == nil || got.Missing == nil || len(got.Library)+len(got.Missing) != 0 {
|
|
t.Errorf("got %+v, %v, want two empty lists", got, err)
|
|
}
|
|
}
|
|
|
|
func TestOnlyAnArtistThatWasPlayedGoesOnTheWishlist(t *testing.T) {
|
|
r := newRig(true)
|
|
ctx := context.Background()
|
|
// The model is built by the wish itself when no request for suggestions did it.
|
|
if _, err := r.svc.AddToWishlist(ctx, 1, "nobody I played"); !errors.Is(err, ErrUnknownArtist) {
|
|
t.Errorf("err = %v, want ErrUnknownArtist", err)
|
|
}
|
|
if _, err := r.svc.AddToWishlist(ctx, 1, " "); !errors.Is(err, ErrUnknownArtist) {
|
|
t.Errorf("a blank name: %v, want ErrUnknownArtist", err)
|
|
}
|
|
id, err := r.svc.AddToWishlist(ctx, 1, "canali")
|
|
if err != nil || id != 77 || len(r.wishes.names) != 1 || r.wishes.names[0] != "canali" {
|
|
t.Errorf("AddToWishlist = %d, %v, names %v", id, err, r.wishes.names)
|
|
}
|
|
}
|