feat(lidarrrequests): request lifecycle service (create/list/approve/reject/cancel)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
package lidarrrequests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/lidarrconfig"
|
||||
)
|
||||
|
||||
func newPool(t *testing.T) *pgxpool.Pool {
|
||||
t.Helper()
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test in -short mode")
|
||||
}
|
||||
dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL")
|
||||
if dsn == "" {
|
||||
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
|
||||
}
|
||||
if err := db.Migrate(dsn, slog.New(slog.NewTextHandler(io.Discard, nil))); err != nil {
|
||||
t.Fatalf("migrate: %v", err)
|
||||
}
|
||||
pool, err := pgxpool.New(context.Background(), dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("pool: %v", err)
|
||||
}
|
||||
t.Cleanup(pool.Close)
|
||||
dbtest.ResetDB(t, pool)
|
||||
if _, err := pool.Exec(context.Background(),
|
||||
"DELETE FROM lidarr_requests; UPDATE lidarr_config SET enabled=false, base_url=NULL, api_key=NULL WHERE id=1",
|
||||
); err != nil {
|
||||
t.Fatalf("reset lidarr tables: %v", err)
|
||||
}
|
||||
return pool
|
||||
}
|
||||
|
||||
func seedUser(t *testing.T, pool *pgxpool.Pool) pgtype.UUID {
|
||||
t.Helper()
|
||||
u, err := dbq.New(pool).CreateUser(context.Background(), dbq.CreateUserParams{
|
||||
Username: dbtest.TestUserPrefix + "rqtester", PasswordHash: "x", ApiToken: "x", IsAdmin: false,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("seed user: %v", err)
|
||||
}
|
||||
return u.ID
|
||||
}
|
||||
|
||||
func TestCreate_HappyPath_Artist(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
user := seedUser(t, pool)
|
||||
svc := NewService(pool, lidarrconfig.New(pool), nil, nil)
|
||||
r, err := svc.Create(context.Background(), user, CreateParams{
|
||||
Kind: "artist",
|
||||
LidarrArtistMBID: "069b64b6-7884-4f6a-94cc-e4c1d6c87a01",
|
||||
ArtistName: "Boards of Canada",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Create: %v", err)
|
||||
}
|
||||
if r.Status != dbq.LidarrRequestStatusPending {
|
||||
t.Errorf("status = %v", r.Status)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreate_TrackKindRequiresAlbumMBID(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
user := seedUser(t, pool)
|
||||
svc := NewService(pool, lidarrconfig.New(pool), nil, nil)
|
||||
_, err := svc.Create(context.Background(), user, CreateParams{
|
||||
Kind: "track",
|
||||
LidarrArtistMBID: "a-mbid", ArtistName: "X",
|
||||
LidarrTrackMBID: "t-mbid", TrackTitle: "Y",
|
||||
// missing album fields
|
||||
})
|
||||
if !errors.Is(err, ErrInvalidKindFields) {
|
||||
t.Fatalf("err = %v, want ErrInvalidKindFields", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApprove_NotConfigured(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
user := seedUser(t, pool)
|
||||
svc := NewService(pool, lidarrconfig.New(pool), nil, nil)
|
||||
r, _ := svc.Create(context.Background(), user, CreateParams{
|
||||
Kind: "artist", LidarrArtistMBID: "a-mbid", ArtistName: "X",
|
||||
})
|
||||
_, err := svc.Approve(context.Background(), r.ID, user, ApproveOverrides{})
|
||||
if !errors.Is(err, ErrLidarrDisabled) {
|
||||
t.Fatalf("err = %v, want ErrLidarrDisabled", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReject_TransitionsToRejected(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
user := seedUser(t, pool)
|
||||
svc := NewService(pool, lidarrconfig.New(pool), nil, nil)
|
||||
r, _ := svc.Create(context.Background(), user, CreateParams{
|
||||
Kind: "artist", LidarrArtistMBID: "a-mbid", ArtistName: "X",
|
||||
})
|
||||
rejected, err := svc.Reject(context.Background(), r.ID, user, "low quality")
|
||||
if err != nil {
|
||||
t.Fatalf("Reject: %v", err)
|
||||
}
|
||||
if rejected.Status != dbq.LidarrRequestStatusRejected {
|
||||
t.Errorf("status = %v", rejected.Status)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReject_AlreadyRejectedReturnsErrNotPending(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
user := seedUser(t, pool)
|
||||
svc := NewService(pool, lidarrconfig.New(pool), nil, nil)
|
||||
r, _ := svc.Create(context.Background(), user, CreateParams{
|
||||
Kind: "artist", LidarrArtistMBID: "a-mbid", ArtistName: "X",
|
||||
})
|
||||
_, _ = svc.Reject(context.Background(), r.ID, user, "first")
|
||||
_, err := svc.Reject(context.Background(), r.ID, user, "second")
|
||||
if !errors.Is(err, ErrNotPending) {
|
||||
t.Fatalf("err = %v, want ErrNotPending", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCancel_OwnPendingOnly(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
user := seedUser(t, pool)
|
||||
svc := NewService(pool, lidarrconfig.New(pool), nil, nil)
|
||||
r, _ := svc.Create(context.Background(), user, CreateParams{
|
||||
Kind: "artist", LidarrArtistMBID: "a-mbid", ArtistName: "X",
|
||||
})
|
||||
if _, err := svc.Cancel(context.Background(), r.ID, user); err != nil {
|
||||
t.Fatalf("Cancel: %v", err)
|
||||
}
|
||||
// Second cancel hits "not pending" because we just rejected it.
|
||||
if _, err := svc.Cancel(context.Background(), r.ID, user); !errors.Is(err, ErrNotPending) {
|
||||
t.Errorf("err = %v, want ErrNotPending", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user