| import pytest |
| from fastapi.testclient import TestClient |
|
|
| from src.agents.nexus_oracle_agent import ( |
| ConsolidatedNexusState, |
| LongTermTrends, |
| NexusDependencies, |
| NexusOracleAgent, |
| ShortTermKPIs, |
| ) |
| from src.server.main import app |
|
|
| client = TestClient(app) |
|
|
|
|
| @pytest.mark.asyncio |
| async def test_nexus_oracle_agent_creation(): |
| """Verify that NexusOracleAgent can be initialized and configured.""" |
| agent = NexusOracleAgent() |
| assert agent.name == "NexusOracleAgent" |
| assert agent.model is not None |
| assert agent.get_system_prompt() is not None |
|
|
|
|
| @pytest.mark.asyncio |
| async def test_nexus_oracle_agent_mock_run(monkeypatch): |
| """Verify that the agent returns structured output conforming to ConsolidatedNexusState.""" |
| agent = NexusOracleAgent() |
| deps = NexusDependencies(request_id="test-nexus-oracle-run") |
|
|
| |
| async def mock_run_agent(user_prompt: str, deps: NexusDependencies) -> ConsolidatedNexusState: |
| return ConsolidatedNexusState( |
| system_status="GREEN", |
| health_score=95, |
| short_term_kpis=ShortTermKPIs( |
| daily_token_cost=0.05, |
| active_error_counts=0, |
| system_telemetry={"errors_24h": 0, "cost_24h": 0.05, "active_agents": [], "rag": {}, "knowledge_stats": {}}, |
| team_readiness={"score": 100}, |
| pending_approvals=[], |
| active_alerts=[] |
| ), |
| long_term_trends=LongTermTrends( |
| monthly_budget_forecast="Under budget forecast: $1.50", |
| roi_trend="Positive ROI: 4.5", |
| efficiency_trend="System efficiency optimal at 99.8%", |
| ai_token_usage_30d={}, |
| knowledge_base_roi={}, |
| collaboration_synergy={}, |
| sla_reliability={}, |
| commander_trends=[], |
| business_risks=[] |
| ), |
| main_bottleneck="None - All systems optimal", |
| recommended_actions=[] |
| ) |
|
|
| monkeypatch.setattr(agent, "_run_agent", mock_run_agent) |
|
|
| result = await agent.run( |
| user_prompt="Analyze metrics", |
| deps=deps |
| ) |
|
|
| assert isinstance(result, ConsolidatedNexusState) |
| assert result.system_status == "GREEN" |
| assert result.health_score == 95 |
| assert result.short_term_kpis.daily_token_cost == 0.05 |
| assert result.long_term_trends.roi_trend == "Positive ROI: 4.5" |
| assert len(result.recommended_actions) == 0 |
|
|
|
|
| def test_consolidated_api_endpoint(monkeypatch): |
| """Verify that the GET /api/stats/consolidated endpoint functions and returns stats.""" |
| |
| from src.agents.nexus_oracle_agent import NexusOracleAgent |
|
|
| async def mock_agent_run(self, user_prompt, deps): |
| return ConsolidatedNexusState( |
| system_status="GREEN", |
| health_score=98, |
| short_term_kpis=ShortTermKPIs( |
| daily_token_cost=0.0, |
| active_error_counts=0, |
| system_telemetry={}, |
| team_readiness={}, |
| pending_approvals=[], |
| active_alerts=[] |
| ), |
| long_term_trends=LongTermTrends( |
| monthly_budget_forecast="Good", |
| roi_trend="5.0", |
| efficiency_trend="Stable", |
| ai_token_usage_30d={}, |
| knowledge_base_roi={}, |
| collaboration_synergy={}, |
| sla_reliability={}, |
| commander_trends=[], |
| business_risks=[] |
| ), |
| main_bottleneck="None", |
| recommended_actions=[] |
| ) |
|
|
| monkeypatch.setattr(NexusOracleAgent, "run", mock_agent_run) |
|
|
| |
| from src.server.auth.dependencies import get_current_user |
|
|
| async def mock_get_current_user(): |
| return {"id": "test-user-id", "email": "test@example.com", "role": "admin"} |
|
|
| app.dependency_overrides[get_current_user] = mock_get_current_user |
|
|
| try: |
| response = client.get( |
| "/api/stats/consolidated", |
| headers={"Authorization": "Bearer fake-token"} |
| ) |
| assert response.status_code == 200 |
| json_data = response.json() |
| assert json_data["system_status"] == "GREEN" |
| assert json_data["health_score"] == 98 |
| assert json_data["short_term_kpis"]["daily_token_cost"] == 0.0 |
| assert json_data["long_term_trends"]["roi_trend"] == "5.0" |
| finally: |
| |
| app.dependency_overrides.pop(get_current_user, None) |
|
|
|
|