Spaces:
Runtime error
Runtime error
File size: 2,818 Bytes
644f326 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | import pytest
from unittest.mock import AsyncMock, patch
from langchain_core.messages import AIMessage, HumanMessage, messages_to_dict
from api.helper.conversation_helper import messages_to_dict
def test_messages_to_dict():
raw = [
HumanMessage(content="Hello AI"),
AIMessage(
content="Hello human!",
tool_calls=[{"name": "lookup", "args": {"query": "test"}, "id": "call-1", "type": "tool_call"}],
additional_kwargs={"reasoning": "Use the lookup tool."},
),
]
result = messages_to_dict(raw)
assert result[0]["type"] == "human"
assert result[0]["data"]["content"] == "Hello AI"
assert result[1]["type"] == "ai"
assert result[1]["data"]["content"] == "Hello human!"
assert result[1]["data"]["tool_calls"][0]["name"] == "lookup"
assert result[1]["data"]["additional_kwargs"]["reasoning"] == "Use the lookup tool."
def test_list_conversations(client):
with patch("api.routes.conversation_router.get_all_threads", new_callable=AsyncMock) as mock_get_threads:
mock_get_threads.return_value = [
{"thread_id": "t1", "title": "First chat", "created_at": "2026-01-01", "message_count": 2}
]
response = client.get("/api/v1/conversations")
assert response.status_code == 200
data = response.json()
assert data["success"] is True
assert data["count"] == 1
assert data["threads"][0]["thread_id"] == "t1"
def test_get_conversation(client):
with patch("api.routes.conversation_router.load_conversation", new_callable=AsyncMock) as mock_load:
mock_load.return_value = [
{"type": "human", "content": "Tell me a joke"},
{"type": "ai", "content": "Why did the chicken cross the road?"},
]
response = client.get("/api/v1/conversations/test-t1")
assert response.status_code == 200
data = response.json()
assert data["success"] is True
assert data["thread_id"] == "test-t1"
assert len(data["messages"]) == 2
def test_delete_conversation_success(client):
with patch("api.routes.conversation_router.delete_user_conversation", new_callable=AsyncMock) as mock_del:
mock_del.return_value = True
response = client.delete("/api/v1/conversations/test-t1")
assert response.status_code == 200
data = response.json()
assert data["success"] is True
def test_delete_conversation_not_found(client):
with patch("api.routes.conversation_router.delete_user_conversation", new_callable=AsyncMock) as mock_del:
mock_del.return_value = False
response = client.delete("/api/v1/conversations/not-found-id")
assert response.status_code == 404
data = response.json()
assert data["success"] is False
|