test(lidarrrequests): expand coverage on Service.Approve + Reconciler

- Adds Service tests for ListPending/ListByStatus/ListForUser, the album-
  and unknown-kind validation branches, and an Approve happy path with a
  stub Lidarr server that exercises both default-snapshot and override-
  snapshot persistence.
- Adds Reconciler tests for Run (short tick + cancel), the album-not-yet-
  in-library no-op branch, and the track-kind 'parent album present but no
  tracks yet' branch.

Combined coverage on internal/lidarr*, lidarrconfig, lidarrrequests now
86.5% (lidarrrequests 81.5%), meeting the per-package >=80% target in
spec section 8.
This commit is contained in:
2026-04-30 10:58:21 -04:00
parent ab9be40bb2
commit db37deb6f8
2 changed files with 302 additions and 0 deletions
@@ -5,6 +5,7 @@ import (
"io"
"log/slog"
"testing"
"time"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
@@ -325,3 +326,118 @@ func TestReconciler_DisabledIsNoOp(t *testing.T) {
t.Errorf("status = %v, want approved — reconciler should no-op when disabled", got.Status)
}
}
// TestReconciler_RunRunsAtLeastOneTick exercises Run's goroutine loop with a
// short tick, verifies it picks up an approved row and transitions it to
// completed before the test cancels the context.
func TestReconciler_RunRunsAtLeastOneTick(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
pool := newPool(t)
enableLidarrForPool(t, pool)
q := dbq.New(pool)
user := seedUser(t, pool)
const mbid = "run-mbid"
seedArtist(t, q, "Run Artist", mbid)
req := seedApprovedRequestDirect(t, q, user, CreateParams{
Kind: "artist", LidarrArtistMBID: mbid, ArtistName: "Run Artist",
})
rec := NewReconciler(pool, lidarrconfig.New(pool), newTestLogger())
rec.tick = 25 * time.Millisecond
done := make(chan struct{})
go func() {
rec.Run(ctx)
close(done)
}()
// Poll the row up to ~2s for the reconciler to flip it. Beats sleeping
// a fixed amount on slow CI.
deadline := time.Now().Add(2 * time.Second)
for {
got, err := q.GetLidarrRequestByID(ctx, req.ID)
if err == nil && got.Status == dbq.LidarrRequestStatusCompleted {
break
}
if time.Now().After(deadline) {
t.Fatalf("status not completed within 2s; last err=%v", err)
}
time.Sleep(10 * time.Millisecond)
}
cancel()
select {
case <-done:
case <-time.After(time.Second):
t.Fatal("Run did not exit within 1s of cancel")
}
}
// TestReconciler_TrackKindNoTracksInAlbumYet: the parent album exists in the
// library but no track rows have been ingested yet. Reconciler should leave
// the row approved so a subsequent tick can pick it up once tracks land.
func TestReconciler_TrackKindNoTracksInAlbumYet(t *testing.T) {
pool := newPool(t)
q := dbq.New(pool)
ctx := context.Background()
enableLidarrForPool(t, pool)
user := seedUser(t, pool)
artistMBID := "ar-mbid-tracks-pending"
albumMBID := "al-mbid-tracks-pending"
artist := seedArtist(t, q, "Pending Tracks Artist", artistMBID)
_ = seedAlbum(t, q, artist.ID, "Album Without Tracks", albumMBID)
// No tracks seeded.
req := seedApprovedRequestDirect(t, q, user, CreateParams{
Kind: "track", LidarrArtistMBID: artistMBID, ArtistName: "Pending Tracks Artist",
LidarrAlbumMBID: albumMBID, AlbumTitle: "Album Without Tracks",
LidarrTrackMBID: "t-mbid-not-yet", TrackTitle: "Some Track",
})
rec := NewReconciler(pool, lidarrconfig.New(pool), newTestLogger())
if err := rec.tickOnce(ctx); err != nil {
t.Fatalf("tickOnce: %v", err)
}
got, err := q.GetLidarrRequestByID(ctx, req.ID)
if err != nil {
t.Fatalf("GetLidarrRequestByID: %v", err)
}
if got.Status != dbq.LidarrRequestStatusApproved {
t.Errorf("status = %v, want approved (album present, no tracks yet)", got.Status)
}
}
// TestReconciler_AlbumKindNoMatchInAlbumsTable exercises the album branch's
// no-rows return path when the request's album_mbid isn't in the library yet.
func TestReconciler_AlbumKindNoMatchInAlbumsTable(t *testing.T) {
pool := newPool(t)
q := dbq.New(pool)
ctx := context.Background()
enableLidarrForPool(t, pool)
user := seedUser(t, pool)
req := seedApprovedRequestDirect(t, q, user, CreateParams{
Kind: "album", LidarrArtistMBID: "ar-no-match", ArtistName: "Nope Artist",
LidarrAlbumMBID: "al-not-in-library", AlbumTitle: "Nope Album",
})
rec := NewReconciler(pool, lidarrconfig.New(pool), newTestLogger())
if err := rec.tickOnce(ctx); err != nil {
t.Fatalf("tickOnce: %v", err)
}
got, err := q.GetLidarrRequestByID(ctx, req.ID)
if err != nil {
t.Fatalf("GetLidarrRequestByID: %v", err)
}
if got.Status != dbq.LidarrRequestStatusApproved {
t.Errorf("status = %v, want approved (album not yet in library)", got.Status)
}
}