The name, comment, removals and additions are separate writes, and the songs to add were only found missing when the last of them ran, so an unknown songIdToAdd answered error 70 over a playlist already renamed and trimmed. Every song to add is now looked up first, alongside the index check, and an unknown one answers 70 with nothing changed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
203 lines
7.9 KiB
Go
203 lines
7.9 KiB
Go
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))
|
|
}
|
|
|
|
// So does a song to add that isn't in the library, though it comes
|
|
// after a rename and a removal that would each have worked.
|
|
unknown := uuidToID(pgtype.UUID{Bytes: [16]byte{0xee}, Valid: true})
|
|
q = "playlistId=" + pid + "&name=Changed&songIndexToRemove=0&songIdToAdd=" + unknown
|
|
if code := call(t, h.updatePlaylist, f.user, q, nil); code != ErrDataNotFound {
|
|
t.Errorf("unknown song to add: error %d, want %d", code, ErrDataNotFound)
|
|
}
|
|
d = stored(t, h.playlists, f.user, id)
|
|
if got, want := storedTitles(d), []string{"Jetty", "Gale"}; d.Name != "Night Drive" || !slices.Equal(got, want) {
|
|
t.Errorf("after an unknown song: name %q, songs %v; want Night Drive, %v", d.Name, got, want)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|