Mechanical only, and separated from the judgment calls that follow so the reviewable changes are not buried in a 98-file whitespace diff. 227 automatic fixes: 60 blank lines carrying whitespace, 60 unsorted import blocks, 34 Optional[X] to X | None, 28 unused imports, 16 deprecated typing imports, 12 datetime.timezone.utc to datetime.UTC, and assorted smaller modernisations. Then `ruff format` over src and tests: 98 files reformatted, 35 already conforming. No file among the unused-import findings defines __all__ or is an __init__.py, so nothing here removes a re-export. `make test`: 658 passed, unchanged from HEAD. Two things observed while verifying, neither addressed here: `pytest tests/` cannot collect — tests/e2e/test_orchestration_e2e.py uses an `e2e` marker that is not registered, and the config is strict about markers. This fails identically at HEAD, so it predates this change; `make test` passes because it ignores tests/e2e, tests/integration and tests/contracts. test_tatlock_tool_call_logging_calculator is flaky. It failed once in a full run with these changes and passed on the next, passes in isolation with them, and fails in isolation at HEAD. It is order- or timing-dependent, not a regression from this commit — established by running the full suite both ways rather than by reasoning about which change could have caused it. Co-Authored-By: Claude <noreply@anthropic.com>
94 lines
3.9 KiB
Python
94 lines
3.9 KiB
Python
"""
|
|
Unit tests for backend selection (Ollama primary, Claude fallback).
|
|
|
|
These tests set the cached health-check globals directly so they are
|
|
deterministic regardless of which services are reachable.
|
|
"""
|
|
|
|
import pytest
|
|
|
|
from src.anthropic import model_selector
|
|
from src.core.config import config
|
|
|
|
|
|
@pytest.fixture
|
|
def local_first(monkeypatch):
|
|
"""Baseline: local-first config, both backends healthy."""
|
|
monkeypatch.setattr(config, "PREFER_CLOUD_BACKEND", False)
|
|
monkeypatch.setattr(config, "ANTHROPIC_API_KEY", "sk-test-fake")
|
|
monkeypatch.setattr(model_selector, "_claude_available", True)
|
|
monkeypatch.setattr(model_selector, "_ollama_available", True)
|
|
|
|
|
|
class TestResolveBackend:
|
|
def test_default_is_ollama(self, local_first):
|
|
assert model_selector.resolve_backend() == "ollama"
|
|
|
|
def test_prefer_cloud_config_selects_claude(self, local_first, monkeypatch):
|
|
monkeypatch.setattr(config, "PREFER_CLOUD_BACKEND", True)
|
|
assert model_selector.resolve_backend() == "claude"
|
|
|
|
def test_prefer_cloud_override_selects_claude(self, local_first):
|
|
assert model_selector.resolve_backend(prefer_cloud=True) == "claude"
|
|
|
|
def test_prefer_cloud_without_claude_falls_back_to_ollama(self, local_first, monkeypatch):
|
|
monkeypatch.setattr(config, "PREFER_CLOUD_BACKEND", True)
|
|
monkeypatch.setattr(model_selector, "_claude_available", False)
|
|
assert model_selector.resolve_backend() == "ollama"
|
|
|
|
def test_ollama_down_falls_back_to_claude(self, local_first, monkeypatch):
|
|
monkeypatch.setattr(model_selector, "_ollama_available", False)
|
|
assert model_selector.resolve_backend() == "claude"
|
|
|
|
def test_ollama_down_without_claude_stays_ollama(self, local_first, monkeypatch):
|
|
monkeypatch.setattr(model_selector, "_ollama_available", False)
|
|
monkeypatch.setattr(model_selector, "_claude_available", False)
|
|
assert model_selector.resolve_backend() == "ollama"
|
|
|
|
def test_unknown_ollama_state_counts_as_available(self, local_first, monkeypatch):
|
|
monkeypatch.setattr(model_selector, "_ollama_available", None)
|
|
assert model_selector.resolve_backend() == "ollama"
|
|
|
|
|
|
class TestGetModel:
|
|
def test_ollama_backend_returns_openai_chat_model(self, local_first):
|
|
from pydantic_ai.models.openai import OpenAIChatModel
|
|
|
|
model = model_selector.get_model()
|
|
assert isinstance(model, OpenAIChatModel)
|
|
assert model.model_name == config.OLLAMA_DEFAULT_MODEL
|
|
|
|
def test_claude_backend_returns_anthropic_model(self, local_first):
|
|
from pydantic_ai.models.anthropic import AnthropicModel
|
|
|
|
model = model_selector.get_model(prefer_cloud=True)
|
|
assert isinstance(model, AnthropicModel)
|
|
assert model.model_name == config.ANTHROPIC_MODEL
|
|
|
|
|
|
class TestToolChoiceSettings:
|
|
def test_ollama_forces_tool_choice(self, local_first):
|
|
settings = model_selector.get_tool_choice_settings()
|
|
assert settings.get("extra_body") == {"tool_choice": "required"}
|
|
|
|
def test_claude_uses_native_tool_choice(self, local_first, monkeypatch):
|
|
monkeypatch.setattr(config, "PREFER_CLOUD_BACKEND", True)
|
|
settings = model_selector.get_tool_choice_settings()
|
|
assert not settings.get("extra_body")
|
|
|
|
|
|
class TestGetModelInfo:
|
|
def test_reports_ollama_primary(self, local_first):
|
|
info = model_selector.get_model_info()
|
|
assert info["backend"] == "ollama"
|
|
assert info["model"] == config.OLLAMA_DEFAULT_MODEL
|
|
assert info["ollama_available"] is True
|
|
assert info["claude_available"] is True
|
|
assert info["prefer_cloud"] is False
|
|
|
|
def test_reports_claude_when_ollama_down(self, local_first, monkeypatch):
|
|
monkeypatch.setattr(model_selector, "_ollama_available", False)
|
|
info = model_selector.get_model_info()
|
|
assert info["backend"] == "claude"
|
|
assert info["model"] == config.ANTHROPIC_MODEL
|