feat: complete Phase 6 - Hybrid Coexistence testing, PWA kiosk mode, static SPA mount and Pilot manual (v0.6.0-beta)

This commit is contained in:
Carlos Tello
2026-10-13 15:58:47 -03:00
parent 1ba74f7929
commit a99b928bb5
12 changed files with 450 additions and 10 deletions
+194
View File
@@ -0,0 +1,194 @@
# ==============================================================================
# Pruebas de Convivencia Híbrida (Fase 6: Piloto y Compatibilidad Desktop / Web)
# ==============================================================================
from datetime import datetime
import pytest
from httpx import AsyncClient, ASGITransport
from app.main import app
from app.core.database import db_session
@pytest.mark.asyncio
async def test_convivencia_ingreso_desktop_cierre_desde_web():
"""
Escenario 1: Un ingreso generado por el puesto de escritorio existente (PyQt / VB6)
es visualizado inmediatamente en la web y puede ser egresado con apertura de barrera.
"""
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://test") as client:
# Autenticación web
login_res = await client.post(
"/api/v1/auth/login",
json={"username": "guardia", "password": "guardia"},
)
token = login_res.cookies.get("access_token")
# 1. Simulación de ingreso registrado por el sistema de escritorio existente
test_ticket_id = 8899001122
doc_desktop = "88776655"
ahora = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
with db_session() as conn:
cursor = conn.cursor()
# Asegurar persona
cursor.execute("SELECT TOP 1 PersonaID FROM PerDocCatVal WHERE Documento = %s", (doc_desktop,))
row = cursor.fetchone()
if row:
pid = row[0]
else:
cursor.execute("SELECT COALESCE(MAX(PersonaID), 0) + 1 FROM Per")
pid = cursor.fetchone()[0]
cursor.execute(
"""
INSERT INTO Per (PersonaID, Apellido, Nombre, Sexo, FechaInicio, FechaFin, FechaNacimiento, CREATEdByID, LastUpdateDateByID)
VALUES (%s, 'PILOTO_DESKTOP', 'JUAN', 'M', '2026-01-01', '2099-12-31', '1990-01-01', 2, 2)
""",
(pid,),
)
cursor.execute(
"INSERT INTO PerDocCatVal (PersonaID, DocumentoID, CategoriaID, ValorID, Documento) VALUES (%s, 100000, 11, 1, %s)",
(pid, doc_desktop),
)
# Insertar ticket activo estilo Desktop legado
cursor.execute(
"INSERT INTO tck (TicketID, Numero, FechaHoraIngreso, ModuloIngresoID) VALUES (%s, 1234, %s, 100004)",
(test_ticket_id, ahora),
)
cursor.execute(
"INSERT INTO tckper (TicketID, PersonaID, RolID, EsMenor) VALUES (%s, %s, 1, 0)",
(test_ticket_id, pid),
)
cursor.execute("INSERT INTO tckprx (TicketID, PropiedadID, Valor) VALUES (%s, 4, 'DSK111')", (test_ticket_id,))
cursor.execute("INSERT INTO tckprx (TicketID, PropiedadID, Valor) VALUES (%s, 7, '200')", (test_ticket_id,))
try:
# 2. Consultar tickets activos desde la Web
active_res = await client.get("/api/v1/access/tickets/active", cookies={"access_token": token})
assert active_res.status_code == 200
tickets = active_res.json()
ticket_encontrado = next((t for t in tickets if t["ticket_id"] == test_ticket_id), None)
assert ticket_encontrado is not None
assert ticket_encontrado["patente"] == "DSK111"
assert ticket_encontrado["apellido"] == "PILOTO_DESKTOP"
# 3. Registrar Salida desde la Web
exit_res = await client.post(
"/api/v1/access/exit",
json={
"ticket_id": test_ticket_id,
"modulo_salida_id": 100005,
"patente_salida": "DSK111",
"abrir_barrera": True,
},
cookies={"access_token": token},
)
assert exit_res.status_code == 200
assert exit_res.json()["status"] == "ok"
assert exit_res.json()["barrier_opened"] is True
# 4. Verificar que se archivó en tckhst y se liberó de tck
with db_session() as conn:
cursor = conn.cursor()
cursor.execute("SELECT TicketID FROM tck WHERE TicketID = %s", (test_ticket_id,))
assert cursor.fetchone() is None
cursor.execute("SELECT TicketID FROM tckhst WHERE TicketID = %s", (test_ticket_id,))
assert cursor.fetchone() is not None
finally:
# Limpieza de registros de prueba
with db_session() as conn:
cursor = conn.cursor()
cursor.execute("DELETE FROM tckper WHERE TicketID = %s", (test_ticket_id,))
cursor.execute("DELETE FROM tckprx WHERE TicketID = %s", (test_ticket_id,))
cursor.execute("DELETE FROM tck WHERE TicketID = %s", (test_ticket_id,))
cursor.execute("DELETE FROM tckhst WHERE TicketID = %s", (test_ticket_id,))
cursor.execute("DELETE FROM PerDocCatVal WHERE Documento = %s", (doc_desktop,))
cursor.execute("DELETE FROM Per WHERE Apellido = 'PILOTO_DESKTOP'")
cursor.execute("DELETE FROM pdtmdl WHERE ModuloID IN (100004, 100005) AND Extra = %s", (str(test_ticket_id),))
@pytest.mark.asyncio
async def test_convivencia_ingreso_web_cierre_desde_desktop():
"""
Escenario 2: Un ingreso realizado desde la aplicación web genera las filas exactas
en tck, tckper, tckprx que el sistema legado de escritorio lee, valida y egresa.
"""
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://test") as client:
login_res = await client.post(
"/api/v1/auth/login",
json={"username": "guardia", "password": "guardia"},
)
token = login_res.cookies.get("access_token")
doc_web = "88776656"
# 1. Registrar Ingreso desde la Web
entry_payload = {
"modulo_ingreso_id": 100004,
"documento": doc_web,
"apellido": "PILOTO_WEB",
"nombre": "MARCOS",
"sexo": "M",
"patente": "WEB222",
"lote_codigo": "305",
"motivo": "VISITA PILOTO",
"cantidad_ocupantes": 1,
"abrir_barrera": True,
}
entry_res = await client.post("/api/v1/access/entry", json=entry_payload, cookies={"access_token": token})
assert entry_res.status_code == 200
ticket_id = entry_res.json()["ticket_id"]
try:
# 2. Verificar que las tablas vivas contengan exactamente la estructura requerida por Desktop
with db_session() as conn:
cursor = conn.cursor(as_dict=True)
cursor.execute("SELECT * FROM tck WHERE TicketID = %s", (ticket_id,))
tck_row = cursor.fetchone()
assert tck_row is not None
assert tck_row["ModuloIngresoID"] == 100004
cursor.execute("SELECT * FROM tckper WHERE TicketID = %s", (ticket_id,))
tp_row = cursor.fetchone()
assert tp_row is not None
assert tp_row["RolID"] == 1
cursor.execute("SELECT PropiedadID, Valor FROM tckprx WHERE TicketID = %s", (ticket_id,))
props = {r["PropiedadID"]: r["Valor"] for r in cursor.fetchall()}
assert props.get(4) == "WEB222" # Patente
assert props.get(7) == "305" # Lote
# 3. Simular que el sistema de escritorio registra la salida (mueve a tckhst y borra de tck)
ahora = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
with db_session() as conn:
cursor = conn.cursor()
cursor.execute(
"""
INSERT INTO tckhst (TicketID, Numero, FechaHoraIngreso, ModuloIngresoID, FechaHoraSalida, ModuloSalidaID)
VALUES (%s, 0, %s, 100004, %s, 100005)
""",
(ticket_id, ahora, ahora),
)
cursor.execute("DELETE FROM tckper WHERE TicketID = %s", (ticket_id,))
cursor.execute("DELETE FROM tckprx WHERE TicketID = %s", (ticket_id,))
cursor.execute("DELETE FROM tck WHERE TicketID = %s", (ticket_id,))
# 4. Verificar que la Web ya no lo liste como activo en predio
active_res = await client.get("/api/v1/access/tickets/active", cookies={"access_token": token})
assert active_res.status_code == 200
assert all(t["ticket_id"] != ticket_id for t in active_res.json())
finally:
# Limpieza
with db_session() as conn:
cursor = conn.cursor()
cursor.execute("DELETE FROM tckper WHERE TicketID = %s", (ticket_id,))
cursor.execute("DELETE FROM tckprx WHERE TicketID = %s", (ticket_id,))
cursor.execute("DELETE FROM tck WHERE TicketID = %s", (ticket_id,))
cursor.execute("DELETE FROM tckhst WHERE TicketID = %s", (ticket_id,))
cursor.execute("DELETE FROM PerDocCatVal WHERE Documento = %s", (doc_web,))
cursor.execute("DELETE FROM Per WHERE Apellido = 'PILOTO_WEB'")
cursor.execute("DELETE FROM pdtmdl WHERE ModuloID IN (100004, 100005) AND Extra = %s", (str(ticket_id),))