"""Run against a built bundle: SQUADDS_ML_API_ROOT (default: the folder above tests/). Each session uses a fresh Hugging Face cache, so the tests can see which weights were downloaded.""" import os import sys import tempfile import time from pathlib import Path import pytest import yaml ROOT = Path(os.environ.get("SQUADDS_ML_API_ROOT", Path(__file__).resolve().parents[1])) sys.path[:0] = [str(ROOT), str(ROOT / "src")] TOKEN = None try: from huggingface_hub import get_token TOKEN = get_token() except Exception: # noqa: BLE001 pass os.environ["HF_HUB_CACHE"] = tempfile.mkdtemp(prefix="squadds_ml_api_hub_") os.environ.setdefault("KERAS_BACKEND", "torch") os.environ.setdefault("SQUADDS_ML_API_RATE_PER_MIN", "0") GDS_DIR = Path(os.environ.get("SQUADDS_ML_API_TEST_GDS", "/Users/shanto/LFL/tl_squadds/deploy/build_flagship_docker/PlanarCapacitanceModel/examples")) CAP = ("qcap", "qcap-fast") def _client(registry_file=None): from fastapi.testclient import TestClient from squadds_ml_api.api import create_app app = create_app(ROOT, registry_file) c = TestClient(app) c.__enter__() reg = app.state.registry t = time.time() while any(m.state == "loading" for m in reg.models.values()) and time.time() - t < 600: time.sleep(0.2) return c @pytest.fixture(scope="session") def client(): c = _client() c.hub_listing = sorted(os.listdir(os.environ["HF_HUB_CACHE"])) yield c c.__exit__(None, None, None) def _forced(tmp_path_factory, visibility): reg = yaml.safe_load((ROOT / "registry.yaml").read_text()) for k in CAP: reg["models"][k]["visibility"] = visibility p = tmp_path_factory.mktemp("reg") / "registry.yaml" p.write_text(yaml.safe_dump(reg, sort_keys=False)) return _client(p) @pytest.fixture(scope="session") def gated_client(tmp_path_factory): if not TOKEN: pytest.skip("no Hugging Face token") c = _forced(tmp_path_factory, "gated") yield c c.__exit__(None, None, None) @pytest.fixture(scope="session") def hidden_client(tmp_path_factory): c = _forced(tmp_path_factory, "hidden") yield c c.__exit__(None, None, None) @pytest.fixture(scope="session") def auth(): return {"Authorization": f"Bearer {TOKEN}"}