From 56c0805cfe87dd8f73564b36f6a66f694fd3b677 Mon Sep 17 00:00:00 2001 From: Aniketchavan7 <91446947+Aniketchavan7@users.noreply.github.com> Date: Fri, 20 Nov 2026 12:21:20 +0530 Subject: [PATCH] feat(api): add health check and diagnostics endpoints with integration tests Exposes /api/v1/health and /api/v1/diagnostics endpoints to verify configuration readiness. Co-authored-by: yalongwastaken --- backend/server.py | 35 +++++++++++++++++++++++++++++------ tests/test_server_api.py | 23 +++++++++++++++++++++++ 2 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 tests/test_server_api.py diff --git a/backend/server.py b/backend/server.py index 42f179a..26dec31 100644 --- a/backend/server.py +++ b/backend/server.py @@ -4,6 +4,7 @@ from pydantic import BaseModel from backend.websocket_manager import WebSocketManager from backend.utils import write_md_to_pdf, write_md_to_word, write_text_to_md +from backend.config_validator import validate_runtime_environment import time import json import os @@ -15,12 +16,14 @@ class ResearchRequest(BaseModel): agent: str -app = FastAPI() +app = FastAPI(title="Spring Money AI Agent Server", version="0.1.0") -app.mount("/site", StaticFiles(directory="./frontend"), name="site") -app.mount("/static", StaticFiles(directory="./frontend/static"), name="static") +if os.path.exists("./frontend"): + app.mount("/site", StaticFiles(directory="./frontend"), name="site") +if os.path.exists("./frontend/static"): + app.mount("/static", StaticFiles(directory="./frontend/static"), name="static") -templates = Jinja2Templates(directory="./frontend") +templates = Jinja2Templates(directory="./frontend") if os.path.exists("./frontend") else None manager = WebSocketManager() @@ -32,9 +35,30 @@ def startup_event(): os.makedirs("outputs") app.mount("/outputs", StaticFiles(directory="outputs"), name="outputs") + @app.get("/") async def read_root(request: Request): - return templates.TemplateResponse('index.html', {"request": request, "report": None}) + if templates: + return templates.TemplateResponse('index.html', {"request": request, "report": None}) + return {"status": "ok", "service": "Spring Money AI Agent API"} + + +@app.get("/api/v1/health") +async def health_check(): + """Health check endpoint for container orchestrators and load balancers.""" + env_info = validate_runtime_environment() + return { + "status": "healthy" if env_info["valid"] else "degraded", + "service": "Spring Money AI Agent", + "timestamp": time.time(), + "warnings": env_info["warnings"], + } + + +@app.get("/api/v1/diagnostics") +async def diagnostics(): + """Detailed runtime configuration and active environment diagnostics.""" + return validate_runtime_environment() @app.websocket("/ws") @@ -62,4 +86,3 @@ async def websocket_endpoint(websocket: WebSocket): except WebSocketDisconnect: await manager.disconnect(websocket) - diff --git a/tests/test_server_api.py b/tests/test_server_api.py new file mode 100644 index 0000000..b8b1ac5 --- /dev/null +++ b/tests/test_server_api.py @@ -0,0 +1,23 @@ +from fastapi.testclient import TestClient +from backend.server import app + +client = TestClient(app) + + +def test_health_check_endpoint(): + """Verify health endpoint responds with standard status structure.""" + response = client.get("/api/v1/health") + assert response.status_code == 200 + data = response.json() + assert "status" in data + assert data["service"] == "Spring Money AI Agent" + assert "timestamp" in data + + +def test_diagnostics_endpoint(): + """Verify diagnostics reports configuration parameters.""" + response = client.get("/api/v1/diagnostics") + assert response.status_code == 200 + data = response.json() + assert "config" in data + assert "valid" in data