Files
minstrel/internal/subsonic/star_test.go
T
bvandeusenandClaude Opus 5.5 8ba1d109c3 fix(events): a like made in a Subsonic app reaches the other apps, and an open web tab follows likes and playlist changes (#8077)
The native like endpoints announce a like on the event bus; Subsonic star
and unstar announced nothing, so the web and Android apps showed a stale
heart until they refetched. Subsonic now publishes the same events, through
one shape both surfaces build (eventbus.LikeChanged, eventbus.LedBy).

The web listened only for the nightly rebuild and notifications, so an open
tab ignored likes and playlist changes made in another tab, on Android or
in a Subsonic app. It now refreshes its liked views on any like event and
its playlists (and the open one) on playlist.created/updated/deleted/
tracks_changed.

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

351 lines
13 KiB
Go

package subsonic
import (
"context"
"encoding/json"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
"github.com/jackc/pgx/v5/pgxpool"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
"git.fabledsword.com/bvandeusen/minstrel/internal/eventbus"
"git.fabledsword.com/bvandeusen/minstrel/internal/playevents"
)
func testStarPool(t *testing.T) (*pgxpool.Pool, dbq.User, dbq.Track, dbq.Album, dbq.Artist) {
t.Helper()
pool := dbtest.Pool(t)
q := dbq.New(pool)
u, _ := q.CreateUser(context.Background(), dbq.CreateUserParams{
Username: dbtest.TestUserPrefix + "tester", PasswordHash: "x", ApiTokenHash: "x", IsAdmin: false,
})
a, _ := q.UpsertArtist(context.Background(), dbq.UpsertArtistParams{Name: "X", SortName: "X"})
al, _ := q.UpsertAlbum(context.Background(), dbq.UpsertAlbumParams{Title: "X", SortTitle: "X", ArtistID: a.ID})
tr, _ := q.UpsertTrack(context.Background(), dbq.UpsertTrackParams{
Title: "Y", AlbumID: al.ID, ArtistID: a.ID, FilePath: "/tmp/star.flac", DurationMs: 100_000,
})
return pool, u, tr, al, a
}
func newStarHandlers(pool *pgxpool.Pool) *mediaHandlers {
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
w := playevents.NewWriter(pool, logger, 30*time.Minute, 0.5, 30000)
return newMediaHandlers(pool, w, logger)
}
func TestHandleStar_TrackIDInsertsRow(t *testing.T) {
pool, user, track, _, _ := testStarPool(t)
m := newStarHandlers(pool)
q := url.Values{}
q.Set("id", uuidToID(track.ID))
req := httptest.NewRequest(http.MethodGet, "/rest/star?"+q.Encode(), nil)
ctx := context.WithValue(req.Context(), userCtxKey, user)
w := httptest.NewRecorder()
m.handleStar(w, req.WithContext(ctx))
if w.Code != http.StatusOK {
t.Fatalf("status = %d", w.Code)
}
var count int
_ = pool.QueryRow(context.Background(),
"SELECT count(*) FROM general_likes WHERE user_id=$1", user.ID).Scan(&count)
if count != 1 {
t.Errorf("count = %d", count)
}
}
func TestHandleStar_AllThreeIDsAtOnce(t *testing.T) {
pool, user, track, album, artist := testStarPool(t)
m := newStarHandlers(pool)
q := url.Values{}
q.Set("id", uuidToID(track.ID))
q.Set("albumId", uuidToID(album.ID))
q.Set("artistId", uuidToID(artist.ID))
req := httptest.NewRequest(http.MethodGet, "/rest/star?"+q.Encode(), nil)
ctx := context.WithValue(req.Context(), userCtxKey, user)
w := httptest.NewRecorder()
m.handleStar(w, req.WithContext(ctx))
if w.Code != http.StatusOK {
t.Fatalf("status = %d", w.Code)
}
var tCount, alCount, arCount int
_ = pool.QueryRow(context.Background(), "SELECT count(*) FROM general_likes WHERE user_id=$1", user.ID).Scan(&tCount)
_ = pool.QueryRow(context.Background(), "SELECT count(*) FROM general_likes_albums WHERE user_id=$1", user.ID).Scan(&alCount)
_ = pool.QueryRow(context.Background(), "SELECT count(*) FROM general_likes_artists WHERE user_id=$1", user.ID).Scan(&arCount)
if tCount != 1 || alCount != 1 || arCount != 1 {
t.Errorf("counts: tracks=%d albums=%d artists=%d", tCount, alCount, arCount)
}
}
func TestHandleStar_UnknownTrackReturnsError70(t *testing.T) {
pool, user, _, _, _ := testStarPool(t)
m := newStarHandlers(pool)
q := url.Values{}
q.Set("id", "00000000-0000-0000-0000-000000000000")
req := httptest.NewRequest(http.MethodGet, "/rest/star?"+q.Encode(), nil)
ctx := context.WithValue(req.Context(), userCtxKey, user)
w := httptest.NewRecorder()
m.handleStar(w, req.WithContext(ctx))
body := w.Body.String()
// Subsonic error envelope returns 200 OK with status="failed" inside the body.
// The exact code is 70 (data not found).
if w.Code != http.StatusOK || !contains(body, `"code":70`) && !contains(body, `code="70"`) {
t.Errorf("expected error 70 envelope, got status=%d body=%s", w.Code, body)
}
}
func TestHandleStar_PartialMissingAtomic(t *testing.T) {
pool, user, track, _, _ := testStarPool(t)
m := newStarHandlers(pool)
q := url.Values{}
q.Set("id", uuidToID(track.ID))
q.Set("albumId", "00000000-0000-0000-0000-000000000000")
req := httptest.NewRequest(http.MethodGet, "/rest/star?"+q.Encode(), nil)
ctx := context.WithValue(req.Context(), userCtxKey, user)
w := httptest.NewRecorder()
m.handleStar(w, req.WithContext(ctx))
// Album missing → atomic refusal: track NOT starred.
var count int
_ = pool.QueryRow(context.Background(),
"SELECT count(*) FROM general_likes WHERE user_id=$1", user.ID).Scan(&count)
if count != 0 {
t.Errorf("track was starred despite album miss; count = %d, want 0", count)
}
}
func TestHandleUnstar_RemovesAndIsIdempotent(t *testing.T) {
pool, user, track, _, _ := testStarPool(t)
m := newStarHandlers(pool)
// First star.
q := url.Values{}
q.Set("id", uuidToID(track.ID))
star := httptest.NewRequest(http.MethodGet, "/rest/star?"+q.Encode(), nil)
starCtx := context.WithValue(star.Context(), userCtxKey, user)
starResp := httptest.NewRecorder()
m.handleStar(starResp, star.WithContext(starCtx))
// Unstar.
un := httptest.NewRequest(http.MethodGet, "/rest/unstar?"+q.Encode(), nil)
unCtx := context.WithValue(un.Context(), userCtxKey, user)
unResp := httptest.NewRecorder()
m.handleUnstar(unResp, un.WithContext(unCtx))
if unResp.Code != http.StatusOK {
t.Fatalf("unstar status = %d", unResp.Code)
}
var count int
_ = pool.QueryRow(context.Background(),
"SELECT count(*) FROM general_likes WHERE user_id=$1", user.ID).Scan(&count)
if count != 0 {
t.Errorf("count after unstar = %d, want 0", count)
}
// Idempotent — unstar again.
un2 := httptest.NewRequest(http.MethodGet, "/rest/unstar?"+q.Encode(), nil)
un2 = un2.WithContext(context.WithValue(un2.Context(), userCtxKey, user))
un2Resp := httptest.NewRecorder()
m.handleUnstar(un2Resp, un2)
if un2Resp.Code != http.StatusOK {
t.Errorf("second unstar status = %d", un2Resp.Code)
}
}
func contains(s, substr string) bool {
for i := 0; i+len(substr) <= len(s); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}
func TestHandleGetStarred2_ReturnsAllThreeArrays(t *testing.T) {
pool, user, track, album, artist := testStarPool(t)
m := newStarHandlers(pool)
q := dbq.New(pool)
// Star one of each.
_, _ = q.LikeTrack(context.Background(), dbq.LikeTrackParams{UserID: user.ID, TrackID: track.ID})
_ = q.LikeAlbum(context.Background(), dbq.LikeAlbumParams{UserID: user.ID, AlbumID: album.ID})
_ = q.LikeArtist(context.Background(), dbq.LikeArtistParams{UserID: user.ID, ArtistID: artist.ID})
req := httptest.NewRequest(http.MethodGet, "/rest/getStarred2?f=json", nil)
ctx := context.WithValue(req.Context(), userCtxKey, user)
w := httptest.NewRecorder()
m.handleGetStarred2(w, req.WithContext(ctx))
if w.Code != http.StatusOK {
t.Fatalf("status = %d body=%s", w.Code, w.Body.String())
}
// Subsonic JSON envelope: {"subsonic-response":{...,"starred2":{...}}}
var raw map[string]any
_ = json.Unmarshal(w.Body.Bytes(), &raw)
resp, _ := raw["subsonic-response"].(map[string]any)
starred, _ := resp["starred2"].(map[string]any)
if starred == nil {
t.Fatalf("missing starred2 envelope: %v", raw)
}
songs, _ := starred["song"].([]any)
albums, _ := starred["album"].([]any)
artists, _ := starred["artist"].([]any)
if len(songs) != 1 || len(albums) != 1 || len(artists) != 1 {
t.Errorf("counts: songs=%d albums=%d artists=%d", len(songs), len(albums), len(artists))
}
}
func TestHandleGetStarred2_CrossUserIsolation(t *testing.T) {
pool, alice, track, _, _ := testStarPool(t)
q := dbq.New(pool)
bob, _ := q.CreateUser(context.Background(), dbq.CreateUserParams{
Username: dbtest.TestUserPrefix + "bob", PasswordHash: "x", ApiTokenHash: "y", IsAdmin: false,
})
_, _ = q.LikeTrack(context.Background(), dbq.LikeTrackParams{UserID: alice.ID, TrackID: track.ID})
m := newStarHandlers(pool)
req := httptest.NewRequest(http.MethodGet, "/rest/getStarred2?f=json", nil)
ctx := context.WithValue(req.Context(), userCtxKey, bob)
w := httptest.NewRecorder()
m.handleGetStarred2(w, req.WithContext(ctx))
var raw map[string]any
_ = json.Unmarshal(w.Body.Bytes(), &raw)
resp, _ := raw["subsonic-response"].(map[string]any)
starred, _ := resp["starred2"].(map[string]any)
songs, _ := starred["song"].([]any)
if len(songs) != 0 {
t.Errorf("bob's starred songs should be empty, got %d", len(songs))
}
}
func TestHandleStar_DuringNowPlaying_WritesContextualLike(t *testing.T) {
pool, user, track1, _, _ := testStarPool(t)
// Add a second track so star refers to a different track than the one playing.
q := dbq.New(pool)
a, _ := q.UpsertArtist(context.Background(), dbq.UpsertArtistParams{Name: "Y", SortName: "Y"})
al, _ := q.UpsertAlbum(context.Background(), dbq.UpsertAlbumParams{Title: "Y", SortTitle: "Y", ArtistID: a.ID})
track2, _ := q.UpsertTrack(context.Background(), dbq.UpsertTrackParams{
Title: "Star Me", AlbumID: al.ID, ArtistID: a.ID, FilePath: "/tmp/t2.flac", DurationMs: 100_000,
})
m := newStarHandlers(pool)
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
m.logger = logger
// Start now-playing for track1 (creates open play_event with vector).
q1 := url.Values{}
q1.Set("id", uuidToID(track1.ID))
q1.Set("submission", "false")
scrobReq := httptest.NewRequest(http.MethodGet, "/rest/scrobble?"+q1.Encode(), nil)
scrobCtx := context.WithValue(scrobReq.Context(), userCtxKey, user)
scrobResp := httptest.NewRecorder()
m.handleScrobble(scrobResp, scrobReq.WithContext(scrobCtx))
if scrobResp.Code != http.StatusOK {
t.Fatalf("scrobble: %d", scrobResp.Code)
}
// Star track2.
q2 := url.Values{}
q2.Set("id", uuidToID(track2.ID))
starReq := httptest.NewRequest(http.MethodGet, "/rest/star?"+q2.Encode(), nil)
starCtx := context.WithValue(starReq.Context(), userCtxKey, user)
starResp := httptest.NewRecorder()
m.handleStar(starResp, starReq.WithContext(starCtx))
if starResp.Code != http.StatusOK {
t.Fatalf("star: %d", starResp.Code)
}
// Verify contextual_likes row.
var count int
_ = pool.QueryRow(context.Background(),
"SELECT count(*) FROM contextual_likes WHERE user_id=$1 AND track_id=$2", user.ID, track2.ID).Scan(&count)
if count != 1 {
t.Errorf("contextual_likes count = %d, want 1", count)
}
}
// Starred songs list only what the listener can play: a liked song whose
// file is missing, or that they quarantined, would fail to stream or play the
// copy they hid. The web's liked list still shows both (it can mark them).
func TestHandleGetStarred2_LeavesOutMissingAndQuarantinedSongs(t *testing.T) {
pool, user, playable, album, artist := testStarPool(t)
ctx := context.Background()
q := dbq.New(pool)
missing, _ := q.UpsertTrack(ctx, dbq.UpsertTrackParams{
Title: "Gone", AlbumID: album.ID, ArtistID: artist.ID, FilePath: "/tmp/star-gone.flac", DurationMs: 100_000,
})
hidden, _ := q.UpsertTrack(ctx, dbq.UpsertTrackParams{
Title: "Hidden", AlbumID: album.ID, ArtistID: artist.ID, FilePath: "/tmp/star-hidden.flac", DurationMs: 100_000,
})
for _, tr := range []dbq.Track{playable, missing, hidden} {
if _, err := q.LikeTrack(ctx, dbq.LikeTrackParams{UserID: user.ID, TrackID: tr.ID}); err != nil {
t.Fatalf("like: %v", err)
}
}
if _, err := pool.Exec(ctx, `UPDATE tracks SET missing_since = now() WHERE id = $1`, missing.ID); err != nil {
t.Fatalf("mark missing: %v", err)
}
if _, err := pool.Exec(ctx, `INSERT INTO quarantine (user_id, track_id, reason) VALUES ($1, $2, 'bad_rip')`, user.ID, hidden.ID); err != nil {
t.Fatalf("quarantine: %v", err)
}
req := httptest.NewRequest(http.MethodGet, "/rest/getStarred2?f=json", nil)
w := httptest.NewRecorder()
newStarHandlers(pool).handleGetStarred2(w, req.WithContext(context.WithValue(req.Context(), userCtxKey, user)))
var raw map[string]any
_ = json.Unmarshal(w.Body.Bytes(), &raw)
resp, _ := raw["subsonic-response"].(map[string]any)
starred, _ := resp["starred2"].(map[string]any)
songs, _ := starred["song"].([]any)
if len(songs) != 1 {
t.Fatalf("starred2 listed %d songs, want only the playable one: %v", len(songs), songs)
}
if got, _ := songs[0].(map[string]any)["title"].(string); got != "Y" {
t.Fatalf("starred2 song = %q, want the playable one", got)
}
all, err := q.ListLikedTrackRows(ctx, dbq.ListLikedTrackRowsParams{UserID: user.ID, Limit: 10})
if err != nil || len(all) != 3 {
t.Fatalf("the web's liked list has %d rows (err %v), want all 3", len(all), err)
}
}
// A star made in a Subsonic app is announced as the native like endpoint
// announces one, so an open web tab or the Android app follows it.
func TestHandleStar_TellsTheOtherApps(t *testing.T) {
pool, user, track, _, _ := testStarPool(t)
m := newStarHandlers(pool)
m.bus = eventbus.New()
events, cancel := m.bus.Subscribe(8)
defer cancel()
req := httptest.NewRequest(http.MethodGet, "/rest/star?id="+uuidToID(track.ID), nil)
w := httptest.NewRecorder()
m.handleStar(w, req.WithContext(context.WithValue(req.Context(), userCtxKey, user)))
if w.Code != http.StatusOK {
t.Fatalf("status = %d body=%s", w.Code, w.Body.String())
}
select {
case e := <-events:
if e.Kind != "track.liked" || e.UserID != user.ID.String() || e.Data["entity_id"] != track.ID.String() {
t.Fatalf("event = %+v, want track.liked for the starred track", e)
}
case <-time.After(time.Second):
t.Fatal("no event after a star")
}
}