372 lines
13 KiB
Python
372 lines
13 KiB
Python
"""
|
|
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 y contenga UdeCi y RAG."""
|
|
# Docente
|
|
prompt_docente = generate_system_prompt_for_role("docente")
|
|
assert "UdeCi" in prompt_docente
|
|
assert "DOCENTE" in prompt_docente
|
|
assert "INFORMACIÓN OFICIAL DEL SISTEMA (RAG)" in prompt_docente
|
|
|
|
# Alumno
|
|
prompt_alumno = generate_system_prompt_for_role("alumno")
|
|
assert "UdeCi" in prompt_alumno
|
|
assert "ALUMNO" in prompt_alumno
|
|
assert "INFORMACIÓN OFICIAL DEL SISTEMA (RAG)" in prompt_alumno
|
|
|
|
# Bedelía
|
|
prompt_bedelia = generate_system_prompt_for_role("bedelia", user_name="Juan")
|
|
assert "UdeCi" in prompt_bedelia
|
|
assert "Juan" in prompt_bedelia
|
|
assert ("BEDELÍA" in prompt_bedelia or "BEDELIA" in prompt_bedelia)
|
|
assert "INFORMACIÓN OFICIAL DEL SISTEMA (RAG)" in prompt_bedelia
|
|
|
|
# Admin
|
|
prompt_admin = generate_system_prompt_for_role("admin", user_name="Carlos")
|
|
assert "UdeCi" in prompt_admin
|
|
assert "Carlos" in prompt_admin
|
|
assert "ADMINISTRADOR" in prompt_admin
|
|
assert "Gobierno & Sistema" in prompt_admin
|
|
|
|
|
|
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 data["name"] == "UdeCi"
|
|
assert "role" in data
|
|
assert "default_options" 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(init_database):
|
|
"""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 "UdeCi" in data["detail"] or "servidor local de IA" in data["detail"]
|
|
|
|
|
|
def test_ai_chat_endpoint_timeout_failure(init_database):
|
|
"""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
|
|
|
|
|
|
def test_live_context_service_roles_and_entities(init_database):
|
|
"""Verifica la extracción contextual de entidades reales adaptada a cada rol."""
|
|
from app.services.live_context_service import LiveContextService
|
|
from app.models.career import Career
|
|
from app.models.subject import Subject, Commission
|
|
from app.models.classroom import Classroom
|
|
from app.models.enrollment import StudentEnrollment
|
|
from app import db
|
|
|
|
# 1. Crear entidades de prueba
|
|
career = Career(name="Ingeniería en Software", code="ING-SOFT")
|
|
db.session.add(career)
|
|
db.session.flush()
|
|
|
|
subj = Subject(name="Algoritmos y Estructuras de Datos", code="ALG101", career_id=career.id)
|
|
db.session.add(subj)
|
|
db.session.flush()
|
|
|
|
teacher = User(
|
|
name="Profesor Alan Turing",
|
|
email="turing@unicaba.edu.ar",
|
|
role="Docente",
|
|
is_active=True
|
|
)
|
|
teacher.set_password("pass123")
|
|
student = User(
|
|
name="Ada Lovelace",
|
|
email="ada@unicaba.edu.ar",
|
|
role="Alumno",
|
|
is_active=True
|
|
)
|
|
student.set_password("pass123")
|
|
db.session.add_all([teacher, student])
|
|
db.session.flush()
|
|
|
|
comm = Commission(
|
|
subject_id=subj.id,
|
|
code="C1",
|
|
semester="1C",
|
|
year=2026,
|
|
teacher_id=teacher.id,
|
|
max_students=30,
|
|
current_students=1,
|
|
schedule="Martes 18:00 a 22:00",
|
|
shift="Noche"
|
|
)
|
|
db.session.add(comm)
|
|
db.session.flush()
|
|
|
|
enrollment = StudentEnrollment(
|
|
student_id=student.id,
|
|
commission_id=comm.id,
|
|
status="activo"
|
|
)
|
|
classroom = Classroom(
|
|
building="Sede Central",
|
|
code="Aula 204",
|
|
floor="2",
|
|
capacity=40,
|
|
is_active=True
|
|
)
|
|
db.session.add_all([enrollment, classroom])
|
|
db.session.commit()
|
|
|
|
# 2. Snapshot para Alumno
|
|
alumno_snapshot = LiveContextService.get_system_snapshot(
|
|
role="alumno",
|
|
user_id=str(student.id),
|
|
user_email=student.email,
|
|
user_name=student.name
|
|
)
|
|
assert "DATOS DE CURSADA DEL ALUMNO" in alumno_snapshot
|
|
assert "Algoritmos y Estructuras de Datos" in alumno_snapshot
|
|
assert "Comisión C1" in alumno_snapshot
|
|
assert "Alan Turing" in alumno_snapshot
|
|
|
|
# 3. Snapshot para Docente
|
|
docente_snapshot = LiveContextService.get_system_snapshot(
|
|
role="docente",
|
|
user_id=str(teacher.id),
|
|
user_email=teacher.email,
|
|
user_name=teacher.name
|
|
)
|
|
assert "DATOS ACADÉMICOS DEL DOCENTE" in docente_snapshot
|
|
assert "COMISIONES A TU CARGO" in docente_snapshot
|
|
assert "Algoritmos y Estructuras de Datos" in docente_snapshot
|
|
|
|
# 4. Snapshot para Bedelía
|
|
bedelia_snapshot = LiveContextService.get_system_snapshot(role="bedelia")
|
|
assert "BALANCE OPERATIVO DE BEDELÍA" in bedelia_snapshot
|
|
assert "Comisiones activas" in bedelia_snapshot
|
|
|
|
# 5. Snapshot para Administrador
|
|
admin_snapshot = LiveContextService.get_system_snapshot(role="admin")
|
|
assert "GOBIERNO Y ESTADÍSTICAS DEL SISTEMA" in admin_snapshot
|
|
assert "Alumnos" in admin_snapshot
|
|
|
|
# 6. Búsqueda por palabras clave en query
|
|
query_snapshot = LiveContextService.get_system_snapshot(
|
|
role="alumno",
|
|
query="¿Dónde curso algoritmos?"
|
|
)
|
|
assert "ASIGNATURAS COINCIDENTES" in query_snapshot
|
|
assert "ALG101" in query_snapshot
|