From fb852bd62e58862aa468f7eb9a4eb9c7ba039c80 Mon Sep 17 00:00:00 2001 From: Alexandre Teixeira <111787685+alteixeira20@users.noreply.github.com> Date: Thu, 4 Jun 2026 22:28:00 +0100 Subject: [PATCH] fix(tests): restore webhook manager after review test import Restores src.webhook_manager after a review-regression test imports it against a fake src.database. Fixes one focused #2580 CI-baseline pollution bucket. --- tests/test_review_regressions.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/test_review_regressions.py b/tests/test_review_regressions.py index 742fb4f..747867e 100644 --- a/tests/test_review_regressions.py +++ b/tests/test_review_regressions.py @@ -484,7 +484,25 @@ async def test_webhook_tool_reuses_private_url_validation(): fake_src_db = types.ModuleType("src.database") fake_src_db.SessionLocal = fake_core_db.SessionLocal fake_src_db.Webhook = object + # Importing do_manage_webhooks below re-executes src.webhook_manager bound to + # the faked src.database, whose Webhook is plain `object`. Save BOTH the + # sys.modules entry AND the parent-package attribute (src.webhook_manager) so + # the real module can be restored afterwards. Without this the polluted + # module leaks into the cache and breaks sibling tests that call + # WebhookManager._deliver (which evaluates `Webhook.id == webhook_id`). + _ABSENT = object() + _wm_saved_module = sys.modules.get("src.webhook_manager", _ABSENT) + _src_pkg = sys.modules.get("src") + _wm_saved_attr = ( + getattr(_src_pkg, "webhook_manager", _ABSENT) if _src_pkg is not None else _ABSENT + ) + + # Drop both bindings so the import re-executes against the fake src.database, + # still exercising the intended import path. sys.modules.pop("src.webhook_manager", None) + if _src_pkg is not None and hasattr(_src_pkg, "webhook_manager"): + delattr(_src_pkg, "webhook_manager") + monkeypatch = pytest.MonkeyPatch() monkeypatch.setitem(sys.modules, "core.database", fake_core_db) monkeypatch.setitem(sys.modules, "src.database", fake_src_db) @@ -498,6 +516,18 @@ async def test_webhook_tool_reuses_private_url_validation(): ) finally: monkeypatch.undo() + # Restore src.webhook_manager to its exact pre-test state at BOTH the + # sys.modules and parent-package attribute level. + if _wm_saved_module is _ABSENT: + sys.modules.pop("src.webhook_manager", None) + else: + sys.modules["src.webhook_manager"] = _wm_saved_module + if _src_pkg is not None: + if _wm_saved_attr is _ABSENT: + if hasattr(_src_pkg, "webhook_manager"): + delattr(_src_pkg, "webhook_manager") + else: + setattr(_src_pkg, "webhook_manager", _wm_saved_attr) assert result["exit_code"] == 1 assert "private/internal" in result["error"]