feat: replace fake pipeline with staged adapter-driven pipeline
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,20 +1,101 @@
|
||||
from lyra_worker.adapters.fakes import FakeQobuz, FakeSoulseek, FakeYouTube, FailingAdapter
|
||||
from lyra_worker.claim import claim_next
|
||||
from lyra_worker.pipeline import run_pipeline
|
||||
from tests.conftest import insert_request
|
||||
|
||||
|
||||
def test_pipeline_drives_job_to_imported(conn):
|
||||
job_id = insert_request(conn)
|
||||
claim_next(conn)
|
||||
|
||||
run_pipeline(conn, job_id, stage_delay=0.0)
|
||||
|
||||
def _job_state(conn, job_id):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute('SELECT state, "currentStage" FROM "Job" WHERE id = %s', (job_id,))
|
||||
state, stage = cur.fetchone()
|
||||
cur.execute('SELECT status FROM "Request" WHERE id = (SELECT "requestId" FROM "Job" WHERE id = %s)', (job_id,))
|
||||
req_status = cur.fetchone()[0]
|
||||
return cur.fetchone()
|
||||
|
||||
assert state == "imported"
|
||||
assert stage == "import"
|
||||
assert req_status == "completed"
|
||||
|
||||
def _request_status(conn, job_id):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
'SELECT status FROM "Request" WHERE id = (SELECT "requestId" FROM "Job" WHERE id = %s)',
|
||||
(job_id,),
|
||||
)
|
||||
return cur.fetchone()[0]
|
||||
|
||||
|
||||
def test_success_picks_best_source_and_imports(conn):
|
||||
job_id = insert_request(conn, artist="Radiohead", album="In Rainbows")
|
||||
claim_next(conn)
|
||||
run_pipeline(conn, job_id, [FakeYouTube(), FakeSoulseek(), FakeQobuz()], dest_root="/tmp/lib")
|
||||
|
||||
assert _job_state(conn, job_id) == ("imported", "import")
|
||||
assert _request_status(conn, job_id) == "completed"
|
||||
with conn.cursor() as cur:
|
||||
cur.execute('SELECT source FROM "Candidate" WHERE "jobId" = %s AND chosen = true', (job_id,))
|
||||
assert cur.fetchone()[0] == "qobuz" # highest quality won
|
||||
cur.execute('SELECT source, format FROM "LibraryItem" WHERE artist = %s', ("Radiohead",))
|
||||
row = cur.fetchone()
|
||||
assert row[0] == "qobuz"
|
||||
|
||||
|
||||
def test_falls_through_when_best_download_fails(conn):
|
||||
job_id = insert_request(conn, artist="Radiohead", album="In Rainbows")
|
||||
claim_next(conn)
|
||||
# FailingAdapter is source 'qobuz' (tier 0) whose download fails; Soulseek should win.
|
||||
run_pipeline(conn, job_id, [FailingAdapter(), FakeSoulseek()], dest_root="/tmp/lib")
|
||||
|
||||
assert _job_state(conn, job_id) == ("imported", "import")
|
||||
with conn.cursor() as cur:
|
||||
cur.execute('SELECT source FROM "Candidate" WHERE "jobId" = %s AND chosen = true', (job_id,))
|
||||
assert cur.fetchone()[0] == "soulseek"
|
||||
|
||||
|
||||
def test_no_match_goes_to_needs_attention(conn):
|
||||
job_id = insert_request(conn, artist="Radiohead", album="In Rainbows")
|
||||
claim_next(conn)
|
||||
run_pipeline(conn, job_id, [FakeQobuz(matches=False)], dest_root="/tmp/lib")
|
||||
|
||||
assert _job_state(conn, job_id)[0] == "needs_attention"
|
||||
assert _request_status(conn, job_id) == "needs_attention"
|
||||
|
||||
|
||||
def test_all_downloads_fail_goes_to_needs_attention(conn):
|
||||
job_id = insert_request(conn, artist="Radiohead", album="In Rainbows")
|
||||
claim_next(conn)
|
||||
run_pipeline(conn, job_id, [FailingAdapter()], dest_root="/tmp/lib")
|
||||
|
||||
assert _job_state(conn, job_id)[0] == "needs_attention"
|
||||
|
||||
|
||||
def test_incomplete_download_goes_to_needs_attention(conn):
|
||||
from lyra_worker.adapters.fakes import FakeQobuz
|
||||
from lyra_worker.types import DownloadResult
|
||||
|
||||
class TruncatingQobuz(FakeQobuz):
|
||||
def download(self, candidate, dest, on_progress):
|
||||
on_progress(1.0)
|
||||
# reports fewer tracks than the candidate promised -> integrity check fails
|
||||
return DownloadResult(ok=True, path=dest, track_count=candidate.track_count - 1)
|
||||
|
||||
job_id = insert_request(conn, artist="Radiohead", album="In Rainbows")
|
||||
claim_next(conn)
|
||||
run_pipeline(conn, job_id, [TruncatingQobuz()], dest_root="/tmp/lib")
|
||||
|
||||
assert _job_state(conn, job_id)[0] == "needs_attention"
|
||||
assert _request_status(conn, job_id) == "needs_attention"
|
||||
|
||||
|
||||
def test_dedupe_skips_already_in_library(conn):
|
||||
# First acquisition
|
||||
job1 = insert_request(conn, artist="Radiohead", album="In Rainbows")
|
||||
claim_next(conn)
|
||||
run_pipeline(conn, job1, [FakeQobuz()], dest_root="/tmp/lib")
|
||||
# Second request for the same album
|
||||
job2 = insert_request(conn, artist="Radiohead", album="In Rainbows")
|
||||
claim_next(conn)
|
||||
run_pipeline(conn, job2, [FakeQobuz()], dest_root="/tmp/lib")
|
||||
|
||||
assert _job_state(conn, job2) == ("imported", "import")
|
||||
with conn.cursor() as cur:
|
||||
# dedupe path creates no candidates for the second job
|
||||
cur.execute('SELECT count(*) FROM "Candidate" WHERE "jobId" = %s', (job2,))
|
||||
assert cur.fetchone()[0] == 0
|
||||
cur.execute('SELECT count(*) FROM "LibraryItem" WHERE artist = %s AND album = %s',
|
||||
("Radiohead", "In Rainbows"))
|
||||
assert cur.fetchone()[0] == 1 # not duplicated
|
||||
|
||||
Reference in New Issue
Block a user