release / go (push) Successful in 1m46s
release / web (push) Successful in 1m15s
release / govulncheck (push) Successful in 20s
release / integration (push) Successful in 4m13s
release / android (push) Successful in 4m45s
release / Build signed APK (push) Successful in 5m16s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 16s
release / Verify release artifacts (tag releases only) (push) Skipped
An app that asks for lyrics by title without an artist got the first matching song with lyrics, which could be another artist's song of the same name. It now answers empty, so the app can look elsewhere. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
326 lines
13 KiB
Go
326 lines
13 KiB
Go
package subsonic
|
|
|
|
import (
|
|
"context"
|
|
"encoding/xml"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
|
)
|
|
|
|
func starts(l songLyrics) []int64 {
|
|
out := make([]int64, len(l.lines))
|
|
for i, line := range l.lines {
|
|
if line.Start == nil {
|
|
out[i] = -1
|
|
continue
|
|
}
|
|
out[i] = *line.Start
|
|
}
|
|
return out
|
|
}
|
|
|
|
func values(l songLyrics) []string {
|
|
out := make([]string, len(l.lines))
|
|
for i, line := range l.lines {
|
|
out[i] = line.Value
|
|
}
|
|
return out
|
|
}
|
|
|
|
// LRC text is timed, in time order, a line sung twice once per time; its
|
|
// header tags are dropped and its offset kept. Anything else is a line per
|
|
// line, without the blank lines around it.
|
|
func TestParseLyrics_TimedFromLRCAndPlainOtherwise(t *testing.T) {
|
|
lrc := "\ufeff[ar:Tin Orchard]\r\n[ti:Dusk]\r\n[offset:+250]\r\n" +
|
|
"[00:12.50]First light\r\n[00:20.00][01:05.25]Over the hill\r\n[00:30.1]Down\r\n[1:02:5]Late\r\n"
|
|
l, ok := parseLyrics(lrc)
|
|
if !ok || !l.synced {
|
|
t.Fatalf("LRC: ok=%v synced=%v, want timed lyrics", ok, l.synced)
|
|
}
|
|
if got, want := starts(l), []int64{12500, 20000, 30100, 62500, 65250}; !slices.Equal(got, want) {
|
|
t.Errorf("starts = %v, want %v", got, want)
|
|
}
|
|
if got, want := values(l), []string{"First light", "Over the hill", "Down", "Late", "Over the hill"}; !slices.Equal(got, want) {
|
|
t.Errorf("lines = %q, want %q", got, want)
|
|
}
|
|
if l.offset != 250 {
|
|
t.Errorf("offset = %d, want 250", l.offset)
|
|
}
|
|
|
|
l, ok = parseLyrics("\n\nFirst light\n\n[Chorus]\nOver the hill\n\n")
|
|
if !ok || l.synced {
|
|
t.Fatalf("plain: ok=%v synced=%v, want untimed lyrics", ok, l.synced)
|
|
}
|
|
if got, want := values(l), []string{"First light", "", "[Chorus]", "Over the hill"}; !slices.Equal(got, want) {
|
|
t.Errorf("plain lines = %q, want %q", got, want)
|
|
}
|
|
if got := starts(l); !slices.Equal(got, []int64{-1, -1, -1, -1}) {
|
|
t.Errorf("plain starts = %v, want none", got)
|
|
}
|
|
|
|
if _, ok := parseLyrics(" \n\t\n"); ok {
|
|
t.Error("blank text: found lyrics, want none")
|
|
}
|
|
}
|
|
|
|
// The .lrc file beside a song is its lyrics; a song with neither that nor a
|
|
// readable lyrics tag has none.
|
|
func TestReadLyrics_FromTheLrcBesideTheSong(t *testing.T) {
|
|
dir := t.TempDir()
|
|
song := filepath.Join(dir, "Dusk.flac")
|
|
if err := os.WriteFile(song, []byte("not really audio"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, ok := readLyrics(song); ok {
|
|
t.Error("no .lrc and no readable tags: found lyrics, want none")
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "Dusk.lrc"), []byte("[00:01.00]First light\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
l, ok := readLyrics(song)
|
|
if !ok || !l.synced || !slices.Equal(values(l), []string{"First light"}) {
|
|
t.Errorf("with Dusk.lrc: %+v ok=%v, want its one timed line", l, ok)
|
|
}
|
|
if _, ok := readLyrics(filepath.Join(dir, "Missing.flac")); ok {
|
|
t.Error("a file that is not there: found lyrics, want none")
|
|
}
|
|
}
|
|
|
|
func TestGetLyrics_ReadsTheSongsLrcByIdOrByName(t *testing.T) {
|
|
f := seedPicks(t)
|
|
dir := t.TempDir()
|
|
dusk := filepath.Join(dir, "Dusk.flac")
|
|
f.exec(t, `UPDATE tracks SET file_path = $2 WHERE id = $1`, f.tracks["Dusk"].ID, dusk)
|
|
if err := os.WriteFile(filepath.Join(dir, "Dusk.lrc"), []byte("[00:01.00]First light\n[00:04.00]Gone by noon\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
var byID struct {
|
|
LyricsList LyricsList `json:"lyricsList"`
|
|
}
|
|
if code := call(t, f.browse().getLyricsBySongID, f.user, "id="+uuidToID(f.tracks["Dusk"].ID), &byID); code != 0 {
|
|
t.Fatalf("getLyricsBySongId: error %d", code)
|
|
}
|
|
if n := len(byID.LyricsList.StructuredLyrics); n != 1 {
|
|
t.Fatalf("Dusk has %d sets of lyrics, want 1", n)
|
|
}
|
|
got := byID.LyricsList.StructuredLyrics[0]
|
|
if !got.Synced || got.DisplayTitle != "Dusk" || got.DisplayArtist != "Tin Orchard" || got.Lang != "und" || len(got.Lines) != 2 ||
|
|
got.Lines[1].Value != "Gone by noon" || got.Lines[1].Start == nil || *got.Lines[1].Start != 4000 {
|
|
t.Errorf("Dusk's lyrics = %+v, want its two timed lines", got)
|
|
}
|
|
|
|
if code := call(t, f.browse().getLyricsBySongID, f.user, "id="+uuidToID(f.tracks["Harbour"].ID), &byID); code != 0 {
|
|
t.Fatalf("getLyricsBySongId: error %d", code)
|
|
}
|
|
if byID.LyricsList.StructuredLyrics == nil || len(byID.LyricsList.StructuredLyrics) != 0 {
|
|
t.Errorf("Harbour's lyrics = %+v, want an empty list", byID.LyricsList.StructuredLyrics)
|
|
}
|
|
|
|
var byName struct {
|
|
Lyrics Lyrics `json:"lyrics"`
|
|
}
|
|
if code := call(t, f.browse().getLyrics, f.user, "artist=tin+orchard&title=DUSK", &byName); code != 0 {
|
|
t.Fatalf("getLyrics: error %d", code)
|
|
}
|
|
if byName.Lyrics != (Lyrics{Artist: "Tin Orchard", Title: "Dusk", Value: "First light\nGone by noon"}) {
|
|
t.Errorf("getLyrics = %+v, want Dusk's text", byName.Lyrics)
|
|
}
|
|
byName.Lyrics = Lyrics{}
|
|
if code := call(t, f.browse().getLyrics, f.user, "artist=Someone+Else&title=Dusk", &byName); code != 0 {
|
|
t.Fatalf("getLyrics: error %d", code)
|
|
}
|
|
if byName.Lyrics != (Lyrics{}) {
|
|
t.Errorf("another artist's Dusk = %+v, want no lyrics", byName.Lyrics)
|
|
}
|
|
|
|
// Without an artist, a title only one artist has is that artist's song;
|
|
// a title two artists have is neither's, even when only one has lyrics.
|
|
if code := call(t, f.browse().getLyrics, f.user, "title=Dusk", &byName); code != 0 {
|
|
t.Fatalf("getLyrics: error %d", code)
|
|
}
|
|
if byName.Lyrics.Value != "First light\nGone by noon" {
|
|
t.Errorf("Dusk by title alone = %+v, want Tin Orchard's text", byName.Lyrics)
|
|
}
|
|
ctx := context.Background()
|
|
q := dbq.New(f.pool)
|
|
other, err := q.UpsertArtist(ctx, dbq.UpsertArtistParams{Name: "Quiet Harbour", SortName: "Quiet Harbour"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := q.UpsertTrack(ctx, dbq.UpsertTrackParams{
|
|
Title: "Dusk", AlbumID: f.albums["Paper Boats"].ID, ArtistID: other.ID,
|
|
FilePath: filepath.Join(dir, "Quiet Harbour - Dusk.flac"), DurationMs: 200_000,
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
byName.Lyrics = Lyrics{}
|
|
if code := call(t, f.browse().getLyrics, f.user, "title=Dusk", &byName); code != 0 {
|
|
t.Fatalf("getLyrics: error %d", code)
|
|
}
|
|
if byName.Lyrics != (Lyrics{}) {
|
|
t.Errorf("Dusk by title alone, two artists' = %+v, want no lyrics", byName.Lyrics)
|
|
}
|
|
}
|
|
|
|
// Artist info is the MusicBrainz id and the similar artists in the library,
|
|
// reached from the artist or from one of their albums or songs.
|
|
func TestGetArtistInfo_IsTheMBIDAndSimilarArtistsInTheLibrary(t *testing.T) {
|
|
f := seedPicks(t)
|
|
ctx := context.Background()
|
|
q := dbq.New(f.pool)
|
|
other, err := q.UpsertArtist(ctx, dbq.UpsertArtistParams{Name: "Quiet Harbour", SortName: "Quiet Harbour"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := q.UpsertAlbum(ctx, dbq.UpsertAlbumParams{Title: "Low Water", SortTitle: "Low Water", ArtistID: other.ID}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
const mbid = "0f3e6c2a-1b4d-4e8f-9a7c-5d2b8e1f4a6c"
|
|
f.exec(t, `UPDATE artists SET mbid = $2 WHERE id = $1`, f.artist.ID, mbid)
|
|
f.exec(t, `INSERT INTO artist_similarity (artist_a_id, artist_b_id, score, source) VALUES ($1, $2, 0.8, 'listenbrainz')`,
|
|
f.artist.ID, other.ID)
|
|
|
|
for _, id := range []string{uuidToID(f.artist.ID), uuidToID(f.albums["Paper Boats"].ID), uuidToID(f.tracks["Gale"].ID)} {
|
|
var got struct {
|
|
ArtistInfo2 ArtistInfo `json:"artistInfo2"`
|
|
}
|
|
if code := call(t, f.browse().getArtistInfo2, f.user, "id="+id, &got); code != 0 {
|
|
t.Fatalf("getArtistInfo2 %s: error %d", id, code)
|
|
}
|
|
want := ArtistInfo{MusicBrainzID: mbid, SimilarArtists: []ArtistRef{{ID: uuidToID(other.ID), Name: "Quiet Harbour", CoverArt: uuidToID(other.ID), AlbumCount: 1}}}
|
|
if got.ArtistInfo2.MusicBrainzID != want.MusicBrainzID || !slices.Equal(got.ArtistInfo2.SimilarArtists, want.SimilarArtists) {
|
|
t.Errorf("getArtistInfo2 %s = %+v, want %+v", id, got.ArtistInfo2, want)
|
|
}
|
|
}
|
|
|
|
var folder struct {
|
|
ArtistInfo ArtistInfo `json:"artistInfo"`
|
|
}
|
|
if code := call(t, f.browse().getArtistInfo, f.user, "id="+uuidToID(f.artist.ID)+"&count=0", &folder); code != 0 {
|
|
t.Fatalf("getArtistInfo: error %d", code)
|
|
}
|
|
if folder.ArtistInfo.SimilarArtists == nil || len(folder.ArtistInfo.SimilarArtists) != 0 {
|
|
t.Errorf("count=0: similar artists %v, want an empty list", folder.ArtistInfo.SimilarArtists)
|
|
}
|
|
if code := call(t, f.browse().getArtistInfo2, f.user, "id=00000000-0000-4000-8000-000000000001", nil); code != ErrDataNotFound {
|
|
t.Errorf("an unknown id: code %d, want %d", code, ErrDataNotFound)
|
|
}
|
|
|
|
const albumMBID = "7a1c3e5b-2d4f-4a6b-8c9d-0e1f2a3b4c5d"
|
|
f.exec(t, `UPDATE albums SET mbid = $2 WHERE id = $1`, f.albums["Paper Boats"].ID, albumMBID)
|
|
var album struct {
|
|
AlbumInfo AlbumInfo `json:"albumInfo"`
|
|
}
|
|
if code := call(t, f.browse().getAlbumInfo, f.user, "id="+uuidToID(f.albums["Paper Boats"].ID), &album); code != 0 {
|
|
t.Fatalf("getAlbumInfo: error %d", code)
|
|
}
|
|
if album.AlbumInfo.MusicBrainzID != albumMBID {
|
|
t.Errorf("album info = %+v, want MusicBrainz id %s", album.AlbumInfo, albumMBID)
|
|
}
|
|
}
|
|
|
|
// In XML the similar artists are similarArtist elements and the id is an
|
|
// element of its own.
|
|
func TestArtistInfoXML(t *testing.T) {
|
|
out, err := xml.Marshal(ArtistInfo2Response{
|
|
Envelope: NewEnvelope("ok"),
|
|
ArtistInfo2: ArtistInfo{MusicBrainzID: "m", SimilarArtists: []ArtistRef{{ID: "a", Name: "Quiet Harbour"}}},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("marshal: %v", err)
|
|
}
|
|
if want := `<artistInfo2><musicBrainzId>m</musicBrainzId><similarArtist id="a" name="Quiet Harbour"></similarArtist></artistInfo2>`; !strings.Contains(string(out), want) {
|
|
t.Errorf("XML = %s, want it to hold %s", out, want)
|
|
}
|
|
}
|
|
|
|
// The scan status counts the songs whose files are present and says whether
|
|
// the latest scan is still running; only an admin starts one, and the answer
|
|
// says scanning at once.
|
|
func TestScanStatus_AndOnlyAnAdminStartsAScan(t *testing.T) {
|
|
f := seedPicks(t)
|
|
// Scan runs are not cleared between tests.
|
|
f.exec(t, `DELETE FROM scan_runs`)
|
|
started := 0
|
|
sc := &scanHandlers{pool: f.pool, start: func(context.Context) (bool, error) { started++; return true, nil }}
|
|
|
|
var got struct {
|
|
ScanStatus ScanStatus `json:"scanStatus"`
|
|
}
|
|
if code := call(t, sc.getScanStatus, f.user, "", &got); code != 0 {
|
|
t.Fatalf("getScanStatus: error %d", code)
|
|
}
|
|
// Seven songs, one of them missing.
|
|
if got.ScanStatus != (ScanStatus{Count: 6}) {
|
|
t.Errorf("before any scan: %+v, want 6 songs and not scanning", got.ScanStatus)
|
|
}
|
|
f.exec(t, `INSERT INTO scan_runs (started_at, finished_at) VALUES (now() - interval '1 hour', now() - interval '50 minutes')`)
|
|
f.exec(t, `INSERT INTO scan_runs DEFAULT VALUES`)
|
|
if code := call(t, sc.getScanStatus, f.user, "", &got); code != 0 {
|
|
t.Fatalf("getScanStatus: error %d", code)
|
|
}
|
|
if !got.ScanStatus.Scanning {
|
|
t.Errorf("with a scan running: %+v, want scanning", got.ScanStatus)
|
|
}
|
|
|
|
if code := call(t, sc.startScan, f.user, "", nil); code != ErrNotAuthorized || started != 0 {
|
|
t.Errorf("a listener's startScan: code %d, %d scans started; want %d and none", code, started, ErrNotAuthorized)
|
|
}
|
|
f.exec(t, `DELETE FROM scan_runs`)
|
|
admin := f.user
|
|
admin.IsAdmin = true
|
|
got.ScanStatus = ScanStatus{}
|
|
if code := call(t, sc.startScan, admin, "", &got); code != 0 {
|
|
t.Fatalf("an admin's startScan: error %d", code)
|
|
}
|
|
if started != 1 || !got.ScanStatus.Scanning || got.ScanStatus.Count != 6 {
|
|
t.Errorf("an admin's startScan: %d started, status %+v; want one, scanning, 6 songs", started, got.ScanStatus)
|
|
}
|
|
sc.start = func(context.Context) (bool, error) { return false, errors.New("scanner down") }
|
|
if code := call(t, sc.startScan, admin, "", nil); code != ErrGeneric {
|
|
t.Errorf("a scan that fails to start: code %d, want %d", code, ErrGeneric)
|
|
}
|
|
}
|
|
|
|
func TestGetUsers_ListsEveryoneForAnAdminOnly(t *testing.T) {
|
|
h := &userHandlers{list: func(context.Context) ([]dbq.ListUsersRow, error) {
|
|
return []dbq.ListUsersRow{{Username: "ada", IsAdmin: true}, {Username: "ben"}}, nil
|
|
}}
|
|
var got struct {
|
|
Users UsersContainer `json:"users"`
|
|
}
|
|
if code := call(t, h.getUsers, dbq.User{Username: "ada", IsAdmin: true}, "", &got); code != 0 {
|
|
t.Fatalf("getUsers: error %d", code)
|
|
}
|
|
if len(got.Users.Users) != 2 || got.Users.Users[0].Username != "ada" || !got.Users.Users[0].AdminRole ||
|
|
got.Users.Users[1].Username != "ben" || got.Users.Users[1].AdminRole || !got.Users.Users[1].StreamRole {
|
|
t.Errorf("users = %+v, want ada (admin) and ben (listener)", got.Users.Users)
|
|
}
|
|
if code := call(t, h.getUsers, dbq.User{Username: "ben"}, "", nil); code != ErrNotAuthorized {
|
|
t.Errorf("a listener's getUsers: code %d, want %d", code, ErrNotAuthorized)
|
|
}
|
|
}
|
|
|
|
// A rating is accepted and not kept: Minstrel's only rating is a like.
|
|
func TestSetRating_AcceptsAValidRating(t *testing.T) {
|
|
m := &mediaHandlers{}
|
|
user := dbq.User{Username: "ada"}
|
|
for query, want := range map[string]int{
|
|
"id=x&rating=5": 0,
|
|
"id=x&rating=0": 0,
|
|
"id=x&rating=6": ErrGeneric,
|
|
"id=x": ErrMissingParameter,
|
|
} {
|
|
if code := call(t, m.handleSetRating, user, query, nil); code != want {
|
|
t.Errorf("setRating %s: code %d, want %d", query, code, want)
|
|
}
|
|
}
|
|
}
|