from lyra_worker.adapters.fakes import FakeMbBrowser from lyra_worker.main import maybe_run_scan from lyra_worker.types import MBTarget class FakeProbe: def probe(self, path): from lyra_worker.probe import ProbeResult return ProbeResult(artist="", album="", fmt="FLAC", quality_class=3) class FakeResolver: def resolve(self, artist, album): return MBTarget(artist=artist, album=album, rg_mbid=f"rg-{album}", artist_mbid="a1") class RaisingProbe: def probe(self, path): raise RuntimeError("probe boom") def _set(conn, key, value): with conn.cursor() as cur: cur.execute( 'INSERT INTO "Config"(key, value, secret, "updatedAt") VALUES (%s, %s, false, now()) ' 'ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, "updatedAt" = now()', (key, value), ) conn.commit() def _get(conn, key): with conn.cursor() as cur: cur.execute('SELECT value FROM "Config" WHERE key = %s', (key,)) row = cur.fetchone() return row[0] if row else None def _album(tmp_path, artist, folder): d = tmp_path / artist / folder d.mkdir(parents=True) (d / "01.flac").write_bytes(b"") def _cfg(conn): from lyra_worker.config import get_config return get_config(conn) def test_scan_completes_in_one_chunk_writes_result_and_clears_flag(conn, tmp_path): _album(tmp_path, "John Mayer", "Continuum (2006)") _set(conn, "scan.requested", "true") # one album, default chunk size 25 -> starts + finishes in a single call maybe_run_scan(conn, FakeResolver(), FakeProbe(), FakeMbBrowser(), _cfg(conn), dest_root=str(tmp_path)) assert _get(conn, "scan.requested") == "false" # flag cleared assert _get(conn, "scan.inProgress") == "false" # scan finished assert "imported" in (_get(conn, "scan.result") or "") # summary written with conn.cursor() as cur: cur.execute('SELECT count(*) FROM "LibraryItem"') assert cur.fetchone()[0] == 1 def test_scan_chunks_across_iterations_and_totals_correctly(conn, tmp_path): for i in range(3): _album(tmp_path, "Artist", f"Album {i}") _set(conn, "scan.requested", "true") _set(conn, "scan.chunkSize", "2") # first call starts the scan and processes chunk 1 (2 albums), leaving it in progress maybe_run_scan(conn, FakeResolver(), FakeProbe(), FakeMbBrowser(), _cfg(conn), dest_root=str(tmp_path)) assert _get(conn, "scan.inProgress") == "true" assert _get(conn, "scan.requested") == "false" assert _get(conn, "scan.progress") == "2/0" # second call resumes from the cursor, finishes the last album maybe_run_scan(conn, FakeResolver(), FakeProbe(), FakeMbBrowser(), _cfg(conn), dest_root=str(tmp_path)) assert _get(conn, "scan.inProgress") == "false" assert _get(conn, "scan.result") == "imported 3, skipped 0" with conn.cursor() as cur: cur.execute('SELECT count(*) FROM "LibraryItem"') assert cur.fetchone()[0] == 3 def test_scan_chunk_exception_clears_worklist_not_just_flags(conn, tmp_path): # scan_chunk commits per item, so if it throws mid-chunk the except block must # still clear the worklist — otherwise the done + not-yet-processed ScanWorkItem # rows for this scan_id are orphaned forever (the next scan mints a fresh uuid). _album(tmp_path, "Artist", "Album") _set(conn, "scan.requested", "true") maybe_run_scan(conn, FakeResolver(), RaisingProbe(), FakeMbBrowser(), _cfg(conn), dest_root=str(tmp_path)) assert _get(conn, "scan.inProgress") == "false" with conn.cursor() as cur: cur.execute('SELECT count(*) FROM "ScanWorkItem"') assert cur.fetchone()[0] == 0 def test_maybe_run_scan_idle_when_not_requested_or_in_progress(conn, tmp_path): # no flags set -> no-op, no result written maybe_run_scan(conn, FakeResolver(), FakeProbe(), FakeMbBrowser(), _cfg(conn), dest_root=str(tmp_path)) assert _get(conn, "scan.result") is None assert _get(conn, "scan.inProgress") is None