feat: first commit
This commit is contained in:
239
tests/test_llm.py
Normal file
239
tests/test_llm.py
Normal file
@@ -0,0 +1,239 @@
|
||||
"""Unit tests for the llm provider abstraction module."""
|
||||
|
||||
import os
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
class TestDefaultProviderBehavior:
|
||||
"""3.1 - Default provider behavior: no env vars → ChatOpenAI with gpt-4o-mini."""
|
||||
|
||||
@patch("llm.ChatOpenAI")
|
||||
def test_default_provider_returns_chatopenai(self, mock_openai):
|
||||
"""With no env vars set, create_model() should use ChatOpenAI."""
|
||||
mock_instance = MagicMock()
|
||||
mock_openai.return_value = mock_instance
|
||||
|
||||
env = {"PATH": os.environ.get("PATH", "")}
|
||||
with patch.dict(os.environ, env, clear=True):
|
||||
from llm import create_model
|
||||
|
||||
result = create_model()
|
||||
|
||||
mock_openai.assert_called_once_with(model="gpt-4o-mini")
|
||||
assert result is mock_instance
|
||||
|
||||
@patch("llm.ChatOpenAI")
|
||||
def test_default_model_is_gpt4o_mini(self, mock_openai):
|
||||
"""Default model name should be gpt-4o-mini when MODEL_NAME is not set."""
|
||||
mock_openai.return_value = MagicMock()
|
||||
|
||||
env = {"PATH": os.environ.get("PATH", "")}
|
||||
with patch.dict(os.environ, env, clear=True):
|
||||
from llm import create_model
|
||||
|
||||
create_model()
|
||||
|
||||
mock_openai.assert_called_once_with(model="gpt-4o-mini")
|
||||
|
||||
|
||||
class TestProviderDefaultModels:
|
||||
"""3.2 - Each provider's default model name and instance type."""
|
||||
|
||||
@patch("llm.ChatOpenAI")
|
||||
def test_openai_default_model(self, mock_openai):
|
||||
"""OpenAI provider should default to gpt-4o-mini."""
|
||||
mock_openai.return_value = MagicMock()
|
||||
|
||||
with patch.dict(os.environ, {"LLM_PROVIDER": "openai"}, clear=True):
|
||||
from llm import create_model
|
||||
|
||||
create_model()
|
||||
|
||||
mock_openai.assert_called_once_with(model="gpt-4o-mini")
|
||||
|
||||
@patch("llm.ChatOpenAI")
|
||||
def test_llama_server_default_model(self, mock_openai):
|
||||
"""Llama-server provider should default to local-model."""
|
||||
mock_openai.return_value = MagicMock()
|
||||
|
||||
with patch.dict(os.environ, {"LLM_PROVIDER": "llama-server"}, clear=True):
|
||||
from llm import create_model
|
||||
|
||||
create_model()
|
||||
|
||||
mock_openai.assert_called_once()
|
||||
_, kwargs = mock_openai.call_args
|
||||
assert kwargs["model"] == "local-model"
|
||||
|
||||
@patch("llm.ChatBedrock")
|
||||
def test_bedrock_default_model(self, mock_bedrock):
|
||||
"""Bedrock provider should default to us.anthropic.claude-3-5-sonnet-20241022-v2:0."""
|
||||
mock_bedrock.return_value = MagicMock()
|
||||
|
||||
with patch.dict(os.environ, {"LLM_PROVIDER": "bedrock"}, clear=True):
|
||||
from llm import create_model
|
||||
|
||||
create_model()
|
||||
|
||||
mock_bedrock.assert_called_once()
|
||||
_, kwargs = mock_bedrock.call_args
|
||||
assert kwargs["model_id"] == "us.anthropic.claude-3-5-sonnet-20241022-v2:0"
|
||||
|
||||
|
||||
class TestLlamaServerDefaults:
|
||||
"""3.3 - Llama-server defaults: URL, API key, model name."""
|
||||
|
||||
@patch("llm.ChatOpenAI")
|
||||
def test_llama_server_default_url(self, mock_openai):
|
||||
"""Llama-server should default to http://localhost:8080/v1."""
|
||||
mock_openai.return_value = MagicMock()
|
||||
|
||||
with patch.dict(os.environ, {"LLM_PROVIDER": "llama-server"}, clear=True):
|
||||
from llm import create_model
|
||||
|
||||
create_model()
|
||||
|
||||
_, kwargs = mock_openai.call_args
|
||||
assert kwargs["base_url"] == "http://localhost:8080/v1"
|
||||
|
||||
@patch("llm.ChatOpenAI")
|
||||
def test_llama_server_api_key(self, mock_openai):
|
||||
"""Llama-server should set api_key to 'not-needed'."""
|
||||
mock_openai.return_value = MagicMock()
|
||||
|
||||
with patch.dict(os.environ, {"LLM_PROVIDER": "llama-server"}, clear=True):
|
||||
from llm import create_model
|
||||
|
||||
create_model()
|
||||
|
||||
_, kwargs = mock_openai.call_args
|
||||
assert kwargs["api_key"] == "not-needed"
|
||||
|
||||
@patch("llm.ChatOpenAI")
|
||||
def test_llama_server_default_model_name(self, mock_openai):
|
||||
"""Llama-server should default to 'local-model'."""
|
||||
mock_openai.return_value = MagicMock()
|
||||
|
||||
with patch.dict(os.environ, {"LLM_PROVIDER": "llama-server"}, clear=True):
|
||||
from llm import create_model
|
||||
|
||||
create_model()
|
||||
|
||||
_, kwargs = mock_openai.call_args
|
||||
assert kwargs["model"] == "local-model"
|
||||
|
||||
@patch("llm.ChatOpenAI")
|
||||
def test_llama_server_custom_url(self, mock_openai):
|
||||
"""Llama-server should use LLAMA_SERVER_URL when set."""
|
||||
mock_openai.return_value = MagicMock()
|
||||
|
||||
with patch.dict(
|
||||
os.environ,
|
||||
{"LLM_PROVIDER": "llama-server", "LLAMA_SERVER_URL": "http://myserver:9090/v1"},
|
||||
clear=True,
|
||||
):
|
||||
from llm import create_model
|
||||
|
||||
create_model()
|
||||
|
||||
_, kwargs = mock_openai.call_args
|
||||
assert kwargs["base_url"] == "http://myserver:9090/v1"
|
||||
|
||||
|
||||
class TestImportSideEffects:
|
||||
"""3.4 - Importing llm module does not create model instances."""
|
||||
|
||||
def test_import_does_not_call_constructors(self):
|
||||
"""Importing the llm module should not instantiate any models."""
|
||||
with patch("llm.ChatOpenAI") as mock_openai, \
|
||||
patch("llm.ChatBedrock") as mock_bedrock:
|
||||
# Force re-import by reloading
|
||||
import importlib
|
||||
import llm
|
||||
|
||||
importlib.reload(llm)
|
||||
|
||||
mock_openai.assert_not_called()
|
||||
mock_bedrock.assert_not_called()
|
||||
|
||||
def test_registry_and_defaults_are_dicts(self):
|
||||
"""PROVIDER_REGISTRY and DEFAULT_MODELS should be dicts at module level."""
|
||||
from llm import DEFAULT_MODELS, PROVIDER_REGISTRY
|
||||
|
||||
assert isinstance(PROVIDER_REGISTRY, dict)
|
||||
assert isinstance(DEFAULT_MODELS, dict)
|
||||
|
||||
def test_no_model_instances_at_module_level(self):
|
||||
"""No BaseChatModel instances should exist at module level."""
|
||||
import llm
|
||||
from langchain_core.language_models import BaseChatModel
|
||||
|
||||
for attr_name in dir(llm):
|
||||
attr = getattr(llm, attr_name)
|
||||
assert not isinstance(attr, BaseChatModel), (
|
||||
f"Module-level attribute '{attr_name}' is a model instance"
|
||||
)
|
||||
|
||||
|
||||
class TestProviderRegistry:
|
||||
"""3.5 - PROVIDER_REGISTRY exposes expected provider names."""
|
||||
|
||||
def test_registry_has_expected_providers(self):
|
||||
"""Registry should contain openai, llama-server, and bedrock."""
|
||||
from llm import PROVIDER_REGISTRY
|
||||
|
||||
expected = {"openai", "llama-server", "bedrock"}
|
||||
assert set(PROVIDER_REGISTRY.keys()) == expected
|
||||
|
||||
def test_registry_values_are_callable(self):
|
||||
"""All registry values should be callable factory functions."""
|
||||
from llm import PROVIDER_REGISTRY
|
||||
|
||||
for name, factory in PROVIDER_REGISTRY.items():
|
||||
assert callable(factory), f"Factory for '{name}' is not callable"
|
||||
|
||||
def test_registry_has_three_providers(self):
|
||||
"""Registry should have exactly 3 providers."""
|
||||
from llm import PROVIDER_REGISTRY
|
||||
|
||||
assert len(PROVIDER_REGISTRY) == 3
|
||||
|
||||
|
||||
class TestCreateAgentBackwardCompatibility:
|
||||
"""3.6 - create_agent() still registers all 5 tools after refactor."""
|
||||
|
||||
@patch("llm.ChatOpenAI")
|
||||
def test_create_agent_has_all_five_tools(self, mock_openai):
|
||||
"""create_agent() should register read_file, grep, write_file, list_dir, replace_in_file."""
|
||||
mock_model = MagicMock()
|
||||
mock_openai.return_value = mock_model
|
||||
|
||||
with patch.dict(os.environ, {"OPENAI_API_KEY": "test-key"}):
|
||||
from main import create_agent
|
||||
|
||||
agent = create_agent()
|
||||
|
||||
tool_node = agent.nodes.get("tools")
|
||||
assert tool_node is not None, "Agent should have a 'tools' node"
|
||||
|
||||
tool_names = set(tool_node.bound.tools_by_name.keys())
|
||||
expected = {"read_file", "grep", "write_file", "list_dir", "replace_in_file"}
|
||||
assert tool_names == expected, f"Expected tools {expected}, got {tool_names}"
|
||||
|
||||
@patch("llm.ChatOpenAI")
|
||||
def test_create_agent_calls_create_model(self, mock_openai):
|
||||
"""create_agent() should use create_model() to get the LLM."""
|
||||
mock_openai.return_value = MagicMock()
|
||||
|
||||
with patch.dict(os.environ, {"OPENAI_API_KEY": "test-key"}), \
|
||||
patch("agent.create_model") as mock_create_model:
|
||||
mock_model = MagicMock()
|
||||
mock_model.bind_tools.return_value = MagicMock()
|
||||
mock_create_model.return_value = mock_model
|
||||
from main import create_agent
|
||||
|
||||
create_agent()
|
||||
|
||||
mock_create_model.assert_called_once()
|
||||
Reference in New Issue
Block a user