Files
MeshChatX/tests/backend/conftest.py
T
Sudo-Ivan 0ae45b45c0 Add temporary log directory setup for tests and improve input validation in fuzz tests
- Configured a temporary directory for log files in tests to prevent permission issues in restricted environments.
- Improved input validation in fuzz tests for `root_folder_name` and `docs_file` to exclude null characters, enhancing robustness.
- Introduced a new test suite for property-based testing of telemetry and utility functions, ensuring stability and correctness across various scenarios.
2026-01-14 12:40:28 -06:00

53 lines
1.6 KiB
Python

import asyncio
import os
import tempfile
from unittest.mock import patch
import pytest
# Set log dir to a temporary directory for tests to avoid permission issues
# in restricted environments like sandboxes.
os.environ["MESHCHAT_LOG_DIR"] = tempfile.mkdtemp()
@pytest.fixture(autouse=True)
def global_mocks():
with (
patch("meshchatx.meshchat.AsyncUtils") as mock_async_utils,
patch(
"meshchatx.src.backend.identity_context.IdentityContext.start_background_threads",
return_value=None,
),
patch("meshchatx.meshchat.generate_ssl_certificate", return_value=None),
patch("threading.Thread"),
patch("asyncio.sleep", side_effect=lambda *args, **kwargs: asyncio.sleep(0)),
):
# Mock run_async to properly close coroutines
def mock_run_async(coro):
if asyncio.iscoroutine(coro):
try:
# If it's a coroutine, we should close it if it's not being awaited
coro.close()
except RuntimeError:
pass
elif hasattr(coro, "__await__"):
# Handle other awaitables
pass
mock_async_utils.run_async.side_effect = mock_run_async
yield {
"async_utils": mock_async_utils,
}
@pytest.fixture(autouse=True)
def cleanup_sqlite_connections():
yield
# After each test, try to close any lingering sqlite connections if possible
# This is a bit hard globally without tracking them, but we can at least
# trigger GC which often helps with ResourceWarnings.
import gc
gc.collect()