import pytest
from pathlib import Path
import json
from retoors.main import create_app
from retoors.services.user_service import UserService
from retoors.services.config_service import ConfigService
from pytest_mock import MockerFixture # Import MockerFixture
from unittest import mock # For AsyncMock
import aiojobs # Import aiojobs to patch it
@pytest.fixture
async def client(aiohttp_client, mocker: MockerFixture):
app = create_app() # Define app here
# Directly set app["scheduler"] to a mock object
mock_scheduler_instance = mocker.MagicMock()
mock_scheduler_instance.spawn = mocker.AsyncMock()
mock_scheduler_instance.close = mocker.AsyncMock() # Ensure close is awaitable
app["scheduler"] = mock_scheduler_instance
# Create temporary data files for testing
base_path = Path(__file__).parent.parent
data_path = base_path / "data"
data_path.mkdir(exist_ok=True)
users_file = data_path / "users.json"
with open(users_file, "w") as f:
json.dump([], f)
config_file = data_path / "config.json"
with open(config_file, "w") as f:
json.dump({"price_per_gb": 0.0}, f)
app["user_service"] = UserService(users_file)
app["config_service"] = ConfigService(config_file)
yield await aiohttp_client(app)
# Clean up temporary files
users_file.unlink(missing_ok=True)
config_file.unlink(missing_ok=True) # Use missing_ok for robustness
@pytest.fixture
def mock_send_email(mocker: MockerFixture):
"""
Fixture to mock the send_email function.
This fixture will return the mock that was patched globally by the client fixture.
"""
# Access the globally patched mock
return mocker.patch("retoors.helpers.email_sender.send_email")