Files
minstrel/internal/playlists/offline_replay_test.go
T
bvandeusenandClaude Opus 5.5 a77f3ae6ff
release / go (push) Successful in 1m59s
release / web (push) Successful in 1m23s
release / govulncheck (push) Successful in 22s
release / integration (push) Successful in 4m38s
release / android (push) Successful in 4m45s
release / Build signed APK (push) Successful in 5m22s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 17s
release / Verify release artifacts (tag releases only) (push) Skipped
feat(playlists): changes a phone queues offline are safe to send twice (#8023)
- A new playlist can carry the client's own id; sending the create
  again finds the same list.
- Removing a song can name the song expected at that position; if
  another song is there now, nothing is removed (409 list_changed).
- A reorder can give the songs in their new order (track_ids); sending
  it again changes nothing.
- The playlist service tells the owner's clients about every change
  itself, so a change made from a Subsonic app refreshes the web and
  Android apps too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 10:22:40 -05:00

162 lines
5.4 KiB
Go

package playlists_test
import (
"context"
"errors"
"slices"
"testing"
"time"
"github.com/jackc/pgx/v5/pgtype"
"git.fabledsword.com/bvandeusen/minstrel/internal/eventbus"
"git.fabledsword.com/bvandeusen/minstrel/internal/playlists"
)
// The writes a phone queues while offline may reach the server more than
// once. These tests send each one twice and check the second changes
// nothing.
func id(b byte) pgtype.UUID { return pgtype.UUID{Bytes: [16]byte{b}, Valid: true} }
func TestOrderByTracks_ArrangesNamedEntriesInTheirOwnPlaces(t *testing.T) {
a, b, c, x := id(1), id(2), id(3), id(9)
gone := pgtype.UUID{}
cases := []struct {
name string
current, want []pgtype.UUID
perm []int32
}{
{"swap two around an entry left alone", []pgtype.UUID{a, gone, b, c}, []pgtype.UUID{c, a}, []int32{3, 1, 2, 0}},
{"an id not in the list is skipped", []pgtype.UUID{a, b}, []pgtype.UUID{x, b, a}, []int32{1, 0}},
{"a song listed twice matches its entries in turn", []pgtype.UUID{a, b, a}, []pgtype.UUID{a, a, b}, []int32{0, 2, 1}},
{"the current order is the identity", []pgtype.UUID{a, b, c}, []pgtype.UUID{a, b, c}, []int32{0, 1, 2}},
}
for _, tc := range cases {
if got := playlists.OrderByTracks(tc.current, tc.want); !slices.Equal(got, tc.perm) {
t.Errorf("%s: perm = %v, want %v", tc.name, got, tc.perm)
}
}
}
func TestCreateWithID_ASecondSendFindsTheSameList(t *testing.T) {
pool := newPool(t)
alice := seedUser(t, pool, "alice")
bob := seedUser(t, pool, "bob")
svc := playlists.NewService(pool, nil, t.TempDir())
ctx := context.Background()
chosen := randomUUID()
first, err := svc.CreateWithID(ctx, alice.ID, chosen, "Road Songs", "", false)
if err != nil {
t.Fatalf("create: %v", err)
}
if first.ID != chosen {
t.Fatalf("id = %s, want the client's %s", uuidString(first.ID), uuidString(chosen))
}
again, err := svc.CreateWithID(ctx, alice.ID, chosen, "Road Songs", "", false)
if err != nil || again.ID != chosen {
t.Fatalf("second send: %v, id %s; want the same list", err, uuidString(again.ID))
}
lists, err := svc.List(ctx, alice.ID)
if err != nil {
t.Fatal(err)
}
if len(lists) != 1 {
t.Errorf("alice has %d lists, want 1", len(lists))
}
if _, err := svc.CreateWithID(ctx, bob.ID, chosen, "Mine now", "", false); !errors.Is(err, playlists.ErrInvalidInput) {
t.Errorf("bob reusing alice's id: %v, want ErrInvalidInput", err)
}
}
func TestRemoveTrack_ASecondSendRemovesNothingMore(t *testing.T) {
pool := newPool(t)
user := seedUser(t, pool, "alice")
a := seedTrack(t, pool, "A", "Artist")
b := seedTrack(t, pool, "B", "Artist")
c := seedTrack(t, pool, "C", "Artist")
svc := playlists.NewService(pool, nil, t.TempDir())
ctx := context.Background()
pl, _ := svc.Create(ctx, user.ID, "Test", "", false)
_ = svc.AppendTracks(ctx, user.ID, pl.ID, []pgtype.UUID{a.ID, b.ID, c.ID})
if err := svc.RemoveTrack(ctx, user.ID, pl.ID, 1, b.ID); err != nil {
t.Fatalf("remove B: %v", err)
}
// C has moved into position 1; the same request must not take it.
if err := svc.RemoveTrack(ctx, user.ID, pl.ID, 1, b.ID); !errors.Is(err, playlists.ErrListChanged) {
t.Errorf("second send: %v, want ErrListChanged", err)
}
got, _ := svc.Get(ctx, user.ID, pl.ID)
if titles := trackTitles(got); !slices.Equal(titles, []string{"A", "C"}) {
t.Errorf("tracks = %v, want A, C", titles)
}
}
func TestReorderTracks_ASecondSendChangesNothing(t *testing.T) {
pool := newPool(t)
user := seedUser(t, pool, "alice")
a := seedTrack(t, pool, "A", "Artist")
b := seedTrack(t, pool, "B", "Artist")
c := seedTrack(t, pool, "C", "Artist")
svc := playlists.NewService(pool, nil, t.TempDir())
ctx := context.Background()
pl, _ := svc.Create(ctx, user.ID, "Test", "", false)
_ = svc.AppendTracks(ctx, user.ID, pl.ID, []pgtype.UUID{a.ID, b.ID, c.ID})
for range 2 {
if err := svc.ReorderTracks(ctx, user.ID, pl.ID, []pgtype.UUID{c.ID, a.ID, b.ID}); err != nil {
t.Fatalf("reorder: %v", err)
}
got, _ := svc.Get(ctx, user.ID, pl.ID)
if titles := trackTitles(got); !slices.Equal(titles, []string{"C", "A", "B"}) {
t.Errorf("tracks = %v, want C, A, B", titles)
}
}
}
// Every change tells the owner's clients, whichever client made it.
func TestChanges_TellTheOwnersClients(t *testing.T) {
pool := newPool(t)
user := seedUser(t, pool, "alice")
a := seedTrack(t, pool, "A", "Artist")
bus := eventbus.New()
events, stop := bus.Subscribe(16)
defer stop()
svc := playlists.NewService(pool, nil, t.TempDir())
svc.PublishTo(bus)
ctx := context.Background()
pl, _ := svc.Create(ctx, user.ID, "Test", "", false)
name := "Renamed"
_, _ = svc.Update(ctx, user.ID, pl.ID, playlists.UpdateInput{Name: &name})
_ = svc.AppendTracks(ctx, user.ID, pl.ID, []pgtype.UUID{a.ID})
_ = svc.Delete(ctx, user.ID, pl.ID)
var kinds []string
for range 4 {
select {
case e := <-events:
if e.UserID != uuidString(user.ID) || e.Data["playlist_id"] != uuidString(pl.ID) {
t.Errorf("%s went to %s about %v", e.Kind, e.UserID, e.Data["playlist_id"])
}
kinds = append(kinds, e.Kind)
case <-time.After(2 * time.Second):
t.Fatalf("events so far %v, want four", kinds)
}
}
want := []string{"playlist.created", "playlist.updated", "playlist.tracks_changed", "playlist.deleted"}
if !slices.Equal(kinds, want) {
t.Errorf("events = %v, want %v", kinds, want)
}
}
func trackTitles(d *playlists.PlaylistDetail) []string {
out := make([]string, 0, len(d.Tracks))
for _, tr := range d.Tracks {
out = append(out, tr.Title)
}
return out
}