fix(likes): the web marks every copy of a liked song, not only the one tapped (#8126)
A like is on the song: the server likes every copy sharing its song_key, but the web's optimistic update marked only the tapped track, so the same song on another album or a compilation showed an empty heart until the liked ids were fetched again. A track like or unlike now answers 200 with the ids of every copy it changed, the asked one first, and the web marks them all. Clients that ignore the body (Android) are unaffected. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
@@ -46,11 +47,11 @@ func TestLikeTrack_Idempotent(t *testing.T) {
|
||||
track := seedTrack(t, pool, album.ID, artist.ID, "Something", 3, 183_000)
|
||||
|
||||
w := callLike(h, user, http.MethodPost, "/api/likes/tracks/"+uuidToString(track.ID))
|
||||
if w.Code != http.StatusNoContent {
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("first like: status = %d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
w = callLike(h, user, http.MethodPost, "/api/likes/tracks/"+uuidToString(track.ID))
|
||||
if w.Code != http.StatusNoContent {
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("second like (idempotent): status = %d", w.Code)
|
||||
}
|
||||
var count int
|
||||
@@ -89,11 +90,39 @@ func TestUnlikeTrack_IdempotentEvenWhenAbsent(t *testing.T) {
|
||||
track := seedTrack(t, pool, album.ID, artist.ID, "Something", 3, 183_000)
|
||||
|
||||
w := callLike(h, user, http.MethodDelete, "/api/likes/tracks/"+uuidToString(track.ID))
|
||||
if w.Code != http.StatusNoContent {
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// A like is on the song, so its answer names every copy it reached, and an
|
||||
// unlike every copy it cleared, the tapped one first.
|
||||
func TestLikeTrack_AnswersEveryCopyOfTheSong(t *testing.T) {
|
||||
h, pool := testHandlers(t)
|
||||
truncateLibrary(t, pool)
|
||||
user := seedUser(t, pool, "alice", "x", false)
|
||||
artist := seedArtist(t, pool, "Copper Lanterns")
|
||||
album := seedAlbum(t, pool, artist.ID, "Harbour Street", 1969)
|
||||
best := seedAlbum(t, pool, artist.ID, "Best Of", 1975)
|
||||
first := seedTrack(t, pool, album.ID, artist.ID, "Something", 3, 183_000)
|
||||
copied := seedTrack(t, pool, best.ID, artist.ID, "Something", 1, 183_000)
|
||||
if _, err := pool.Exec(context.Background(), "UPDATE tracks SET song_id = $1 WHERE id = $2", first.ID, copied.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := []string{uuidToString(copied.ID), uuidToString(first.ID)}
|
||||
|
||||
for _, method := range []string{http.MethodPost, http.MethodDelete} {
|
||||
w := callLike(h, user, method, "/api/likes/tracks/"+uuidToString(copied.ID))
|
||||
var got trackLikeView
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
|
||||
t.Fatalf("%s: decode %q: %v", method, w.Body.String(), err)
|
||||
}
|
||||
if !slices.Equal(got.TrackIDs, want) {
|
||||
t.Errorf("%s: track_ids = %v, want %v", method, got.TrackIDs, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestListLikedTracks_PaginatedAndSortedDescByLikedAt(t *testing.T) {
|
||||
h, pool := testHandlers(t)
|
||||
truncateLibrary(t, pool)
|
||||
|
||||
Reference in New Issue
Block a user