forked from Rockachopa/Timmy-time-dashboard
chore: resolve 15 low-hanging-fruit tech debt issues
This commit is contained in:
80
tests/unit/test_db_pool.py
Normal file
80
tests/unit/test_db_pool.py
Normal file
@@ -0,0 +1,80 @@
|
||||
"""Tests for the thread-local SQLite ConnectionPool."""
|
||||
|
||||
import sqlite3
|
||||
import threading
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from infrastructure.db_pool import ConnectionPool
|
||||
|
||||
pytestmark = pytest.mark.unit
|
||||
|
||||
|
||||
def test_pool_creates_connection(tmp_path: Path):
|
||||
"""Test that the pool successfully creates a SQLite connection."""
|
||||
db_file = tmp_path / "test.db"
|
||||
pool = ConnectionPool(db_file)
|
||||
|
||||
conn = pool.get_connection()
|
||||
assert isinstance(conn, sqlite3.Connection)
|
||||
|
||||
cursor = conn.execute("SELECT 1")
|
||||
assert cursor.fetchone()[0] == 1
|
||||
|
||||
|
||||
def test_pool_reuses_connection_same_thread(tmp_path: Path):
|
||||
"""Test that multiple calls in the same thread return the same connection."""
|
||||
db_file = tmp_path / "test.db"
|
||||
pool = ConnectionPool(db_file)
|
||||
|
||||
conn1 = pool.get_connection()
|
||||
conn2 = pool.get_connection()
|
||||
assert conn1 is conn2
|
||||
|
||||
|
||||
def test_pool_different_connections_different_threads(tmp_path: Path):
|
||||
"""Test that different threads receive distinct connections."""
|
||||
db_file = tmp_path / "test.db"
|
||||
pool = ConnectionPool(db_file)
|
||||
|
||||
conn1 = pool.get_connection()
|
||||
conn2_list = []
|
||||
|
||||
def _worker():
|
||||
conn2_list.append(pool.get_connection())
|
||||
|
||||
thread = threading.Thread(target=_worker)
|
||||
thread.start()
|
||||
thread.join()
|
||||
|
||||
assert len(conn2_list) == 1
|
||||
conn2 = conn2_list[0]
|
||||
assert conn1 is not conn2
|
||||
|
||||
|
||||
def test_pool_close_connection(tmp_path: Path):
|
||||
"""Test that connection is closed and cleared from thread local."""
|
||||
db_file = tmp_path / "test.db"
|
||||
pool = ConnectionPool(db_file)
|
||||
|
||||
conn1 = pool.get_connection()
|
||||
pool.close_connection()
|
||||
|
||||
# Getting a new connection should create a new object
|
||||
conn2 = pool.get_connection()
|
||||
assert conn1 is not conn2
|
||||
|
||||
|
||||
def test_pool_context_manager(tmp_path: Path):
|
||||
"""Test that the context manager yields a connection and closes it after."""
|
||||
db_file = tmp_path / "test.db"
|
||||
pool = ConnectionPool(db_file)
|
||||
|
||||
with pool.connection() as conn1:
|
||||
assert isinstance(conn1, sqlite3.Connection)
|
||||
|
||||
# After exiting the context manager, the connection should be closed implicitly
|
||||
# resulting in a new connection object for the next request.
|
||||
conn2 = pool.get_connection()
|
||||
assert conn1 is not conn2
|
||||
91
tests/unit/test_health_routes.py
Normal file
91
tests/unit/test_health_routes.py
Normal file
@@ -0,0 +1,91 @@
|
||||
"""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()
|
||||
Reference in New Issue
Block a user