sonda/internal/storage/subsonic_test.go
tgiachi ccee5e8719 feat: OpenSubsonic playback, covers, lists and search
Apps can play and browse: stream and download send the original file (Range,
HEAD), getCoverArt serves the cover of an album, an artist or the album of a
song (300 pixels or less is the small one), getAlbumList2 knows every kind of
list, getRandomSongs and getSongsByGenre list songs, search3 finds artists,
albums and songs, and the starred calls answer with nothing so apps do not break.

- catalog.Filter has IDs to read albums and songs by id
- storage.SubsonicRepo answers the album and song lists (random, newest,
  frequent, recent, by name, by artist, by year, by genre)
2026-11-16 09:45:07 +01:00

84 lines
2.7 KiB
Go

package storage
import (
"context"
"testing"
"time"
"github.com/uptrace/bun"
)
func TestTheExtrasOfSongsAndAlbumsComeFromTheirTagsAndFiles(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
s := seedCatalog(t, db)
repo := NewSubsonicRepo(db, 20*time.Second)
exec := func(q string, args ...any) {
t.Helper()
if _, err := db.ExecContext(ctx, q, args...); err != nil {
t.Fatal(err)
}
}
airbag, paranoid, kind := s.track["Airbag"], s.track["Paranoid Android"], s.track["So What"]
exec("UPDATE tracks SET year = 1997 WHERE id = ?", airbag)
exec("UPDATE tracks SET year = 1998 WHERE id = ?", paranoid)
exec("INSERT INTO track_genres (track_id, genre) VALUES (?, 'rock'), (?, 'art rock'), (?, 'rock')", airbag, airbag, paranoid)
exec("UPDATE audio_files SET size_bytes = 4242, bitrate = 320, mtime_unix = 1000 WHERE track_id = ? AND missing_at IS NULL AND rel_path LIKE 'r/%'", airbag)
exec("UPDATE audio_files SET mtime_unix = 500 WHERE track_id = ?", paranoid)
tracks, err := repo.Tracks(ctx, []int64{airbag, kind, s.track["Ghost"]})
if err != nil {
t.Fatal(err)
}
a := tracks[airbag]
if a.Year != 1997 || a.Size != 4242 || a.BitrateKbps != 320 || a.Suffix != "flac" || a.AddedAt.Unix() != 1000 {
t.Errorf("Airbag: %+v", a)
}
if len(a.Genres) != 2 || a.Genres[0] != "art rock" || a.Genres[1] != "rock" {
t.Errorf("genres of Airbag: %v", a.Genres)
}
if k := tracks[kind]; k.Year != 0 || k.Suffix != "mp3" || len(k.Genres) != 0 {
t.Errorf("So What: %+v", k)
}
if _, ok := tracks[s.track["Ghost"]]; ok {
t.Error("a song with no file to play has extras")
}
var ok int64
if err := db.QueryRowContext(ctx, "SELECT id FROM albums WHERE title = 'OK Computer'").Scan(&ok); err != nil {
t.Fatal(err)
}
albums, err := repo.Albums(ctx, []int64{ok})
if err != nil {
t.Fatal(err)
}
al := albums[ok]
if al.Year != 1997 || al.AddedAt.Unix() != 1 && al.AddedAt.Unix() != 500 {
t.Errorf("OK Computer: %+v", al)
}
if len(al.Genres) != 2 || al.Genres[0] != "art rock" || al.Genres[1] != "rock" {
t.Errorf("genres of the album: %v", al.Genres)
}
genres, err := repo.Genres(ctx)
if err != nil || len(genres) != 2 {
t.Fatalf("Genres = %+v, %v", genres, err)
}
if g := genres[1]; g.Name != "rock" || g.Songs != 2 || g.Albums != 1 {
t.Errorf("rock = %+v", g)
}
})
}
func TestNoIdsNoExtras(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
repo := NewSubsonicRepo(db, 20*time.Second)
if m, err := repo.Tracks(context.Background(), nil); err != nil || len(m) != 0 {
t.Fatalf("%v, %v", m, err)
}
if m, err := repo.Albums(context.Background(), nil); err != nil || len(m) != 0 {
t.Fatalf("%v, %v", m, err)
}
})
}