Files
Lyra/worker/tests/test_monitor_reconcile.py
T
2026-07-11 12:29:26 +02:00

87 lines
3.3 KiB
Python

from lyra_worker.monitor import MonitorConfig, reconcile
from tests.conftest import insert_monitored_release
CFG = MonitorConfig(enabled=True, quality_cutoff=2)
def _make_job(conn, rel_id, artist, album, state):
with conn.cursor() as cur:
cur.execute(
'INSERT INTO "Request" (id, artist, album, status, "monitoredReleaseId", "createdAt") '
"VALUES (gen_random_uuid()::text, %s, %s, 'pending', %s, now()) RETURNING id",
(artist, album, rel_id),
)
req = cur.fetchone()[0]
cur.execute(
'INSERT INTO "Job" (id, "requestId", state, "currentStage", attempts, "createdAt", "updatedAt") '
"VALUES (gen_random_uuid()::text, %s, %s, 'import', 1, now(), now()) RETURNING id",
(req, state),
)
job = cur.fetchone()[0]
conn.commit()
return req, job
def _add_library(conn, req, artist, album, quality):
with conn.cursor() as cur:
cur.execute(
'INSERT INTO "LibraryItem" (id, "requestId", artist, album, path, source, format, '
'"qualityClass", "importedAt") '
"VALUES (gen_random_uuid()::text, %s, %s, %s, '/m', 'qobuz', 'FLAC', %s, now())",
(req, artist, album, quality),
)
conn.commit()
def _release(conn, rel_id):
with conn.cursor() as cur:
cur.execute(
'SELECT state, "currentQualityClass", "firstGrabbedAt" IS NOT NULL '
'FROM "MonitoredRelease" WHERE id = %s',
(rel_id,),
)
return cur.fetchone()
def test_imported_below_cutoff_becomes_grabbed(conn):
rel = insert_monitored_release(conn, artist_name="A", album="B", rg_mbid="rg-1",
monitored=True, state="wanted")
req, job = _make_job(conn, rel, "A", "B", "imported")
_add_library(conn, req, "A", "B", 1)
reconcile(conn, job, CFG)
assert _release(conn, rel) == ("grabbed", 1, True)
def test_imported_at_cutoff_becomes_fulfilled(conn):
rel = insert_monitored_release(conn, artist_name="A", album="B", rg_mbid="rg-1",
monitored=True, state="wanted")
req, job = _make_job(conn, rel, "A", "B", "imported")
_add_library(conn, req, "A", "B", 3)
reconcile(conn, job, CFG)
assert _release(conn, rel) == ("fulfilled", 3, True)
def test_needs_attention_leaves_wanted(conn):
rel = insert_monitored_release(conn, artist_name="A", album="B", rg_mbid="rg-1",
monitored=True, state="wanted")
_req, job = _make_job(conn, rel, "A", "B", "needs_attention")
reconcile(conn, job, CFG)
assert _release(conn, rel) == ("wanted", None, False)
def test_unlinked_job_is_ignored(conn):
with conn.cursor() as cur:
cur.execute(
'INSERT INTO "Request" (id, artist, album, status, "createdAt") '
"VALUES (gen_random_uuid()::text, 'A', 'B', 'completed', now()) RETURNING id"
)
req = cur.fetchone()[0]
cur.execute(
'INSERT INTO "Job" (id, "requestId", state, "currentStage", attempts, "createdAt", "updatedAt") '
"VALUES (gen_random_uuid()::text, %s, 'imported', 'import', 1, now(), now()) RETURNING id",
(req,),
)
job = cur.fetchone()[0]
conn.commit()
reconcile(conn, job, CFG) # no monitoredReleaseId → no-op, must not raise