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.
This commit is contained in:
Alexandre Teixeira
2026-06-04 22:28:00 +01:00
committed by GitHub
parent 7ddc5eaef4
commit fb852bd62e

View File

@@ -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"]