mirror of
https://git.quad4.io/RNS-Things/MeshChatX.git
synced 2026-04-26 19:35:38 +00:00
0ae45b45c0
- 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.
53 lines
1.6 KiB
Python
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()
|