release / web (push) Successful in 1m34s
release / go (push) Successful in 2m0s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 3m24s
release / android (push) Successful in 4m46s
release / Build signed APK (push) Successful in 5m12s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Publish the release image to GHCR (dispatch only) (push) Skipped
release / Build + push container image (push) Successful in 29s
release / Verify release artifacts (tag releases only) (push) Skipped
- The like/unlike story runs as one sequence that checks the rows after each step. - The invite-only sign-up cases are one test, which now also checks that a refused sign-up creates no account. - Demoting an admin, and stopping at the last one, is one test. - A wrong password and an unknown user are one test, which now also checks that the two refusals are identical. - The cover-path resolver tests repeated the cover handler's tests branch for branch; the handler tests stay. - The Range-from-start stream test only re-checked http.ServeContent. - The APK's nil-user 401 is covered by the mounted-route walk. - 68 copies of the MINSTREL_TEST_DATABASE_URL skip go: every one of those tests reaches dbtest.Pool, which already skips. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
273 lines
10 KiB
Go
273 lines
10 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"slices"
|
|
"testing"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/Fabled-Sword/minstrel/internal/db/dbq"
|
|
)
|
|
|
|
func callLike(h *handlers, user dbq.User, method, path string) *httptest.ResponseRecorder {
|
|
req := httptest.NewRequest(method, path, nil)
|
|
req = withUser(req, user)
|
|
w := httptest.NewRecorder()
|
|
likesRouter(h).ServeHTTP(w, req)
|
|
return w
|
|
}
|
|
|
|
// likesRouter matches the routes registered in Mount but without RequireUser
|
|
// so tests can inject the user via context directly.
|
|
func likesRouter(h *handlers) http.Handler {
|
|
r := chi.NewRouter()
|
|
r.Post("/api/likes/tracks/{id}", h.handleLikeTrack)
|
|
r.Delete("/api/likes/tracks/{id}", h.handleUnlikeTrack)
|
|
r.Post("/api/likes/albums/{id}", h.handleLikeAlbum)
|
|
r.Delete("/api/likes/albums/{id}", h.handleUnlikeAlbum)
|
|
r.Post("/api/likes/artists/{id}", h.handleLikeArtist)
|
|
r.Delete("/api/likes/artists/{id}", h.handleUnlikeArtist)
|
|
r.Get("/api/likes/tracks", h.handleListLikedTracks)
|
|
r.Get("/api/likes/albums", h.handleListLikedAlbums)
|
|
r.Get("/api/likes/artists", h.handleListLikedArtists)
|
|
r.Get("/api/likes/ids", h.handleGetLikedIDs)
|
|
return r
|
|
}
|
|
|
|
// 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)
|
|
// seedTrack files each title at its own path, so the compilation copy
|
|
// gets a title of its own; song_id is what makes it the same song.
|
|
copied := seedTrack(t, pool, best.ID, artist.ID, "Something (Best Of)", 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)
|
|
user := seedUser(t, pool, "alice", "x", false)
|
|
artist := seedArtist(t, pool, "X")
|
|
album := seedAlbum(t, pool, artist.ID, "X", 1990)
|
|
t1 := seedTrack(t, pool, album.ID, artist.ID, "First", 1, 100_000)
|
|
t2 := seedTrack(t, pool, album.ID, artist.ID, "Second", 2, 100_000)
|
|
|
|
// Like t1 then t2 — newest should come first.
|
|
_ = callLike(h, user, http.MethodPost, "/api/likes/tracks/"+uuidToString(t1.ID))
|
|
_ = callLike(h, user, http.MethodPost, "/api/likes/tracks/"+uuidToString(t2.ID))
|
|
|
|
w := callLike(h, user, http.MethodGet, "/api/likes/tracks?limit=10&offset=0")
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status = %d body=%s", w.Code, w.Body.String())
|
|
}
|
|
var resp struct {
|
|
Items []TrackRef `json:"items"`
|
|
Total int `json:"total"`
|
|
Limit int `json:"limit"`
|
|
Offset int `json:"offset"`
|
|
}
|
|
_ = json.Unmarshal(w.Body.Bytes(), &resp)
|
|
if resp.Total != 2 || len(resp.Items) != 2 {
|
|
t.Fatalf("shape: %+v", resp)
|
|
}
|
|
if resp.Items[0].Title != "Second" {
|
|
t.Errorf("first item = %q, want %q (most recent)", resp.Items[0].Title, "Second")
|
|
}
|
|
}
|
|
|
|
func TestGetLikedIDs_ReturnsAllThreeArrays(t *testing.T) {
|
|
h, pool := testHandlers(t)
|
|
truncateLibrary(t, pool)
|
|
user := seedUser(t, pool, "alice", "x", false)
|
|
artist := seedArtist(t, pool, "X")
|
|
album := seedAlbum(t, pool, artist.ID, "X", 1990)
|
|
track := seedTrack(t, pool, album.ID, artist.ID, "T", 1, 100_000)
|
|
|
|
_ = callLike(h, user, http.MethodPost, "/api/likes/tracks/"+uuidToString(track.ID))
|
|
_ = callLike(h, user, http.MethodPost, "/api/likes/albums/"+uuidToString(album.ID))
|
|
_ = callLike(h, user, http.MethodPost, "/api/likes/artists/"+uuidToString(artist.ID))
|
|
|
|
w := callLike(h, user, http.MethodGet, "/api/likes/ids")
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status = %d", w.Code)
|
|
}
|
|
var resp struct {
|
|
TrackIDs []string `json:"track_ids"`
|
|
AlbumIDs []string `json:"album_ids"`
|
|
ArtistIDs []string `json:"artist_ids"`
|
|
}
|
|
_ = json.Unmarshal(w.Body.Bytes(), &resp)
|
|
if len(resp.TrackIDs) != 1 || len(resp.AlbumIDs) != 1 || len(resp.ArtistIDs) != 1 {
|
|
t.Errorf("ids = %+v", resp)
|
|
}
|
|
}
|
|
|
|
func TestGetLikedIDs_CrossUserIsolation(t *testing.T) {
|
|
h, pool := testHandlers(t)
|
|
truncateLibrary(t, pool)
|
|
alice := seedUser(t, pool, "alice", "x", false)
|
|
bob := seedUser(t, pool, "bob", "x", false)
|
|
artist := seedArtist(t, pool, "X")
|
|
album := seedAlbum(t, pool, artist.ID, "X", 1990)
|
|
track := seedTrack(t, pool, album.ID, artist.ID, "T", 1, 100_000)
|
|
|
|
// Alice likes the track.
|
|
_ = callLike(h, alice, http.MethodPost, "/api/likes/tracks/"+uuidToString(track.ID))
|
|
// Bob queries his ids — should be empty.
|
|
w := callLike(h, bob, http.MethodGet, "/api/likes/ids")
|
|
var resp struct {
|
|
TrackIDs []string `json:"track_ids"`
|
|
}
|
|
_ = json.Unmarshal(w.Body.Bytes(), &resp)
|
|
if len(resp.TrackIDs) != 0 {
|
|
t.Errorf("bob's track_ids should be empty, got %v", resp.TrackIDs)
|
|
}
|
|
}
|
|
|
|
func TestLikeAlbumAndArtist_HappyPath(t *testing.T) {
|
|
h, pool := testHandlers(t)
|
|
truncateLibrary(t, pool)
|
|
user := seedUser(t, pool, "alice", "x", false)
|
|
artist := seedArtist(t, pool, "X")
|
|
album := seedAlbum(t, pool, artist.ID, "X", 1990)
|
|
|
|
w := callLike(h, user, http.MethodPost, "/api/likes/albums/"+uuidToString(album.ID))
|
|
if w.Code != http.StatusNoContent {
|
|
t.Errorf("album: status = %d", w.Code)
|
|
}
|
|
w = callLike(h, user, http.MethodPost, "/api/likes/artists/"+uuidToString(artist.ID))
|
|
if w.Code != http.StatusNoContent {
|
|
t.Errorf("artist: status = %d", w.Code)
|
|
}
|
|
w = callLike(h, user, http.MethodGet, "/api/likes/albums?limit=10")
|
|
var resp struct {
|
|
Total int `json:"total"`
|
|
}
|
|
_ = json.Unmarshal(w.Body.Bytes(), &resp)
|
|
if resp.Total != 1 {
|
|
t.Errorf("album total = %d", resp.Total)
|
|
}
|
|
}
|
|
|
|
func TestLikeTrack_DuringOpenPlayEvent_WritesContextualLike(t *testing.T) {
|
|
h, pool := testHandlers(t)
|
|
truncateLibrary(t, pool)
|
|
user := seedUser(t, pool, "alice", "x", false)
|
|
artist := seedArtist(t, pool, "X")
|
|
album := seedAlbum(t, pool, artist.ID, "X", 1990)
|
|
playing := seedTrack(t, pool, album.ID, artist.ID, "Now Playing", 1, 100_000)
|
|
other := seedTrack(t, pool, album.ID, artist.ID, "Other", 2, 100_000)
|
|
|
|
// Simulate a play_started: insert via the writer.
|
|
w := callEvents(h, user, []byte(`{"type":"play_started","track_id":"`+uuidToString(playing.ID)+`"}`))
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("play_started failed: %d %s", w.Code, w.Body.String())
|
|
}
|
|
// Like the OTHER track. With an open play_event for `playing`,
|
|
// the contextual_likes row should reference `other` and the playing
|
|
// track's session vector.
|
|
if r := callLike(h, user, http.MethodPost, "/api/likes/tracks/"+uuidToString(other.ID)); r.Code != http.StatusOK {
|
|
t.Fatalf("like: %d", r.Code)
|
|
}
|
|
var count int
|
|
_ = pool.QueryRow(context.Background(),
|
|
"SELECT count(*) FROM contextual_likes WHERE user_id=$1 AND track_id=$2", user.ID, other.ID).Scan(&count)
|
|
if count != 1 {
|
|
t.Errorf("contextual_likes count = %d, want 1", count)
|
|
}
|
|
}
|
|
|
|
func TestLikeTrack_NoOpenPlayEvent_NoContextualLike(t *testing.T) {
|
|
h, pool := testHandlers(t)
|
|
truncateLibrary(t, pool)
|
|
user := seedUser(t, pool, "alice", "x", false)
|
|
artist := seedArtist(t, pool, "X")
|
|
album := seedAlbum(t, pool, artist.ID, "X", 1990)
|
|
track := seedTrack(t, pool, album.ID, artist.ID, "T", 1, 100_000)
|
|
|
|
if r := callLike(h, user, http.MethodPost, "/api/likes/tracks/"+uuidToString(track.ID)); r.Code != http.StatusOK {
|
|
t.Fatalf("like: %d", r.Code)
|
|
}
|
|
var count int
|
|
_ = pool.QueryRow(context.Background(),
|
|
"SELECT count(*) FROM contextual_likes WHERE user_id=$1", user.ID).Scan(&count)
|
|
if count != 0 {
|
|
t.Errorf("contextual_likes count = %d, want 0 (no open play)", count)
|
|
}
|
|
}
|
|
|
|
// Liking and unliking a track can be repeated safely. Unliking a track that
|
|
// was never liked is fine. A second like adds no row. An unlike removes the
|
|
// like and closes, but keeps, the contextual like written during the open
|
|
// play. Liking again opens a new contextual like, so the history grows.
|
|
func TestLikeTrack_LikeAndUnlikeRepeatSafelyAndKeepTheHistory(t *testing.T) {
|
|
h, pool := testHandlers(t)
|
|
truncateLibrary(t, pool)
|
|
user := seedUser(t, pool, "alice", "x", false)
|
|
artist := seedArtist(t, pool, "X")
|
|
album := seedAlbum(t, pool, artist.ID, "X", 1990)
|
|
playing := seedTrack(t, pool, album.ID, artist.ID, "Playing", 1, 100_000)
|
|
liked := seedTrack(t, pool, album.ID, artist.ID, "Liked", 2, 100_000)
|
|
path := "/api/likes/tracks/" + uuidToString(liked.ID)
|
|
|
|
count := func(query string) int {
|
|
t.Helper()
|
|
var n int
|
|
if err := pool.QueryRow(context.Background(), query, user.ID, liked.ID).Scan(&n); err != nil {
|
|
t.Fatalf("%s: %v", query, err)
|
|
}
|
|
return n
|
|
}
|
|
const (
|
|
general = "SELECT count(*) FROM general_likes WHERE user_id=$1 AND track_id=$2"
|
|
ctxAll = "SELECT count(*) FROM contextual_likes WHERE user_id=$1 AND track_id=$2"
|
|
ctxOpen = "SELECT count(*) FROM contextual_likes WHERE user_id=$1 AND track_id=$2 AND deleted_at IS NULL"
|
|
)
|
|
_ = callEvents(h, user, []byte(`{"type":"play_started","track_id":"`+uuidToString(playing.ID)+`"}`))
|
|
|
|
for _, step := range []struct {
|
|
method string
|
|
general, ctxAll, ctxOpen int
|
|
}{
|
|
{http.MethodDelete, 0, 0, 0},
|
|
{http.MethodPost, 1, 1, 1},
|
|
{http.MethodPost, 1, 1, 1},
|
|
{http.MethodDelete, 0, 1, 0},
|
|
{http.MethodPost, 1, 2, 1},
|
|
} {
|
|
if w := callLike(h, user, step.method, path); w.Code != http.StatusOK {
|
|
t.Fatalf("%s: status %d body=%s", step.method, w.Code, w.Body.String())
|
|
}
|
|
g, a, o := count(general), count(ctxAll), count(ctxOpen)
|
|
if g != step.general || a != step.ctxAll || o != step.ctxOpen {
|
|
t.Fatalf("after %s: likes=%d contextual=%d open=%d, want %d %d %d",
|
|
step.method, g, a, o, step.general, step.ctxAll, step.ctxOpen)
|
|
}
|
|
}
|
|
}
|