test(api): merge repeated cases into tables, drop skips dbtest already makes (#8295)
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>
This commit is contained in:
2026-12-02 10:35:48 -05:00
co-authored by Claude Opus 5.5
parent 81cc29595b
commit 3fd3c1f7c6
23 changed files with 145 additions and 580 deletions
+38 -110
View File
@@ -38,44 +38,6 @@ func likesRouter(h *handlers) http.Handler {
return r
}
func TestLikeTrack_Idempotent(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)
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.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.StatusOK {
t.Errorf("second like (idempotent): 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("rows = %d, want 1", count)
}
}
func TestUnlikeTrack_IdempotentEvenWhenAbsent(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)
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.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) {
@@ -259,86 +221,52 @@ func TestLikeTrack_NoOpenPlayEvent_NoContextualLike(t *testing.T) {
}
}
func TestLikeTrack_RepeatedLike_NoDuplicate(t *testing.T) {
// 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)
other := seedTrack(t, pool, album.ID, artist.ID, "Other", 2, 100_000)
liked := seedTrack(t, pool, album.ID, artist.ID, "Liked", 2, 100_000)
path := "/api/likes/tracks/" + uuidToString(liked.ID)
// Open play.
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)+`"}`))
// First like — captures.
_ = callLike(h, user, http.MethodPost, "/api/likes/tracks/"+uuidToString(other.ID))
// Second like — :execrows=0, no contextual write.
_ = callLike(h, user, http.MethodPost, "/api/likes/tracks/"+uuidToString(other.ID))
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 (no duplicate from idempotent re-like)", count)
}
}
func TestUnlikeTrack_SoftDeletesContextualLikes(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, "P", 1, 100_000)
other := seedTrack(t, pool, album.ID, artist.ID, "O", 2, 100_000)
_ = callEvents(h, user, []byte(`{"type":"play_started","track_id":"`+uuidToString(playing.ID)+`"}`))
_ = callLike(h, user, http.MethodPost, "/api/likes/tracks/"+uuidToString(other.ID))
if r := callLike(h, user, http.MethodDelete, "/api/likes/tracks/"+uuidToString(other.ID)); r.Code != http.StatusOK {
t.Fatalf("unlike: %d", r.Code)
}
// general_likes deleted.
var glCount int
_ = pool.QueryRow(context.Background(),
"SELECT count(*) FROM general_likes WHERE user_id=$1 AND track_id=$2", user.ID, other.ID).Scan(&glCount)
if glCount != 0 {
t.Errorf("general_likes count = %d, want 0", glCount)
}
// contextual_likes row exists but deleted_at is set.
var deletedAtValid bool
_ = pool.QueryRow(context.Background(),
"SELECT deleted_at IS NOT NULL FROM contextual_likes WHERE user_id=$1 AND track_id=$2 LIMIT 1",
user.ID, other.ID).Scan(&deletedAtValid)
if !deletedAtValid {
t.Errorf("deleted_at not set after unlike")
}
}
func TestLikeUnlikeRelike_HistoryAccumulates(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, "P", 1, 100_000)
other := seedTrack(t, pool, album.ID, artist.ID, "O", 2, 100_000)
_ = callEvents(h, user, []byte(`{"type":"play_started","track_id":"`+uuidToString(playing.ID)+`"}`))
_ = callLike(h, user, http.MethodPost, "/api/likes/tracks/"+uuidToString(other.ID))
_ = callLike(h, user, http.MethodDelete, "/api/likes/tracks/"+uuidToString(other.ID))
// Re-like in same session — new row should be inserted.
_ = callLike(h, user, http.MethodPost, "/api/likes/tracks/"+uuidToString(other.ID))
var total, active int
_ = pool.QueryRow(context.Background(),
"SELECT count(*) FROM contextual_likes WHERE user_id=$1 AND track_id=$2", user.ID, other.ID).Scan(&total)
_ = pool.QueryRow(context.Background(),
"SELECT count(*) FROM contextual_likes WHERE user_id=$1 AND track_id=$2 AND deleted_at IS NULL",
user.ID, other.ID).Scan(&active)
if total != 2 {
t.Errorf("total = %d, want 2 (history accumulates)", total)
}
if active != 1 {
t.Errorf("active = %d, want 1", active)
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)
}
}
}