import asyncio
import pytest
from pathlib import Path
from unittest.mock import AsyncMock, MagicMock
import tempfile
from pyr.core.config import PyrConfig
from pyr.core.app import PyrApp
from pyr.ai.client import BaseAIClient
from pyr.storage.database import DatabaseManager
@pytest.fixture
def temp_dir():
with tempfile.TemporaryDirectory() as td:
yield Path(td)
@pytest.fixture
def test_config(temp_dir):
return PyrConfig(
provider="openai",
model="gpt-3.5-turbo",
api_key="test-key",
base_url="https://api.openai.com",
db_path=str(temp_dir / "test.db"),
cache_dir=str(temp_dir / "cache"),
verbose=True,
syntax_highlight=True,
use_tools=True,
)
@pytest.fixture
def mock_ai_client():
client = AsyncMock(spec=BaseAIClient)
client.chat = AsyncMock(return_value="Test response")
client.chat_with_tools = AsyncMock(return_value=MagicMock(content="Tool response"))
client.close = AsyncMock()
client.add_system_message = AsyncMock()
client.add_user_message = AsyncMock()
client.add_assistant_message = AsyncMock()
return client
@pytest.fixture
async def test_app(test_config, mock_ai_client):
app = PyrApp(test_config)
app.ai_client = mock_ai_client
await app.startup()
yield app
await app.shutdown()
@pytest.fixture
async def test_db(temp_dir):
db_path = temp_dir / "test.db"
db = DatabaseManager(str(db_path))
await db.initialize()
yield db
await db.close()
@pytest.fixture(scope="session")
def event_loop():
loop = asyncio.new_event_loop()
yield loop
loop.close()