Files
AI_engine/tests/test_tool_registry.py
2026-09-22 15:29:39 +05:30

56 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Unit tests for ToolRegistry and Interactive Question Asking."""
import pytest
import asyncio
from core.tool_registry import Tool, ToolParameter, ToolRegistry
from core.interactive_question import QuestionManager, QuestionOption, question_manager
@pytest.mark.asyncio
async def test_tool_registry_registration_and_execution():
registry = ToolRegistry()
async def sample_handler(order_id: str, count: int = 1):
return {"order": order_id, "items": count}
tool = Tool(
name="test_tool",
description="A test tool",
handler=sample_handler,
parameters=[
ToolParameter(name="order_id", type="string", description="Order ID"),
ToolParameter(name="count", type="integer", description="Count", required=False, default=1),
],
)
registry.register(tool)
assert registry.get("test_tool") is not None
schemas = registry.get_schemas()
assert len(schemas) == 1
assert schemas[0]["name"] == "test_tool"
assert "order_id" in schemas[0]["parameters"]["properties"]
result = await registry.execute_tool("test_tool", order_id="ORD-100")
assert result == {"order": "ORD-100", "items": 1}
@pytest.mark.asyncio
async def test_question_manager_flow():
qm = QuestionManager()
q = qm.create_question(
prompt="Select target batch:",
options=[
{"id": "morning", "label": "Morning Batch", "badge": "12 AM–9 AM"},
{"id": "afternoon", "label": "Afternoon Batch", "badge": "9 AM–4 PM"},
],
allow_custom_input=False,
)
assert q.question_id.startswith("q-")
assert len(q.options) == 2
assert q.options[0].badge == "12 AM–9 AM"
assert not q.answered
qm.answer_question(q.question_id, "morning")
assert q.answered is True
assert q.answer == "morning"