"""Tests for health endpoints.""" import pytest from fastapi.testclient import TestClient from unittest.mock import patch, AsyncMock from src.main import app @pytest.fixture def client(): """Create a test client.""" return TestClient(app) class TestRootEndpoint: """Test root endpoint.""" def test_root_returns_200(self, client): """Root endpoint should return 200.""" response = client.get("/") assert response.status_code == 200 def test_root_returns_service_info(self, client): """Root endpoint should return service information.""" response = client.get("/") data = response.json() assert "service" in data assert "version" in data assert "status" in data assert data["service"] == "Core Code API" assert data["status"] == "healthy" def test_root_returns_documentation_links(self, client): """Root endpoint should return documentation links.""" response = client.get("/") data = response.json() assert "documentation" in data assert "swagger_ui" in data["documentation"] assert "redoc" in data["documentation"] def test_root_returns_endpoints(self, client): """Root endpoint should return available endpoints.""" response = client.get("/") data = response.json() assert "endpoints" in data assert "health" in data["endpoints"] class TestHealthEndpoint: """Test /health endpoint.""" @patch("src.controllers.health_controller.get_ollama_client") def test_health_returns_200_when_ollama_healthy(self, mock_get_ollama, client): """Health endpoint should return 200 when Ollama is healthy.""" mock_client = AsyncMock() mock_client.health_check.return_value = True mock_get_ollama.return_value = mock_client response = client.get("/health") assert response.status_code == 200 @patch("src.controllers.health_controller.get_ollama_client") def test_health_returns_status(self, mock_get_ollama, client): """Health endpoint should return status information.""" mock_client = AsyncMock() mock_client.health_check.return_value = True mock_get_ollama.return_value = mock_client response = client.get("/health") data = response.json() assert "status" in data assert "ollama_connected" in data @patch("src.controllers.health_controller.get_ollama_client") def test_health_returns_ollama_connected_true(self, mock_get_ollama, client): """Health should report Ollama connected when healthy.""" mock_client = AsyncMock() mock_client.health_check.return_value = True mock_get_ollama.return_value = mock_client response = client.get("/health") data = response.json() assert data["ollama_connected"] is True @patch("src.controllers.health_controller.get_ollama_client") def test_health_returns_ollama_connected_false(self, mock_get_ollama, client): """Health should report Ollama disconnected when unhealthy.""" mock_client = AsyncMock() mock_client.health_check.return_value = False mock_get_ollama.return_value = mock_client response = client.get("/health") data = response.json() assert data["ollama_connected"] is False class TestOpenAPIEndpoint: """Test OpenAPI documentation endpoints.""" def test_openapi_spec_available(self, client): """OpenAPI spec should be available.""" response = client.get("/openapi.json") assert response.status_code == 200 data = response.json() assert "openapi" in data assert "info" in data def test_swagger_ui_available(self, client): """Swagger UI should be available.""" response = client.get("/docs") assert response.status_code == 200 def test_redoc_available(self, client): """ReDoc should be available.""" response = client.get("/redoc") assert response.status_code == 200