# SPDX-License-Identifier: Apache-2.0 """Host tests of the HTTP server with the fake device (no ttnn): routes, schemas, encodings, errors, batching.""" import base64 import concurrent.futures import os import numpy as np import pytest from tt_minilm.tests.gates import model_dir # noqa: E402 MODEL_DIR = model_dir() pytestmark = pytest.mark.skipif(not os.path.isfile(os.path.join(MODEL_DIR, "tokenizer.json")), reason="tokenizer files not available") @pytest.fixture() def client(monkeypatch): from fastapi.testclient import TestClient from tt_minilm.server.app import create_app from tt_minilm.server.fake_device import FakeModel monkeypatch.setenv("MINILM_MAX_BATCH", "4") monkeypatch.setenv("MINILM_MAX_INPUTS", "50") monkeypatch.delenv("TT_MESH_SHAPE", raising=False) holder = {} def factory(cfg): holder["m"] = FakeModel(MODEL_DIR, cfg) return holder["m"] app = create_app(model_factory=factory) with TestClient(app) as c: c.model = holder["m"] yield c def test_health_info_models(client): r = client.get("/health") assert r.status_code == 200 and r.json()["status"] == "ok" info = client.get("/info").json() assert info["limits"]["dim"] == 384 and info["limits"]["max_batch"] == 4 assert client.get("/v1/models").json()["data"][0]["id"] == "all-MiniLM-L6-v2" assert "POST /v1/embeddings" in client.get("/").json()["routes"] def test_openai_float_and_base64(client): texts = ["hello world", "A man is playing a guitar.", "", "x " * 300] r = client.post("/v1/embeddings", json={"input": texts, "model": "m1"}) assert r.status_code == 200, r.text d = r.json() assert d["object"] == "list" and d["model"] == "m1" and len(d["data"]) == 4 ids = client.model.tokenize(texts) want = client.model.expected(ids) got = np.array([e["embedding"] for e in d["data"]], dtype=np.float32) assert np.allclose(got, want, atol=1e-6) assert np.allclose(np.linalg.norm(got, axis=1), 1.0, atol=1e-5) assert d["usage"]["prompt_tokens"] == sum(len(x) for x in ids) r = client.post("/v1/embeddings", json={"input": "hello world", "encoding_format": "base64"}) v = np.frombuffer(base64.b64decode(r.json()["data"][0]["embedding"]), dtype="