feat(catalog): add the read API for artists, albums and tracks

- feat(api): GET /api/v1/artists, /albums and /tracks with q (case and accent
  insensitive substring), limit and offset (default 50, at most 200) and a
  total; filters library_id, artist_id and album_id; GET /artists/{id},
  /albums/{id} and /tracks/{id}; any signed-in user can read
- feat(catalog): only tracks with a file that is not flagged missing are
  visible, so everything shown can be played; a track with several files is
  listed once; album artists without credited tracks (compilations) are listed
- feat(storage): results are ordered the same way on SQLite and PostgreSQL
  (plain byte order), LIKE wildcards in the search text are escaped, and a
  page costs a fixed number of queries: related items and counts are loaded in
  batches
- feat(storage): tracks get a title_key computed in Go (migration 0006, with
  a startup refresh), like artists and albums
- test: ordering, paging, search with accents and wildcards, library filter,
  duplicate encodings, missing files, details, and a query-count guard on
  both databases
This commit is contained in:
tgiachi 2026-11-11 13:43:38 +01:00
commit e14c523828
12 changed files with 1821 additions and 5 deletions

View file

@ -16,6 +16,7 @@ import (
httpapi "codeberg.org/squidd/sonda/internal/api/http"
"codeberg.org/squidd/sonda/internal/auth"
"codeberg.org/squidd/sonda/internal/catalog"
"codeberg.org/squidd/sonda/internal/config"
"codeberg.org/squidd/sonda/internal/library"
"codeberg.org/squidd/sonda/internal/listening"
@ -103,6 +104,7 @@ func run() error {
Listening: listening.NewService(storage.NewListeningRepo(db)),
Scan: scanControl{ctx: ctx, m: scans},
Libraries: libraries,
Catalog: catalog.NewService(storage.NewCatalogRepo(db)),
Streams: streaming.NewService(libraryRepo),
})

View file

@ -0,0 +1,257 @@
package httpapi
import (
"context"
"net/http"
"sort"
"testing"
"codeberg.org/squidd/sonda/internal/catalog"
)
// fakeCatalog returns canned data and records the last filter it received.
type fakeCatalog struct {
last catalog.Filter
err error
artists []catalog.Artist
albums []catalog.Album
tracks []catalog.Track
}
func (f *fakeCatalog) Artists(_ context.Context, fl catalog.Filter) (catalog.Page[catalog.Artist], error) {
f.last = fl
return catalog.Page[catalog.Artist]{Items: f.artists, Total: 123, Limit: fl.Limit, Offset: fl.Offset}, f.err
}
func (f *fakeCatalog) Albums(_ context.Context, fl catalog.Filter) (catalog.Page[catalog.Album], error) {
f.last = fl
return catalog.Page[catalog.Album]{Items: f.albums, Total: 7, Limit: fl.Limit, Offset: fl.Offset}, f.err
}
func (f *fakeCatalog) Tracks(_ context.Context, fl catalog.Filter) (catalog.Page[catalog.Track], error) {
f.last = fl
return catalog.Page[catalog.Track]{Items: f.tracks, Total: 9, Limit: fl.Limit, Offset: fl.Offset}, f.err
}
func (f *fakeCatalog) Artist(_ context.Context, id int64) (catalog.Artist, error) {
if f.err != nil {
return catalog.Artist{}, f.err
}
return catalog.Artist{ID: id, Name: "Radiohead", AlbumCount: 2, TrackCount: 20}, nil
}
func (f *fakeCatalog) Album(_ context.Context, id int64) (catalog.Album, error) {
if f.err != nil {
return catalog.Album{}, f.err
}
return catalog.Album{ID: id, Title: "OK Computer", TrackCount: 12, DurationMs: 3200000,
Artist: &catalog.ArtistRef{ID: 1, Name: "Radiohead"}}, nil
}
func (f *fakeCatalog) Track(_ context.Context, id int64) (catalog.Track, error) {
if f.err != nil {
return catalog.Track{}, f.err
}
return catalog.Track{ID: id, Title: "Airbag", DurationMs: 284000, TrackNo: 1, DiscNo: 1, Format: "flac",
Album: &catalog.AlbumRef{ID: 3, Title: "OK Computer"},
Artists: []catalog.ArtistRef{{ID: 1, Name: "Radiohead"}}}, nil
}
var catalogRoutes = []string{
"/api/v1/artists", "/api/v1/albums", "/api/v1/tracks",
"/api/v1/artists/1", "/api/v1/albums/1", "/api/v1/tracks/1",
}
func catalogAPI(t *testing.T, fc *fakeCatalog) (http.Handler, string) {
t.Helper()
h := newTestAPIWith(t, func(d *Deps) { d.Catalog = fc })
return h, registerAndLogin(t, h)
}
func keys(m map[string]any) []string {
var out []string
for k := range m {
out = append(out, k)
}
sort.Strings(out)
return out
}
func sameKeys(t *testing.T, what string, got map[string]any, want ...string) {
t.Helper()
sort.Strings(want)
g := keys(got)
if len(g) != len(want) {
t.Errorf("%s keys = %v, want %v", what, g, want)
return
}
for i := range g {
if g[i] != want[i] {
t.Errorf("%s keys = %v, want %v", what, g, want)
return
}
}
}
func TestCatalogRoutesRequireAuth(t *testing.T) {
h, _ := catalogAPI(t, &fakeCatalog{})
for _, path := range catalogRoutes {
if r := do(t, h, "GET", path, "", nil); r.Code != http.StatusUnauthorized {
t.Errorf("GET %s without a token: %d", path, r.Code)
}
if r := do(t, h, "GET", path+"?access_token=x", "", nil); r.Code != http.StatusUnauthorized {
t.Errorf("GET %s: a token in the URL must not work here: %d", path, r.Code)
}
}
}
func TestAnySignedInUserCanBrowse(t *testing.T) {
fc := &fakeCatalog{}
h, admin := catalogAPI(t, fc)
_, bob := addUser(t, h, admin, "bob", false)
for _, path := range catalogRoutes {
if r := do(t, h, "GET", path, bob, nil); r.Code != http.StatusOK {
t.Errorf("GET %s as a normal user: %d %v", path, r.Code, r.Body)
}
}
}
func TestCatalogListShapeAndPaging(t *testing.T) {
fc := &fakeCatalog{
artists: []catalog.Artist{{ID: 1, Name: "Björk", AlbumCount: 3, TrackCount: 30}},
albums: []catalog.Album{{ID: 2, Title: "Post", TrackCount: 11, DurationMs: 2400000, Artist: &catalog.ArtistRef{ID: 1, Name: "Björk"}}},
tracks: []catalog.Track{{ID: 3, Title: "Army of Me", DurationMs: 235000, TrackNo: 1, DiscNo: 1, Format: "flac",
Album: &catalog.AlbumRef{ID: 2, Title: "Post"}, Artists: []catalog.ArtistRef{{ID: 1, Name: "Björk"}}}},
}
h, tok := catalogAPI(t, fc)
r := do(t, h, "GET", "/api/v1/artists?limit=20&offset=40", tok, nil)
if r.Code != http.StatusOK || r.Body["total"] != float64(123) || r.Body["limit"] != float64(20) || r.Body["offset"] != float64(40) {
t.Fatalf("artists page: %d %v", r.Code, r.Body)
}
sameKeys(t, "artists response", r.Body, "items", "total", "limit", "offset")
a := r.Body["items"].([]any)[0].(map[string]any)
sameKeys(t, "artist", a, "id", "name", "album_count", "track_count")
if a["name"] != "Björk" || a["album_count"] != float64(3) || a["track_count"] != float64(30) {
t.Errorf("artist = %v", a)
}
r = do(t, h, "GET", "/api/v1/albums", tok, nil)
al := r.Body["items"].([]any)[0].(map[string]any)
sameKeys(t, "album", al, "id", "title", "artist", "track_count", "duration_ms")
sameKeys(t, "album artist", al["artist"].(map[string]any), "id", "name")
r = do(t, h, "GET", "/api/v1/tracks", tok, nil)
tr := r.Body["items"].([]any)[0].(map[string]any)
// No file path, root or library path may ever appear.
sameKeys(t, "track", tr, "id", "title", "duration_ms", "track_no", "disc_no", "album", "artists", "format")
sameKeys(t, "track album", tr["album"].(map[string]any), "id", "title")
if tr["format"] != "flac" || len(tr["artists"].([]any)) != 1 {
t.Errorf("track = %v", tr)
}
}
func TestCatalogEmptyListIsAnArrayNotNull(t *testing.T) {
h, tok := catalogAPI(t, &fakeCatalog{})
for _, path := range []string{"/api/v1/artists", "/api/v1/albums", "/api/v1/tracks"} {
r := do(t, h, "GET", path, tok, nil)
items, ok := r.Body["items"].([]any)
if !ok || len(items) != 0 {
t.Errorf("%s: items = %#v, want an empty array", path, r.Body["items"])
}
}
}
func TestTrackWithoutAlbumHasNullAlbum(t *testing.T) {
fc := &fakeCatalog{tracks: []catalog.Track{{ID: 1, Title: "Loose", Artists: nil}}}
h, tok := catalogAPI(t, fc)
r := do(t, h, "GET", "/api/v1/tracks", tok, nil)
tr := r.Body["items"].([]any)[0].(map[string]any)
if v, present := tr["album"]; !present || v != nil {
t.Errorf("album = %#v, want an explicit null", v)
}
if arts, ok := tr["artists"].([]any); !ok || len(arts) != 0 {
t.Errorf("artists = %#v, want an empty array", tr["artists"])
}
}
func TestCatalogQueryParametersReachTheService(t *testing.T) {
fc := &fakeCatalog{}
h, tok := catalogAPI(t, fc)
do(t, h, "GET", "/api/v1/tracks?q=black+sabbath&limit=25&offset=50&library_id=2&artist_id=3&album_id=4", tok, nil)
want := catalog.Filter{Query: "black sabbath", Limit: 25, Offset: 50, LibraryID: 2, ArtistID: 3, AlbumID: 4}
if fc.last != want {
t.Errorf("filter = %+v, want %+v", fc.last, want)
}
do(t, h, "GET", "/api/v1/albums?artist_id=9", tok, nil)
if fc.last.ArtistID != 9 || fc.last.Limit != 0 {
t.Errorf("album filter = %+v (unset paging must stay 0 for the service to default)", fc.last)
}
// Special characters survive URL decoding.
do(t, h, "GET", "/api/v1/artists?q=100%25+%26+more", tok, nil)
if fc.last.Query != "100% & more" {
t.Errorf("q = %q", fc.last.Query)
}
}
func TestCatalogRejectsBadParameters(t *testing.T) {
fc := &fakeCatalog{}
h, tok := catalogAPI(t, fc)
for _, qs := range []string{"limit=abc", "offset=x", "library_id=1.5", "artist_id=-", "album_id=%20", "limit=99999999999999999999"} {
r := do(t, h, "GET", "/api/v1/tracks?"+qs, tok, nil)
if r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" {
t.Errorf("?%s: %d %v", qs, r.Code, r.Body)
}
}
// The service's own validation is a 400 too.
fc.err = catalog.ErrInvalidQuery
r := do(t, h, "GET", "/api/v1/tracks?limit=-1", tok, nil)
if r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" {
t.Errorf("service rejection: %d %v", r.Code, r.Body)
}
}
func TestCatalogDetails(t *testing.T) {
h, tok := catalogAPI(t, &fakeCatalog{})
r := do(t, h, "GET", "/api/v1/artists/5", tok, nil)
sameKeys(t, "artist", r.Body, "id", "name", "album_count", "track_count")
if r.Code != http.StatusOK || r.Body["id"] != float64(5) {
t.Errorf("artist: %d %v", r.Code, r.Body)
}
r = do(t, h, "GET", "/api/v1/albums/6", tok, nil)
sameKeys(t, "album", r.Body, "id", "title", "artist", "track_count", "duration_ms")
r = do(t, h, "GET", "/api/v1/tracks/7", tok, nil)
sameKeys(t, "track", r.Body, "id", "title", "duration_ms", "track_no", "disc_no", "album", "artists", "format")
if r.Code != http.StatusOK || r.Body["id"] != float64(7) {
t.Errorf("track: %d %v", r.Code, r.Body)
}
}
func TestCatalogDetailErrors(t *testing.T) {
fc := &fakeCatalog{err: catalog.ErrNotFound}
h, tok := catalogAPI(t, fc)
for path, code := range map[string]string{
"/api/v1/artists/1": "artist_not_found",
"/api/v1/albums/1": "album_not_found",
"/api/v1/tracks/1": "track_not_found",
} {
r := do(t, h, "GET", path, tok, nil)
if r.Code != http.StatusNotFound || errCode(r) != code {
t.Errorf("%s: %d %v, want 404 %s", path, r.Code, r.Body, code)
}
}
for _, path := range []string{"/api/v1/artists/abc", "/api/v1/albums/0", "/api/v1/tracks/-1"} {
if r := do(t, h, "GET", path, tok, nil); r.Code != http.StatusBadRequest {
t.Errorf("%s: %d, want 400", path, r.Code)
}
}
}
// The stream route lives under /tracks/{id}: the detail route must not shadow it.
func TestTrackDetailDoesNotShadowTheStreamRoute(t *testing.T) {
h, tok := catalogAPI(t, &fakeCatalog{})
r := do(t, h, "GET", "/api/v1/tracks/1/stream", tok, nil)
if r.Code == http.StatusOK && r.Body["title"] != nil {
t.Fatal("the stream URL answered with track details")
}
}

