sonda/internal/storage/subsonic_test.go
tgiachi 1ae8e77d81 feat: OpenSubsonic core, login with a device password
/rest speaks the OpenSubsonic API (Subsonic 1.16.1) so that other apps can browse
the music of a server. A call logs in with the password of a device: an apiKey,
Subsonic's token (u, t, s) or the password itself (u, p). The account password is
never accepted there.

- devices: ByKey, ByToken, ByPassword and the note of last use
- opensubsonic package: JSON and XML answers, ping, getLicense,
  getOpenSubsonicExtensions, tokenInfo, getMusicFolders, getArtists, getArtist,
  getAlbum, getSong, getGenres
- storage.SubsonicRepo: year, genres, size, bitrate, suffix and added date
2026-11-16 09:32:11 +01:00

83 lines
2.7 KiB
Go

package storage
import (
"context"
"testing"
"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)
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)
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)
}
})
}