Spaces:
Sleeping
Sleeping
Download tests/conftest.py from anne-voigt/pdac-knowledge-agent-dev: direct link, hf CLI and curl.
- Browser
- Download file 6.93 kB
-
https://huggingface.co/spaces/anne-voigt/pdac-knowledge-agent-dev/resolve/main/tests/conftest.py
- Command line
-
hf download hf://spaces/anne-voigt/pdac-knowledge-agent-dev/tests/conftest.py
-
curl -L -o conftest.py https://huggingface.co/spaces/anne-voigt/pdac-knowledge-agent-dev/resolve/main/tests/conftest.py
6.93 kB
| """Hermetic test setup — outbound network is BLOCKED before collection. | |
| The block is enforced, not asserted (the genomics/spatial pattern): the socket patch is in place | |
| at conftest import time, before any test module loads, so a test that would reach Open Targets, | |
| UniProt or any other source fails loudly instead of quietly depending on the network. Responses | |
| come from recorded Phase 0 fixtures (``tests/fixtures/``) or synthetic dicts, through | |
| ``ToolClient(runner=...)``. | |
| Model-provider variables are removed for the session as well: this Space must start without them | |
| (ADR-0001 §2), and a developer shell that happens to export ``ANTHROPIC_API_KEY`` must not turn | |
| every startup test into a refusal. Tests that need one set it themselves. | |
| """ | |
| from __future__ import annotations | |
| import json | |
| import os | |
| import socket | |
| from pathlib import Path | |
| import pytest | |
| os.environ.setdefault("HF_HUB_OFFLINE", "1") | |
| os.environ.pop("ACCESS_CONTROL", None) # gate dark-launched off unless a test sets it | |
| from src.tu_client import LLM_KEY_VARS | |
| for _var in LLM_KEY_VARS: | |
| os.environ.pop(_var, None) | |
| _real_connect = socket.socket.connect | |
| OUTBOUND_ATTEMPTS: list = [] | |
| def _blocked_connect(self, address): | |
| host = address[0] if isinstance(address, tuple) else address | |
| # AF_UNIX (a filesystem path) is local IPC. | |
| if isinstance(address, (str, bytes)) or str(host) in ("127.0.0.1", "::1", "localhost"): | |
| return _real_connect(self, address) | |
| OUTBOUND_ATTEMPTS.append(address) | |
| raise RuntimeError(f"Outbound network blocked in tests (attempted {address!r})") | |
| socket.socket.connect = _blocked_connect # type: ignore[method-assign] | |
| # gradio's local LoginButton mock reaches for a HuggingFace identity while the app graph builds at | |
| # `import gradio_ui`. Stub `_get_mocked_oauth_info` (NOT `whoami`, which gradio calls through its | |
| # own reference) — the pdac-spatial-agent lesson of 2026-09-28. A gradio upgrade can rename this | |
| # private, and `setattr` on a missing name succeeds silently, so fail here if it is gone. | |
| import gradio.oauth as _gr_oauth | |
| _GRADIO_OAUTH_MOCK_HOOK = "_get_mocked_oauth_info" | |
| if not hasattr(_gr_oauth, _GRADIO_OAUTH_MOCK_HOOK): | |
| raise RuntimeError( | |
| f"gradio.oauth.{_GRADIO_OAUTH_MOCK_HOOK} no longer exists; re-point the offline OAuth " | |
| "stub in tests/conftest.py (a stale stub, not a network problem)." | |
| ) | |
| _gr_oauth._get_mocked_oauth_info = lambda: { # type: ignore[assignment] | |
| "access_token": "test-token", | |
| "token_type": "bearer", | |
| "expires_at": 0, | |
| "expires_in": 28800, | |
| "scope": "openid profile", | |
| "userinfo": { | |
| "sub": "test-user-id", | |
| "name": "Test User", | |
| "preferred_username": "test-user", | |
| "profile": "https://huggingface.co/test-user", | |
| "picture": None, | |
| "email": None, | |
| }, | |
| } | |
| FIXTURES = Path(__file__).resolve().parent / "fixtures" | |
| def load_fixture(relpath: str) -> dict: | |
| return json.loads((FIXTURES / relpath).read_text(encoding="utf-8")) | |
| class FakeClock: | |
| """A clock that only moves when told to; ``sleep`` advances it.""" | |
| def __init__(self, start: float = 1_760_000_000.0): | |
| self.now = start | |
| self.sleeps: list[float] = [] | |
| def __call__(self) -> float: | |
| return self.now | |
| def sleep(self, seconds: float) -> None: | |
| self.sleeps.append(seconds) | |
| self.now += seconds | |
| def advance(self, seconds: float) -> None: | |
| self.now += seconds | |
| class FakeRunner: | |
| """Stands in for ToolUniverse's ``run_one_function``: canned responses, every call recorded.""" | |
| def __init__(self, responses: dict[str, object] | None = None): | |
| self.responses = dict(responses or {}) | |
| self.calls: list[tuple[str, dict]] = [] | |
| def __call__(self, tool: str, args: dict): | |
| self.calls.append((tool, dict(args))) | |
| response = self.responses[tool] | |
| if isinstance(response, BaseException): | |
| raise response | |
| if callable(response): | |
| return response(args) | |
| return response | |
| def clock() -> FakeClock: | |
| return FakeClock() | |
| def memory_cache(clock): | |
| from src.cache import ResponseCache | |
| return ResponseCache(":memory:", clock=clock) | |
| def tnfrsf10b_response() -> dict: | |
| """The real Open Targets response for DR5, recorded through ToolUniverse 1.5.6 in Phase 0.""" | |
| return load_fixture("phase0/opentargets_drugs_TNFRSF10B.json")["response"] | |
| # --------------------------------------------------------------------------- # | |
| # Cassette replay: what the sources said on the recording day, with sockets blocked. | |
| # --------------------------------------------------------------------------- # | |
| CASSETTES = FIXTURES / "cassettes" | |
| SCENARIOS = sorted(p.stem for p in CASSETTES.glob("*.json")) | |
| class CassetteRunner: | |
| """Answers each (tool, arguments) with the response recorded for it, or fails loudly.""" | |
| def __init__(self, name: str): | |
| from src.cache import cache_key | |
| self.name = name | |
| self.cassette = json.loads((CASSETTES / f"{name}.json").read_text(encoding="utf-8")) | |
| self._key = cache_key | |
| self.responses = { | |
| cache_key(c["tool"], c["args"]): c["response"] for c in self.cassette["calls"] | |
| } | |
| self.calls: list[tuple[str, dict]] = [] | |
| def __call__(self, tool: str, args: dict): | |
| self.calls.append((tool, dict(args))) | |
| try: | |
| return self.responses[self._key(tool, args)] | |
| except KeyError: | |
| raise AssertionError( | |
| f"{tool} {args} is not in cassette {self.name!r}: the endpoint now makes a call " | |
| "that was not recorded. Re-record with scripts/record_fixtures.py and review the diff." | |
| ) from None | |
| def replay_client(name: str): | |
| """(client, runner) replaying cassette ``name`` at its recording time, rate limits on a fake clock.""" | |
| from src.cache import ResponseCache | |
| from src.ratelimit import RateLimiters | |
| from src.tu_client import ToolClient | |
| runner = CassetteRunner(name) | |
| clock = FakeClock(start=runner.cassette["_recorded"]["recorded_at_epoch"]) | |
| client = ToolClient( | |
| runner=runner, | |
| cache=ResponseCache(":memory:", clock=clock), | |
| limiters=RateLimiters(clock=clock, sleep=clock.sleep), | |
| clock=clock, | |
| tooluniverse_version=runner.cassette["_recorded"]["tooluniverse_version"], | |
| ) | |
| return client, runner | |
| def replay(): | |
| """``replay(name)`` → the endpoint's payload for that recorded scenario.""" | |
| from src.endpoints import ENDPOINTS, _common | |
| runners = [] | |
| def run(name: str) -> dict: | |
| client, runner = replay_client(name) | |
| runners.append(runner) | |
| _common.set_client(client) | |
| meta = runner.cassette["_recorded"] | |
| return ENDPOINTS[meta["endpoint"]][0](*meta["arguments"]) | |
| yield run | |
| _common.set_client(None) | |