Files
minstrel/internal/playlists/offline_replay_test.go
T
bvandeusenandClaude Opus 5.5 ad17849205 fix(playlists): a replayed append whose reply was lost adds nothing more (#8122)
An append that reached the server but whose reply never reached the
phone stayed in the offline queue and was sent again, and since an
append is an action, the song landed twice. Android now names each
append with a request id, carried by the live send and its queued row
alike; POST /api/playlists/{id}/tracks takes it as request_id, and the
server records it per playlist in the append's own transaction
(playlist_append_requests, pruned after 30 days). A send whose id is
already recorded changes nothing; a failed append rolls its id back
with it. Without an id an append behaves as before, so queued rows
from older builds and Subsonic clients are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 11:52:12 -05:00

210 lines
7.3 KiB
Go

package playlists_test
import (
"context"
"errors"
"slices"
"testing"
"time"
"github.com/jackc/pgx/v5/pgtype"
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
"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 := dbtest.Pool(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 := dbtest.Pool(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 := dbtest.Pool(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 := dbtest.Pool(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
}
func TestAppendTracksOnce_ASecondSendWithTheSameRequestAddsNothing(t *testing.T) {
pool := dbtest.Pool(t)
user := seedUser(t, pool, "alice")
a := seedTrack(t, pool, "A", "Artist")
b := seedTrack(t, pool, "B", "Artist")
svc := playlists.NewService(pool, nil, t.TempDir())
ctx := context.Background()
pl, _ := svc.Create(ctx, user.ID, "Test", "", false)
sent := randomUUID()
for range 2 {
if err := svc.AppendTracksOnce(ctx, user.ID, pl.ID, []pgtype.UUID{a.ID}, sent); err != nil {
t.Fatalf("append A: %v", err)
}
}
// A new request is a new append, even of the same song.
if err := svc.AppendTracksOnce(ctx, user.ID, pl.ID, []pgtype.UUID{a.ID, b.ID}, randomUUID()); err != nil {
t.Fatalf("append A, B: %v", err)
}
got, _ := svc.Get(ctx, user.ID, pl.ID)
if titles := trackTitles(got); !slices.Equal(titles, []string{"A", "A", "B"}) {
t.Errorf("tracks = %v, want A, A, B", titles)
}
}
func TestAppendTracksOnce_AFailedAppendDoesNotUseUpItsRequest(t *testing.T) {
pool := dbtest.Pool(t)
user := seedUser(t, pool, "alice")
a := seedTrack(t, pool, "A", "Artist")
svc := playlists.NewService(pool, nil, t.TempDir())
ctx := context.Background()
pl, _ := svc.Create(ctx, user.ID, "Test", "", false)
sent := randomUUID()
err := svc.AppendTracksOnce(ctx, user.ID, pl.ID, []pgtype.UUID{a.ID, randomUUID()}, sent)
if !errors.Is(err, playlists.ErrTrackNotFound) {
t.Fatalf("append with a missing track: %v, want ErrTrackNotFound", err)
}
if err := svc.AppendTracksOnce(ctx, user.ID, pl.ID, []pgtype.UUID{a.ID}, sent); err != nil {
t.Fatalf("append A: %v", err)
}
got, _ := svc.Get(ctx, user.ID, pl.ID)
if titles := trackTitles(got); !slices.Equal(titles, []string{"A"}) {
t.Errorf("tracks = %v, want A: the failed send rolled back with its request id", titles)
}
}