from __future__ import annotations import asyncio import json import httpx import pytest from hub_core.runtime.config import RuntimeSettings from hub_core.runtime.repo_manager_client import HTTPRepoProjectionClient def test_http_repo_projection_client_forwards_cursor_and_bearer_token() -> None: seen: list[httpx.Request] = [] async def handler(request: httpx.Request) -> httpx.Response: seen.append(request) return httpx.Response(200, json={"contract_id": "example"}) async def exercise() -> dict: client = HTTPRepoProjectionClient( "https://repo-manager.invalid", api_token="test-token", transport=httpx.MockTransport(handler), ) try: return dict(await client.fetch_classification_page("signed-cursor")) finally: await client.aclose() payload = asyncio.run(exercise()) assert payload == {"contract_id": "example"} assert seen[0].url.path == "/ports/repositories/classifications" assert seen[0].url.params["cursor"] == "signed-cursor" assert seen[0].headers["authorization"] == "Bearer test-token" def test_http_repo_projection_client_rejects_non_object_json() -> None: async def handler(_: httpx.Request) -> httpx.Response: return httpx.Response(200, content=json.dumps([])) async def exercise() -> None: client = HTTPRepoProjectionClient( "http://repo-manager.invalid", transport=httpx.MockTransport(handler), ) try: await client.fetch_classification_page(None) finally: await client.aclose() with pytest.raises(ValueError, match="JSON object"): asyncio.run(exercise()) def test_runtime_settings_load_repo_projection_transport(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setenv("HUB_CORE_REPO_MANAGER_BASE_URL", "http://repo-manager:8020") monkeypatch.setenv("HUB_CORE_REPO_MANAGER_API_TOKEN", "token") monkeypatch.setenv("HUB_CORE_REPO_MANAGER_TIMEOUT_SECONDS", "4.5") monkeypatch.setenv("HUB_CORE_REPO_PROJECTION_REFRESH_SECONDS", "45") settings = RuntimeSettings.from_env() assert settings.repo_manager_base_url == "http://repo-manager:8020" assert settings.repo_manager_api_token == "token" assert settings.repo_manager_timeout_seconds == 4.5 assert settings.repo_projection_refresh_seconds == 45.0