feat: add operational panel presentation helpers
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
from datetime import datetime, timedelta, timezone
|
||||
import pytest
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
from sqlalchemy import create_engine
|
||||
@@ -44,14 +45,27 @@ def test_password_and_event_redaction_and_retention():
|
||||
assert session.get(AgentEvent, old.id) is None and session.get(AgentEvent, keep.id) is not None
|
||||
|
||||
|
||||
def test_local_development_admin_accepts_operator_supplied_short_password():
|
||||
def test_short_local_password_requires_explicit_setting(monkeypatch):
|
||||
session_factory = setup_db()
|
||||
with session_factory() as session:
|
||||
monkeypatch.delenv("ALLOW_WEAK_DEV_PASSWORD", raising=False)
|
||||
with pytest.raises(ValueError):
|
||||
create_admin(session, "admin", "admin")
|
||||
monkeypatch.setenv("ALLOW_WEAK_DEV_PASSWORD", "1")
|
||||
admin = create_admin(session, "admin", "admin")
|
||||
assert admin.username == "admin"
|
||||
assert verify_password("admin", admin.password_hash)
|
||||
|
||||
|
||||
def test_production_admin_keeps_strong_password_minimum(monkeypatch):
|
||||
session_factory = setup_db()
|
||||
with session_factory() as session:
|
||||
monkeypatch.setenv("APP_ENV", "production")
|
||||
monkeypatch.setenv("ALLOW_WEAK_DEV_PASSWORD", "1")
|
||||
with pytest.raises(ValueError):
|
||||
create_admin(session, "admin", "admin")
|
||||
|
||||
|
||||
def test_health_model_separates_safe_blocked_from_technical_failure():
|
||||
now = datetime.now(timezone.utc)
|
||||
recent = SimpleNamespace(last_seen_at=now, failed_products=0, last_job_status="COMPLETED")
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from types import SimpleNamespace
|
||||
|
||||
from central.app.panel_presentation import (
|
||||
format_age,
|
||||
format_duration,
|
||||
format_ui_datetime,
|
||||
health_label,
|
||||
operational_priority,
|
||||
)
|
||||
|
||||
|
||||
def test_ui_times_use_configured_timezone_and_human_age(monkeypatch):
|
||||
monkeypatch.setenv("UI_TIMEZONE", "America/Bahia")
|
||||
value = datetime(2026, 9, 22, 19, 11, tzinfo=timezone.utc)
|
||||
|
||||
assert format_ui_datetime(value) == "22/09/2026 16:11"
|
||||
assert format_age(value, datetime(2026, 9, 22, 19, 19, tzinfo=timezone.utc)) == "há 8 min"
|
||||
|
||||
|
||||
def test_ui_datetime_treats_naive_values_as_utc():
|
||||
value = datetime(2026, 9, 22, 19, 11)
|
||||
|
||||
assert format_ui_datetime(value) == "22/09/2026 16:11"
|
||||
|
||||
|
||||
def test_age_handles_empty_and_future_values():
|
||||
now = datetime(2026, 9, 22, 19, 19, tzinfo=timezone.utc)
|
||||
|
||||
assert format_age(None, now) == "-"
|
||||
assert format_age(now + timedelta(minutes=1), now) == "agora"
|
||||
assert format_age(now - timedelta(hours=2), now) == "há 2 h"
|
||||
|
||||
|
||||
def test_duration_and_priority_are_deterministic():
|
||||
start = datetime(2026, 9, 22, 19, 0, tzinfo=timezone.utc)
|
||||
finish = start + timedelta(seconds=194)
|
||||
|
||||
assert format_duration(None, finish) == "-"
|
||||
assert format_duration(start, None) == "em andamento"
|
||||
assert format_duration(start, finish) == "3 min 14 s"
|
||||
assert operational_priority(SimpleNamespace(blocker_type="TECHNICAL_FAILURE", field="", reason_code="X")) == "CRITICAL"
|
||||
|
||||
|
||||
def test_health_label_maps_known_states_and_preserves_unknown_states():
|
||||
assert health_label("ONLINE") == "Online"
|
||||
assert health_label("DEGRADED") == "Degradado"
|
||||
assert health_label("OFFLINE") == "Offline"
|
||||
assert health_label("FAILED") == "Falha"
|
||||
assert health_label("UNKNOWN") == "Desconhecido"
|
||||
assert health_label("NEW_STATE") == "NEW_STATE"
|
||||
Reference in New Issue
Block a user