63 lines
1.8 KiB
Python
63 lines
1.8 KiB
Python
"""Тесты для чат-агента."""
|
|
import pytest
|
|
from unittest.mock import AsyncMock
|
|
from uuid import uuid4
|
|
|
|
from agents.chat_agent import ChatAgent
|
|
from agents.gigachat_client import GigaChatClient
|
|
from models.gigachat_types import GigaChatMessage, GigaChatResponse, GigaChatUsage, GigaChatChoice
|
|
from services.cache_service import CacheService
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_gigachat():
|
|
"""Фикстура для мокового GigaChat клиента."""
|
|
return AsyncMock(spec=GigaChatClient)
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_cache():
|
|
"""Фикстура для мокового CacheService."""
|
|
return AsyncMock(spec=CacheService)
|
|
|
|
|
|
@pytest.fixture
|
|
def chat_agent(mock_gigachat, mock_cache):
|
|
"""Фикстура для ChatAgent."""
|
|
return ChatAgent(gigachat=mock_gigachat, cache=mock_cache)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_chat_basic(chat_agent, mock_gigachat, mock_cache):
|
|
"""Тест базового чата."""
|
|
user_id = uuid4()
|
|
message = "Привет!"
|
|
|
|
mock_response = GigaChatResponse(
|
|
id="test_id",
|
|
object="chat.completion",
|
|
created=1234567890,
|
|
model="GigaChat-2-Lite",
|
|
choices=[
|
|
GigaChatChoice(
|
|
message=GigaChatMessage(role="assistant", content="Привет! Как дела? 🌍"),
|
|
index=0,
|
|
)
|
|
],
|
|
usage=GigaChatUsage(prompt_tokens=50, completion_tokens=10, total_tokens=60),
|
|
)
|
|
|
|
mock_gigachat.chat_with_response.return_value = mock_response
|
|
mock_cache.get_context.return_value = []
|
|
|
|
response, tokens = await chat_agent.chat(
|
|
user_id=user_id,
|
|
message=message,
|
|
conversation_id="test_conv",
|
|
)
|
|
|
|
assert response == "Привет! Как дела? 🌍"
|
|
assert tokens == 60
|
|
mock_cache.add_message.assert_called()
|
|
|