diff --git a/cli/serve/app.py b/cli/serve/app.py index 82e652fb7..22e7f4e56 100644 --- a/cli/serve/app.py +++ b/cli/serve/app.py @@ -7,7 +7,7 @@ import sys import time import uuid -from typing import Any, Literal, cast +from typing import Any, cast try: import typer @@ -52,6 +52,18 @@ ) +@app.get("/health") +async def health_check() -> dict[str, str]: + """Basic liveness check endpoint. + + Returns a 200 OK status to signal that the Python process is alive and responding. + + Returns: + dict: A dictionary with status "pass". + """ + return {"status": "pass"} + + @app.exception_handler(RequestValidationError) async def validation_exception_handler( request: Request, exc: RequestValidationError diff --git a/test/cli/test_serve.py b/test/cli/test_serve.py index 889049613..473ddc69d 100644 --- a/test/cli/test_serve.py +++ b/test/cli/test_serve.py @@ -10,7 +10,7 @@ from fastapi.testclient import TestClient from pydantic import BaseModel, ValidationError -from cli.serve.app import make_chat_endpoint, validation_exception_handler +from cli.serve.app import app, make_chat_endpoint, validation_exception_handler from cli.serve.models import ( ChatCompletion, ChatCompletionRequest, @@ -45,6 +45,28 @@ def sample_request(): ) +class TestHealthCheckEndpoint: + """Tests for the health check endpoint.""" + + @pytest.fixture(scope="class") + def client(self, request) -> TestClient: + """Set up the test client.""" + + # /health is registered at module-load time — TestClient(app) is correct here + return TestClient(app) + + def test_health_check(self, client: TestClient): + """Test that /health GET endpoint returns 200 with correct JSON response.""" + response = client.get("/health") + + assert response.status_code == 200 + assert response.json() == {"status": "pass"} + + def test_health_check_rejects_post(self, client: TestClient): + """Test that /health POST endpoint returns 405""" + assert client.post("/health").status_code == 405 + + class TestChatEndpoint: """Tests for the chat completion endpoint.""" @@ -722,8 +744,6 @@ async def test_text_format_no_schema(self, mock_module): @pytest.mark.asyncio async def test_json_schema_missing_schema_field(self, mock_module): """Test that json_schema without schema field raises ValidationError.""" - from pydantic import ValidationError - # Should raise ValidationError when creating ResponseFormat with pytest.raises(ValidationError) as exc_info: ResponseFormat(