Spaces:
Sleeping
Sleeping
Download tests/test_preflight.py from DerXanity/farient-deckcheck: direct link, hf CLI and curl.
- Browser
- Download file 5.22 kB
-
https://huggingface.co/spaces/DerXanity/farient-deckcheck/resolve/main/tests/test_preflight.py
- Command line
-
hf download hf://spaces/DerXanity/farient-deckcheck/tests/test_preflight.py
-
curl -L -o test_preflight.py https://huggingface.co/spaces/DerXanity/farient-deckcheck/resolve/main/tests/test_preflight.py
5.22 kB
| """Tests for GET /health/preflight endpoint.""" | |
| from __future__ import annotations | |
| import os | |
| from pathlib import Path | |
| from unittest.mock import patch | |
| import pytest | |
| from fastapi.testclient import TestClient | |
| from schemas.api import PreflightResponse | |
| # --------------------------------------------------------------------------- | |
| # Fixtures | |
| # --------------------------------------------------------------------------- | |
| def app(tmp_path: Path, monkeypatch: pytest.MonkeyPatch): | |
| root = tmp_path / "audits" | |
| root.mkdir(parents=True, exist_ok=True) | |
| monkeypatch.setenv("AUDIT_JOBS_ROOT", str(root)) | |
| monkeypatch.setenv("OPENAI_API_KEY", "sk-test-key") | |
| import services.renderer as renderer_module | |
| monkeypatch.setattr(renderer_module, "warmup", lambda: None) | |
| from main import create_app | |
| return create_app() | |
| def client(app): | |
| return TestClient(app) | |
| # --------------------------------------------------------------------------- | |
| # Test 1: All checks pass β ready=True, warnings=[] | |
| # --------------------------------------------------------------------------- | |
| def test_preflight_all_pass(client: TestClient, monkeypatch: pytest.MonkeyPatch): | |
| """When env + binaries are present the endpoint reports ready=True.""" | |
| monkeypatch.setenv("OPENAI_API_KEY", "sk-test") | |
| import shutil as _shutil | |
| original_which = _shutil.which | |
| def fake_which(name): | |
| if name in ("soffice", "pdftoppm"): | |
| return f"/usr/bin/{name}" | |
| return original_which(name) | |
| with patch("services.preflight.shutil.which", side_effect=fake_which): | |
| response = client.get("/health/preflight") | |
| assert response.status_code == 200 | |
| body = response.json() | |
| parsed = PreflightResponse.model_validate(body) | |
| assert parsed.ready is True | |
| assert parsed.warnings == [] | |
| assert parsed.checks.jobs_root_writable is True | |
| assert parsed.checks.openai_api_key is True | |
| assert parsed.checks.soffice is True | |
| assert parsed.checks.pdftoppm is True | |
| assert parsed.jobs_root != "" | |
| # --------------------------------------------------------------------------- | |
| # Test 2: Missing OPENAI_API_KEY β ready=False, appropriate warning | |
| # --------------------------------------------------------------------------- | |
| def test_preflight_missing_openai_key(client: TestClient, monkeypatch: pytest.MonkeyPatch): | |
| """Missing OPENAI_API_KEY causes ready=False with a meaningful warning.""" | |
| monkeypatch.delenv("OPENAI_API_KEY", raising=False) | |
| import shutil as _shutil | |
| original_which = _shutil.which | |
| def fake_which(name): | |
| if name in ("soffice", "pdftoppm"): | |
| return f"/usr/bin/{name}" | |
| return original_which(name) | |
| with patch("services.preflight.shutil.which", side_effect=fake_which): | |
| response = client.get("/health/preflight") | |
| assert response.status_code == 200 | |
| body = response.json() | |
| parsed = PreflightResponse.model_validate(body) | |
| assert parsed.ready is False | |
| assert parsed.checks.openai_api_key is False | |
| assert any("OPENAI_API_KEY" in w for w in parsed.warnings) | |
| # --------------------------------------------------------------------------- | |
| # Test 3: jobs_root not writable β ready=False, appropriate warning | |
| # --------------------------------------------------------------------------- | |
| def test_preflight_unwritable_jobs_root( | |
| tmp_path: Path, monkeypatch: pytest.MonkeyPatch | |
| ): | |
| """An unwritable jobs_root causes ready=False with a meaningful warning.""" | |
| monkeypatch.setenv("OPENAI_API_KEY", "sk-test") | |
| import services.renderer as renderer_module | |
| monkeypatch.setattr(renderer_module, "warmup", lambda: None) | |
| # Patch _writable_dir to simulate an unwritable directory. | |
| with patch("services.preflight._writable_dir", return_value=False): | |
| monkeypatch.setenv("AUDIT_JOBS_ROOT", str(tmp_path / "audits")) | |
| from main import create_app | |
| app = create_app() | |
| with TestClient(app) as fresh_client: | |
| response = fresh_client.get("/health/preflight") | |
| assert response.status_code == 200 | |
| body = response.json() | |
| parsed = PreflightResponse.model_validate(body) | |
| assert parsed.ready is False | |
| assert parsed.checks.jobs_root_writable is False | |
| assert any("not writable" in w for w in parsed.warnings) | |
| # --------------------------------------------------------------------------- | |
| # Test 4: Endpoint always returns 200 even when all checks fail | |
| # --------------------------------------------------------------------------- | |
| def test_preflight_always_200(client: TestClient, monkeypatch: pytest.MonkeyPatch): | |
| """The preflight endpoint never raises β it always returns HTTP 200.""" | |
| monkeypatch.delenv("OPENAI_API_KEY", raising=False) | |
| with patch("services.preflight._writable_dir", return_value=False): | |
| with patch("services.preflight.shutil.which", return_value=None): | |
| response = client.get("/health/preflight") | |
| assert response.status_code == 200 | |
| body = response.json() | |
| parsed = PreflightResponse.model_validate(body) | |
| assert parsed.ready is False | |
| # All four warnings should be present | |
| assert len(parsed.warnings) == 4 | |