View file

@ -0,0 +1,230 @@
package httpapi
import (
"errors"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"codeberg.org/squidd/sonda/internal/catalog"
)
// catalogHandlers serve the read-only catalog to any signed-in user. No file
// path, library path or directory is ever part of these responses.
type catalogHandlers struct{ svc catalogService }
type artistRefJSON struct {
ID int64 `json:"id"`
Name string `json:"name"`
}
type albumRefJSON struct {
ID int64 `json:"id"`
Title string `json:"title"`
}
type artistJSON struct {
ID int64 `json:"id"`
Name string `json:"name"`
AlbumCount int `json:"album_count"`
TrackCount int `json:"track_count"`
}
type albumJSON struct {
ID int64 `json:"id"`
Title string `json:"title"`
Artist *artistRefJSON `json:"artist"`
TrackCount int `json:"track_count"`
DurationMs int64 `json:"duration_ms"`
}
type trackJSON struct {
ID int64 `json:"id"`
Title string `json:"title"`
DurationMs int64 `json:"duration_ms"`
TrackNo int `json:"track_no"`
DiscNo int `json:"disc_no"`
Album *albumRefJSON `json:"album"`
Artists []artistRefJSON `json:"artists"`
Format string `json:"format"`
}
func toArtistJSON(a catalog.Artist) artistJSON {
return artistJSON{ID: a.ID, Name: a.Name, AlbumCount: a.AlbumCount, TrackCount: a.TrackCount}
}
func toAlbumJSON(a catalog.Album) albumJSON {
out := albumJSON{ID: a.ID, Title: a.Title, TrackCount: a.TrackCount, DurationMs: a.DurationMs}
if a.Artist != nil {
out.Artist = &artistRefJSON{ID: a.Artist.ID, Name: a.Artist.Name}
}
return out
}
func toTrackJSON(t catalog.Track) trackJSON {
out := trackJSON{
ID: t.ID, Title: t.Title, DurationMs: t.DurationMs, TrackNo: t.TrackNo, DiscNo: t.DiscNo,
Format: t.Format, Artists: make([]artistRefJSON, len(t.Artists)),
}
if t.Album != nil {
out.Album = &albumRefJSON{ID: t.Album.ID, Title: t.Album.Title}
}
for i, a := range t.Artists {
out.Artists[i] = artistRefJSON{ID: a.ID, Name: a.Name}
}
return out
}
// parseFilter reads q, limit, offset and the id filters from the query string.
// Unset values stay zero so the service can apply its defaults.
func parseFilter(c *gin.Context) (catalog.Filter, bool) {
f := catalog.Filter{Query: c.Query("q")}
for _, p := range []struct {
name string
dst *int64
}{
{"library_id", &f.LibraryID}, {"artist_id", &f.ArtistID}, {"album_id", &f.AlbumID},
} {
v, ok := queryInt(c, p.name)
if !ok {
return f, false
}
*p.dst = v
}
limit, ok := queryInt(c, "limit")
if !ok {
return f, false
}
offset, ok := queryInt(c, "offset")
if !ok {
return f, false
}
// Clamp before narrowing, so a huge number cannot wrap into a small one.
f.Limit, f.Offset = clampInt(limit), clampInt(offset)
return f, true
}
// queryInt reads an optional integer parameter; it answers 400 itself when the
// value is not a number.
func queryInt(c *gin.Context, name string) (int64, bool) {
raw := c.Query(name)
if raw == "" {
return 0, true
}
n, err := strconv.ParseInt(raw, 10, 64)
if err != nil {
writeError(c, http.StatusBadRequest, "invalid_input", name+" must be a whole number")
return 0, false
}
return n, true
}
func clampInt(n int64) int {
const max = 1 << 31
switch {
case n > max:
return max
case n < -max:
return -max
}
return int(n)
}
func writeCatalogError(c *gin.Context, err error, notFoundCode string) {
switch {
case errors.Is(err, catalog.ErrInvalidQuery):
writeError(c, http.StatusBadRequest, "invalid_input", err.Error())
case errors.Is(err, catalog.ErrNotFound):
writeError(c, http.StatusNotFound, notFoundCode, "not found")
default:
_ = c.Error(err)
writeError(c, http.StatusInternalServerError, "internal", "internal error")
}
}
func writePage[T any, J any](c *gin.Context, p catalog.Page[T], conv func(T) J) {
items := make([]J, len(p.Items))
for i, it := range p.Items {
items[i] = conv(it)
}
c.JSON(http.StatusOK, gin.H{"items": items, "total": p.Total, "limit": p.Limit, "offset": p.Offset})
}
func (h *catalogHandlers) listArtists(c *gin.Context) {
f, ok := parseFilter(c)
if !ok {
return
}
p, err := h.svc.Artists(c.Request.Context(), f)
if err != nil {
writeCatalogError(c, err, "artist_not_found")
return
}
writePage(c, p, toArtistJSON)
}
func (h *catalogHandlers) listAlbums(c *gin.Context) {
f, ok := parseFilter(c)
if !ok {
return
}
p, err := h.svc.Albums(c.Request.Context(), f)
if err != nil {
writeCatalogError(c, err, "album_not_found")
return
}
writePage(c, p, toAlbumJSON)
}
func (h *catalogHandlers) listTracks(c *gin.Context) {
f, ok := parseFilter(c)
if !ok {
return
}
p, err := h.svc.Tracks(c.Request.Context(), f)
if err != nil {
writeCatalogError(c, err, "track_not_found")
return
}
writePage(c, p, toTrackJSON)
}
func (h *catalogHandlers) getArtist(c *gin.Context) {
id, ok := pathID(c)
if !ok {
return
}
a, err := h.svc.Artist(c.Request.Context(), id)
if err != nil {
writeCatalogError(c, err, "artist_not_found")
return
}
c.JSON(http.StatusOK, toArtistJSON(a))
}
func (h *catalogHandlers) getAlbum(c *gin.Context) {
id, ok := pathID(c)
if !ok {
return
}
a, err := h.svc.Album(c.Request.Context(), id)
if err != nil {
writeCatalogError(c, err, "album_not_found")
return
}
c.JSON(http.StatusOK, toAlbumJSON(a))
}
func (h *catalogHandlers) getTrack(c *gin.Context) {
id, ok := pathID(c)
if !ok {
return
}
t, err := h.svc.Track(c.Request.Context(), id)
if err != nil {
writeCatalogError(c, err, "track_not_found")
return
}
c.JSON(http.StatusOK, toTrackJSON(t))
}

