mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-08-25 03:27:15 +00:00
refactor(test): consolidate FakeApp and add sys.modules isolation utility
- Extract tests/utils/import_isolation.py with isolated_sys_modules context manager - Extend tests/factories/app.py FakeApp with handler-specific attributes - Refactor test_chat_handler.py to use centralized FakeApp and cached imports - Refactor test_command_handler.py with mock_execute_factory fixture - Refactor test_smoke.py to move import-time sys.modules manipulation into fixture - Add SQLite migration integration tests (G-002) - Add HTTP API smoke integration tests (G-005) - Update CI workflow to call pytest for SQLite migrations (G-004) Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -18,6 +18,8 @@ class FakeApp:
|
||||
command_prefix: list[str] = ["/", "!"],
|
||||
command_enable: bool = True,
|
||||
pipeline_concurrency: int = 10,
|
||||
admins: list[str] | None = None,
|
||||
**extra_attrs,
|
||||
):
|
||||
self.logger = self._create_mock_logger()
|
||||
self.sess_mgr = self._create_mock_session_manager()
|
||||
@@ -30,9 +32,19 @@ class FakeApp:
|
||||
command_prefix=command_prefix,
|
||||
command_enable=command_enable,
|
||||
pipeline_concurrency=pipeline_concurrency,
|
||||
admins=admins or [],
|
||||
)
|
||||
self.task_mgr = self._create_mock_task_manager()
|
||||
|
||||
# Handler-specific optional attributes
|
||||
self.telemetry = self._create_mock_telemetry()
|
||||
self.survey = None
|
||||
self.cmd_mgr = self._create_mock_cmd_mgr()
|
||||
|
||||
# Apply any extra attributes for specific test scenarios
|
||||
for name, value in extra_attrs.items():
|
||||
setattr(self, name, value)
|
||||
|
||||
# Captured outbound messages (for assertions)
|
||||
self._outbound_messages: list = []
|
||||
|
||||
@@ -82,11 +94,13 @@ class FakeApp:
|
||||
command_prefix: list[str],
|
||||
command_enable: bool,
|
||||
pipeline_concurrency: int,
|
||||
admins: list[str],
|
||||
):
|
||||
instance_config = Mock()
|
||||
instance_config.data = {
|
||||
"command": {"prefix": command_prefix, "enable": command_enable},
|
||||
"concurrency": {"pipeline": pipeline_concurrency},
|
||||
"admins": admins,
|
||||
}
|
||||
return instance_config
|
||||
|
||||
@@ -95,6 +109,16 @@ class FakeApp:
|
||||
task_mgr.create_task = Mock()
|
||||
return task_mgr
|
||||
|
||||
def _create_mock_telemetry(self):
|
||||
telemetry = AsyncMock()
|
||||
telemetry.start_send_task = AsyncMock()
|
||||
return telemetry
|
||||
|
||||
def _create_mock_cmd_mgr(self):
|
||||
cmd_mgr = AsyncMock()
|
||||
cmd_mgr.execute = AsyncMock()
|
||||
return cmd_mgr
|
||||
|
||||
def capture_message(self, message):
|
||||
"""Capture an outbound message for test assertions."""
|
||||
self._outbound_messages.append(message)
|
||||
|
||||
Reference in New Issue
Block a user