From 9093230320febf29ff947cb4017323dab32cff2b Mon Sep 17 00:00:00 2001 From: byrongamatos Date: Sat, 11 Jul 2026 11:22:31 +0200 Subject: [PATCH] test: reset enrichment worker state between tests (CodeRabbit) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit lib/enrichment.py now owns the worker, and it stays imported for the whole session while the `server` fixtures pop-and-reimport `server` — so the cancel Event / status dict / caches would leak across tests, and a stale `_enrich_cancel` could short-circuit a later direct `_background_enrich()`. An autouse conftest fixture clears that process-global state before each test. Co-Authored-By: Claude Opus 4.8 (1M context) --- tests/conftest.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/conftest.py b/tests/conftest.py index 5294711..561e1e9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -9,6 +9,33 @@ import structlog _LOGGING_NAMES = ("feedBack", "uvicorn", "uvicorn.error", "uvicorn.access") +@pytest.fixture(autouse=True) +def _reset_enrichment_state(): + """Reset the enrichment worker's process-global state between tests. + + The `server` fixtures pop-and-reimport `server`, but `lib/enrichment.py` + (which now owns the worker) stays imported for the whole session, so its + module globals — the cancel Event, the status dict, the caches — would + otherwise leak across tests. A test that set `_enrich_cancel` (or a stale + `running` status) could silently short-circuit a later direct + `_background_enrich()` call. Clear it up front so each test starts clean. + """ + try: + import enrichment + except Exception: + yield + return + enrichment._enrich_cancel.clear() + enrichment._enrich_pending_pass = False + enrichment._enrich_status.update( + {"running": False, "processed": 0, "last_pass_at": None, + "total": 0, "matched": 0, "current": None}) + enrichment._enrich_last_fetch = 0.0 + enrichment._artist_alias_cache.clear() + enrichment._caa_index_locks.clear() + yield + + @pytest.fixture() def isolate_logging(): """Restore feedBack / uvicorn logger state after each test.