Files
Timmy-time-dashboard/tests/unit/test_health_routes.py
kimi c3de29212f
Some checks failed
Tests / lint (pull_request) Has been cancelled
Tests / test (pull_request) Has been cancelled
chore: resolve 15 low-hanging-fruit tech debt issues
2026-03-22 11:27:47 -04:00

92 lines
2.9 KiB
Python

"""Tests for the health and sovereignty endpoints."""
from unittest.mock import patch
import pytest
from fastapi.testclient import TestClient
from dashboard.app import app
from dashboard.routes.health import DependencyStatus
pytestmark = pytest.mark.unit
client = TestClient(app)
@pytest.fixture
def mock_ollama_healthy():
with patch("dashboard.routes.health.check_ollama", return_value=True):
yield
@pytest.fixture
def mock_ollama_unavailable():
with patch("dashboard.routes.health.check_ollama", return_value=False):
yield
@pytest.fixture
def mock_check_ollama_sovereignty():
dep = DependencyStatus(
name="Ollama AI",
status="healthy",
sovereignty_score=10,
details={"url": "http://localhost:11434"},
)
with patch("dashboard.routes.health._check_ollama", return_value=dep):
yield
def test_health_check_healthy(mock_ollama_healthy):
"""Test legacy health check endpoint when Ollama is up."""
response = client.get("/health")
assert response.status_code == 200
data = response.json()
assert data["status"] == "ok"
assert data["services"]["ollama"] == "up"
assert data["agents"]["agent"]["status"] == "idle"
def test_health_check_degraded(mock_ollama_unavailable):
"""Test legacy health check endpoint when Ollama is down."""
response = client.get("/health")
assert response.status_code == 200
data = response.json()
assert data["status"] == "degraded"
assert data["services"]["ollama"] == "down"
assert data["agents"]["agent"]["status"] == "offline"
def test_health_status_panel_healthy(mock_ollama_healthy):
"""Test HTML status panel rendering."""
response = client.get("/health/status")
assert response.status_code == 200
assert "text/html" in response.headers["content-type"]
assert "UP" in response.text
assert "#10b981" in response.text
def test_sovereignty_check(mock_check_ollama_sovereignty):
"""Test comprehensive sovereignty audit report."""
with (
patch("dashboard.routes.health._check_lightning") as mock_lightning,
patch("dashboard.routes.health._check_sqlite") as mock_sqlite,
):
mock_lightning.return_value = DependencyStatus(
name="Lightning", status="unavailable", sovereignty_score=8, details={}
)
mock_sqlite.return_value = DependencyStatus(
name="SQLite", status="healthy", sovereignty_score=10, details={}
)
response = client.get("/health/sovereignty")
assert response.status_code == 200
data = response.json()
# (10 + 8 + 10) / 3 = 9.3
assert data["overall_score"] == 9.3
assert len(data["dependencies"]) == 3
# Ensure recommendations contain note about unavailable dependency
recommendations = " ".join(data["recommendations"])
assert "unavailable" in recommendations.lower()