"""FastAPI contracts that do not download model weights.""" from pathlib import Path from fastapi.testclient import TestClient from app import create_app from config import Settings from core.loader import ModelLoader def make_app(tmp_path: Path): ModelLoader.reset_instance() settings = Settings( output_folder=tmp_path / "output", tmp_folder=tmp_path / "tmp", static_folder=tmp_path / "static", device="cpu", ) return create_app(settings) def test_health_does_not_load_a_model(tmp_path: Path) -> None: application = make_app(tmp_path) with TestClient(application) as client: response = client.get("/health") assert response.status_code == 200 assert response.json()["status"] == "ok" assert response.json()["active_model"] is None def test_root_redirects_to_gradio_ui(tmp_path: Path) -> None: application = make_app(tmp_path) with TestClient(application) as client: response = client.get("/?__theme=dark", follow_redirects=False) direct_response = client.get("/ui", follow_redirects=False) assert response.status_code == 307 assert response.headers["location"] == "/ui/?__theme=dark" assert direct_response.status_code == 307 assert direct_response.headers["location"] == "/ui/" def test_mounted_gradio_ui_runtime_options(tmp_path: Path) -> None: application = make_app(tmp_path) mount = next( route for route in application.routes if route.path == "/ui" and hasattr(route.app, "get_blocks") ) blocks = mount.app.get_blocks() assert blocks.ssr_mode is False assert blocks.mcp_server is True def test_validation_errors_have_stable_shape(tmp_path: Path) -> None: application = make_app(tmp_path) with TestClient(application) as client: response = client.post( "/api/image", json={"prompt": "city", "width": 257, "height": 768, "steps": 4, "seed": 1}, ) assert response.status_code == 400 assert response.json()["success"] is False assert response.json()["code"] == "invalid_dimensions" assert response.headers["X-Request-ID"]