"""The HTTP server on a tiny offline model.""" import pytest from fastapi.testclient import TestClient import discerna from discerna.contract import DecisionRequest, DecisionResponse, check_response from discerna.contract.images import to_data_url from discerna.server import create_app from probes import TEXT_PROBES, circles from tiny import tiny_folder @pytest.fixture(scope="module") def text_client(tmp_path_factory): model = discerna.load(tiny_folder(tmp_path_factory.mktemp("text")), device="cpu") return TestClient(create_app(model)), model def test_health_and_model_list(text_client): client, _ = text_client assert client.get("/healthz").json() == {"ok": True} data = client.get("/v1/models").json()["data"][0] assert data["id"] == "tiny" and data["modalities"] == ["text"] def test_decide_matches_the_engine(text_client): client, model = text_client body = TEXT_PROBES["ticket"] reply = client.post("/v1/decide", json=body) assert reply.status_code == 200 response = DecisionResponse.model_validate(reply.json()) request = DecisionRequest.model_validate(body) assert check_response(request, response) == [] assert response.answers == model.decide(request).answers def test_invalid_body_is_422(text_client): client, _ = text_client assert client.post("/v1/decide", json={"state": "x", "questions": {}}).status_code == 422 def test_images_to_a_text_only_model_are_413(text_client): client, _ = text_client body = {"state": None, "images": [to_data_url(circles(2))], "questions": {"n": {"type": "noul", "instructions": "Two circles?"}}} reply = client.post("/v1/decide", json=body) assert reply.status_code == 413 and reply.json()["detail"]["error"] == "unsupported" def test_images_are_answered_by_a_vision_model(tmp_path): client = TestClient(create_app(discerna.load(tiny_folder(tmp_path / "v", vision=True), device="cpu"))) body = {"state": None, "images": [to_data_url(circles(3))], "questions": {"count": {"type": "choice", "instructions": "How many circles?", "criteria": {"two": None, "three": None}}}} reply = client.post("/v1/decide", json=body) assert reply.status_code == 200 assert set(reply.json()["answers"]["count"]["probabilities"]) == {"two", "three"}