feat: servidor mcp institucional y asistente interactivo guiado con spotlight didactico (SDD-011, v3.8.27)
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
"""
|
||||
backend/tests/test_mcp_and_walkthrough.py - Pruebas para el Servidor MCP y Walkthrough (SDD-011)
|
||||
Valida herramientas MCP institucionales, endpoints JSON-RPC 2.0 / REST y directivas de Spotlight.
|
||||
"""
|
||||
from unittest.mock import AsyncMock, patch, MagicMock
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from main import app
|
||||
from mcp_server import EduSpaceMCPServer
|
||||
from app.services.guide_service import GuideService
|
||||
|
||||
|
||||
def test_mcp_server_manifest_and_execution():
|
||||
"""Valida el manifiesto de herramientas MCP y la ejecución local."""
|
||||
manifest = EduSpaceMCPServer.get_tools_manifest()
|
||||
assert len(manifest) >= 3
|
||||
tool_names = [t["name"] for t in manifest]
|
||||
assert "get_application_routes" in tool_names
|
||||
assert "get_canonical_9_steps" in tool_names
|
||||
assert "get_step_guide" in tool_names
|
||||
|
||||
# 1. get_application_routes
|
||||
routes_all = EduSpaceMCPServer.execute_tool("get_application_routes", {})
|
||||
assert "routes" in routes_all
|
||||
assert routes_all["total"] >= 10
|
||||
|
||||
routes_alumno = EduSpaceMCPServer.execute_tool("get_application_routes", {"role": "ALUMNO"})
|
||||
assert routes_alumno["total"] < routes_all["total"]
|
||||
assert any("/schedule/today_schedule" in r["path"] for r in routes_alumno["routes"])
|
||||
|
||||
# 2. get_canonical_9_steps
|
||||
steps_result = EduSpaceMCPServer.execute_tool("get_canonical_9_steps", {})
|
||||
assert "steps" in steps_result
|
||||
assert steps_result["total"] == 9
|
||||
assert steps_result["steps"][0]["name"] == "Sedes"
|
||||
assert steps_result["steps"][8]["name"] == "Reserva de Espacio"
|
||||
|
||||
# 3. get_step_guide
|
||||
guide_aula = EduSpaceMCPServer.execute_tool("get_step_guide", {"action": "crear_aula"})
|
||||
assert "steps" in guide_aula
|
||||
assert guide_aula["target_url"] == "/classrooms/list_classrooms"
|
||||
assert len(guide_aula["steps"]) >= 3
|
||||
|
||||
|
||||
def test_mcp_rest_endpoints():
|
||||
"""Valida los endpoints REST /api/v1/mcp/tools y /api/v1/mcp/execute."""
|
||||
client = TestClient(app)
|
||||
|
||||
# GET /tools
|
||||
tools_res = client.get("/api/v1/mcp/tools")
|
||||
assert tools_res.status_code == 200
|
||||
assert tools_res.json()["status"] == "success"
|
||||
|
||||
# POST /execute válido
|
||||
exec_res = client.post("/api/v1/mcp/execute", json={
|
||||
"name": "get_canonical_9_steps",
|
||||
"arguments": {}
|
||||
})
|
||||
assert exec_res.status_code == 200
|
||||
data = exec_res.json()
|
||||
assert data["status"] == "success"
|
||||
assert data["result"]["total"] == 9
|
||||
|
||||
# POST /execute inválido
|
||||
bad_res = client.post("/api/v1/mcp/execute", json={
|
||||
"name": "herramienta_fantasma"
|
||||
})
|
||||
assert bad_res.status_code == 400
|
||||
|
||||
|
||||
def test_mcp_jsonrpc_endpoint():
|
||||
"""Valida el endpoint estándar JSON-RPC 2.0 /api/v1/mcp."""
|
||||
client = TestClient(app)
|
||||
|
||||
# tools/list
|
||||
rpc_list = client.post("/api/v1/mcp", json={
|
||||
"jsonrpc": "2.0",
|
||||
"id": 1,
|
||||
"method": "tools/list",
|
||||
"params": {}
|
||||
})
|
||||
assert rpc_list.status_code == 200
|
||||
res1 = rpc_list.json()
|
||||
assert res1["jsonrpc"] == "2.0"
|
||||
assert "tools" in res1["result"]
|
||||
|
||||
# tools/call
|
||||
rpc_call = client.post("/api/v1/mcp", json={
|
||||
"jsonrpc": "2.0",
|
||||
"id": 2,
|
||||
"method": "tools/call",
|
||||
"params": {
|
||||
"name": "get_step_guide",
|
||||
"arguments": {"action": "crear_sede"}
|
||||
}
|
||||
})
|
||||
assert rpc_call.status_code == 200
|
||||
res2 = rpc_call.json()
|
||||
assert res2["jsonrpc"] == "2.0"
|
||||
assert res2["result"]["data"]["target_url"] == "/buildings"
|
||||
|
||||
|
||||
def test_guide_service_intent_detection():
|
||||
"""Valida que GuideService identifique consultas de acción y construya GuideStepDTO."""
|
||||
# Crear Aula
|
||||
step_aula = GuideService.detect_guide_step("¿Cómo creo un aula?", "bedelia")
|
||||
assert step_aula is not None
|
||||
assert step_aula.target_url == "/classrooms/list_classrooms"
|
||||
assert "#btnOpenCreateClassroomModal" in step_aula.selector
|
||||
|
||||
# Crear Sede
|
||||
step_sede = GuideService.detect_guide_step("Quiero crear una sede nueva", "bedelia")
|
||||
assert step_sede is not None
|
||||
assert step_sede.target_url == "/buildings"
|
||||
|
||||
# Consulta no guiada
|
||||
step_none = GuideService.detect_guide_step("¿Quién es el decano?", "alumno")
|
||||
assert step_none is None
|
||||
|
||||
|
||||
def test_chat_endpoint_emits_guide_step_when_requested(init_database):
|
||||
"""Verifica que el endpoint /api/chat adjunte el guide_step cuando se solicita tutorial."""
|
||||
client = TestClient(app)
|
||||
|
||||
mock_response = MagicMock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.json.return_value = {
|
||||
"choices": [
|
||||
{
|
||||
"message": {
|
||||
"role": "assistant",
|
||||
"content": "Para crear un aula, presiona el botón Nueva Aula. OPCIONES: [Siguiente paso | Cancelar]"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
mock_response.raise_for_status = MagicMock()
|
||||
|
||||
with patch("httpx.AsyncClient.post", new_callable=AsyncMock) as mock_post:
|
||||
mock_post.return_value = mock_response
|
||||
|
||||
res = client.post("/api/chat", json={
|
||||
"messages": [{"role": "user", "content": "¿Cómo creo un aula?"}],
|
||||
"user_role": "bedelia"
|
||||
})
|
||||
assert res.status_code == 200
|
||||
data = res.json()
|
||||
assert "guide_step" in data
|
||||
assert data["guide_step"] is not None
|
||||
assert data["guide_step"]["target_url"] == "/classrooms/list_classrooms"
|
||||
assert "#btnOpenCreateClassroomModal" in data["guide_step"]["selector"]
|
||||
assert data["guide_step"]["step_number"] == 1
|
||||
Reference in New Issue
Block a user