sonda/internal/storage/lidarr_requests_test.go
tgiachi a880b87a9e chore: move the module path to git.kmalloc.org/squid/sonda
Migrating hosting from Codeberg to git.kmalloc.org. Renames the
module path in go.mod, every internal import, and the OCI image
source label in the Dockerfile. Build and full test suite verified
green after the rename.
2026-11-15 17:30:29 +01:00

136 lines
5.3 KiB
Go

package storage
import (
"context"
"errors"
"testing"
"time"
"github.com/uptrace/bun"
"git.kmalloc.org/squid/sonda/internal/providers/lidarr"
)
func TestLidarrRequestsAreKeptOnceAndSayWhenMusicArrives(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
users := NewUserRepo(db)
alice, err := users.CreateUser(ctx, "alice", "h", true)
if err != nil {
t.Fatal(err)
}
bob, err := users.CreateUser(ctx, "bob", "h", true)
if err != nil {
t.Fatal(err)
}
lib, err := NewLibraryRepo(db).CreateLibrary(ctx, "music", "/music")
if err != nil {
t.Fatal(err)
}
// Two artists named by a provider, with no music: one with an id, one without.
var chet, sade int64
if err := db.QueryRow("INSERT INTO artists (mbid, name, name_key, search_key, discovered) VALUES ('11111111-1111-1111-1111-111111111111', 'Chet Baker', 'chet baker', ?, ?) RETURNING id", SearchKey("Chet Baker"), true).Scan(&chet); err != nil {
t.Fatal(err)
}
if err := db.QueryRow("INSERT INTO artists (name, name_key, search_key, discovered) VALUES ('Sade', 'sade', ?, ?) RETURNING id", SearchKey("Sade"), true).Scan(&sade); err != nil {
t.Fatal(err)
}
repo := NewLidarrRequestRepo(db)
t0 := time.Unix(1_000_000, 0)
if err := repo.Record(ctx, alice.ID, chet, t0); err != nil {
t.Fatal(err)
}
if err := repo.Record(ctx, alice.ID, sade, t0.Add(time.Hour)); err != nil {
t.Fatal(err)
}
if err := repo.Record(ctx, alice.ID, chet, t0.Add(2*time.Hour)); err != nil { // sent again: the first date stays
t.Fatal(err)
}
if err := repo.Record(ctx, bob.ID, sade, t0); err != nil {
t.Fatal(err)
}
if err := repo.Record(ctx, alice.ID, 999999, t0); !errors.Is(err, lidarr.ErrNoSuchArtist) {
t.Errorf("an artist that does not exist: %v, want ErrNoSuchArtist", err)
}
got, err := repo.List(ctx, alice.ID, "", 50, 0, t0.Add(3*time.Hour))
if err != nil {
t.Fatal(err)
}
if got.Total != 2 || got.Waiting != 2 || got.Arrived != 0 || len(got.Items) != 2 {
t.Fatalf("list = %+v, want her two, both waiting", got)
}
if got.Items[0].Name != "Sade" || got.Items[1].Name != "Chet Baker" || got.Items[1].RequestedAt.Unix() != t0.Unix() || got.Items[1].MBID == "" || got.Items[0].MBID != "" {
t.Errorf("items = %+v, want newest first, the first date kept and the id when there is one", got.Items)
}
if got.Items[0].Status() != lidarr.Waiting {
t.Errorf("status = %q, want waiting", got.Items[0].Status())
}
// Music of Chet Baker is scanned in: the artist of the request has a file now.
affinitySongBy(t, db, lib.ID, "a.flac", "Almost Blue", "Chet Baker")
arrival := t0.Add(4 * time.Hour)
got, err = repo.List(ctx, alice.ID, "", 50, 0, arrival)
if err != nil {
t.Fatal(err)
}
if got.Waiting != 1 || got.Arrived != 1 {
t.Fatalf("counts = %d waiting, %d arrived, want 1 and 1", got.Waiting, got.Arrived)
}
var chetItem lidarr.Request
for _, it := range got.Items {
if it.Name == "Chet Baker" {
chetItem = it
}
}
if chetItem.Status() != lidarr.Arrived || chetItem.ArrivedAt.Unix() != arrival.Unix() {
t.Errorf("Chet Baker = %+v, want arrived now", chetItem)
}
// The day it arrived does not move on later reads.
again, _ := repo.List(ctx, alice.ID, lidarr.Arrived, 50, 0, arrival.Add(24*time.Hour))
if again.Total != 1 || again.Items[0].ArrivedAt.Unix() != arrival.Unix() {
t.Errorf("arrived = %+v, want the day kept", again)
}
waiting, _ := repo.List(ctx, alice.ID, lidarr.Waiting, 50, 0, arrival)
if waiting.Total != 1 || waiting.Items[0].Name != "Sade" {
t.Errorf("waiting = %+v, want Sade", waiting)
}
// Music that a scan made as another artist of the same name also counts.
affinitySongBy(t, db, lib.ID, "b.flac", "Smooth Operator", "SADE")
both, _ := repo.List(ctx, alice.ID, "", 50, 0, arrival.Add(time.Hour))
if both.Waiting != 0 || both.Arrived != 2 {
t.Errorf("counts = %d waiting, %d arrived, want all arrived: %+v", both.Waiting, both.Arrived, both.Items)
}
// A page, and past the end.
page, _ := repo.List(ctx, alice.ID, "", 1, 1, arrival)
if len(page.Items) != 1 || page.Total != 2 || page.Items[0].Name != "Chet Baker" {
t.Errorf("second page = %+v", page)
}
if past, _ := repo.List(ctx, alice.ID, "", 10, 5, arrival); len(past.Items) != 0 || past.Total != 2 {
t.Errorf("past the end = %+v", past)
}
// Bob's request is his own and still waiting for himself until he looks: the list of one never shows another's.
mine, _ := repo.List(ctx, bob.ID, "", 50, 0, arrival)
if mine.Total != 1 || mine.Items[0].Name != "Sade" {
t.Errorf("bob's = %+v, want his one", mine)
}
// An artist removed from the server leaves the request, with its name.
var ghost int64
if err := db.QueryRow("INSERT INTO artists (name, name_key, search_key, discovered) VALUES ('Ghost', 'ghost', ?, ?) RETURNING id", SearchKey("Ghost"), true).Scan(&ghost); err != nil {
t.Fatal(err)
}
if err := repo.Record(ctx, bob.ID, ghost, t0); err != nil {
t.Fatal(err)
}
if _, err := db.ExecContext(ctx, "DELETE FROM artists WHERE id = ?", ghost); err != nil {
t.Fatal(err)
}
kept, _ := repo.List(ctx, bob.ID, lidarr.Waiting, 50, 0, arrival)
if len(kept.Items) != 1 || kept.Items[0].Name != "Ghost" || kept.Items[0].ArtistID != 0 {
t.Errorf("after the artist is removed: %+v, want the request with no artist id", kept.Items)
}
})
}