|
| 1 | +"""Tests for context assembly and database history queries.""" |
| 2 | + |
| 3 | +import tempfile |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from agentic_node_ops.context import build_hermes_context |
| 9 | +from agentic_node_ops.database import Database |
| 10 | +from agentic_node_ops.types import NotificationPayload, Severity |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def temp_db(): |
| 15 | + """Provide a temporary SQLite database for testing.""" |
| 16 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 17 | + db_path = Path(tmpdir) / "test_incidents.db" |
| 18 | + db = Database(db_path=str(db_path)) |
| 19 | + yield db |
| 20 | + |
| 21 | + |
| 22 | +def test_get_runbook_stats_empty(temp_db: Database): |
| 23 | + """Test get_runbook_stats returns 0.0 success rate when no data exists.""" |
| 24 | + stats = temp_db.get_runbook_stats("test_runbook") |
| 25 | + assert stats["success_rate"] == 0.0 |
| 26 | + assert stats["failed_cases"] == ["None recorded"] |
| 27 | + |
| 28 | + |
| 29 | +def test_get_runbook_stats_with_data(temp_db: Database): |
| 30 | + """Test get_runbook_stats calculates success rate correctly.""" |
| 31 | + with temp_db._get_connection() as conn: |
| 32 | + conn.execute( |
| 33 | + """ |
| 34 | + INSERT INTO runbook_outcomes (id, runbook_id, host, action_taken, outcome, time_to_resolve) |
| 35 | + VALUES ('1', 'rb1', 'host1', 'restart', 'resolved', 60), |
| 36 | + ('2', 'rb1', 'host1', 'restart', 'did_not_help', 30), |
| 37 | + ('3', 'rb1', 'host2', 'wipe', 'resolved', 120) |
| 38 | + """ |
| 39 | + ) |
| 40 | + conn.commit() |
| 41 | + |
| 42 | + stats = temp_db.get_runbook_stats("rb1") |
| 43 | + assert stats["success_rate"] == pytest.approx(2 / 3) |
| 44 | + assert len(stats["failed_cases"]) == 1 |
| 45 | + assert "restart (did_not_help)" in stats["failed_cases"][0] |
| 46 | + |
| 47 | + |
| 48 | +def test_get_host_baselines_empty(temp_db: Database): |
| 49 | + """Test get_host_baselines returns empty dict when no data exists.""" |
| 50 | + baselines = temp_db.get_host_baselines("host1") |
| 51 | + assert baselines == {} |
| 52 | + |
| 53 | + |
| 54 | +def test_get_host_baselines_with_data(temp_db: Database): |
| 55 | + """Test get_host_baselines returns correct metrics.""" |
| 56 | + with temp_db._get_connection() as conn: |
| 57 | + conn.execute( |
| 58 | + """ |
| 59 | + INSERT INTO host_fingerprints (host, metric, baseline_p50, baseline_p95, last_updated) |
| 60 | + VALUES ('host1', 'peer_count', 45.0, 60.0, '2024-01-01'), |
| 61 | + ('host1', 'validator_count', 10.0, 12.0, '2024-01-01') |
| 62 | + """ |
| 63 | + ) |
| 64 | + conn.commit() |
| 65 | + |
| 66 | + baselines = temp_db.get_host_baselines("host1") |
| 67 | + assert len(baselines) == 2 |
| 68 | + assert baselines["peer_count"]["p50"] == 45.0 |
| 69 | + assert baselines["peer_count"]["p95"] == 60.0 |
| 70 | + assert baselines["validator_count"]["p50"] == 10.0 |
| 71 | + |
| 72 | + |
| 73 | +def test_build_hermes_context_no_history(temp_db: Database): |
| 74 | + """Test build_hermes_context formats correctly with no history.""" |
| 75 | + payload = NotificationPayload( |
| 76 | + incident_id="inc-1", |
| 77 | + alert_type="consensus_desync", |
| 78 | + severity=Severity.HIGH, |
| 79 | + host="node-1", |
| 80 | + title="Consensus Desync on node-1", |
| 81 | + summary="Node is out of sync", |
| 82 | + diagnostics={"peer_count": "2", "syncing": "true"}, |
| 83 | + runbook_id="consensus_desync", |
| 84 | + ) |
| 85 | + |
| 86 | + context = build_hermes_context(payload, temp_db) |
| 87 | + |
| 88 | + assert "consensus_desync" in context |
| 89 | + assert "node-1" in context |
| 90 | + assert "No prior incidents recorded" in context |
| 91 | + assert "No operator corrections recorded" in context |
| 92 | + assert "0% of the time" in context |
| 93 | + assert "No baselines recorded" in context |
| 94 | + assert '"peer_count": "2"' in context |
| 95 | + |
| 96 | + |
| 97 | +def test_build_hermes_context_with_history(temp_db: Database): |
| 98 | + """Test build_hermes_context includes history and baselines when present.""" |
| 99 | + # Insert incident |
| 100 | + with temp_db._get_connection() as conn: |
| 101 | + conn.execute( |
| 102 | + """ |
| 103 | + INSERT INTO incidents (id, alert_type, host, severity, fired_at, outcome, hermes_analysis) |
| 104 | + VALUES ('inc-0', 'consensus_desync', 'node-1', 'high', '2024-01-01 10:00:00', 'resolved', 'Restarted peer') |
| 105 | + """ |
| 106 | + ) |
| 107 | + # Insert correction |
| 108 | + conn.execute( |
| 109 | + """ |
| 110 | + INSERT INTO operator_corrections (id, incident_id, alert_type, host, correction) |
| 111 | + VALUES ('corr-1', 'inc-0', 'consensus_desync', 'node-1', 'Always check peer count first') |
| 112 | + """ |
| 113 | + ) |
| 114 | + # Insert runbook outcome |
| 115 | + conn.execute( |
| 116 | + """ |
| 117 | + INSERT INTO runbook_outcomes (id, runbook_id, host, action_taken, outcome, time_to_resolve) |
| 118 | + VALUES ('out-1', 'consensus_desync', 'node-1', 'restart_consensus_client', 'resolved', 60) |
| 119 | + """ |
| 120 | + ) |
| 121 | + # Insert baseline |
| 122 | + conn.execute( |
| 123 | + """ |
| 124 | + INSERT INTO host_fingerprints (host, metric, baseline_p50, baseline_p95, last_updated) |
| 125 | + VALUES ('node-1', 'peer_count', 50.0, 80.0, '2024-01-01') |
| 126 | + """ |
| 127 | + ) |
| 128 | + conn.commit() |
| 129 | + |
| 130 | + payload = NotificationPayload( |
| 131 | + incident_id="inc-1", |
| 132 | + alert_type="consensus_desync", |
| 133 | + severity=Severity.HIGH, |
| 134 | + host="node-1", |
| 135 | + title="Consensus Desync on node-1", |
| 136 | + summary="Node is out of sync", |
| 137 | + diagnostics={"peer_count": "2"}, |
| 138 | + runbook_id="consensus_desync", |
| 139 | + ) |
| 140 | + |
| 141 | + context = build_hermes_context(payload, temp_db) |
| 142 | + |
| 143 | + assert "resolved (severity: high, analysis: Restarted peer)" in context |
| 144 | + assert "Always check peer count first" in context |
| 145 | + assert "100% of the time" in context |
| 146 | + assert "peer_count: p50=50.0, p95=80.0" in context |
0 commit comments