sonda/internal/favorites/favorites_test.go
tgiachi 74f683d26e feat: favourites of songs, albums and artists, in the API and in apps
A person can mark songs, albums and artists as favourites. They are theirs
alone: never shown to a friend, never sent anywhere. Only what can be played is
listed.

- migration 0035 favorites, internal/favorites, storage.FavoritesRepo
- PUT/DELETE /me/favorites/{kind}/{id}, GET /me/favorites/{kind} and
  GET /me/favorites/ids, in the OpenAPI spec
- catalog.Filter IDs now also reads artists by id
- OpenSubsonic: star, unstar, getStarred2, the starred date on songs, albums and
  artists, and getAlbumList2 of type starred
2026-11-16 10:51:07 +01:00

202 lines
5.4 KiB
Go

package favorites
import (
"context"
"errors"
"sort"
"testing"
"time"
"git.kmalloc.org/squid/sonda/internal/catalog"
)
type key struct {
user int64
kind Kind
id int64
}
type memStore struct{ at map[key]time.Time }
func (m *memStore) Add(_ context.Context, user int64, kind Kind, id int64, at time.Time) error {
if m.at == nil {
m.at = map[key]time.Time{}
}
if _, has := m.at[key{user, kind, id}]; !has {
m.at[key{user, kind, id}] = at
}
return nil
}
func (m *memStore) Remove(_ context.Context, user int64, kind Kind, id int64) error {
delete(m.at, key{user, kind, id})
return nil
}
func (m *memStore) ids(user int64, kind Kind) []int64 {
var out []int64
for k := range m.at {
if k.user == user && k.kind == kind {
out = append(out, k.id)
}
}
sort.Slice(out, func(i, j int) bool { return m.at[key{user, kind, out[i]}].After(m.at[key{user, kind, out[j]}]) })
return out
}
func (m *memStore) Page(_ context.Context, user int64, kind Kind, limit, offset int) ([]int64, int, error) {
all := m.ids(user, kind)
if offset >= len(all) {
return []int64{}, len(all), nil
}
end := min(len(all), offset+limit)
return all[offset:end], len(all), nil
}
func (m *memStore) IDs(_ context.Context, user int64, kind Kind) ([]int64, error) {
return m.ids(user, kind), nil
}
func (m *memStore) Marks(_ context.Context, user int64, kind Kind, ids []int64) (map[int64]time.Time, error) {
out := map[int64]time.Time{}
for _, id := range ids {
if at, ok := m.at[key{user, kind, id}]; ok {
out[id] = at
}
}
return out, nil
}
// The catalogue of the tests: artist 1 has music, artist 2 was only named by a provider, album 7 and track 70 exist.
type fakeCatalog struct{}
func (fakeCatalog) Artist(_ context.Context, id int64) (catalog.Artist, error) {
switch id {
case 1:
return catalog.Artist{ID: 1, Name: "A", AlbumCount: 1, TrackCount: 2}, nil
case 2:
return catalog.Artist{ID: 2, Name: "Named only"}, nil
}
return catalog.Artist{}, catalog.ErrNotFound
}
func (fakeCatalog) Album(_ context.Context, id int64) (catalog.Album, error) {
if id == 7 {
return catalog.Album{ID: 7}, nil
}
return catalog.Album{}, catalog.ErrNotFound
}
func (fakeCatalog) Track(_ context.Context, id int64) (catalog.Track, error) {
if id == 70 || id == 71 {
return catalog.Track{ID: id}, nil
}
return catalog.Track{}, catalog.ErrNotFound
}
func newService() (*Service, *memStore) {
st := &memStore{}
s := NewService(st, fakeCatalog{})
n := int64(0)
s.now = func() time.Time { n++; return time.Unix(1_700_000_000+n, 0) }
return s, st
}
func TestOnlyWhatIsInTheCatalogueCanBeAFavourite(t *testing.T) {
s, _ := newService()
ctx := context.Background()
for _, c := range []struct {
kind Kind
id int64
}{{Artist, 1}, {Album, 7}, {Track, 70}} {
if err := s.Add(ctx, 1, c.kind, c.id); err != nil {
t.Errorf("%s %d: %v", c.kind, c.id, err)
}
}
for _, c := range []struct {
kind Kind
id int64
}{{Artist, 99}, {Artist, 2}, {Album, 8}, {Track, 99}, {Track, 0}, {Track, -1}, {Kind("song"), 70}} {
if err := s.Add(ctx, 1, c.kind, c.id); !errors.Is(err, ErrNotFound) {
t.Errorf("%s %d: %v", c.kind, c.id, err)
}
}
}
func TestAddingTwiceKeepsTheFirstDateAndRemovingIsForgiving(t *testing.T) {
s, st := newService()
ctx := context.Background()
_ = s.Add(ctx, 1, Track, 70)
first := st.at[key{1, Track, 70}]
_ = s.Add(ctx, 1, Track, 70)
if !st.at[key{1, Track, 70}].Equal(first) {
t.Fatal("the date moved")
}
if err := s.Remove(ctx, 1, Track, 70); err != nil {
t.Fatal(err)
}
if err := s.Remove(ctx, 1, Track, 70); err != nil {
t.Fatalf("removing what is not a favourite: %v", err)
}
if err := s.Remove(ctx, 1, Kind("nope"), 1); !errors.Is(err, ErrNotFound) {
t.Fatalf("an unknown kind: %v", err)
}
}
func TestEachPersonHasTheirOwnFavourites(t *testing.T) {
s, _ := newService()
ctx := context.Background()
_ = s.Add(ctx, 1, Track, 70)
_ = s.Add(ctx, 2, Track, 71)
a, _ := s.IDs(ctx, 1)
b, _ := s.IDs(ctx, 2)
if len(a.Tracks) != 1 || a.Tracks[0] != 70 || len(b.Tracks) != 1 || b.Tracks[0] != 71 {
t.Fatalf("%v %v", a, b)
}
if len(a.Albums) != 0 || a.Albums == nil || a.Artists == nil {
t.Fatalf("empty kinds are empty lists: %+v", a)
}
}
func TestAPageIsNewestFirst(t *testing.T) {
s, _ := newService()
ctx := context.Background()
_ = s.Add(ctx, 1, Track, 70)
_ = s.Add(ctx, 1, Track, 71)
ids, total, err := s.Page(ctx, 1, Track, 1, 0)
if err != nil || total != 2 || len(ids) != 1 || ids[0] != 71 {
t.Fatalf("%v %d %v", ids, total, err)
}
if _, _, err := s.Page(ctx, 1, Track, -1, 0); !errors.Is(err, ErrInvalid) {
t.Fatalf("negative limit: %v", err)
}
if _, _, err := s.Page(ctx, 1, Kind("x"), 10, 0); !errors.Is(err, ErrNotFound) {
t.Fatalf("unknown kind: %v", err)
}
if ids, _, _ := s.Page(ctx, 1, Track, 0, 0); len(ids) != 2 {
t.Fatalf("default limit gives %v", ids)
}
}
func TestParseKindReadsThePathForm(t *testing.T) {
for in, want := range map[string]Kind{"artists": Artist, "albums": Album, "tracks": Track} {
if got, ok := ParseKind(in); !ok || got != want {
t.Errorf("%s: %v %v", in, got, ok)
}
}
for _, in := range []string{"", "artist", "songs", "Tracks"} {
if _, ok := ParseKind(in); ok {
t.Errorf("%q was accepted", in)
}
}
}
func TestMarksTellWhenAnItemBecameAFavourite(t *testing.T) {
s, _ := newService()
ctx := context.Background()
_ = s.Add(ctx, 1, Track, 70)
m, err := s.Marks(ctx, 1, Track, []int64{70, 71})
if err != nil || len(m) != 1 || m[70].IsZero() {
t.Fatalf("%v %v", m, err)
}
}