View file

@ -11,6 +11,7 @@ import (
"go.uber.org/zap"
"codeberg.org/squidd/sonda/internal/auth"
"codeberg.org/squidd/sonda/internal/catalog"
"codeberg.org/squidd/sonda/internal/library"
"codeberg.org/squidd/sonda/internal/listening"
"codeberg.org/squidd/sonda/internal/streaming"
@ -51,6 +52,15 @@ type scanService interface {
Status() library.Status
}
type catalogService interface {
Artists(ctx context.Context, f catalog.Filter) (catalog.Page[catalog.Artist], error)
Albums(ctx context.Context, f catalog.Filter) (catalog.Page[catalog.Album], error)
Tracks(ctx context.Context, f catalog.Filter) (catalog.Page[catalog.Track], error)
Artist(ctx context.Context, id int64) (catalog.Artist, error)
Album(ctx context.Context, id int64) (catalog.Album, error)
Track(ctx context.Context, id int64) (catalog.Track, error)
}
type libraryService interface {
List(ctx context.Context) ([]library.Library, error)
Create(ctx context.Context, name, path string) (library.Library, error)
@ -66,6 +76,7 @@ type Deps struct {
Listening listenService
Scan scanService
Libraries libraryService
Catalog catalogService
Streams streamService
}
@ -95,6 +106,14 @@ func NewRouter(d Deps) *gin.Engine {
lib := &libraryHandlers{scan: d.Scan}
protected.GET("/library/scan", lib.scanStatus)
cat := &catalogHandlers{svc: d.Catalog}
protected.GET("/artists", cat.listArtists)
protected.GET("/artists/:id", cat.getArtist)
protected.GET("/albums", cat.listAlbums)
protected.GET("/albums/:id", cat.getAlbum)
protected.GET("/tracks", cat.listTracks)
protected.GET("/tracks/:id", cat.getTrack)
// Audio loaded by an <audio> element cannot send headers, so this route also
// takes the access token as a query parameter. It never records a listen.
st := &streamHandlers{svc: d.Streams}

177
internal/catalog/catalog.go Normal file
View file

@ -0,0 +1,177 @@
// Package catalog is the read side of the library: browsing and searching
// artists, albums and tracks.
//
// Only tracks with at least one file that is not flagged missing are visible,
// so everything the catalog shows can be played. File paths are never part of
// the data returned here.
package catalog
import (
"context"
"errors"
"fmt"
"strings"
"unicode/utf8"
)
// Errors returned by the service.
var (
ErrNotFound = errors.New("not found")
ErrInvalidQuery = errors.New("invalid query")
)
// Paging and search bounds.
const (
DefaultLimit = 50
MaxLimit = 200
MaxQueryLen = 200 // characters
)
// Filter selects and pages a list. Zero IDs mean "any". Each list uses the
// fields that make sense for it and ignores the others.
type Filter struct {
Query string // case and accent insensitive substring of the name or title
LibraryID int64 // only items with a playable file in this library
ArtistID int64 // albums: album artist; tracks: credited artist
AlbumID int64 // tracks only
Limit int
Offset int
}
// Page is one page of a list together with the size of the whole result.
type Page[T any] struct {
Items []T
Total int
Limit int
Offset int
}
// ArtistRef and AlbumRef are the short forms used inside other items.
type ArtistRef struct {
ID int64
Name string
}
type AlbumRef struct {
ID int64
Title string
}
// Artist is an artist with the amount of music that can be played.
type Artist struct {
ID int64
Name string
AlbumCount int
TrackCount int
}
// Album is an album with its album artist, if it has one.
type Album struct {
ID int64
Title string
Artist *ArtistRef
TrackCount int
DurationMs int64
}
// Track is a playable track. Format is that of its first playable file.
type Track struct {
ID int64
Title string
DurationMs int64
TrackNo int
DiscNo int
Album *AlbumRef
Artists []ArtistRef
Format string
}
// Store reads the catalog. Lists are ordered deterministically and must load
// related data in batches. Get methods return ErrNotFound for an id that does
// not exist or has nothing playable.
type Store interface {
ListArtists(ctx context.Context, f Filter) (Page[Artist], error)
ListAlbums(ctx context.Context, f Filter) (Page[Album], error)
ListTracks(ctx context.Context, f Filter) (Page[Track], error)
GetArtist(ctx context.Context, id int64) (Artist, error)
GetAlbum(ctx context.Context, id int64) (Album, error)
GetTrack(ctx context.Context, id int64) (Track, error)
}
// Service validates queries and reads the catalog.
type Service struct{ store Store }
// NewService builds a Service.
func NewService(store Store) *Service { return &Service{store: store} }
// normalize validates f and fills in the paging defaults.
func normalize(f Filter) (Filter, error) {
if f.Limit < 0 || f.Offset < 0 {
return f, fmt.Errorf("%w: limit and offset cannot be negative", ErrInvalidQuery)
}
if f.LibraryID < 0 || f.ArtistID < 0 || f.AlbumID < 0 {
return f, fmt.Errorf("%w: ids cannot be negative", ErrInvalidQuery)
}
switch {
case f.Limit == 0:
f.Limit = DefaultLimit
case f.Limit > MaxLimit:
f.Limit = MaxLimit
}
f.Query = strings.TrimSpace(f.Query)
if utf8.RuneCountInString(f.Query) > MaxQueryLen {
return f, fmt.Errorf("%w: q is longer than %d characters", ErrInvalidQuery, MaxQueryLen)
}
return f, nil
}
// Artists lists artists that have something playable.
func (s *Service) Artists(ctx context.Context, f Filter) (Page[Artist], error) {
f, err := normalize(f)
if err != nil {
return Page[Artist]{}, err
}
return s.store.ListArtists(ctx, f)
}
// Albums lists albums that have something playable.
func (s *Service) Albums(ctx context.Context, f Filter) (Page[Album], error) {
f, err := normalize(f)
if err != nil {
return Page[Album]{}, err
}
return s.store.ListAlbums(ctx, f)
}
// Tracks lists playable tracks.
func (s *Service) Tracks(ctx context.Context, f Filter) (Page[Track], error) {
f, err := normalize(f)
if err != nil {
return Page[Track]{}, err
}
return s.store.ListTracks(ctx, f)
}
// Artist returns one artist.
func (s *Service) Artist(ctx context.Context, id int64) (Artist, error) {
if id <= 0 {
return Artist{}, ErrNotFound
}
return s.store.GetArtist(ctx, id)
}
// Album returns one album.
func (s *Service) Album(ctx context.Context, id int64) (Album, error) {
if id <= 0 {
return Album{}, ErrNotFound
}
return s.store.GetAlbum(ctx, id)
}
// Track returns one track.
func (s *Service) Track(ctx context.Context, id int64) (Track, error) {
if id <= 0 {
return Track{}, ErrNotFound
}
return s.store.GetTrack(ctx, id)
}

View file

@ -0,0 +1,136 @@
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)
}
}

