Files
nanobot/tests/agent/conftest.py
T
KDB db918bddd3 test: stabilize flaky and environment-dependent tests
- test_catalog_bounds_failure_only_keys failed 3/5 runs on Windows: all
  three get() calls land within one time.monotonic() tick, so the eviction
  victim in _reserve() is decided by hash-seeded set iteration order among
  tied recency keys. Pin the catalog to a synthetic strictly-increasing
  monotonic clock (the constructor already accepts one) so the oldest
  entry is evicted deterministically. 5/5 stable after, 3/5 failures before.
- test_equivalent_workspace_paths_share_one_store requires directory
  symlinks, which need admin/developer mode on Windows (WinError 1314).
  Skip with the same pattern the file already uses for file symlinks.
- tests/agent/conftest.py: make_provider() assigned through a spec'd mock
  by reading provider.estimate_prompt_tokens first, which raises
  AttributeError because the attribute is not on the LLMProvider class
  spec; assign a MagicMock directly instead. Latent - no test hit the
  default-provider path until now.
2026-09-07 10:54:26 +08:00

96 lines
2.8 KiB
Python

"""Shared fixtures and helpers for agent tests."""
from __future__ import annotations
import os
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from nanobot.agent.loop import AgentLoop
from nanobot.bus.queue import MessageBus
from nanobot.providers.base import LLMProvider
@pytest.fixture
def cmd_python() -> str:
"""Return the Python command name available to ExecTool tests."""
return "python" if os.name == "nt" else "python3"
def make_provider(
default_model: str = "test-model",
*,
max_tokens: int = 4096,
spec: bool = True,
) -> MagicMock:
"""Create a spec-limited LLM provider mock."""
mock_type = MagicMock(spec=LLMProvider) if spec else MagicMock()
provider = mock_type
provider.get_default_model.return_value = default_model
provider.generation = SimpleNamespace(
max_tokens=max_tokens,
temperature=0.1,
reasoning_effort=None,
)
provider.estimate_prompt_tokens = MagicMock(return_value=(10_000, "test"))
return provider
def make_loop(
tmp_path: Path,
*,
model: str = "test-model",
context_window_tokens: int = 128_000,
session_ttl_minutes: int = 0,
unified_session: bool = False,
tools_config=None,
model_presets: dict | None = None,
hooks: list | None = None,
provider: MagicMock | None = None,
patch_deps: bool = False,
) -> AgentLoop:
"""Create a real AgentLoop for testing.
Args:
patch_deps: If True, patch ContextBuilder/SessionManager/SubagentManager
during construction (needed when workspace has no real files).
"""
bus = MessageBus()
if provider is None:
provider = make_provider(default_model=model)
kwargs = dict(
bus=bus,
provider=provider,
workspace=tmp_path,
model=model,
context_window_tokens=context_window_tokens,
session_ttl_minutes=session_ttl_minutes,
unified_session=unified_session,
)
if tools_config is not None:
kwargs["tools_config"] = tools_config
if model_presets is not None:
kwargs["model_presets"] = model_presets
if hooks is not None:
kwargs["hooks"] = hooks
if patch_deps:
with patch("nanobot.agent.loop.ContextBuilder"), \
patch("nanobot.agent.loop.SessionManager"), \
patch("nanobot.agent.loop.SubagentManager") as mock_sub_mgr:
mock_sub_mgr.return_value.cancel_by_session = AsyncMock(return_value=0)
return AgentLoop(**kwargs)
return AgentLoop(**kwargs)
@pytest.fixture
def loop_factory(tmp_path):
"""Fixture providing a factory for creating AgentLoop instances."""
def _factory(**kwargs):
return make_loop(tmp_path, **kwargs)
return _factory