release / web (push) Successful in 1m20s
release / go (push) Successful in 1m49s
release / govulncheck (push) Successful in 20s
release / integration (push) Successful in 4m3s
release / android (push) Successful in 4m26s
release / Attach APK to the Release (tag releases only) (push) Canceled after 0s
release / Build + push container image (push) Canceled after 0s
release / Verify release artifacts (tag releases only) (push) Canceled after 0s
release / Build signed APK (push) Canceled after 4m43s
A scrobble whose id named no track reached the play writer and came back as a generic failure (0). The handler now looks the song up first, as stream does, and answers 70 with nothing recorded; a lookup that fails for another reason is still a generic error. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
150 lines
4.7 KiB
Go
150 lines
4.7 KiB
Go
package subsonic
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
"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/playevents"
|
|
)
|
|
|
|
func testScrobblePool(t *testing.T) (*pgxpool.Pool, dbq.User, dbq.Track) {
|
|
t.Helper()
|
|
pool := dbtest.Pool(t)
|
|
q := dbq.New(pool)
|
|
u, err := q.CreateUser(context.Background(), dbq.CreateUserParams{
|
|
Username: dbtest.TestUserPrefix + "tester", PasswordHash: "x", ApiTokenHash: "x", IsAdmin: false,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("user: %v", err)
|
|
}
|
|
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/scrob.flac", DurationMs: 100_000,
|
|
})
|
|
return pool, u, tr
|
|
}
|
|
|
|
func newScrobbleHandlers(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 TestHandleScrobble_SubmissionFalseInsertsOpenPlayEvent(t *testing.T) {
|
|
pool, user, track := testScrobblePool(t)
|
|
m := newScrobbleHandlers(pool)
|
|
|
|
q := url.Values{}
|
|
q.Set("id", uuidToID(track.ID))
|
|
q.Set("submission", "false")
|
|
req := httptest.NewRequest(http.MethodGet, "/rest/scrobble?"+q.Encode(), nil)
|
|
ctx := context.WithValue(req.Context(), userCtxKey, user)
|
|
w := httptest.NewRecorder()
|
|
m.handleScrobble(w, req.WithContext(ctx))
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status = %d", w.Code)
|
|
}
|
|
rows, _ := pool.Query(context.Background(),
|
|
"SELECT id FROM play_events WHERE user_id=$1 AND ended_at IS NULL", user.ID)
|
|
defer rows.Close()
|
|
count := 0
|
|
for rows.Next() {
|
|
count++
|
|
}
|
|
if count != 1 {
|
|
t.Errorf("open play_events count = %d, want 1", count)
|
|
}
|
|
}
|
|
|
|
func TestHandleScrobble_SubmissionTrueWritesCompletedPlay(t *testing.T) {
|
|
pool, user, track := testScrobblePool(t)
|
|
m := newScrobbleHandlers(pool)
|
|
|
|
q := url.Values{}
|
|
q.Set("id", uuidToID(track.ID))
|
|
q.Set("submission", "true")
|
|
req := httptest.NewRequest(http.MethodGet, "/rest/scrobble?"+q.Encode(), nil)
|
|
ctx := context.WithValue(req.Context(), userCtxKey, user)
|
|
w := httptest.NewRecorder()
|
|
m.handleScrobble(w, req.WithContext(ctx))
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status = %d", w.Code)
|
|
}
|
|
rows, _ := pool.Query(context.Background(),
|
|
"SELECT ended_at IS NOT NULL FROM play_events WHERE user_id=$1", user.ID)
|
|
defer rows.Close()
|
|
closed := 0
|
|
for rows.Next() {
|
|
var c bool
|
|
_ = rows.Scan(&c)
|
|
if c {
|
|
closed++
|
|
}
|
|
}
|
|
if closed != 1 {
|
|
t.Errorf("closed play_events = %d, want 1", closed)
|
|
}
|
|
}
|
|
|
|
// A scrobble's time is bounded like a native play's: an app whose clock is
|
|
// years out can't date the play years out.
|
|
func TestHandleScrobble_AWildClockIsBoundedToNow(t *testing.T) {
|
|
pool, user, track := testScrobblePool(t)
|
|
m := newScrobbleHandlers(pool)
|
|
|
|
q := url.Values{}
|
|
q.Set("id", uuidToID(track.ID))
|
|
q.Set("submission", "true")
|
|
q.Set("time", strconv.FormatInt(time.Now().AddDate(5, 0, 0).UnixMilli(), 10))
|
|
req := httptest.NewRequest(http.MethodGet, "/rest/scrobble?"+q.Encode(), nil)
|
|
w := httptest.NewRecorder()
|
|
m.handleScrobble(w, req.WithContext(context.WithValue(req.Context(), userCtxKey, user)))
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status = %d", w.Code)
|
|
}
|
|
var startedAt time.Time
|
|
if err := pool.QueryRow(context.Background(),
|
|
"SELECT started_at FROM play_events WHERE user_id=$1", user.ID).Scan(&startedAt); err != nil {
|
|
t.Fatalf("read play: %v", err)
|
|
}
|
|
if startedAt.After(time.Now().Add(time.Minute)) {
|
|
t.Fatalf("play dated %v, want no later than now", startedAt)
|
|
}
|
|
}
|
|
|
|
// A scrobble of a song the library doesn't hold answers "not found" (70),
|
|
// as stream and getSong do, and records nothing.
|
|
func TestHandleScrobble_AnUnknownSongIsNotFound(t *testing.T) {
|
|
pool, user, _ := testScrobblePool(t)
|
|
m := newScrobbleHandlers(pool)
|
|
unknown := uuidToID(pgtype.UUID{Bytes: [16]byte{0xee}, Valid: true})
|
|
|
|
for _, submission := range []string{"true", "false"} {
|
|
if code := call(t, m.handleScrobble, user, "id="+unknown+"&submission="+submission, nil); code != ErrDataNotFound {
|
|
t.Errorf("submission=%s: error %d, want %d", submission, code, ErrDataNotFound)
|
|
}
|
|
}
|
|
var n int
|
|
if err := pool.QueryRow(context.Background(), "SELECT count(*) FROM play_events WHERE user_id=$1", user.ID).Scan(&n); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n != 0 {
|
|
t.Errorf("play_events = %d, want none", n)
|
|
}
|
|
}
|