486
internal/storage/catalog.go Normal file
View file

@ -0,0 +1,486 @@
package storage
import (
"context"
"database/sql"
"fmt"
"strings"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect"
"codeberg.org/squidd/sonda/internal/catalog"
)
// CatalogRepo reads the catalog. It implements catalog.Store.
//
// Everything listed is a track with at least one audio file that is not flagged
// missing ("visible"). Lists run a fixed number of queries: one count, one page
// and a few batched lookups for counts and related items, never one per row.
// The SQL is written to behave the same on SQLite and PostgreSQL.
type CatalogRepo struct{ db *bun.DB }
// NewCatalogRepo builds a CatalogRepo.
func NewCatalogRepo(db *bun.DB) *CatalogRepo { return &CatalogRepo{db: db} }
// ---- query helpers ----
// where collects AND-ed conditions together with their arguments, in order.
type where struct {
parts []string
args []any
}
func (w *where) add(sql string, args ...any) {
w.parts = append(w.parts, sql)
w.args = append(w.args, args...)
}
func (w *where) sql() string {
if len(w.parts) == 0 {
return "1=1"
}
return strings.Join(w.parts, " AND ")
}
// visible is the condition "this track has a playable file", optionally inside
// one library. alias is the name of the tracks table in the surrounding query.
func visible(alias string, libraryID int64) (string, []any) {
q := "EXISTS (SELECT 1 FROM audio_files f WHERE f.track_id = " + alias + ".id AND f.missing_at IS NULL"
var args []any
if libraryID != 0 {
q += " AND f.library_id = ?"
args = append(args, libraryID)
}
return q + ")", args
}
// likePattern turns a search text into a LIKE pattern for the match keys. The
// wildcard characters of the text are escaped with '!', so "100%" only matches
// a literal percent sign.
func likePattern(q string) string {
esc := strings.NewReplacer("!", "!!", "%", "!%", "_", "!_").Replace(matchKey(q))
return "%" + esc + "%"
}
// binary orders by plain byte value. PostgreSQL would otherwise follow the
// database locale while SQLite compares bytes, and listings would differ.
func (r *CatalogRepo) binary(expr string) string {
if r.db.Dialect().Name() == dialect.PG {
return expr + ` COLLATE "C"`
}
return expr
}
func (r *CatalogRepo) count(ctx context.Context, query string, args ...any) (int, error) {
var n int
if err := r.db.QueryRowContext(ctx, query, args...).Scan(&n); err != nil {
return 0, fmt.Errorf("count: %w", err)
}
return n, nil
}
// pairs runs a query that returns (id, number) rows and collects them in a map.
func (r *CatalogRepo) pairs(ctx context.Context, query string, args ...any) (map[int64]int64, error) {
rows, err := r.db.QueryContext(ctx, query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
out := map[int64]int64{}
for rows.Next() {
var id, n int64
if err := rows.Scan(&id, &n); err != nil {
return nil, err
}
out[id] = n
}
return out, rows.Err()
}
// ---- artists ----
// ListArtists lists artists with a playable credited track or a playable album.
func (r *CatalogRepo) ListArtists(ctx context.Context, f catalog.Filter) (catalog.Page[catalog.Artist], error) {
return r.listArtists(ctx, f, 0)
}
// GetArtist returns one artist, or catalog.ErrNotFound.
func (r *CatalogRepo) GetArtist(ctx context.Context, id int64) (catalog.Artist, error) {
p, err := r.listArtists(ctx, catalog.Filter{Limit: 1}, id)
if err != nil {
return catalog.Artist{}, err
}
if len(p.Items) == 0 {
return catalog.Artist{}, catalog.ErrNotFound
}
return p.Items[0], nil
}
func (r *CatalogRepo) listArtists(ctx context.Context, f catalog.Filter, id int64) (catalog.Page[catalog.Artist], error) {
page := catalog.Page[catalog.Artist]{Items: []catalog.Artist{}, Limit: f.Limit, Offset: f.Offset}
vis, visArgs := visible("t", f.LibraryID)
var w where
// An artist counts when credited on a playable track, or when it is the
// album artist of an album with a playable track (compilations).
w.add(`(EXISTS (SELECT 1 FROM track_artists ta JOIN tracks t ON t.id = ta.track_id WHERE ta.artist_id = a.id AND `+vis+`)
OR EXISTS (SELECT 1 FROM albums al JOIN tracks t ON t.album_id = al.id WHERE al.artist_id = a.id AND `+vis+`))`,
append(append([]any{}, visArgs...), visArgs...)...)
if id != 0 {
w.add("a.id = ?", id)
}
if f.Query != "" {
w.add("a.name_key LIKE ? ESCAPE '!'", likePattern(f.Query))
}
total, err := r.count(ctx, "SELECT COUNT(*) FROM artists a WHERE "+w.sql(), w.args...)
if err != nil {
return page, err
}
page.Total = total
if f.Offset >= total {
return page, nil
}
rows, err := r.db.QueryContext(ctx,
"SELECT a.id, a.name FROM artists a WHERE "+w.sql()+" ORDER BY "+r.binary("a.name_key")+", a.id LIMIT ? OFFSET ?",
append(append([]any{}, w.args...), f.Limit, f.Offset)...)
if err != nil {
return page, fmt.Errorf("list artists: %w", err)
}
defer rows.Close()
var ids []int64
for rows.Next() {
var a catalog.Artist
if err := rows.Scan(&a.ID, &a.Name); err != nil {
return page, err
}
page.Items = append(page.Items, a)
ids = append(ids, a.ID)
}
if err := rows.Err(); err != nil {
return page, err
}
if len(ids) == 0 {
return page, nil
}
tracks, err := r.pairs(ctx,
`SELECT ta.artist_id, COUNT(DISTINCT t.id) FROM track_artists ta JOIN tracks t ON t.id = ta.track_id
WHERE ta.artist_id IN (?) AND `+vis+` GROUP BY ta.artist_id`,
append([]any{bun.In(ids)}, visArgs...)...)
if err != nil {
return page, fmt.Errorf("count artist tracks: %w", err)
}
albums, err := r.pairs(ctx,
`SELECT x.artist_id, COUNT(DISTINCT x.album_id) FROM (
SELECT ta.artist_id AS artist_id, t.album_id AS album_id
FROM track_artists ta JOIN tracks t ON t.id = ta.track_id
WHERE ta.artist_id IN (?) AND t.album_id IS NOT NULL AND `+vis+`
UNION
SELECT al.artist_id AS artist_id, al.id AS album_id FROM albums al
WHERE al.artist_id IN (?) AND EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = al.id AND `+vis+`)
) x GROUP BY x.artist_id`,
append(append(append([]any{bun.In(ids)}, visArgs...), bun.In(ids)), visArgs...)...)
if err != nil {
return page, fmt.Errorf("count artist albums: %w", err)
}
for i := range page.Items {
page.Items[i].TrackCount = int(tracks[page.Items[i].ID])
page.Items[i].AlbumCount = int(albums[page.Items[i].ID])
}
return page, nil
}
// ---- albums ----
// ListAlbums lists albums that have a playable track.
func (r *CatalogRepo) ListAlbums(ctx context.Context, f catalog.Filter) (catalog.Page[catalog.Album], error) {
return r.listAlbums(ctx, f, 0)
}
// GetAlbum returns one album, or catalog.ErrNotFound.
func (r *CatalogRepo) GetAlbum(ctx context.Context, id int64) (catalog.Album, error) {
p, err := r.listAlbums(ctx, catalog.Filter{Limit: 1}, id)
if err != nil {
return catalog.Album{}, err
}
if len(p.Items) == 0 {
return catalog.Album{}, catalog.ErrNotFound
}
return p.Items[0], nil
}
func (r *CatalogRepo) listAlbums(ctx context.Context, f catalog.Filter, id int64) (catalog.Page[catalog.Album], error) {
page := catalog.Page[catalog.Album]{Items: []catalog.Album{}, Limit: f.Limit, Offset: f.Offset}
vis, visArgs := visible("t", f.LibraryID)
var w where
w.add("EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = al.id AND "+vis+")", visArgs...)
if id != 0 {
w.add("al.id = ?", id)
}
if f.ArtistID != 0 {
w.add("al.artist_id = ?", f.ArtistID)
}
if f.Query != "" {
w.add("al.title_key LIKE ? ESCAPE '!'", likePattern(f.Query))
}
total, err := r.count(ctx, "SELECT COUNT(*) FROM albums al WHERE "+w.sql(), w.args...)
if err != nil {
return page, err
}
page.Total = total
if f.Offset >= total {
return page, nil
}
rows, err := r.db.QueryContext(ctx,
`SELECT al.id, al.title, al.artist_id, ar.name FROM albums al
LEFT JOIN artists ar ON ar.id = al.artist_id
WHERE `+w.sql()+` ORDER BY `+r.binary("al.title_key")+`, al.id LIMIT ? OFFSET ?`,
append(append([]any{}, w.args...), f.Limit, f.Offset)...)
if err != nil {
return page, fmt.Errorf("list albums: %w", err)
}
defer rows.Close()
var ids []int64
for rows.Next() {
var (
a catalog.Album
artistID sql.NullInt64
artistName sql.NullString
)
if err := rows.Scan(&a.ID, &a.Title, &artistID, &artistName); err != nil {
return page, err
}
if artistID.Valid {
a.Artist = &catalog.ArtistRef{ID: artistID.Int64, Name: artistName.String}
}
page.Items = append(page.Items, a)
ids = append(ids, a.ID)
}
if err := rows.Err(); err != nil {
return page, err
}
if len(ids) == 0 {
return page, nil
}
type totals struct {
tracks int
ms int64
}
sums := map[int64]totals{}
cr, err := r.db.QueryContext(ctx,
`SELECT t.album_id, COUNT(*), CAST(COALESCE(SUM(t.duration_ms), 0) AS BIGINT) FROM tracks t
WHERE t.album_id IN (?) AND `+vis+` GROUP BY t.album_id`,
append([]any{bun.In(ids)}, visArgs...)...)
if err != nil {
return page, fmt.Errorf("count album tracks: %w", err)
}
defer cr.Close()
for cr.Next() {
var albumID, n, ms int64
if err := cr.Scan(&albumID, &n, &ms); err != nil {
return page, err
}
sums[albumID] = totals{tracks: int(n), ms: ms}
}
if err := cr.Err(); err != nil {
return page, err
}
for i := range page.Items {
page.Items[i].TrackCount = sums[page.Items[i].ID].tracks
page.Items[i].DurationMs = sums[page.Items[i].ID].ms
}
return page, nil
}
// ---- tracks ----
// ListTracks lists playable tracks, grouped by album.
func (r *CatalogRepo) ListTracks(ctx context.Context, f catalog.Filter) (catalog.Page[catalog.Track], error) {
return r.listTracks(ctx, f, 0)
}
// GetTrack returns one track, or catalog.ErrNotFound.
func (r *CatalogRepo) GetTrack(ctx context.Context, id int64) (catalog.Track, error) {
p, err := r.listTracks(ctx, catalog.Filter{Limit: 1}, id)
if err != nil {
return catalog.Track{}, err
}
if len(p.Items) == 0 {
return catalog.Track{}, catalog.ErrNotFound
}
return p.Items[0], nil
}
func (r *CatalogRepo) listTracks(ctx context.Context, f catalog.Filter, id int64) (catalog.Page[catalog.Track], error) {
page := catalog.Page[catalog.Track]{Items: []catalog.Track{}, Limit: f.Limit, Offset: f.Offset}
vis, visArgs := visible("t", f.LibraryID)
var w where
w.add(vis, visArgs...)
if id != 0 {
w.add("t.id = ?", id)
}
if f.AlbumID != 0 {
w.add("t.album_id = ?", f.AlbumID)
}
if f.ArtistID != 0 {
w.add("EXISTS (SELECT 1 FROM track_artists ta WHERE ta.track_id = t.id AND ta.artist_id = ?)", f.ArtistID)
}
if f.Query != "" {
w.add("t.title_key LIKE ? ESCAPE '!'", likePattern(f.Query))
}
total, err := r.count(ctx, "SELECT COUNT(*) FROM tracks t WHERE "+w.sql(), w.args...)
if err != nil {
return page, err
}
page.Total = total
if f.Offset >= total {
return page, nil
}
rows, err := r.db.QueryContext(ctx,
`SELECT t.id, t.title, COALESCE(t.duration_ms, 0), COALESCE(t.track_no, 0), COALESCE(t.disc_no, 0), t.album_id
FROM tracks t LEFT JOIN albums al ON al.id = t.album_id
WHERE `+w.sql()+`
ORDER BY `+r.binary("COALESCE(al.title_key, '')")+`, COALESCE(t.disc_no, 0), COALESCE(t.track_no, 0),
`+r.binary("t.title_key")+`, t.id
LIMIT ? OFFSET ?`,
append(append([]any{}, w.args...), f.Limit, f.Offset)...)
if err != nil {
return page, fmt.Errorf("list tracks: %w", err)
}
defer rows.Close()
albumOf := map[int64]int64{} // track id -> album id
var ids []int64
for rows.Next() {
var (
t catalog.Track
albumID sql.NullInt64
)
if err := rows.Scan(&t.ID, &t.Title, &t.DurationMs, &t.TrackNo, &t.DiscNo, &albumID); err != nil {
return page, err
}
t.Artists = []catalog.ArtistRef{}
if albumID.Valid {
albumOf[t.ID] = albumID.Int64
}
page.Items = append(page.Items, t)
ids = append(ids, t.ID)
}
if err := rows.Err(); err != nil {
return page, err
}
if len(ids) == 0 {
return page, nil
}
return page, r.attachTrackDetails(ctx, &page, ids, albumOf, f.LibraryID)
}
// attachTrackDetails fills album, credits and format for a page of tracks with
// three queries, whatever the page size.
func (r *CatalogRepo) attachTrackDetails(ctx context.Context, page *catalog.Page[catalog.Track], ids []int64, albumOf map[int64]int64, libraryID int64) error {
albums := map[int64]catalog.AlbumRef{}
if len(albumOf) > 0 {
var albumIDs []int64
seen := map[int64]bool{}
for _, a := range albumOf {
if !seen[a] {
seen[a] = true
albumIDs = append(albumIDs, a)
}
}
rows, err := r.db.QueryContext(ctx, "SELECT id, title FROM albums WHERE id IN (?)", bun.In(albumIDs))
if err != nil {
return fmt.Errorf("load albums: %w", err)
}
defer rows.Close()
for rows.Next() {
var a catalog.AlbumRef
if err := rows.Scan(&a.ID, &a.Title); err != nil {
return err
}
albums[a.ID] = a
}
if err := rows.Err(); err != nil {
return err
}
}
credits := map[int64][]catalog.ArtistRef{}
crows, err := r.db.QueryContext(ctx,
`SELECT ta.track_id, a.id, a.name FROM track_artists ta JOIN artists a ON a.id = ta.artist_id
WHERE ta.track_id IN (?) ORDER BY ta.track_id, a.id`, bun.In(ids))
if err != nil {
return fmt.Errorf("load credits: %w", err)
}
defer crows.Close()
for crows.Next() {
var trackID int64
var a catalog.ArtistRef
if err := crows.Scan(&trackID, &a.ID, &a.Name); err != nil {
return err
}
credits[trackID] = append(credits[trackID], a)
}
if err := crows.Err(); err != nil {
return err
}
formats := map[int64]string{}
fq := "SELECT f.track_id, f.format FROM audio_files f WHERE f.track_id IN (?) AND f.missing_at IS NULL"
fargs := []any{bun.In(ids)}
if libraryID != 0 {
fq += " AND f.library_id = ?"
fargs = append(fargs, libraryID)
}
frows, err := r.db.QueryContext(ctx, fq+" ORDER BY f.id", fargs...)
if err != nil {
return fmt.Errorf("load formats: %w", err)
}
defer frows.Close()
for frows.Next() {
var trackID int64
var format string
if err := frows.Scan(&trackID, &format); err != nil {
return err
}
if _, ok := formats[trackID]; !ok { // the first playable file wins
formats[trackID] = normalizeFormat(format)
}
}
if err := frows.Err(); err != nil {
return err
}
for i := range page.Items {
t := &page.Items[i]
if albumID, ok := albumOf[t.ID]; ok {
if ref, ok := albums[albumID]; ok {
a := ref
t.Album = &a
}
}
if c := credits[t.ID]; c != nil {
t.Artists = c
}
t.Format = formats[t.ID]
}
return nil
}
// normalizeFormat shows TagLib's "mpeg" as the familiar "mp3".
func normalizeFormat(f string) string {
if f == "mpeg" {
return "mp3"
}
return f
}

View file

@ -0,0 +1,479 @@
package storage
import (
"context"
"errors"
"sync/atomic"
"testing"
"time"
"github.com/uptrace/bun"
"codeberg.org/squidd/sonda/internal/catalog"
"codeberg.org/squidd/sonda/internal/library"
)
// seeded describes the library built by seedCatalog.
type seeded struct {
rock, jazz int64 // library ids
track map[string]int64 // track id by title
repo *CatalogRepo
}
func pf(rel, title, album, albumArtist string, trackNo, discNo int, format string, artists ...string) library.ParsedFile {
return library.ParsedFile{
FileInfo: library.FileInfo{RelPath: rel, Size: 100, MTimeUnix: 1},
Metadata: library.Metadata{
Tags: library.Tags{
Title: title, Album: album, AlbumArtist: albumArtist,
Artists: artists, TrackNo: trackNo, DiscNo: discNo,
},
Properties: library.Properties{Format: format, DurationMs: 60000, SampleRate: 44100},
},
}
}
// seedCatalog builds two libraries:
//
// rock: Radiohead - OK Computer (Airbag, Paranoid Android, Karma Police, and
// Ghost whose only file is missing), Björk - Post and a loose track,
// "100% Pure", "Under_score", "Ünïcode Trck", and an album of only
// missing files.
// jazz: Miles Davis - Kind of Blue (tracks listed out of order), a
// compilation "Various Hits" credited to two artists, and a second
// encoding of Airbag.
func seedCatalog(t *testing.T, db *bun.DB) seeded {
t.Helper()
ctx := context.Background()
lib := NewLibraryRepo(db)
s := seeded{track: map[string]int64{}, repo: NewCatalogRepo(db)}
rock, err := lib.CreateLibrary(ctx, "rock", "/lib/rock")
if err != nil {
t.Fatal(err)
}
jazz, err := lib.CreateLibrary(ctx, "jazz", "/lib/jazz")
if err != nil {
t.Fatal(err)
}
s.rock, s.jazz = rock.ID, jazz.ID
save := func(libID int64, f library.ParsedFile) {
t.Helper()
if _, err := lib.SaveFile(ctx, libID, f, 0); err != nil {
t.Fatal(err)
}
}
save(s.rock, pf("r/1.flac", "Airbag", "OK Computer", "Radiohead", 1, 1, "flac", "Radiohead"))
save(s.rock, pf("r/2.flac", "Paranoid Android", "OK Computer", "Radiohead", 2, 1, "flac", "Radiohead"))
save(s.rock, pf("r/3.flac", "Karma Police", "OK Computer", "Radiohead", 3, 1, "flac", "Radiohead"))
save(s.rock, pf("r/4.flac", "Ghost", "OK Computer", "Radiohead", 4, 1, "flac", "Radiohead"))
save(s.rock, pf("b/1.flac", "Army of Me", "Post", "Björk", 1, 1, "flac", "Björk"))
save(s.rock, pf("b/2.flac", "Loose Song", "", "", 0, 0, "flac", "Björk"))
save(s.rock, pf("c/1.flac", "100% Pure", "", "", 0, 0, "flac", "Wild Cards"))
save(s.rock, pf("c/2.flac", "Under_score", "", "", 0, 0, "flac", "Wild Cards"))
save(s.rock, pf("c/3.flac", "Ünïcode Trck", "", "", 0, 0, "flac", "Ünïcode Band"))
save(s.rock, pf("h/1.flac", "Hidden Song", "Hidden Album", "Only Missing", 1, 1, "flac", "Only Missing"))
save(s.jazz, pf("m/3.mp3", "Blue in Green", "Kind of Blue", "Miles Davis", 3, 1, "mpeg", "Miles Davis"))
save(s.jazz, pf("m/1.mp3", "So What", "Kind of Blue", "Miles Davis", 1, 1, "mpeg", "Miles Davis"))
save(s.jazz, pf("m/2.mp3", "Freddie Freeloader", "Kind of Blue", "Miles Davis", 2, 1, "mpeg", "Miles Davis"))
save(s.jazz, pf("v/1.mp3", "Song A", "Various Hits", "Various Artists", 1, 1, "mpeg", "Artist A"))
save(s.jazz, pf("v/2.mp3", "Song B", "Various Hits", "Various Artists", 2, 1, "mpeg", "Artist B"))
rows, err := db.Query("SELECT id, title FROM tracks")
if err != nil {
t.Fatal(err)
}
for rows.Next() {
var id int64
var title string
if err := rows.Scan(&id, &title); err != nil {
t.Fatal(err)
}
s.track[title] = id
}
_ = rows.Close()
// A second encoding of Airbag in the jazz library.
if _, err := db.Exec(`INSERT INTO audio_files (track_id, library_id, rel_path, format, size_bytes, mtime_unix)
VALUES (?, ?, 'airbag.mp3', 'mpeg', 1, 1)`, s.track["Airbag"], s.jazz); err != nil {
t.Fatal(err)
}
// Files that are gone: Ghost and the whole hidden album.
if _, err := db.Exec(`UPDATE audio_files SET missing_at = ? WHERE track_id IN (?, ?)`,
time.Now().Unix(), s.track["Ghost"], s.track["Hidden Song"]); err != nil {
t.Fatal(err)
}
return s
}
func artistNames(p catalog.Page[catalog.Artist]) []string {
var out []string
for _, a := range p.Items {
out = append(out, a.Name)
}
return out
}
func trackTitles(p catalog.Page[catalog.Track]) []string {
var out []string
for _, tr := range p.Items {
out = append(out, tr.Title)
}
return out
}
func albumTitles(p catalog.Page[catalog.Album]) []string {
var out []string
for _, a := range p.Items {
out = append(out, a.Title)
}
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 page(limit int) catalog.Filter { return catalog.Filter{Limit: limit} }
// ---- artists ----
func TestCatalogArtists(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
s := seedCatalog(t, db)
got, err := s.repo.ListArtists(context.Background(), page(50))
if err != nil {
t.Fatal(err)
}
// Sorted by name in plain byte order, the same on both databases (so
// the accented name comes last). "Only Missing" has nothing playable;
// "Various Artists" is only an album artist but its album is playable.
want := []string{"Artist A", "Artist B", "Björk", "Miles Davis", "Radiohead", "Various Artists", "Wild Cards", "Ünïcode Band"}
if !equal(artistNames(got), want) || got.Total != len(want) {
t.Fatalf("artists = %v (total %d), want %v", artistNames(got), got.Total, want)
}
byName := map[string]catalog.Artist{}
for _, a := range got.Items {
byName[a.Name] = a
}
if r := byName["Radiohead"]; r.TrackCount != 3 || r.AlbumCount != 1 {
t.Errorf("Radiohead = %+v, want 3 tracks (Ghost is missing) and 1 album", r)
}
if v := byName["Various Artists"]; v.TrackCount != 0 || v.AlbumCount != 1 {
t.Errorf("Various Artists = %+v, want 0 credited tracks and 1 album", v)
}
if b := byName["Björk"]; b.TrackCount != 2 || b.AlbumCount != 1 {
t.Errorf("Björk = %+v, want 2 tracks (one without album) and 1 album", b)
}
})
}
func TestCatalogPaging(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
s := seedCatalog(t, db)
ctx := context.Background()
first, _ := s.repo.ListArtists(ctx, catalog.Filter{Limit: 3})
second, _ := s.repo.ListArtists(ctx, catalog.Filter{Limit: 3, Offset: 3})
if len(first.Items) != 3 || len(second.Items) != 3 || first.Total != 8 || second.Total != 8 {
t.Fatalf("pages: %d and %d items, totals %d/%d", len(first.Items), len(second.Items), first.Total, second.Total)
}
if first.Items[2].ID == second.Items[0].ID {
t.Error("pages overlap")
}
if first.Limit != 3 || second.Offset != 3 {
t.Errorf("limit/offset not echoed: %d %d", first.Limit, second.Offset)
}
past, err := s.repo.ListArtists(ctx, catalog.Filter{Limit: 3, Offset: 100})
if err != nil || past.Items == nil || len(past.Items) != 0 || past.Total != 8 {
t.Errorf("past the end: %+v err=%v (want an empty, non-nil slice and the real total)", past, err)
}
})
}
func TestCatalogSearch(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
s := seedCatalog(t, db)
ctx := context.Background()
search := func(q string) catalog.Filter { return catalog.Filter{Query: q, Limit: 50} }
// Case and accents behave the same on both databases.
for _, q := range []string{"björk", "BJÖRK", "jö"} {
if got, _ := s.repo.ListArtists(ctx, search(q)); !equal(artistNames(got), []string{"Björk"}) {
t.Errorf("artists %q = %v", q, artistNames(got))
}
}
if got, _ := s.repo.ListArtists(ctx, search("ÜNÏ")); !equal(artistNames(got), []string{"Ünïcode Band"}) {
t.Errorf("artists ÜNÏ = %v", artistNames(got))
}
if got, _ := s.repo.ListTracks(ctx, search("ÜNÏCODE")); !equal(trackTitles(got), []string{"Ünïcode Trck"}) {
t.Errorf("tracks ÜNÏCODE = %v", trackTitles(got))
}
if got, _ := s.repo.ListAlbums(ctx, search("kind OF")); !equal(albumTitles(got), []string{"Kind of Blue"}) {
t.Errorf("albums kind OF = %v", albumTitles(got))
}
if got, _ := s.repo.ListArtists(ctx, search("zzz")); got.Total != 0 || len(got.Items) != 0 {
t.Errorf("no match: %+v", got)
}
// LIKE wildcards in the query are plain characters.
if got, _ := s.repo.ListTracks(ctx, search("100%")); !equal(trackTitles(got), []string{"100% Pure"}) {
t.Errorf(`"100%%" = %v, want only the title containing a percent sign`, trackTitles(got))
}
if got, _ := s.repo.ListTracks(ctx, search("%")); !equal(trackTitles(got), []string{"100% Pure"}) {
t.Errorf(`"%%" = %v, must not match everything`, trackTitles(got))
}
if got, _ := s.repo.ListTracks(ctx, search("_")); !equal(trackTitles(got), []string{"Under_score"}) {
t.Errorf(`"_" = %v, must not match every one-character position`, trackTitles(got))
}
if got, _ := s.repo.ListTracks(ctx, search("!")); got.Total != 0 {
t.Errorf(`"!" (the escape character) = %v`, trackTitles(got))
}
})
}
func TestCatalogLibraryFilter(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
s := seedCatalog(t, db)
ctx := context.Background()
jazz, _ := s.repo.ListArtists(ctx, catalog.Filter{LibraryID: s.jazz, Limit: 50})
// Radiohead is here only because Airbag also has a file in this library.
want := []string{"Artist A", "Artist B", "Miles Davis", "Radiohead", "Various Artists"}
if !equal(artistNames(jazz), want) {
t.Fatalf("jazz artists = %v, want %v", artistNames(jazz), want)
}
for _, a := range jazz.Items {
if a.Name == "Radiohead" && (a.TrackCount != 1 || a.AlbumCount != 1) {
t.Errorf("counts must follow the filter: Radiohead = %+v, want 1 track", a)
}
}
tracks, _ := s.repo.ListTracks(ctx, catalog.Filter{LibraryID: s.jazz, Limit: 50})
if tracks.Total != 6 { // 3 + 2 + Airbag
t.Errorf("jazz tracks = %d, want 6: %v", tracks.Total, trackTitles(tracks))
}
albums, _ := s.repo.ListAlbums(ctx, catalog.Filter{LibraryID: s.jazz, Limit: 50})
if !equal(albumTitles(albums), []string{"Kind of Blue", "OK Computer", "Various Hits"}) {
t.Errorf("jazz albums = %v", albumTitles(albums))
}
for _, a := range albums.Items {
if a.Title == "OK Computer" && a.TrackCount != 1 {
t.Errorf("OK Computer in jazz = %+v, want only Airbag", a)
}
}
if none, _ := s.repo.ListTracks(ctx, catalog.Filter{LibraryID: 999, Limit: 50}); none.Total != 0 {
t.Errorf("unknown library: %d tracks", none.Total)
}
})
}
// ---- albums ----
func TestCatalogAlbums(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
s := seedCatalog(t, db)
ctx := context.Background()
got, err := s.repo.ListAlbums(ctx, page(50))
if err != nil {
t.Fatal(err)
}
// "Hidden Album" has only missing files.
if want := []string{"Kind of Blue", "OK Computer", "Post", "Various Hits"}; !equal(albumTitles(got), want) || got.Total != 4 {
t.Fatalf("albums = %v (total %d), want %v", albumTitles(got), got.Total, want)
}
by := map[string]catalog.Album{}
for _, a := range got.Items {
by[a.Title] = a
}
ok := by["OK Computer"]
if ok.TrackCount != 3 || ok.DurationMs != 180000 || ok.Artist == nil || ok.Artist.Name != "Radiohead" {
t.Errorf("OK Computer = %+v, want 3 tracks, 3 minutes, Radiohead", ok)
}
if v := by["Various Hits"]; v.Artist == nil || v.Artist.Name != "Various Artists" || v.TrackCount != 2 {
t.Errorf("Various Hits = %+v", v)
}
byArtist, _ := s.repo.ListAlbums(ctx, catalog.Filter{ArtistID: ok.Artist.ID, Limit: 50})
if !equal(albumTitles(byArtist), []string{"OK Computer"}) {
t.Errorf("albums of Radiohead = %v", albumTitles(byArtist))
}
})
}
// ---- tracks ----
func TestCatalogTracks(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
s := seedCatalog(t, db)
ctx := context.Background()
all, err := s.repo.ListTracks(ctx, page(50))
if err != nil {
t.Fatal(err)
}
titles := trackTitles(all)
count := map[string]int{}
for _, ti := range titles {
count[ti]++
}
if count["Airbag"] != 1 {
t.Errorf("Airbag appears %d times: a track with two files must be listed once", count["Airbag"])
}
if count["Ghost"] != 0 || count["Hidden Song"] != 0 {
t.Errorf("tracks without a playable file were listed: %v", titles)
}
if all.Total != 13 || len(all.Items) != 13 {
t.Errorf("total = %d, items = %d, want 13: %v", all.Total, len(all.Items), titles)
}
// Albums in title order; inside an album by disc, track number, title.
var kind []string
for _, tr := range all.Items {
if tr.Album != nil && tr.Album.Title == "Kind of Blue" {
kind = append(kind, tr.Title)
}
}
if !equal(kind, []string{"So What", "Freddie Freeloader", "Blue in Green"}) {
t.Errorf("Kind of Blue order = %v", kind)
}
by := map[string]catalog.Track{}
for _, tr := range all.Items {
by[tr.Title] = tr
}
airbag := by["Airbag"]
if airbag.Album == nil || airbag.Album.Title != "OK Computer" || len(airbag.Artists) != 1 ||
airbag.Artists[0].Name != "Radiohead" || airbag.TrackNo != 1 || airbag.DiscNo != 1 || airbag.DurationMs != 60000 {
t.Errorf("Airbag = %+v", airbag)
}
if airbag.Format != "flac" {
t.Errorf("Airbag format = %q, want the first playable file's (flac)", airbag.Format)
}
if f := by["So What"].Format; f != "mp3" {
t.Errorf("format = %q, want mpeg shown as mp3", f)
}
if loose := by["Loose Song"]; loose.Album != nil || loose.TrackNo != 0 || len(loose.Artists) != 1 {
t.Errorf("a track without album: %+v", loose)
}
if a := by["Song A"].Artists; len(a) != 1 || a[0].Name != "Artist A" {
t.Errorf("compilation track credits = %+v", a)
}
})
}
func TestCatalogTrackFilters(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
s := seedCatalog(t, db)
ctx := context.Background()
albums, _ := s.repo.ListAlbums(ctx, catalog.Filter{Query: "ok comp", Limit: 5})
ok := albums.Items[0]
byAlbum, _ := s.repo.ListTracks(ctx, catalog.Filter{AlbumID: ok.ID, Limit: 50})
if !equal(trackTitles(byAlbum), []string{"Airbag", "Paranoid Android", "Karma Police"}) {
t.Errorf("tracks of OK Computer = %v", trackTitles(byAlbum))
}
artists, _ := s.repo.ListArtists(ctx, catalog.Filter{Query: "artist a", Limit: 5})
byArtist, _ := s.repo.ListTracks(ctx, catalog.Filter{ArtistID: artists.Items[0].ID, Limit: 50})
if !equal(trackTitles(byArtist), []string{"Song A"}) {
t.Errorf("tracks of Artist A = %v", trackTitles(byArtist))
}
both, _ := s.repo.ListTracks(ctx, catalog.Filter{AlbumID: ok.ID, Query: "karma", Limit: 50})
if !equal(trackTitles(both), []string{"Karma Police"}) {
t.Errorf("filters combine with AND: %v", trackTitles(both))
}
if none, _ := s.repo.ListTracks(ctx, catalog.Filter{AlbumID: 999, Limit: 50}); none.Total != 0 {
t.Errorf("unknown album: %d tracks", none.Total)
}
})
}
// ---- details ----
func TestCatalogDetails(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
s := seedCatalog(t, db)
ctx := context.Background()
tr, err := s.repo.GetTrack(ctx, s.track["Airbag"])
if err != nil || tr.Title != "Airbag" || tr.Album == nil || len(tr.Artists) != 1 || tr.Format != "flac" {
t.Fatalf("GetTrack = %+v err=%v", tr, err)
}
if _, err := s.repo.GetTrack(ctx, s.track["Ghost"]); !errors.Is(err, catalog.ErrNotFound) {
t.Errorf("a track with only missing files: err = %v, want ErrNotFound", err)
}
if _, err := s.repo.GetTrack(ctx, 99999); !errors.Is(err, catalog.ErrNotFound) {
t.Errorf("unknown track: err = %v", err)
}
artists, _ := s.repo.ListArtists(ctx, catalog.Filter{Query: "various", Limit: 5})
va, err := s.repo.GetArtist(ctx, artists.Items[0].ID)
if err != nil || va.Name != "Various Artists" || va.AlbumCount != 1 {
t.Errorf("an album artist without credits: %+v err=%v", va, err)
}
var hiddenArtist int64
if err := db.QueryRow("SELECT id FROM artists WHERE name = 'Only Missing'").Scan(&hiddenArtist); err != nil {
t.Fatal(err)
}
if _, err := s.repo.GetArtist(ctx, hiddenArtist); !errors.Is(err, catalog.ErrNotFound) {
t.Errorf("an artist with nothing playable: err = %v, want ErrNotFound", err)
}
var hiddenAlbum, okAlbum int64
_ = db.QueryRow("SELECT id FROM albums WHERE title = 'Hidden Album'").Scan(&hiddenAlbum)
_ = db.QueryRow("SELECT id FROM albums WHERE title = 'OK Computer'").Scan(&okAlbum)
if _, err := s.repo.GetAlbum(ctx, hiddenAlbum); !errors.Is(err, catalog.ErrNotFound) {
t.Errorf("an album with nothing playable: err = %v", err)
}
al, err := s.repo.GetAlbum(ctx, okAlbum)
if err != nil || al.TrackCount != 3 || al.Artist == nil {
t.Errorf("GetAlbum = %+v err=%v", al, err)
}
})
}
// A page must cost a fixed number of queries, never one per row.
func TestCatalogListsDoNotQueryPerRow(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
s := seedCatalog(t, db)
var queries atomic.Int64
db.AddQueryHook(countingHook{&queries})
for name, run := range map[string]func() error{
"artists": func() error { _, err := s.repo.ListArtists(context.Background(), page(50)); return err },
"albums": func() error { _, err := s.repo.ListAlbums(context.Background(), page(50)); return err },
"tracks": func() error { _, err := s.repo.ListTracks(context.Background(), page(50)); return err },
} {
queries.Store(0)
if err := run(); err != nil {
t.Fatal(err)
}
if n := queries.Load(); n > 6 {
t.Errorf("%s: %d queries for one page, want a small constant", name, n)
}
}
})
}
type countingHook struct{ n *atomic.Int64 }
func (countingHook) BeforeQuery(ctx context.Context, _ *bun.QueryEvent) context.Context { return ctx }
func (h countingHook) AfterQuery(context.Context, *bun.QueryEvent) { h.n.Add(1) }

