Files
minstrel/internal/api/playlists_discover_refresh_test.go
T
bvandeusen 0ba31c7816 feat(server/playlists): manual Discover refresh endpoint
POST /api/playlists/system/discover/refresh re-runs the system
playlist build synchronously for the calling user, then returns
their newly-built Discover playlist's UUID and track count.

Used by the frontend's "Refresh" affordances (next task) on the
Discover detail page header and the home Playlists row tile kebab.
Operator's escape hatch when they want a different randomness
without waiting for tomorrow's cron tick (e.g., after pasting a
Last.fm key, after the daily cover-art enrichment expanded their
playable library, or just for the heck of it).

Authenticated user only; the authed sub-router's RequireUser
middleware handles 401. Each user refreshes only their own
Discover — no admin route, no cross-user impersonation.

Adds GetSystemPlaylistByVariantForUser sqlc query for the response
lookup. Returns playlist_id=null and track_count=0 if the build
succeeded but the user's library yielded no eligible tracks
(degenerate empty-library).
2026-05-07 08:39:26 -04:00

65 lines
1.8 KiB
Go

package api
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/go-chi/chi/v5"
"git.fabledsword.com/bvandeusen/minstrel/internal/auth"
)
func newDiscoverRefreshRouter(h *handlers) chi.Router {
r := chi.NewRouter()
r.Route("/api", func(api chi.Router) {
api.Use(auth.RequireUser(h.pool))
api.Post("/playlists/system/discover/refresh", h.handleDiscoverRefresh)
})
return r
}
func TestDiscoverRefresh_AuthenticatedUser_Returns200(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, pool := testHandlers(t)
user := seedUser(t, pool, "discrefresh", "pw", false)
req := httptest.NewRequest(http.MethodPost, "/api/playlists/system/discover/refresh", nil)
req = withUser(req, user)
rec := httptest.NewRecorder()
newDiscoverRefreshRouter(h).ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200; body=%s", rec.Code, rec.Body.String())
}
var resp discoverRefreshResp
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
t.Fatalf("decode: %v", err)
}
// On a fresh user with no library, the build runs and emits a
// Discover playlist (or no row if library is truly empty).
// Either case is OK; we only assert the response shape decoded.
if resp.TrackCount < 0 {
t.Errorf("track_count = %d, want >= 0", resp.TrackCount)
}
}
func TestDiscoverRefresh_NoAuth_Returns401(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, _ := testHandlers(t)
req := httptest.NewRequest(http.MethodPost, "/api/playlists/system/discover/refresh", nil)
rec := httptest.NewRecorder()
newDiscoverRefreshRouter(h).ServeHTTP(rec, req)
if rec.Code != http.StatusUnauthorized {
t.Errorf("status = %d, want 401", rec.Code)
}
}