initial commit heh
This commit is contained in:
78 files changed
+11181
No files matched your search
@@ -0,0 +1,152 @@
|
||||
"""Unit tests for OpenAIAgent supporting DeepSeek and OpenAI-compatible models."""
|
||||
|
||||
import json
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
import pytest
|
||||
|
||||
from homeagent.agent.conversation import ConversationManager
|
||||
from homeagent.agent.openai_agent import OpenAIAgent
|
||||
from homeagent.config import Settings
|
||||
from homeagent.tools.base import tool
|
||||
from homeagent.tools.registry import ToolRegistry
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_deepseek_unconfigured(tmp_path):
|
||||
settings = Settings(
|
||||
LLM_PROVIDER="deepseek",
|
||||
DEEPSEEK_API_KEY="",
|
||||
DATA_DIR=tmp_path,
|
||||
)
|
||||
conv_mgr = ConversationManager(tmp_path)
|
||||
agent = OpenAIAgent(settings=settings, conversation_manager=conv_mgr)
|
||||
|
||||
res = await agent.chat(chat_id=101, thread_id=1, user_message="Hello")
|
||||
assert "DeepSeek API Key не настроен" in res
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_openai_unconfigured(tmp_path):
|
||||
settings = Settings(
|
||||
LLM_PROVIDER="openai",
|
||||
OPENAI_API_KEY="",
|
||||
DATA_DIR=tmp_path,
|
||||
)
|
||||
conv_mgr = ConversationManager(tmp_path)
|
||||
agent = OpenAIAgent(settings=settings, conversation_manager=conv_mgr)
|
||||
|
||||
res = await agent.chat(chat_id=101, thread_id=1, user_message="Hello")
|
||||
assert "OpenAI / OpenAI-compatible API Key не настроен" in res
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_deepseek_simple_chat(tmp_path):
|
||||
settings = Settings(
|
||||
LLM_PROVIDER="deepseek",
|
||||
DEEPSEEK_API_KEY="sk-mock-deepseek",
|
||||
DEEPSEEK_MODEL="deepseek-chat",
|
||||
DATA_DIR=tmp_path,
|
||||
)
|
||||
conv_mgr = ConversationManager(tmp_path)
|
||||
agent = OpenAIAgent(settings=settings, conversation_manager=conv_mgr)
|
||||
|
||||
# Mock OpenAI client
|
||||
mock_client = MagicMock()
|
||||
mock_choice = MagicMock()
|
||||
mock_choice.message.content = "Привет! Я DeepSeek."
|
||||
mock_choice.message.tool_calls = None
|
||||
|
||||
mock_resp = MagicMock()
|
||||
mock_resp.choices = [mock_choice]
|
||||
|
||||
mock_client.chat.completions.create = AsyncMock(return_value=mock_resp)
|
||||
agent._client = mock_client
|
||||
|
||||
reply = await agent.chat(chat_id=123, thread_id=456, user_message="Привет!")
|
||||
assert reply == "Привет! Я DeepSeek."
|
||||
|
||||
# Check conversation history saved
|
||||
history = conv_mgr.get_openai_messages(chat_id=123, thread_id=456)
|
||||
assert len(history) == 2
|
||||
assert history[0]["role"] == "user"
|
||||
assert history[0]["content"] == "Привет!"
|
||||
assert history[1]["role"] == "assistant"
|
||||
assert history[1]["content"] == "Привет! Я DeepSeek."
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_openai_tool_calling_loop(tmp_path):
|
||||
settings = Settings(
|
||||
LLM_PROVIDER="openai",
|
||||
OPENAI_API_KEY="sk-mock-openai",
|
||||
OPENAI_MODEL="gpt-4o",
|
||||
DATA_DIR=tmp_path,
|
||||
)
|
||||
conv_mgr = ConversationManager(tmp_path)
|
||||
registry = ToolRegistry()
|
||||
|
||||
@tool(name="weather_check", description="Check city weather")
|
||||
def weather_check(city: str) -> str:
|
||||
return f"{city}: +22C, ясно"
|
||||
|
||||
registry.register(weather_check)
|
||||
|
||||
agent = OpenAIAgent(settings=settings, registry=registry, conversation_manager=conv_mgr)
|
||||
|
||||
# Turn 1: Model returns tool_call
|
||||
mock_tool_call = MagicMock()
|
||||
mock_tool_call.id = "call_abc123"
|
||||
mock_tool_call.function.name = "weather_check"
|
||||
mock_tool_call.function.arguments = json.dumps({"city": "Москва"})
|
||||
|
||||
choice_1 = MagicMock()
|
||||
choice_1.message.content = "Сейчас проверю погоду..."
|
||||
choice_1.message.tool_calls = [mock_tool_call]
|
||||
resp_1 = MagicMock()
|
||||
resp_1.choices = [choice_1]
|
||||
|
||||
# Turn 2: Model returns final text after getting tool result
|
||||
choice_2 = MagicMock()
|
||||
choice_2.message.content = "В Москве сейчас +22C, ясно!"
|
||||
choice_2.message.tool_calls = None
|
||||
resp_2 = MagicMock()
|
||||
resp_2.choices = [choice_2]
|
||||
|
||||
mock_client = MagicMock()
|
||||
mock_client.chat.completions.create = AsyncMock(side_effect=[resp_1, resp_2])
|
||||
agent._client = mock_client
|
||||
|
||||
reply = await agent.chat(chat_id=999, thread_id=1, user_message="Какая погода в Москве?")
|
||||
assert reply == "В Москве сейчас +22C, ясно!"
|
||||
assert mock_client.chat.completions.create.call_count == 2
|
||||
|
||||
|
||||
def test_legacy_session_sanitization(tmp_path):
|
||||
"""Test that a legacy session with function_call and function_response is properly aligned for OpenAI/DeepSeek."""
|
||||
conv_mgr = ConversationManager(tmp_path)
|
||||
session_file = tmp_path / "sessions" / "session_123_main.json"
|
||||
session_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
legacy_data = [
|
||||
{"role": "user", "parts": [{"text": "какие события на сегодня"}]},
|
||||
{"role": "model", "parts": [{"function_call": {"name": "calendar_today_schedule", "args": {}}}]},
|
||||
{"role": "user", "parts": [{"function_response": {"name": "calendar_today_schedule", "response": {"result": "Встреч нет"}}}]},
|
||||
{"role": "model", "parts": [{"text": "На сегодня встреч нет."}]},
|
||||
]
|
||||
with open(session_file, "w", encoding="utf-8") as f:
|
||||
json.dump(legacy_data, f)
|
||||
|
||||
messages = conv_mgr.get_openai_messages(chat_id=123, thread_id=None)
|
||||
assert len(messages) == 4
|
||||
assert messages[0]["role"] == "user"
|
||||
assert messages[1]["role"] == "assistant"
|
||||
assert messages[1]["tool_calls"] is not None
|
||||
assert len(messages[1]["tool_calls"]) == 1
|
||||
|
||||
tool_call_id = messages[1]["tool_calls"][0]["id"]
|
||||
assert messages[2]["role"] == "tool"
|
||||
# The tool response tool_call_id MUST MATCH the assistant tool_calls id
|
||||
assert messages[2]["tool_call_id"] == tool_call_id
|
||||
assert messages[3]["role"] == "assistant"
|
||||
assert messages[3]["content"] == "На сегодня встреч нет."
|
||||
|
||||
Reference in new issue
Block a user