I can now see on Home what I added to the library lately. - GET /albums takes sort=recent: the albums added last first, by the date of the newest file of the album that is not missing, then the newest album. The default stays by title and any other value is invalid_input. - The albums shelf on Home is Recently added and asks for that order; See all leads to /albums?sort=recent, which the albums page understands. - English and Italian.
173 lines
5.9 KiB
Go
173 lines
5.9 KiB
Go
package catalog
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// recordingStore remembers the filter it was called with.
|
|
type recordingStore struct {
|
|
artistFilter, albumFilter, trackFilter Filter
|
|
err error
|
|
}
|
|
|
|
func (r *recordingStore) ListArtists(_ context.Context, f Filter) (Page[Artist], error) {
|
|
r.artistFilter = f
|
|
return Page[Artist]{Limit: f.Limit, Offset: f.Offset}, r.err
|
|
}
|
|
func (r *recordingStore) ListAlbums(_ context.Context, f Filter) (Page[Album], error) {
|
|
r.albumFilter = f
|
|
return Page[Album]{Limit: f.Limit, Offset: f.Offset}, r.err
|
|
}
|
|
func (r *recordingStore) ListTracks(_ context.Context, f Filter) (Page[Track], error) {
|
|
r.trackFilter = f
|
|
return Page[Track]{Limit: f.Limit, Offset: f.Offset}, r.err
|
|
}
|
|
func (r *recordingStore) GetArtist(context.Context, int64) (Artist, error) { return Artist{}, r.err }
|
|
func (r *recordingStore) GetAlbum(context.Context, int64) (Album, error) { return Album{}, r.err }
|
|
func (r *recordingStore) GetTrack(context.Context, int64) (Track, error) { return Track{}, r.err }
|
|
|
|
func TestPagingDefaultsAndBounds(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
in Filter
|
|
wantLimit int
|
|
wantOffset int
|
|
wantInvalid bool
|
|
}{
|
|
{"defaults", Filter{}, DefaultLimit, 0, false},
|
|
{"explicit", Filter{Limit: 10, Offset: 20}, 10, 20, false},
|
|
{"limit above the maximum is clamped", Filter{Limit: MaxLimit + 500}, MaxLimit, 0, false},
|
|
{"exactly the maximum", Filter{Limit: MaxLimit}, MaxLimit, 0, false},
|
|
{"negative limit", Filter{Limit: -1}, 0, 0, true},
|
|
{"negative offset", Filter{Offset: -5}, 0, 0, true},
|
|
{"huge offset is fine", Filter{Offset: 1 << 40}, DefaultLimit, 1 << 40, false},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
st := &recordingStore{}
|
|
_, err := NewService(st).Tracks(context.Background(), tt.in)
|
|
if tt.wantInvalid {
|
|
if !errors.Is(err, ErrInvalidQuery) {
|
|
t.Fatalf("err = %v, want ErrInvalidQuery", err)
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if st.trackFilter.Limit != tt.wantLimit || st.trackFilter.Offset != tt.wantOffset {
|
|
t.Errorf("store saw limit=%d offset=%d, want %d/%d",
|
|
st.trackFilter.Limit, st.trackFilter.Offset, tt.wantLimit, tt.wantOffset)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestQueryIsTrimmedAndBounded(t *testing.T) {
|
|
st := &recordingStore{}
|
|
s := NewService(st)
|
|
|
|
if _, err := s.Artists(context.Background(), Filter{Query: " björk "}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if st.artistFilter.Query != "björk" {
|
|
t.Errorf("query = %q, want it trimmed", st.artistFilter.Query)
|
|
}
|
|
if _, err := s.Albums(context.Background(), Filter{Query: strings.Repeat("a", MaxQueryLen+1)}); !errors.Is(err, ErrInvalidQuery) {
|
|
t.Errorf("a query over the limit: err = %v", err)
|
|
}
|
|
if _, err := s.Albums(context.Background(), Filter{Query: strings.Repeat("é", MaxQueryLen)}); err != nil {
|
|
t.Errorf("the limit counts characters, not bytes: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestNegativeIDsAreRejected(t *testing.T) {
|
|
s := NewService(&recordingStore{})
|
|
for name, f := range map[string]Filter{
|
|
"library": {LibraryID: -1},
|
|
"artist": {ArtistID: -1},
|
|
"album": {AlbumID: -1},
|
|
} {
|
|
if _, err := s.Tracks(context.Background(), f); !errors.Is(err, ErrInvalidQuery) {
|
|
t.Errorf("%s: err = %v, want ErrInvalidQuery", name, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestEveryListGoesThroughTheSameChecks(t *testing.T) {
|
|
s := NewService(&recordingStore{})
|
|
bad := Filter{Limit: -1}
|
|
if _, err := s.Artists(context.Background(), bad); !errors.Is(err, ErrInvalidQuery) {
|
|
t.Errorf("artists: %v", err)
|
|
}
|
|
if _, err := s.Albums(context.Background(), bad); !errors.Is(err, ErrInvalidQuery) {
|
|
t.Errorf("albums: %v", err)
|
|
}
|
|
if _, err := s.Tracks(context.Background(), bad); !errors.Is(err, ErrInvalidQuery) {
|
|
t.Errorf("tracks: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestStoreErrorsPassThrough(t *testing.T) {
|
|
boom := errors.New("db down")
|
|
s := NewService(&recordingStore{err: boom})
|
|
if _, err := s.Tracks(context.Background(), Filter{}); !errors.Is(err, boom) {
|
|
t.Errorf("list: %v", err)
|
|
}
|
|
if _, err := s.Track(context.Background(), 1); !errors.Is(err, boom) {
|
|
t.Errorf("get: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestDetailRejectsBadIDs(t *testing.T) {
|
|
s := NewService(&recordingStore{})
|
|
if _, err := s.Artist(context.Background(), 0); !errors.Is(err, ErrNotFound) {
|
|
t.Errorf("artist 0: %v", err)
|
|
}
|
|
if _, err := s.Album(context.Background(), -3); !errors.Is(err, ErrNotFound) {
|
|
t.Errorf("album -3: %v", err)
|
|
}
|
|
if _, err := s.Track(context.Background(), 0); !errors.Is(err, ErrNotFound) {
|
|
t.Errorf("track 0: %v", err)
|
|
}
|
|
}
|
|
|
|
// bun writes the arguments into the SQL text, which cannot hold a NUL byte, and
|
|
// PostgreSQL rejects bytes that are not valid UTF-8: both must be a 400, not a 500.
|
|
func TestQueryRejectsNULAndInvalidUTF8(t *testing.T) {
|
|
s := NewService(&recordingStore{})
|
|
for name, q := range map[string]string{
|
|
"NUL byte": "a\x00b",
|
|
"leading NUL": "\x00",
|
|
"invalid UTF-8": "caf\xe9",
|
|
"truncated rune": "\xc3",
|
|
"overlong encoded": "\xc0\xaf",
|
|
} {
|
|
if _, err := s.Tracks(context.Background(), Filter{Query: q}); !errors.Is(err, ErrInvalidQuery) {
|
|
t.Errorf("%s: err = %v, want ErrInvalidQuery", name, err)
|
|
}
|
|
}
|
|
if _, err := s.Tracks(context.Background(), Filter{Query: "café ñ 日本語 🎵"}); err != nil {
|
|
t.Errorf("valid Unicode rejected: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestAlbumsCanBeSortedByTitleOrByWhenTheyWereAdded(t *testing.T) {
|
|
for _, sortBy := range []string{"", "title", "recent"} {
|
|
st := &recordingStore{}
|
|
if _, err := NewService(st).Albums(context.Background(), Filter{Sort: sortBy}); err != nil {
|
|
t.Errorf("sort %q: %v", sortBy, err)
|
|
}
|
|
if st.albumFilter.Sort != sortBy {
|
|
t.Errorf("the store saw sort %q, want %q", st.albumFilter.Sort, sortBy)
|
|
}
|
|
}
|
|
for _, sortBy := range []string{"loudest", "Recent", "title desc"} {
|
|
if _, err := NewService(&recordingStore{}).Albums(context.Background(), Filter{Sort: sortBy}); !errors.Is(err, ErrInvalidQuery) {
|
|
t.Errorf("sort %q: err = %v, want ErrInvalidQuery", sortBy, err)
|
|
}
|
|
}
|
|
}
|