Files
CyberGril/tests/conftest.py
T

87 lines
2.7 KiB
Python

"""共享 fixture。"""
from __future__ import annotations
import asyncio
import contextlib
import sys
from pathlib import Path
import pytest
from cybergril.config import CharacterConfig, ModelsConfig, Settings
from cybergril.memory import Store
# Test-env compatibility only (product targets Python 3.11, deploy/hel874 uses
# uv --python 3.11): this workspace runs 3.10, where asyncio.timeout does not
# exist. The shim mirrors the only shape the product uses —
# ``async with asyncio.timeout(delay)`` raising TimeoutError on expiry.
if sys.version_info < (3, 11) and not hasattr(asyncio, "timeout"):
@contextlib.asynccontextmanager
async def _timeout_compat(delay: float):
loop = asyncio.get_running_loop()
deadline = loop.time() + max(0.0, delay)
current = asyncio.current_task()
def _expire():
if current is not None:
current.cancel()
handle = loop.call_at(deadline, _expire)
try:
yield
except asyncio.CancelledError:
if loop.time() < deadline:
raise
raise TimeoutError from None
finally:
handle.cancel()
asyncio.timeout = _timeout_compat # type: ignore[attr-defined]
try:
from cybergril.think.budget import CATEGORIES, BudgetConfig
except ImportError: # r3 baseline probe runs these tests against older src trees
from cybergril.think.budget import BudgetConfig
CATEGORIES = ("dialogue", "life", "background", "semantic")
def engineering_budget(**overrides) -> BudgetConfig:
"""Engineering caps for isolated tests: real mechanism, generous numbers.
The product default is fail-closed (all request caps 0 = refuse); tests
exercise the same acquire/settle path with plenty of headroom so the gate
itself is what's under test, not the headroom. Against a pre-HEL-882
baseline source tree (r3 baseline probe) the extra fields don't exist and
the old plain token config is used — behavior there predates the gate.
"""
requests = dict.fromkeys(CATEGORIES, 100_000)
requests.update(overrides.pop("daily_requests", {}))
try:
return BudgetConfig(daily_tokens=10_000_000, daily_requests=requests, **overrides)
except TypeError:
return BudgetConfig(daily_tokens=10_000_000, **overrides)
@pytest.fixture
def repo_root() -> Path:
return Path(__file__).resolve().parents[1]
@pytest.fixture
def settings(repo_root: Path) -> Settings:
return Settings(
models=ModelsConfig(),
character=CharacterConfig(identity={"y": 2000, "m": 1, "d": 1, "h": 12}),
db_path=repo_root / "test.db",
)
@pytest.fixture
def store(tmp_path: Path) -> Store:
s = Store(tmp_path / "test.db")
yield s
s.close()