feat(subsonic): apps can make, change and delete playlists (#8021)
release / web (push) Successful in 1m43s
release / go (push) Successful in 2m21s
release / govulncheck (push) Successful in 20s
release / integration (push) Failing after 4m31s
release / Attach APK to the Release (tag releases only) (push) Canceled after 0s
release / Build + push container image (push) Canceled after 0s
release / Verify release artifacts (tag releases only) (push) Canceled after 0s
release / Build signed APK (push) Canceled after 2m44s
release / android (push) Canceled after 3m43s
release / web (push) Successful in 1m43s
release / go (push) Successful in 2m21s
release / govulncheck (push) Successful in 20s
release / integration (push) Failing after 4m31s
release / Attach APK to the Release (tag releases only) (push) Canceled after 0s
release / Build + push container image (push) Canceled after 0s
release / Verify release artifacts (tag releases only) (push) Canceled after 0s
release / Build signed APK (push) Canceled after 2m44s
release / android (push) Canceled after 3m43s
createPlaylist makes a playlist, or with playlistId replaces its songs; updatePlaylist renames it, sets its comment and visibility, removes songs by the index the app was shown and adds songs; deletePlaylist deletes it. A song index counts only the entries getPlaylist lists, so a hidden entry ahead of it doesn't shift which song goes. The playlist service gains ReplaceTracks and RemovePositions; the owner and system-mix checks that each change repeated are one function. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,190 @@
|
||||
package subsonic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/playlists"
|
||||
)
|
||||
|
||||
// writeHandlers are the fixture's handlers with a playlist service whose
|
||||
// covers are drawn into a temporary directory.
|
||||
func (f picksFixture) writeHandlers(t *testing.T) *pickHandlers {
|
||||
h := f.handlers()
|
||||
h.playlists = playlists.NewService(f.pool, h.logger, t.TempDir())
|
||||
return h
|
||||
}
|
||||
|
||||
// newList makes a playlist owned by owner holding songs, by title.
|
||||
func (f picksFixture) newList(t *testing.T, svc *playlists.Service, owner dbq.User, name string, public bool, songs ...string) pgtype.UUID {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
p, err := svc.Create(ctx, owner.ID, name, "", public)
|
||||
if err != nil {
|
||||
t.Fatalf("create %s: %v", name, err)
|
||||
}
|
||||
if len(songs) > 0 {
|
||||
if err := svc.AppendTracks(ctx, owner.ID, p.ID, f.ids(songs...)); err != nil {
|
||||
t.Fatalf("append %s: %v", name, err)
|
||||
}
|
||||
}
|
||||
return p.ID
|
||||
}
|
||||
|
||||
func (f picksFixture) ids(songs ...string) []pgtype.UUID {
|
||||
out := make([]pgtype.UUID, 0, len(songs))
|
||||
for _, s := range songs {
|
||||
out = append(out, f.tracks[s].ID)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// stored reads a playlist as Minstrel keeps it, hidden entries included.
|
||||
func stored(t *testing.T, svc *playlists.Service, owner dbq.User, id pgtype.UUID) *playlists.PlaylistDetail {
|
||||
t.Helper()
|
||||
d, err := svc.Get(context.Background(), owner.ID, id)
|
||||
if err != nil {
|
||||
t.Fatalf("get: %v", err)
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
func storedTitles(d *playlists.PlaylistDetail) []string {
|
||||
out := make([]string, 0, len(d.Tracks))
|
||||
for _, tr := range d.Tracks {
|
||||
out = append(out, tr.Title)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func TestCreatePlaylist_MakesAListOrReplacesOnesSongs(t *testing.T) {
|
||||
f := seedPicks(t)
|
||||
h := f.writeHandlers(t)
|
||||
|
||||
var made struct {
|
||||
Playlist PlaylistWithSongs `json:"playlist"`
|
||||
}
|
||||
q := "name=Road+Songs&songId=" + uuidToID(f.tracks["Dusk"].ID) + "&songId=" + uuidToID(f.tracks["Gale"].ID)
|
||||
if code := call(t, h.createPlaylist, f.user, q, &made); code != 0 {
|
||||
t.Fatalf("create: error %d", code)
|
||||
}
|
||||
if made.Playlist.Name != "Road Songs" || !slices.Equal(titles(made.Playlist.Entries), []string{"Dusk", "Gale"}) {
|
||||
t.Fatalf("made %q with %v, want Road Songs with Dusk, Gale", made.Playlist.Name, titles(made.Playlist.Entries))
|
||||
}
|
||||
id, _ := parseUUID(made.Playlist.ID)
|
||||
if d := stored(t, h.playlists, f.user, id); d.UserID != f.user.ID || d.IsPublic {
|
||||
t.Errorf("owner %v public %v, want the caller's private list", d.UserID, d.IsPublic)
|
||||
}
|
||||
|
||||
var replaced struct {
|
||||
Playlist PlaylistWithSongs `json:"playlist"`
|
||||
}
|
||||
q = "playlistId=" + made.Playlist.ID + "&name=Night+Drive&songId=" + uuidToID(f.tracks["Inlet"].ID)
|
||||
if code := call(t, h.createPlaylist, f.user, q, &replaced); code != 0 {
|
||||
t.Fatalf("replace: error %d", code)
|
||||
}
|
||||
if replaced.Playlist.ID != made.Playlist.ID || replaced.Playlist.Name != "Night Drive" ||
|
||||
!slices.Equal(titles(replaced.Playlist.Entries), []string{"Inlet"}) {
|
||||
t.Errorf("replaced = %s %q %v, want the same list renamed Night Drive holding Inlet",
|
||||
replaced.Playlist.ID, replaced.Playlist.Name, titles(replaced.Playlist.Entries))
|
||||
}
|
||||
|
||||
if code := call(t, h.createPlaylist, f.user, "songId="+uuidToID(f.tracks["Dusk"].ID), nil); code != ErrMissingParameter {
|
||||
t.Errorf("no name or playlistId: error %d, want %d", code, ErrMissingParameter)
|
||||
}
|
||||
|
||||
// A song id that names no song leaves no half-made playlist behind.
|
||||
notASong := uuidToID(f.albums["Paper Boats"].ID)
|
||||
if code := call(t, h.createPlaylist, f.user, "name=Broken&songId="+notASong, nil); code != ErrDataNotFound {
|
||||
t.Errorf("unknown song: error %d, want %d", code, ErrDataNotFound)
|
||||
}
|
||||
var n int
|
||||
if err := f.pool.QueryRow(context.Background(), `SELECT count(*) FROM playlists WHERE name = 'Broken'`).Scan(&n); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n != 0 {
|
||||
t.Errorf("%d playlists named Broken, want none", n)
|
||||
}
|
||||
}
|
||||
|
||||
// Jetty's file is missing, so the app is shown Inlet, Dusk, Gale and its
|
||||
// index 1 is Dusk, though Dusk is third in the stored list.
|
||||
func TestUpdatePlaylist_IndexesCountTheSongsTheAppWasShown(t *testing.T) {
|
||||
f := seedPicks(t)
|
||||
h := f.writeHandlers(t)
|
||||
id := f.newList(t, h.playlists, f.user, "Road Songs", false, "Inlet", "Jetty", "Dusk", "Gale")
|
||||
pid := uuidToID(id)
|
||||
|
||||
q := "playlistId=" + pid + "&songIndexToRemove=1&songIdToAdd=" + uuidToID(f.tracks["Harbour"].ID) +
|
||||
"&name=Night+Drive&comment=after+dark&public=true"
|
||||
if code := call(t, h.updatePlaylist, f.user, q, nil); code != 0 {
|
||||
t.Fatalf("update: error %d", code)
|
||||
}
|
||||
d := stored(t, h.playlists, f.user, id)
|
||||
if got, want := storedTitles(d), []string{"Inlet", "Jetty", "Gale", "Harbour"}; !slices.Equal(got, want) {
|
||||
t.Errorf("songs = %v, want %v", got, want)
|
||||
}
|
||||
if d.Name != "Night Drive" || d.Description != "after dark" || !d.IsPublic {
|
||||
t.Errorf("name %q comment %q public %v, want Night Drive, after dark, true", d.Name, d.Description, d.IsPublic)
|
||||
}
|
||||
|
||||
// Shown now: Inlet, Gale, Harbour. Several indexes all count from
|
||||
// that list.
|
||||
if code := call(t, h.updatePlaylist, f.user, "playlistId="+pid+"&songIndexToRemove=0&songIndexToRemove=2", nil); code != 0 {
|
||||
t.Fatalf("remove two: error %d", code)
|
||||
}
|
||||
if got, want := storedTitles(stored(t, h.playlists, f.user, id)), []string{"Jetty", "Gale"}; !slices.Equal(got, want) {
|
||||
t.Errorf("songs = %v, want %v", got, want)
|
||||
}
|
||||
|
||||
// An index past the shown list changes nothing, not even the name.
|
||||
if code := call(t, h.updatePlaylist, f.user, "playlistId="+pid+"&name=Changed&songIndexToRemove=1", nil); code != ErrDataNotFound {
|
||||
t.Errorf("index past the end: error %d, want %d", code, ErrDataNotFound)
|
||||
}
|
||||
if d := stored(t, h.playlists, f.user, id); d.Name != "Night Drive" || len(d.Tracks) != 2 {
|
||||
t.Errorf("after a refused update: name %q, %d songs; want it unchanged", d.Name, len(d.Tracks))
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlaylistWrites_OnlyTheOwnersOwnLists(t *testing.T) {
|
||||
f := seedPicks(t)
|
||||
h := f.writeHandlers(t)
|
||||
shared := uuidToID(f.newList(t, h.playlists, f.other, "Shared", true, "Gale"))
|
||||
secret := uuidToID(f.newList(t, h.playlists, f.other, "Secret", false, "Gale"))
|
||||
mixID := f.newList(t, h.playlists, f.user, "For you", false, "Gale")
|
||||
f.exec(t, `UPDATE playlists SET kind = 'system', system_variant = 'for_you' WHERE id = $1`, mixID)
|
||||
mix := uuidToID(mixID)
|
||||
song := uuidToID(f.tracks["Dusk"].ID)
|
||||
|
||||
cases := []struct {
|
||||
list string
|
||||
want int
|
||||
}{
|
||||
{shared, ErrNotAuthorized},
|
||||
{secret, ErrDataNotFound},
|
||||
{mix, ErrNotAuthorized},
|
||||
}
|
||||
for _, c := range cases {
|
||||
if code := call(t, h.updatePlaylist, f.user, "playlistId="+c.list+"&songIdToAdd="+song, nil); code != c.want {
|
||||
t.Errorf("update %s: error %d, want %d", c.list, code, c.want)
|
||||
}
|
||||
if code := call(t, h.createPlaylist, f.user, "playlistId="+c.list+"&songId="+song, nil); code != c.want {
|
||||
t.Errorf("replace %s: error %d, want %d", c.list, code, c.want)
|
||||
}
|
||||
if code := call(t, h.deletePlaylist, f.user, "id="+c.list, nil); code != c.want {
|
||||
t.Errorf("delete %s: error %d, want %d", c.list, code, c.want)
|
||||
}
|
||||
}
|
||||
mine := f.newList(t, h.playlists, f.user, "Mine", false, "Dusk")
|
||||
if code := call(t, h.deletePlaylist, f.user, "id="+uuidToID(mine), nil); code != 0 {
|
||||
t.Fatalf("delete own: error %d", code)
|
||||
}
|
||||
if _, err := h.playlists.Get(context.Background(), f.user.ID, mine); !errors.Is(err, playlists.ErrNotFound) {
|
||||
t.Errorf("after delete: %v, want not found", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user