sonda/internal/storage/sharing_test.go
tgiachi a880b87a9e chore: move the module path to git.kmalloc.org/squid/sonda
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.
2026-11-15 17:30:29 +01:00

160 lines
6.7 KiB
Go

package storage
import (
"context"
"errors"
"reflect"
"testing"
"github.com/uptrace/bun"
"git.kmalloc.org/squid/sonda/internal/catalog"
"git.kmalloc.org/squid/sonda/internal/library"
"git.kmalloc.org/squid/sonda/internal/streaming"
)
func TestALibraryIsNotSharedUntilAnAdminSaysSo(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
repo := NewLibraryRepo(db)
id := mkLib(t, repo, "music")
if lib, _ := repo.GetLibrary(ctx, id); lib.Shared {
t.Fatal("a new library must not be shared")
}
lib, err := repo.SetLibraryShared(ctx, id, true)
if err != nil || !lib.Shared {
t.Fatalf("SetLibraryShared = %+v, %v", lib, err)
}
if got, _ := repo.ListLibraries(ctx); len(got) != 1 || !got[0].Shared {
t.Errorf("ListLibraries = %+v", got)
}
// Renaming does not undo it.
if _, err := repo.UpdateLibrary(ctx, id, "renamed", lib.Path); err != nil {
t.Fatal(err)
}
if got, _ := repo.GetLibrary(ctx, id); !got.Shared {
t.Errorf("after a rename: %+v", got)
}
if lib, err = repo.SetLibraryShared(ctx, id, false); err != nil || lib.Shared {
t.Errorf("unshare = %+v, %v", lib, err)
}
if _, err := repo.SetLibraryShared(ctx, 999, true); !errors.Is(err, library.ErrLibraryNotFound) {
t.Errorf("unknown library: %v, want ErrLibraryNotFound", err)
}
})
}
// A friend sees what a listener would see if the only library were the shared ones: the same items, counts and
// formats, and nothing that lives only in a library that is not shared.
func TestFriendsSeeOnlyWhatIsInASharedLibrary(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
s := seedCatalog(t, db)
libs := NewLibraryRepo(db)
shared := NewSharedCatalogRepo(db)
// Nothing is shared: nothing is seen, however the ask is made.
for name, run := range map[string]func() (int, error){
"artists": func() (int, error) { p, err := shared.ListArtists(ctx, catalog.Filter{Limit: 50}); return p.Total, err },
"albums": func() (int, error) { p, err := shared.ListAlbums(ctx, catalog.Filter{Limit: 50}); return p.Total, err },
"tracks": func() (int, error) { p, err := shared.ListTracks(ctx, catalog.Filter{Limit: 50}); return p.Total, err },
} {
if n, err := run(); err != nil || n != 0 {
t.Errorf("%s with nothing shared: %d, %v", name, n, err)
}
}
if _, err := shared.GetTrack(ctx, s.track["So What"]); !errors.Is(err, catalog.ErrNotFound) {
t.Errorf("GetTrack with nothing shared: %v", err)
}
if _, err := libs.SetLibraryShared(ctx, s.jazz, true); err != nil {
t.Fatal(err)
}
all := catalog.Filter{Limit: 200}
inJazz := catalog.Filter{Limit: 200, LibraryID: s.jazz}
for _, c := range []struct {
name string
got func() (any, error)
want func() (any, error)
}{
{"artists", func() (any, error) { return shared.ListArtists(ctx, all) }, func() (any, error) { return s.repo.ListArtists(ctx, inJazz) }},
{"albums", func() (any, error) { return shared.ListAlbums(ctx, all) }, func() (any, error) { return s.repo.ListAlbums(ctx, inJazz) }},
{"tracks", func() (any, error) { return shared.ListTracks(ctx, all) }, func() (any, error) { return s.repo.ListTracks(ctx, inJazz) }},
} {
got, err1 := c.got()
want, err2 := c.want()
if err1 != nil || err2 != nil || !reflect.DeepEqual(got, want) {
t.Errorf("%s: shared view = %+v (%v), want what the jazz library alone shows = %+v (%v)", c.name, got, err1, want, err2)
}
}
// Airbag is in both libraries: it is seen through the shared file, as an mp3.
air, err := shared.GetTrack(ctx, s.track["Airbag"])
if err != nil || air.Format != "mp3" {
t.Errorf("Airbag = %+v, %v: want the format of the shared file", air, err)
}
// Something that lives only in the library that is not shared is not there, by id either.
if _, err := shared.GetTrack(ctx, s.track["Karma Police"]); !errors.Is(err, catalog.ErrNotFound) {
t.Errorf("GetTrack of an unshared track: %v", err)
}
var radiohead, okComputer int64
_ = db.QueryRow("SELECT id FROM artists WHERE name = 'Radiohead'").Scan(&radiohead)
_ = db.QueryRow("SELECT id FROM albums WHERE title = 'OK Computer'").Scan(&okComputer)
if _, err := shared.GetArtist(ctx, radiohead); err != nil {
// Radiohead is credited on Airbag, which the shared library has: it is there, with one track.
t.Errorf("GetArtist(Radiohead) = %v", err)
}
if a, _ := shared.GetArtist(ctx, radiohead); a.TrackCount != 1 {
t.Errorf("Radiohead has %d tracks for a friend, want 1 (Airbag)", a.TrackCount)
}
// Searching and filtering cannot reach past the shared libraries either.
if p, _ := shared.ListTracks(ctx, catalog.Filter{Limit: 50, Query: "karma"}); p.Total != 0 {
t.Errorf("search found %d unshared tracks", p.Total)
}
if p, _ := shared.ListTracks(ctx, catalog.Filter{Limit: 50, LibraryID: s.rock}); p.Total != 0 {
t.Errorf("asking for the unshared library found %d tracks", p.Total)
}
if p, _ := shared.ListTracks(ctx, catalog.Filter{Limit: 50, AlbumID: okComputer}); len(p.Items) != 1 || p.Items[0].Title != "Airbag" {
t.Errorf("tracks of OK Computer for a friend = %v, want only Airbag", trackTitles(p))
}
// A server with the library unshared again shows nothing.
if _, err := libs.SetLibraryShared(ctx, s.jazz, false); err != nil {
t.Fatal(err)
}
if p, _ := shared.ListTracks(ctx, all); p.Total != 0 {
t.Errorf("after unsharing: %d tracks", p.Total)
}
})
}
func TestAFriendIsOnlyServedFilesOfSharedLibraries(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
s := seedCatalog(t, db)
libs := NewLibraryRepo(db)
src := libs.Shared()
if _, err := src.PlayableFile(ctx, s.track["Karma Police"]); !errors.Is(err, streaming.ErrNotFound) {
t.Errorf("nothing shared: %v, want ErrNotFound", err)
}
if _, err := libs.SetLibraryShared(ctx, s.jazz, true); err != nil {
t.Fatal(err)
}
if _, err := src.PlayableFile(ctx, s.track["Karma Police"]); !errors.Is(err, streaming.ErrNotFound) {
t.Errorf("a track of the library that is not shared: %v, want ErrNotFound", err)
}
f, err := src.PlayableFile(ctx, s.track["So What"])
if err != nil || f.Root != "/lib/jazz" || f.RelPath != "m/1.mp3" {
t.Errorf("So What = %+v, %v", f, err)
}
// Airbag has a file in each library: the friend gets the shared one, the listener the oldest.
f, err = src.PlayableFile(ctx, s.track["Airbag"])
if err != nil || f.Root != "/lib/jazz" || f.RelPath != "airbag.mp3" {
t.Errorf("Airbag for a friend = %+v, %v, want the file in the shared library", f, err)
}
if f, _ := libs.PlayableFile(ctx, s.track["Airbag"]); f.Root != "/lib/rock" {
t.Errorf("Airbag for a listener = %+v: the listener's choice must not change", f)
}
})
}