View file

@ -36,6 +36,7 @@ type trackModel struct {
ID int64 `bun:"id,pk,autoincrement"`
Title string `bun:"title,notnull"`
TitleKey string `bun:"title_key,notnull"`
DurationMs int64 `bun:"duration_ms,nullzero"`
TrackNo int `bun:"track_no,nullzero"`
DiscNo int `bun:"disc_no,nullzero"`
@ -132,7 +133,7 @@ func (r *LibraryRepo) SaveFile(ctx context.Context, libraryID int64, f library.P
}
track := &trackModel{
Title: f.Title, DurationMs: f.DurationMs, TrackNo: f.TrackNo, DiscNo: f.DiscNo, AlbumID: albumID,
Title: f.Title, TitleKey: matchKey(f.Title), DurationMs: f.DurationMs, TrackNo: f.TrackNo, DiscNo: f.DiscNo, AlbumID: albumID,
}
if existingID == 0 {
@ -153,7 +154,7 @@ func (r *LibraryRepo) SaveFile(ctx context.Context, libraryID int64, f library.P
}
track.ID = file.TrackID
if _, err := tx.NewUpdate().Model(track).
Column("title", "duration_ms", "track_no", "disc_no", "album_id").
Column("title", "title_key", "duration_ms", "track_no", "disc_no", "album_id").
WherePK().Exec(ctx); err != nil {
return fmt.Errorf("update track: %w", err)
}
@ -316,7 +317,7 @@ func (r *LibraryRepo) DeleteLibrary(ctx context.Context, id int64) error {
return nil
}
// RefreshMatchKeys recomputes name_key and title_key for every artist and album
// RefreshMatchKeys recomputes the match keys of every artist, album and track
// whose stored key differs from the current rule. It is idempotent and cheap
// when nothing changed, so it can run at every start: rows created before
// migration 0003 only have an approximate key from SQL lower().
@ -337,6 +338,21 @@ func (r *LibraryRepo) RefreshMatchKeys(ctx context.Context) error {
}
}
var tracks []trackModel
if err := tx.NewSelect().Model(&tracks).Column("id", "title", "title_key").Scan(ctx); err != nil {
return fmt.Errorf("list tracks: %w", err)
}
for _, t := range tracks {
key := matchKey(t.Title)
if key == t.TitleKey {
continue
}
if _, err := tx.NewUpdate().Model((*trackModel)(nil)).
Set("title_key = ?", key).Where("id = ?", t.ID).Exec(ctx); err != nil {
return fmt.Errorf("update track key: %w", err)
}
}
var albums []albumModel
if err := tx.NewSelect().Model(&albums).Column("id", "title", "title_key").Scan(ctx); err != nil {
return fmt.Errorf("list albums: %w", err)

View file

@ -156,8 +156,8 @@ func TestMigrateCreatesSchemaAndIsIdempotent(t *testing.T) {
if err := db.QueryRow("SELECT COUNT(*) FROM schema_migrations").Scan(&applied); err != nil {
t.Fatal(err)
}
if applied != 5 {
t.Errorf("applied migrations = %d, want 5", applied)
if applied != 6 {
t.Errorf("applied migrations = %d, want 6", applied)
}
})
}

View file

@ -0,0 +1,7 @@
-- Case-insensitive search key for track titles, computed in Go like the artist
-- and album keys (SQL lower() folds different characters on SQLite and PostgreSQL).
ALTER TABLE tracks ADD COLUMN title_key TEXT NOT NULL DEFAULT '';
-- Best effort for existing rows; RefreshMatchKeys corrects them at startup.
UPDATE tracks SET title_key = lower(title);

View file

@ -0,0 +1,7 @@
-- Case-insensitive search key for track titles, computed in Go like the artist
-- and album keys (SQL lower() folds different characters on SQLite and PostgreSQL).
ALTER TABLE tracks ADD COLUMN title_key TEXT NOT NULL DEFAULT '';
-- Best effort for existing rows; RefreshMatchKeys corrects them at startup.
UPDATE tracks SET title_key = lower(title);