Fix zero uptime handling in HTTP health check

- Change uptime_seconds check from falsy to "is not None"
- Preserve 0 uptime for freshly-launched MCP servers
- Add test case for zero uptime edge case

Bug: Previously treated 0 as falsy, returning None instead of 0
Fix: Only return None when uptime_seconds is actually None

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
leex279
2025-11-03 00:11:13 +01:00
parent 23302577a3
commit f85dbe0b25
2 changed files with 29 additions and 1 deletions

View File

@@ -44,9 +44,10 @@ async def get_container_status_http() -> dict[str, Any]:
data = response.json()
# Transform to expected API contract
uptime_value = data.get("uptime_seconds")
return {
"status": "running" if data.get("success") else "unhealthy",
"uptime": int(data.get("uptime_seconds")) if data.get("uptime_seconds") else None,
"uptime": int(uptime_value) if uptime_value is not None else None,
"logs": [], # Historical artifact, kept for API compatibility
}

View File

@@ -138,6 +138,33 @@ async def test_get_container_status_http_unhealthy(mock_mcp_url):
assert result["logs"] == []
@pytest.mark.asyncio
async def test_get_container_status_http_zero_uptime(mock_mcp_url):
"""Test HTTP health check preserves 0 uptime for freshly-launched MCP."""
mock_response = MagicMock()
mock_response.json.return_value = {"success": True, "uptime_seconds": 0, "health": {}}
mock_response.status_code = 200
with (
patch("src.server.api_routes.mcp_api.get_mcp_url", return_value=mock_mcp_url),
patch("src.server.api_routes.mcp_api.get_mcp_monitoring_config") as mock_get_config,
patch("httpx.AsyncClient") as mock_client_class,
):
mock_get_config.return_value = MCPMonitoringConfig(enable_docker_socket=False, health_check_timeout=5)
mock_client = MagicMock()
mock_client.get = AsyncMock(return_value=mock_response)
mock_client_class.return_value.__aenter__.return_value = mock_client
mock_client_class.return_value.__aexit__.return_value = None
result = await get_container_status_http()
assert result["status"] == "running"
assert result["uptime"] == 0 # Important: 0 should be preserved, not None
assert result["logs"] == []
mock_client.get.assert_called_once_with(f"{mock_mcp_url}/health")
@pytest.mark.asyncio
async def test_get_container_status_http_error(mock_mcp_url):
"""Test HTTP health check when an unexpected error occurs."""