Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

test: stop leaking sys.modules stubs across test modules (#2762)

Call White34a67ef67028
diff --git a/tests/test_feishu_event_dispatcher.py b/tests/test_feishu_event_dispatcher.py
index 20914cd0f..4511c9a8b 100644
--- a/tests/test_feishu_event_dispatcher.py
+++ b/tests/test_feishu_event_dispatcher.py
@@ -1,4 +1,3 @@
-import importlib.util
import sys
import types
import unittest
@@ -12,35 +11,6 @@ ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
-def _install_opencode_utils_module() -> None:
- if "aiohttp" not in sys.modules:
- sys.modules["aiohttp"] = types.ModuleType("aiohttp")
-
- if "modules.agents.opencode.utils" in sys.modules:
- return
-
- if "modules.agents" not in sys.modules:
- agents_mod = types.ModuleType("modules.agents")
- agents_mod.__path__ = [str(ROOT / "modules" / "agents")]
- sys.modules["modules.agents"] = agents_mod
- if "modules.agents.opencode" not in sys.modules:
- opencode_mod = types.ModuleType("modules.agents.opencode")
- opencode_mod.__path__ = [str(ROOT / "modules" / "agents" / "opencode")]
- sys.modules["modules.agents.opencode"] = opencode_mod
-
- spec = importlib.util.spec_from_file_location(
- "modules.agents.opencode.utils",
- ROOT / "modules" / "agents" / "opencode" / "utils.py",
- )
- assert spec is not None
- assert spec.loader is not None
- module = importlib.util.module_from_spec(spec)
- sys.modules["modules.agents.opencode.utils"] = module
- spec.loader.exec_module(module)
-
-
-_install_opencode_utils_module()
-
from modules.im.feishu import FeishuBot
diff --git a/tests/test_feishu_post_messages.py b/tests/test_feishu_post_messages.py
index fdd7bb6d0..37bad4bb1 100644
--- a/tests/test_feishu_post_messages.py
+++ b/tests/test_feishu_post_messages.py
@@ -1,7 +1,5 @@
-import importlib.util
import json
import sys
-import types
import unittest
from pathlib import Path
from unittest.mock import AsyncMock
@@ -13,38 +11,6 @@ ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
-def _install_opencode_utils_module() -> None:
- if "aiohttp" not in sys.modules:
- try:
- __import__("aiohttp")
- except ImportError:
- sys.modules["aiohttp"] = types.ModuleType("aiohttp")
-
- if "modules.agents.opencode.utils" in sys.modules:
- return
-
- if "modules.agents" not in sys.modules:
- agents_mod = types.ModuleType("modules.agents")
- agents_mod.__path__ = [str(ROOT / "modules" / "agents")]
- sys.modules["modules.agents"] = agents_mod
- if "modules.agents.opencode" not in sys.modules:
- opencode_mod = types.ModuleType("modules.agents.opencode")
- opencode_mod.__path__ = [str(ROOT / "modules" / "agents" / "opencode")]
- sys.modules["modules.agents.opencode"] = opencode_mod
-
- spec = importlib.util.spec_from_file_location(
- "modules.agents.opencode.utils",
- ROOT / "modules" / "agents" / "opencode" / "utils.py",
- )
- assert spec is not None
- assert spec.loader is not None
- module = importlib.util.module_from_spec(spec)
- sys.modules["modules.agents.opencode.utils"] = module
- spec.loader.exec_module(module)
-
-
-_install_opencode_utils_module()
-
from modules.im.feishu import FeishuBot
diff --git a/tests/test_feishu_routing_card.py b/tests/test_feishu_routing_card.py
index 08b2f7136..b0e017b2a 100644
--- a/tests/test_feishu_routing_card.py
+++ b/tests/test_feishu_routing_card.py
@@ -1,46 +1,10 @@
-import importlib.util
-import sys
-import types
import unittest
-from pathlib import Path
from types import SimpleNamespace
from unittest.mock import AsyncMock
from config.v2_config import LarkConfig
from core.auth import AuthResult
from modules.im.base import MessageContext
-
-
-def _install_opencode_utils_module() -> None:
- if "aiohttp" not in sys.modules:
- sys.modules["aiohttp"] = types.ModuleType("aiohttp")
-
- if "modules.agents.opencode.utils" in sys.modules:
- return
-
- repo_root = Path(__file__).resolve().parents[1]
- if "modules.agents" not in sys.modules:
- agents_mod = types.ModuleType("modules.agents")
- agents_mod.__path__ = [str(repo_root / "modules" / "agents")]
- sys.modules["modules.agents"] = agents_mod
- if "modules.agents.opencode" not in sys.modules:
- opencode_mod = types.ModuleType("modules.agents.opencode")
- opencode_mod.__path__ = [str(repo_root / "modules" / "agents" / "opencode")]
- sys.modules["modules.agents.opencode"] = opencode_mod
-
- spec = importlib.util.spec_from_file_location(
- "modules.agents.opencode.utils",
- repo_root / "modules" / "agents" / "opencode" / "utils.py",
- )
- assert spec is not None
- assert spec.loader is not None
- module = importlib.util.module_from_spec(spec)
- sys.modules["modules.agents.opencode.utils"] = module
- spec.loader.exec_module(module)
-
-
-_install_opencode_utils_module()
-
from modules.im.feishu import FeishuBot
diff --git a/tests/test_slack_app_mention_empty.py b/tests/test_slack_app_mention_empty.py
index a34acfaab..3a04ead2c 100644
--- a/tests/test_slack_app_mention_empty.py
+++ b/tests/test_slack_app_mention_empty.py
@@ -1,112 +1,9 @@
-import importlib
-import sys
-import types
import unittest
-from pathlib import Path
from types import SimpleNamespace
from unittest.mock import AsyncMock, Mock
from config.v2_config import SlackConfig
-
-
-def _install_slack_stubs() -> None:
- if "aiohttp" not in sys.modules:
- aiohttp_mod = types.ModuleType("aiohttp")
-
- class _ClientWebSocketResponse:
- closed = False
-
- class _ClientSession:
- async def close(self):
- return None
-
- class _ClientTimeout:
- def __init__(self, *args, **kwargs):
- pass
-
- aiohttp_mod.ClientWebSocketResponse = _ClientWebSocketResponse
- aiohttp_mod.ClientSession = _ClientSession
- aiohttp_mod.ClientTimeout = _ClientTimeout
- sys.modules["aiohttp"] = aiohttp_mod
-
- if "markdown_to_mrkdwn" not in sys.modules:
- markdown_mod = types.ModuleType("markdown_to_mrkdwn")
-
- class _SlackMarkdownConverter:
- def convert(self, text):
- return text
-
- markdown_mod.SlackMarkdownConverter = _SlackMarkdownConverter
- sys.modules["markdown_to_mrkdwn"] = markdown_mod
-
- if "slack_sdk" not in sys.modules:
- slack_sdk = types.ModuleType("slack_sdk")
- web_mod = types.ModuleType("slack_sdk.web")
- web_async_mod = types.ModuleType("slack_sdk.web.async_client")
- socket_mode_mod = types.ModuleType("slack_sdk.socket_mode")
- socket_mode_aiohttp_mod = types.ModuleType("slack_sdk.socket_mode.aiohttp")
- socket_mode_request_mod = types.ModuleType("slack_sdk.socket_mode.request")
- socket_mode_response_mod = types.ModuleType("slack_sdk.socket_mode.response")
- errors_mod = types.ModuleType("slack_sdk.errors")
-
- class _AsyncWebClient:
- def __init__(self, *args, **kwargs):
- pass
-
- async def auth_test(self):
- return {"user_id": "U_BOT"}
-
- class _SocketModeClient:
- def __init__(self, *args, **kwargs):
- pass
-
- class _SocketModeRequest:
- pass
-
- class _SocketModeResponse:
- def __init__(self, *args, **kwargs):
- pass
-
- class _SlackApiError(Exception):
- def __init__(self, message="", response=None):
- super().__init__(message)
- self.response = response
-
- web_async_mod.AsyncWebClient = _AsyncWebClient
- socket_mode_aiohttp_mod.SocketModeClient = _SocketModeClient
- socket_mode_request_mod.SocketModeRequest = _SocketModeRequest
- socket_mode_response_mod.SocketModeResponse = _SocketModeResponse
- errors_mod.SlackApiError = _SlackApiError
-
- sys.modules["slack_sdk"] = slack_sdk
- sys.modules["slack_sdk.web"] = web_mod
- sys.modules["slack_sdk.web.async_client"] = web_async_mod
- sys.modules["slack_sdk.socket_mode"] = socket_mode_mod
- sys.modules["slack_sdk.socket_mode.aiohttp"] = socket_mode_aiohttp_mod
- sys.modules["slack_sdk.socket_mode.request"] = socket_mode_request_mod
- sys.modules["slack_sdk.socket_mode.response"] = socket_mode_response_mod
- sys.modules["slack_sdk.errors"] = errors_mod
-
-
-def _load_local_slack_bot():
- repo_root = Path(__file__).resolve().parents[1]
- sys.path.insert(0, str(repo_root))
- loaded = sys.modules.get("modules.im.slack")
- loaded_path = Path(getattr(loaded, "__file__", "")).resolve() if loaded is not None else None
- local_path = (repo_root / "modules" / "im" / "slack.py").resolve()
- if loaded is not None and loaded_path == local_path:
- return loaded.SlackBot
- sys.modules.pop("modules.im.slack", None)
- spec = importlib.util.spec_from_file_location("modules.im.slack", local_path)
- assert spec is not None and spec.loader is not None
- module = importlib.util.module_from_spec(spec)
- sys.modules["modules.im.slack"] = module
- spec.loader.exec_module(module)
- return module.SlackBot
-
-
-_install_slack_stubs()
-SlackBot = _load_local_slack_bot()
+from modules.im.slack import SlackBot
class _ResponseLike:
@@ -118,14 +15,6 @@ class _ResponseLike:
class SlackAppMentionEmptyTests(unittest.IsolatedAsyncioTestCase):
- def test_loader_preserves_the_canonical_slack_module(self):
- canonical = sys.modules["modules.im.slack"]
-
- loaded_class = _load_local_slack_bot()
-
- self.assertIs(sys.modules["modules.im.slack"], canonical)
- self.assertIs(loaded_class, canonical.SlackBot)
-
async def test_empty_app_mention_dispatches_empty_message_for_start_menu(self):
slack = SlackBot(SlackConfig(bot_token="xoxb-test"))
received = {}
diff --git a/tests/test_slack_dm_mentions.py b/tests/test_slack_dm_mentions.py
index ae95986a5..c17c2384a 100644
--- a/tests/test_slack_dm_mentions.py
+++ b/tests/test_slack_dm_mentions.py
@@ -1,123 +1,35 @@
-import importlib.util
import asyncio
-import unittest
import sys
-import types
-from pathlib import Path
+import unittest
from types import SimpleNamespace
+from unittest.mock import patch
from config.v2_config import SlackConfig
+from modules.im import slack as slack_module
from modules.im.base import InlineButton, InlineKeyboard, MessageContext
+from modules.im.slack import SlackApiError, SlackBot
-def _install_slack_stubs() -> None:
- if "aiohttp" not in sys.modules:
- try:
- __import__("aiohttp")
- except ImportError:
- aiohttp_mod = types.ModuleType("aiohttp")
-
- class _ClientWebSocketResponse:
- closed = False
-
- class _ClientSession:
- async def close(self):
- return None
-
- class _ClientTimeout:
- def __init__(self, *args, **kwargs):
- pass
-
- aiohttp_mod.ClientWebSocketResponse = _ClientWebSocketResponse
- aiohttp_mod.ClientSession = _ClientSession
- aiohttp_mod.ClientTimeout = _ClientTimeout
- sys.modules["aiohttp"] = aiohttp_mod
-
- if "markdown_to_mrkdwn" not in sys.modules:
- markdown_mod = types.ModuleType("markdown_to_mrkdwn")
-
- class _SlackMarkdownConverter:
- def convert(self, text):
- return text
-
- markdown_mod.SlackMarkdownConverter = _SlackMarkdownConverter
- sys.modules["markdown_to_mrkdwn"] = markdown_mod
+class _OfflineWebClient:
+ """The Slack web client a bot builds here: no network, and it names the bot ``U_BOT``."""
- if "slack_sdk" in sys.modules:
+ def __init__(self, *args, **kwargs):
pass
- slack_sdk = types.ModuleType("slack_sdk")
- web_mod = types.ModuleType("slack_sdk.web")
- web_async_mod = types.ModuleType("slack_sdk.web.async_client")
- socket_mode_mod = types.ModuleType("slack_sdk.socket_mode")
- socket_mode_aiohttp_mod = types.ModuleType("slack_sdk.socket_mode.aiohttp")
- socket_mode_request_mod = types.ModuleType("slack_sdk.socket_mode.request")
- socket_mode_response_mod = types.ModuleType("slack_sdk.socket_mode.response")
- errors_mod = types.ModuleType("slack_sdk.errors")
+ async def auth_test(self):
+ return {"user_id": "U_BOT"}
- class _AsyncWebClient:
- def __init__(self, *args, **kwargs):
- pass
- async def auth_test(self):
- return {"user_id": "U_BOT"}
+# Module-scoped so it holds under pytest and under ``unittest.main()`` alike.
+_offline_web_client = patch.object(slack_module, "AsyncWebClient", _OfflineWebClient)
- class _SocketModeClient:
- def __init__(self, *args, **kwargs):
- pass
- class _SocketModeRequest:
- pass
+def setUpModule():
+ _offline_web_client.start()
- class _SocketModeResponse:
- def __init__(self, *args, **kwargs):
- pass
-
- class _SlackApiError(Exception):
- def __init__(self, message="", response=None):
- super().__init__(message)
- self.response = response
-
- web_async_mod.AsyncWebClient = _AsyncWebClient
- socket_mode_aiohttp_mod.SocketModeClient = _SocketModeClient
- socket_mode_request_mod.SocketModeRequest = _SocketModeRequest
- socket_mode_response_mod.SocketModeResponse = _SocketModeResponse
- errors_mod.SlackApiError = _SlackApiError
-
- if "slack_sdk" not in sys.modules:
- sys.modules["slack_sdk"] = slack_sdk
- sys.modules["slack_sdk.web"] = web_mod
- sys.modules["slack_sdk.web.async_client"] = web_async_mod
- sys.modules["slack_sdk.socket_mode"] = socket_mode_mod
- sys.modules["slack_sdk.socket_mode.aiohttp"] = socket_mode_aiohttp_mod
- sys.modules["slack_sdk.socket_mode.request"] = socket_mode_request_mod
- sys.modules["slack_sdk.socket_mode.response"] = socket_mode_response_mod
- sys.modules["slack_sdk.errors"] = errors_mod
-
- if "modules.agents.opencode.utils" not in sys.modules:
- repo_root = Path(__file__).resolve().parents[1]
- agents_mod = types.ModuleType("modules.agents")
- agents_mod.__path__ = [str(repo_root / "modules" / "agents")]
- opencode_mod = types.ModuleType("modules.agents.opencode")
- opencode_mod.__path__ = [str(repo_root / "modules" / "agents" / "opencode")]
-
- sys.modules["modules.agents"] = agents_mod
- sys.modules["modules.agents.opencode"] = opencode_mod
-
- spec = importlib.util.spec_from_file_location(
- "modules.agents.opencode.utils",
- repo_root / "modules" / "agents" / "opencode" / "utils.py",
- )
- assert spec is not None
- assert spec.loader is not None
- module = importlib.util.module_from_spec(spec)
- sys.modules["modules.agents.opencode.utils"] = module
- spec.loader.exec_module(module)
-
-_install_slack_stubs()
-
-from modules.im.slack import SlackApiError, SlackBot
+def tearDownModule():
+ _offline_web_client.stop()
class _ResponseLike: