"""Shared fixtures: real captures for shape, fakes for behaviour. Anything that must match an upstream (an agent snapshot, an engine's /metrics text, the NUT wire protocol) is asserted against a verbatim capture in tests/fixtures/. Everything that needs a *failure* — a dead host, a refused command, a counter reset — is a fake here. """ from __future__ import annotations import json import sys from pathlib import Path from typing import Any import pytest ROOT = Path(__file__).resolve().parents[1] SRC = ROOT / "src" if str(SRC) not in sys.path: sys.path.insert(0, str(SRC)) from dashboards.rates import RateEstimator # noqa: E402 from dashboards.store import SampleStore # noqa: E402 FIXTURES = Path(__file__).resolve().parent / "fixtures" @pytest.fixture def db(tmp_path: Path) -> Any: store = SampleStore(tmp_path / "t.sqlite3") try: yield store finally: store.close() @pytest.fixture def store(db: Any) -> Any: """Alias: some tests read better with `store`, others with `db`. Same object.""" return db @pytest.fixture def agent_snapshot() -> dict[str, Any]: """A real driveripper snapshot (Threadripper + R9700, 48 logical CPUs).""" return json.loads((FIXTURES / "agent_driveripper.json").read_text()) @pytest.fixture def agent_snapshots() -> dict[str, dict[str, Any]]: """Every captured host, keyed by name — fleet code paths need >1 host to mean anything.""" out: dict[str, dict[str, Any]] = {} for path in sorted(FIXTURES.glob("agent_*.json")): out[path.stem.removeprefix("agent_")] = json.loads(path.read_text()) return out @pytest.fixture def metrics_text() -> Any: """metrics_text('gufo') / metrics_text('vllm') -> the captured exposition verbatim.""" def load(engine: str) -> str: path = FIXTURES / f"metrics_{engine}.txt" if not path.exists(): pytest.skip(f"missing fixture {path.name} — run scripts/capture-fixtures.sh --with-llm") return path.read_text() return load @pytest.fixture def rates() -> RateEstimator: return RateEstimator(span=10.0)