feat: comprehensive test infrastructure improvements
Deploy Dashboard (Dev) / deploy-dev (push) Successful in 2m26s
Run Tests / Backend Tests (push) Failing after 2m36s
Run Tests / Frontend Tests (push) Failing after 2m23s
Run Tests / Test Summary (push) Failing after 13s

- Fix unit test imports: add env setup in conftest.py before module imports
- Add 24 new auth router tests (RBAC, preferences, password validation)
- Add 16 new tests for litellm and chat_summary routers
- Apply black formatting and ruff linting across codebase
- Add pre-commit hooks configuration (black, ruff, file checks)
- Increase CI coverage threshold from 40% to 50%

Test Results:
- 206 tests passing (91 unit + 115 integration)
- Coverage: 58.79% on core modules
- auth.py: 57% → 85%, litellm.py: 23% → 87%, chat_summary.py: 41% → 100%
- auth_service: 96.51%, config: 100%, rbac: 93.48%
This commit is contained in:
Gan, Jimmy
2026-04-08 00:21:32 +08:00
parent fcc95ac23f
commit b981c06d59
44 changed files with 1732 additions and 924 deletions
+15 -18
View File
@@ -1,17 +1,18 @@
"""
Base Agent Class - Foundation for all OPC agents
"""
from typing import Dict, Any, List, Optional
import json
import httpx
import os
from datetime import datetime
from typing import Any
import httpx
class BaseAgent:
"""Base class for all OPC agents"""
def __init__(self, agent_id: str, name: str, role: str, system_prompt: str, config: Dict[str, Any]):
def __init__(self, agent_id: str, name: str, role: str, system_prompt: str, config: dict[str, Any]):
self.agent_id = agent_id
self.name = name
self.role = role
@@ -20,7 +21,7 @@ class BaseAgent:
self.litellm_url = os.getenv("LITELLM_URL", "http://litellm:4005")
self.litellm_api_key = os.getenv("LITELLM_API_KEY", "")
async def execute(self, task: Dict[str, Any], context: Dict[str, Any]) -> Dict[str, Any]:
async def execute(self, task: dict[str, Any], context: dict[str, Any]) -> dict[str, Any]:
"""
Execute agent on a task
Returns: {
@@ -40,7 +41,7 @@ class BaseAgent:
return result
def _build_prompt(self, task: Dict[str, Any], context: Dict[str, Any]) -> str:
def _build_prompt(self, task: dict[str, Any], context: dict[str, Any]) -> str:
"""Build the prompt for the LLM"""
prompt = f"""You are {self.name}, a {self.role} agent.
@@ -77,6 +78,7 @@ Only propose actions you can actually execute. Be specific and actionable."""
async def _call_llm(self, prompt: str) -> str:
"""Call LiteLLM proxy"""
import logging
logger = logging.getLogger(__name__)
model = self.config.get("model", "claude-sonnet-4-6")
@@ -96,11 +98,11 @@ Only propose actions you can actually execute. Be specific and actionable."""
"model": model,
"messages": [
{"role": "system", "content": self.system_prompt},
{"role": "user", "content": prompt}
{"role": "user", "content": prompt},
],
"temperature": temperature,
"max_tokens": 2000
}
"max_tokens": 2000,
},
)
response.raise_for_status()
data = response.json()
@@ -118,7 +120,7 @@ Only propose actions you can actually execute. Be specific and actionable."""
logger.error(f"LiteLLM call failed: {e}")
raise Exception(f"LLM call failed: {str(e)}")
def _parse_response(self, response: str) -> Dict[str, Any]:
def _parse_response(self, response: str) -> dict[str, Any]:
"""Parse LLM response and extract actions"""
try:
# Try to extract JSON from response
@@ -148,16 +150,11 @@ Only propose actions you can actually execute. Be specific and actionable."""
# Fallback: return error action
return {
"reasoning": f"Failed to parse response: {str(e)}",
"actions": [
{
"type": "error",
"message": f"Agent response parsing failed: {str(e)}"
}
],
"requires_approval": False
"actions": [{"type": "error", "message": f"Agent response parsing failed: {str(e)}"}],
"requires_approval": False,
}
def get_capabilities(self) -> List[str]:
def get_capabilities(self) -> list[str]:
"""Get agent capabilities"""
return self.config.get("capabilities", [])