diff --git a/tests/conftest.py b/tests/conftest.py index 7a104a0..b425e5e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -12,6 +12,16 @@ import pytest os.environ.setdefault("TESTING", "1") os.environ.setdefault("JWT_SECRET_KEY", "test-secret-e2e") +# Ensure directories exist for modules with module-level FileHandlers. +# bot.py creates FileHandler(config.BOT_LOG_FILE) at import time. +# config.py reads DATA_DIR at import time. We must ensure the directory +# exists for whatever DATA_DIR resolves to (default: /data in Docker). +import tempfile as _tf +if "DATA_DIR" not in os.environ: + os.environ["DATA_DIR"] = os.path.join(_tf.gettempdir(), ".agnes-test-data") +os.makedirs(os.path.join(os.environ["DATA_DIR"], "notifications"), exist_ok=True) +os.makedirs(os.path.join(os.environ["DATA_DIR"], "state"), exist_ok=True) + @pytest.fixture def e2e_env(tmp_path, monkeypatch): diff --git a/tests/test_telegram_bot.py b/tests/test_telegram_bot.py index c34616d..8e2c1e7 100644 --- a/tests/test_telegram_bot.py +++ b/tests/test_telegram_bot.py @@ -1,24 +1,10 @@ """Tests for Telegram bot message handlers.""" import asyncio -import os -import sys -import tempfile from unittest.mock import AsyncMock, patch import pytest -# Patch BOT_LOG_FILE at import time (before any fixture runs) so that -# bot.py can be imported even when /data/notifications/ doesn't exist. -# This is critical for pytest-xdist where module-scoped fixtures -# may not run before the worker tries to import the module. -_tmp_log = tempfile.mktemp(suffix=".log") - -import services.telegram_bot.config as _cfg -_cfg.BOT_LOG_FILE = _tmp_log -# Ensure the bot module is not cached with the old config -sys.modules.pop("services.telegram_bot.bot", None) - def _make_message(text: str, chat_id: int = 10) -> dict: return {"chat": {"id": chat_id}, "text": text}