eigent/backend/tests/unit/model/test_agent_model_config.py
MkDev11 37ae935e78
feat: support flexible model config for different agents (#1001)
Co-authored-by: mkdev11 <MkDev11@users.noreply.github.com>
Co-authored-by: Wendong-Fan <133094783+Wendong-Fan@users.noreply.github.com>
Co-authored-by: Wendong-Fan <w3ndong.fan@gmail.com>
2026-01-27 04:19:36 +08:00

109 lines
4.2 KiB
Python

"""Unit tests for AgentModelConfig and per-agent model configuration."""
from app.model.chat import AgentModelConfig, NewAgent
class TestAgentModelConfig:
"""Tests for the AgentModelConfig model."""
def test_agent_model_config_creation_empty(self):
"""Test creating an empty AgentModelConfig."""
config = AgentModelConfig()
assert config.model_platform is None
assert config.model_type is None
assert config.api_key is None
assert config.api_url is None
assert config.extra_params is None
def test_agent_model_config_creation_with_values(self):
"""Test creating an AgentModelConfig with values."""
config = AgentModelConfig(
model_platform="openai",
model_type="gpt-4",
api_key="test-key",
api_url="https://api.openai.com/v1",
extra_params={"temperature": 0.7}
)
assert config.model_platform == "openai"
assert config.model_type == "gpt-4"
assert config.api_key == "test-key"
assert config.api_url == "https://api.openai.com/v1"
assert config.extra_params == {"temperature": 0.7}
def test_has_custom_config_false_when_empty(self):
"""Test has_custom_config returns False for empty config."""
config = AgentModelConfig()
assert config.has_custom_config() is False
def test_has_custom_config_true_with_platform(self):
"""Test has_custom_config returns True when platform is set."""
config = AgentModelConfig(model_platform="anthropic")
assert config.has_custom_config() is True
def test_has_custom_config_true_with_model_type(self):
"""Test has_custom_config returns True when model_type is set."""
config = AgentModelConfig(model_type="claude-3-opus")
assert config.has_custom_config() is True
def test_has_custom_config_true_with_both(self):
"""Test has_custom_config returns True when both are set."""
config = AgentModelConfig(
model_platform="anthropic",
model_type="claude-3-opus"
)
assert config.has_custom_config() is True
def test_has_custom_config_true_with_only_api_key(self):
"""Test has_custom_config returns True with only api_key."""
config = AgentModelConfig(api_key="some-key")
assert config.has_custom_config() is True
class TestNewAgentWithModelConfig:
"""Tests for NewAgent with custom_model_config."""
def test_new_agent_without_model_config(self):
"""Test NewAgent creation without custom model config."""
agent = NewAgent(
name="TestAgent",
description="A test agent",
tools=[],
mcp_tools=None
)
assert agent.name == "TestAgent"
assert agent.custom_model_config is None
def test_new_agent_with_model_config(self):
"""Test NewAgent creation with custom model config."""
model_config = AgentModelConfig(
model_platform="openai",
model_type="gpt-4-turbo"
)
agent = NewAgent(
name="CustomModelAgent",
description="An agent with custom model",
tools=[],
mcp_tools=None,
custom_model_config=model_config
)
assert agent.name == "CustomModelAgent"
assert agent.custom_model_config is not None
assert agent.custom_model_config.model_platform == "openai"
assert agent.custom_model_config.model_type == "gpt-4-turbo"
def test_new_agent_serialization_with_model_config(self):
"""Test NewAgent serialization includes model config."""
model_config = AgentModelConfig(
model_platform="anthropic",
model_type="claude-3-sonnet"
)
agent = NewAgent(
name="SerializationTest",
description="Test serialization",
tools=[],
mcp_tools=None,
custom_model_config=model_config
)
data = agent.model_dump()
assert "custom_model_config" in data
assert data["custom_model_config"]["model_platform"] == "anthropic"
assert data["custom_model_config"]["model_type"] == "claude-3-sonnet"