feat: integración de Asistente IA contextual con Llama.cpp Qwen3 Coder 30B (SDD-009, v3.8.24)
This commit is contained in:
@@ -0,0 +1,257 @@
|
||||
"""
|
||||
Suite de pruebas automatizadas para el módulo de Asistente IA (SDD-009).
|
||||
Valida contratos Pydantic v2, inyección de System Prompts por rol,
|
||||
llamadas asíncronas con httpx (mockeadas) y gestión de configuración administrativa.
|
||||
"""
|
||||
from unittest.mock import AsyncMock, patch, MagicMock
|
||||
import pytest
|
||||
from pydantic import ValidationError
|
||||
from fastapi.testclient import TestClient
|
||||
import httpx
|
||||
from werkzeug.security import generate_password_hash
|
||||
|
||||
from main import app
|
||||
from schemas.ai_chat import ChatMessage, ChatRequest, ChatResponse, AISettingsSchema
|
||||
from routers.ai_chat import generate_system_prompt_for_role
|
||||
from app.models.setting import SystemSetting
|
||||
from app.models.user import User
|
||||
|
||||
|
||||
def test_ai_chat_pydantic_schemas_validation():
|
||||
"""Valida restricciones de datos y tipos en Pydantic v2."""
|
||||
# 1. ChatMessage válido
|
||||
msg = ChatMessage(role="user", content="¿Dónde curso hoy?")
|
||||
assert msg.role == "user"
|
||||
assert msg.content == "¿Dónde curso hoy?"
|
||||
|
||||
# 2. Rechazo por rol inválido
|
||||
with pytest.raises(ValidationError):
|
||||
ChatMessage(role="hacker", content="Mensaje")
|
||||
|
||||
# 3. Rechazo por contenido vacío
|
||||
with pytest.raises(ValidationError):
|
||||
ChatMessage(role="user", content="")
|
||||
|
||||
# 4. ChatRequest válido
|
||||
req = ChatRequest(messages=[msg], user_role="docente")
|
||||
assert len(req.messages) == 1
|
||||
assert req.user_role == "docente"
|
||||
|
||||
# 5. ChatRequest rechaza lista vacía de mensajes
|
||||
with pytest.raises(ValidationError):
|
||||
ChatRequest(messages=[], user_role="alumno")
|
||||
|
||||
# 6. AISettingsSchema con validaciones de rango
|
||||
ai_settings = AISettingsSchema(temperature=0.7, timeout=120)
|
||||
assert ai_settings.temperature == 0.7
|
||||
assert ai_settings.timeout == 120
|
||||
|
||||
with pytest.raises(ValidationError):
|
||||
AISettingsSchema(temperature=3.5) # Mayor a 2.0
|
||||
|
||||
|
||||
def test_system_prompt_generation_by_role():
|
||||
"""Verifica que el System Prompt inyectado se adapte rigurosamente al rol."""
|
||||
# Docente
|
||||
prompt_docente = generate_system_prompt_for_role("docente")
|
||||
assert "DOCENTES y PROFESORES" in prompt_docente
|
||||
assert "Gradebook" in prompt_docente
|
||||
assert "Google Meet" in prompt_docente
|
||||
assert "Bedelía" in prompt_docente # Restricción de permisos
|
||||
|
||||
# Alumno
|
||||
prompt_alumno = generate_system_prompt_for_role("alumno")
|
||||
assert "ESTUDIANTES y ALUMNOS" in prompt_alumno
|
||||
assert "Cartelera de Clases de Hoy" in prompt_alumno
|
||||
assert "TERMINANTEMENTE PROHIBIDO" in prompt_alumno
|
||||
|
||||
# Bedelía / Admin
|
||||
prompt_bedelia = generate_system_prompt_for_role("bedelia")
|
||||
assert "BEDELÍA y ADMINISTRADORES" in prompt_bedelia
|
||||
assert "Circuito Secuencial Canónico de 9 Pasos" in prompt_bedelia
|
||||
assert "Matriz de Conflictos" in prompt_bedelia
|
||||
assert "Moodle 4.1" in prompt_bedelia
|
||||
|
||||
|
||||
def test_ai_chat_public_config_endpoint():
|
||||
"""Verifica el endpoint público /api/chat/config utilizado por el widget."""
|
||||
client = TestClient(app)
|
||||
response = client.get("/api/chat/config")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["status"] == "success"
|
||||
assert "enabled" in data
|
||||
assert "model" in data
|
||||
|
||||
|
||||
def test_ai_chat_endpoint_success_mock(init_database):
|
||||
"""Simula una respuesta exitosa 200 de Llama.cpp."""
|
||||
client = TestClient(app)
|
||||
|
||||
mock_response = MagicMock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.json.return_value = {
|
||||
"choices": [
|
||||
{
|
||||
"message": {
|
||||
"role": "assistant",
|
||||
"content": "Para consultar el aula de cursada hoy, accede a la Cartelera de Clases en tu panel principal."
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
mock_response.raise_for_status = MagicMock()
|
||||
|
||||
with patch("httpx.AsyncClient.post", new_callable=AsyncMock) as mock_post:
|
||||
mock_post.return_value = mock_response
|
||||
|
||||
# Prueba en /api/chat
|
||||
res = client.post("/api/chat", json={
|
||||
"messages": [{"role": "user", "content": "¿En qué aula curso hoy?"}],
|
||||
"user_role": "alumno"
|
||||
})
|
||||
assert res.status_code == 200
|
||||
data = res.json()
|
||||
assert "response" in data
|
||||
assert "Cartelera" in data["response"]
|
||||
|
||||
# Prueba en /api/v1/chat
|
||||
res_v1 = client.post("/api/v1/chat", json={
|
||||
"messages": [{"role": "user", "content": "¿Cómo reservo un aula?"}],
|
||||
"user_role": "docente"
|
||||
})
|
||||
assert res_v1.status_code == 200
|
||||
|
||||
|
||||
def test_ai_chat_endpoint_validation_error():
|
||||
"""Valida que una petición malformada responda 422 Unprocessable Entity."""
|
||||
client = TestClient(app)
|
||||
|
||||
# Mensajes vacíos
|
||||
res = client.post("/api/chat", json={
|
||||
"messages": [],
|
||||
"user_role": "alumno"
|
||||
})
|
||||
assert res.status_code == 422
|
||||
|
||||
# Mensaje sin contenido
|
||||
res2 = client.post("/api/chat", json={
|
||||
"messages": [{"role": "user", "content": ""}]
|
||||
})
|
||||
assert res2.status_code == 422
|
||||
|
||||
|
||||
def test_ai_chat_endpoint_connection_failure():
|
||||
"""Simula error de conexión con el servidor Llama.cpp y verifica HTTP 503."""
|
||||
client = TestClient(app)
|
||||
|
||||
with patch("httpx.AsyncClient.post", new_callable=AsyncMock) as mock_post:
|
||||
mock_post.side_effect = httpx.ConnectError("Connection refused")
|
||||
|
||||
res = client.post("/api/chat", json={
|
||||
"messages": [{"role": "user", "content": "Hola tutor"}]
|
||||
})
|
||||
assert res.status_code == 503
|
||||
data = res.json()
|
||||
assert "detail" in data
|
||||
assert "10.0.0.214" in data["detail"] or "motor de IA" in data["detail"]
|
||||
|
||||
|
||||
def test_ai_chat_endpoint_timeout_failure():
|
||||
"""Simula tiempo de espera agotado y verifica HTTP 504."""
|
||||
client = TestClient(app)
|
||||
|
||||
with patch("httpx.AsyncClient.post", new_callable=AsyncMock) as mock_post:
|
||||
mock_post.side_effect = httpx.TimeoutException("Read timed out")
|
||||
|
||||
res = client.post("/api/chat", json={
|
||||
"messages": [{"role": "user", "content": "Pregunta compleja"}]
|
||||
})
|
||||
assert res.status_code == 504
|
||||
data = res.json()
|
||||
assert "tiempo límite" in data["detail"]
|
||||
|
||||
|
||||
def test_ai_chat_disabled_flow(init_database):
|
||||
"""Verifica que si ai_chat_enabled es false, se retorne 503 inmediatamente."""
|
||||
client = TestClient(app)
|
||||
|
||||
SystemSetting.set_value("ai_chat_enabled", "false", category="ai")
|
||||
try:
|
||||
res = client.post("/api/chat", json={
|
||||
"messages": [{"role": "user", "content": "Test"}]
|
||||
})
|
||||
assert res.status_code == 503
|
||||
assert "deshabilitado" in res.json()["detail"].lower()
|
||||
finally:
|
||||
SystemSetting.set_value("ai_chat_enabled", "true", category="ai")
|
||||
|
||||
|
||||
def test_admin_settings_ai_crud_and_test_connection(init_database):
|
||||
"""Verifica los endpoints administrativos de guardado y prueba de conexión de IA."""
|
||||
client = TestClient(app)
|
||||
|
||||
# 1. Crear usuario Admin
|
||||
u = User(
|
||||
email="admin.ai@unicaba.edu.ar",
|
||||
name="Admin AI Settings",
|
||||
password_hash=generate_password_hash("AiAdminSecret!"),
|
||||
role="ADMIN",
|
||||
is_active=True
|
||||
)
|
||||
init_database.session.add(u)
|
||||
init_database.session.commit()
|
||||
|
||||
# Login
|
||||
login_res = client.post("/api/v1/auth/login", json={
|
||||
"email": "admin.ai@unicaba.edu.ar",
|
||||
"password": "AiAdminSecret!"
|
||||
})
|
||||
token = login_res.json()["access_token"]
|
||||
headers = {"Authorization": f"Bearer {token}"}
|
||||
|
||||
# 2. Consultar GET /admin/settings/all
|
||||
all_res = client.get("/api/v1/settings/all", headers=headers)
|
||||
assert all_res.status_code == 200
|
||||
all_data = all_res.json()
|
||||
assert "ai" in all_data["settings"]
|
||||
assert all_data["settings"]["ai"]["model"] == "qwen3-coder:30b"
|
||||
|
||||
# 3. Actualizar POST /admin/settings/ai
|
||||
update_res = client.post("/api/v1/settings/ai", headers=headers, json={
|
||||
"enabled": True,
|
||||
"base_url": "http://10.0.0.214:8080/v1",
|
||||
"api_key": "new_secret_key",
|
||||
"model": "qwen3-coder:30b",
|
||||
"model_name": "Qwen3 Coder 30B (Test)",
|
||||
"temperature": 0.25,
|
||||
"timeout": 45,
|
||||
"context_limit": 32768,
|
||||
"output_limit": 16384
|
||||
})
|
||||
assert update_res.status_code == 200
|
||||
assert update_res.json()["status"] == "success"
|
||||
|
||||
# Verificar persistencia en base de datos
|
||||
assert SystemSetting.get_value("ai_chat_model_name") == "Qwen3 Coder 30B (Test)"
|
||||
assert SystemSetting.get_decrypted_value("ai_chat_api_key") == "new_secret_key"
|
||||
|
||||
# 4. Probar conexión POST /admin/settings/ai/test con mock
|
||||
mock_models_resp = MagicMock()
|
||||
mock_models_resp.status_code = 200
|
||||
mock_models_resp.json.return_value = {
|
||||
"data": [{"id": "qwen3-coder:30b"}, {"id": "llama-3-8b"}]
|
||||
}
|
||||
|
||||
with patch("httpx.AsyncClient.get", new_callable=AsyncMock) as mock_get:
|
||||
mock_get.return_value = mock_models_resp
|
||||
|
||||
test_res = client.post("/api/v1/settings/ai/test", headers=headers, json={
|
||||
"base_url": "http://10.0.0.214:8080/v1",
|
||||
"api_key": "new_secret_key"
|
||||
})
|
||||
assert test_res.status_code == 200
|
||||
test_data = test_res.json()
|
||||
assert test_data["status"] == "success"
|
||||
assert "qwen3-coder:30b" in test_data["models"]
|
||||
assert test_data["latency_ms"] is not None
|
||||
Reference in New Issue
Block a user