sonda/internal/storage/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

127 lines
4.5 KiB
Go

package storage
import (
"context"
"reflect"
"testing"
"time"
"github.com/uptrace/bun"
"git.kmalloc.org/squid/sonda/internal/favorites"
)
func TestFavouritesAreKeptNewestFirstAndOnlyOfWhatCanBePlayed(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
s := seedCatalog(t, db)
repo := NewFavoritesRepo(db)
alice, bob := seedUser(t, db, "a@example.org"), seedUser(t, db, "b@example.org")
at := func(n int64) time.Time { return time.Unix(1_700_000_000+n, 0) }
airbag, karma, ghost := s.track["Airbag"], s.track["Karma Police"], s.track["Ghost"]
for i, id := range []int64{airbag, karma, ghost} {
if err := repo.Add(ctx, alice, favorites.Track, id, at(int64(i))); err != nil {
t.Fatal(err)
}
}
_ = repo.Add(ctx, bob, favorites.Track, airbag, at(5))
// Newest first; Ghost has no file to play, so it is not listed or counted.
ids, total, err := repo.Page(ctx, alice, favorites.Track, 10, 0)
if err != nil || total != 2 || !reflect.DeepEqual(ids, []int64{karma, airbag}) {
t.Fatalf("Page = %v %d %v", ids, total, err)
}
if ids, total, _ := repo.Page(ctx, alice, favorites.Track, 1, 1); total != 2 || !reflect.DeepEqual(ids, []int64{airbag}) {
t.Fatalf("second page = %v %d", ids, total)
}
if ids, _, _ := repo.Page(ctx, alice, favorites.Track, 10, 5); len(ids) != 0 {
t.Fatalf("past the end = %v", ids)
}
all, err := repo.IDs(ctx, alice, favorites.Track)
if err != nil || !reflect.DeepEqual(all, []int64{karma, airbag}) {
t.Fatalf("IDs = %v %v", all, err)
}
// Marking again keeps the first date.
_ = repo.Add(ctx, alice, favorites.Track, airbag, at(99))
m, _ := repo.Marks(ctx, alice, favorites.Track, []int64{airbag, karma, s.track["Army of Me"]})
if len(m) != 2 || !m[airbag].Equal(at(0)) {
t.Fatalf("Marks = %v", m)
}
if err := repo.Remove(ctx, alice, favorites.Track, airbag); err != nil {
t.Fatal(err)
}
if err := repo.Remove(ctx, alice, favorites.Track, airbag); err != nil {
t.Fatalf("removing twice: %v", err)
}
if ids, _ := repo.IDs(ctx, alice, favorites.Track); !reflect.DeepEqual(ids, []int64{karma}) {
t.Fatalf("after remove = %v", ids)
}
if ids, _ := repo.IDs(ctx, bob, favorites.Track); !reflect.DeepEqual(ids, []int64{airbag}) {
t.Fatalf("bob's = %v", ids)
}
})
}
func TestFavouriteAlbumsAndArtistsAreOfWhatCanBePlayed(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
seedCatalog(t, db)
repo := NewFavoritesRepo(db)
uid := seedUser(t, db, "a@example.org")
id := func(table, col, v string) int64 {
var n int64
if err := db.QueryRowContext(ctx, "SELECT id FROM "+table+" WHERE "+col+" = ?", v).Scan(&n); err != nil {
t.Fatal(err)
}
return n
}
ok, hidden := id("albums", "title", "OK Computer"), id("albums", "title", "Hidden Album")
radiohead, onlyMissing := id("artists", "name", "Radiohead"), id("artists", "name", "Only Missing")
at := time.Unix(1_700_000_000, 0)
for _, c := range []struct {
kind favorites.Kind
id int64
}{{favorites.Album, ok}, {favorites.Album, hidden}, {favorites.Artist, radiohead}, {favorites.Artist, onlyMissing}} {
if err := repo.Add(ctx, uid, c.kind, c.id, at); err != nil {
t.Fatal(err)
}
}
albums, total, _ := repo.Page(ctx, uid, favorites.Album, 10, 0)
if total != 1 || !reflect.DeepEqual(albums, []int64{ok}) {
t.Fatalf("albums = %v %d", albums, total)
}
artists, total, _ := repo.Page(ctx, uid, favorites.Artist, 10, 0)
if total != 1 || !reflect.DeepEqual(artists, []int64{radiohead}) {
t.Fatalf("artists = %v %d", artists, total)
}
})
}
func TestFavouritesGoWithTheirPerson(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
s := seedCatalog(t, db)
repo := NewFavoritesRepo(db)
uid := seedUser(t, db, "a@example.org")
_ = repo.Add(ctx, uid, favorites.Track, s.track["Airbag"], time.Unix(1, 0))
if _, err := db.ExecContext(ctx, "DELETE FROM users WHERE id = ?", uid); err != nil {
t.Fatal(err)
}
var n int
if err := db.QueryRowContext(ctx, "SELECT COUNT(*) FROM favorites").Scan(&n); err != nil || n != 0 {
t.Fatalf("%d favourites left, %v", n, err)
}
})
}
func TestAnUnknownKindIsRefusedByTheTable(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
uid := seedUser(t, db, "a@example.org")
if err := NewFavoritesRepo(db).Add(context.Background(), uid, favorites.Kind("song"), 1, time.Unix(1, 0)); err == nil {
t.Fatal("a kind that does not exist was kept")
}
})
}