eigent/backend/tests/app/controller/test_model_controller.py
Jingxian Li f8bf372349
Some checks failed
Test / Run Browser Preview Electron Test (push) Has been cancelled
Test / Run Python Tests (push) Has been cancelled
Test / Run Frontend Guardrails (push) Has been cancelled
CodeQL Advanced / Analyze (actions) (push) Has been cancelled
CodeQL Advanced / Analyze (javascript-typescript) (push) Has been cancelled
CodeQL Advanced / Analyze (python) (push) Has been cancelled
Pre-commit / pre-commit (push) Has been cancelled
Test / Run Web + Local Brain Smoke (push) Has been cancelled
feat: add Meta Model API and Muse Spark (#1904)
Co-authored-by: Jessie Li <jessiee@meta.com>
Co-authored-by: lijingxian <jingxianli18@fudan.edu.cn>
Co-authored-by: Tong Chen <web_chentong@163.com>
2026-09-24 10:36:35 +08:00

615 lines
24 KiB
Python

# ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. =========
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. =========
import socket
from unittest.mock import AsyncMock, MagicMock, patch
import httpx
import pytest
from fastapi import HTTPException
from fastapi.testclient import TestClient
from app.component.model_validation import (
EXPECTED_TOOL_RESULT,
ValidationErrorType,
ValidationResult,
ValidationStage,
)
from app.controller.model_controller import (
ListProviderModelsRequest,
ValidateModelRequest,
ValidateModelResponse,
_provider_models_url,
list_provider_models,
validate_model,
)
@pytest.mark.unit
class TestListProviderModels:
@pytest.fixture(autouse=True)
def public_provider_dns(self):
with patch(
"socket.getaddrinfo",
return_value=[
(
socket.AF_INET,
socket.SOCK_STREAM,
socket.IPPROTO_TCP,
"",
("8.8.8.8", 443),
)
],
):
yield
def test_builds_public_https_model_url(self):
assert (
_provider_models_url("https://api.ant-ling.com/v1/", "/models")
== "https://api.ant-ling.com/v1/models"
)
@pytest.mark.parametrize(
("host", "endpoint"),
[
("http://api.example.com/v1", "/models"),
("https://127.0.0.1/v1", "/models"),
("https://api.example.com/v1", "https://other.example/models"),
],
)
def test_rejects_unsafe_model_urls(self, host: str, endpoint: str):
with pytest.raises(HTTPException) as exc_info:
_provider_models_url(host, endpoint)
assert exc_info.value.status_code == 400
@pytest.mark.asyncio
async def test_fetches_model_list_with_provider_credentials(self):
client = AsyncMock()
client.__aenter__.return_value = client
client.get.return_value = httpx.Response(
200, json={"data": [{"id": "ling-chat"}]}
)
request = ListProviderModelsRequest(
api_host="https://api.ant-ling.com/v1",
models_endpoint="/models",
api_key="secret-key",
)
with patch(
"app.controller.model_controller.httpx.AsyncClient",
return_value=client,
):
result = await list_provider_models(request)
assert result == {"data": [{"id": "ling-chat"}]}
client.get.assert_awaited_once_with(
httpx.URL("https://8.8.8.8/v1/models"),
headers={
"Host": "api.ant-ling.com",
"Authorization": "Bearer secret-key",
"Accept": "application/json",
},
extensions={"sni_hostname": "api.ant-ling.com"},
)
@pytest.mark.asyncio
@pytest.mark.parametrize(("status", "expected"), [(401, 401), (404, 404)])
async def test_preserves_provider_http_status_without_raw_body(
self, status: int, expected: int
):
client = AsyncMock()
client.__aenter__.return_value = client
client.get.return_value = httpx.Response(
status, text="upstream secret"
)
request = ListProviderModelsRequest(
api_host="https://api.example.com/v1",
models_endpoint="/models",
api_key="secret-key",
)
with (
patch(
"app.controller.model_controller.httpx.AsyncClient",
return_value=client,
),
pytest.raises(HTTPException) as exc_info,
):
await list_provider_models(request)
assert exc_info.value.status_code == expected
assert "upstream secret" not in str(exc_info.value.detail)
@pytest.mark.asyncio
async def test_rejects_non_json_provider_response(self):
client = AsyncMock()
client.__aenter__.return_value = client
client.get.return_value = httpx.Response(200, text="not json")
request = ListProviderModelsRequest(
api_host="https://api.example.com/v1",
models_endpoint="/models",
api_key="secret-key",
)
with (
patch(
"app.controller.model_controller.httpx.AsyncClient",
return_value=client,
),
pytest.raises(HTTPException) as exc_info,
):
await list_provider_models(request)
assert exc_info.value.status_code == 502
assert exc_info.value.detail == (
"Provider model endpoint returned an invalid response."
)
@pytest.mark.unit
class TestModelControllerEnhanced:
"""Test cases for enhanced model controller with detailed validation."""
def test_validate_model_request_maps_grok_alias(self):
"""Test request model maps grok alias to openai-compatible-model."""
request_data = ValidateModelRequest(
model_platform="grok",
model_type="grok-3",
api_key="test_key",
)
assert request_data.model_platform == "openai-compatible-model"
def test_validate_model_request_maps_nebius_alias(self):
"""Test request model maps Nebius alias to openai-compatible-model."""
request_data = ValidateModelRequest(
model_platform="nebius",
model_type="meta-llama/Llama-3.3-70B-Instruct",
api_key="test_key",
)
assert request_data.model_platform == "openai-compatible-model"
def test_validate_model_request_maps_meta_alias(self):
"""Test request model maps Meta Model API alias correctly."""
request_data = ValidateModelRequest(
model_platform="meta",
model_type="muse-spark-1.3",
api_key="test_key",
)
assert request_data.model_platform == "openai-compatible-model"
def test_validate_model_request_keeps_supported_platforms_unchanged(self):
"""Test request model keeps native camel-ai platforms unchanged."""
request_data = ValidateModelRequest(
model_platform="mistral",
model_type="mistral-large-latest",
api_key="test_key",
)
assert request_data.model_platform == "mistral"
request_data = ValidateModelRequest(
model_platform="samba-nova",
model_type="Meta-Llama-3.1-8B-Instruct",
api_key="test_key",
)
assert request_data.model_platform == "samba-nova"
@pytest.mark.asyncio
async def test_validate_model_with_diagnostics_success(self):
"""Test successful model validation with diagnostics enabled."""
request_data = ValidateModelRequest(
model_platform="openai",
model_type="gpt-4o",
api_key="test_key",
include_diagnostics=True,
)
mock_agent = MagicMock()
mock_response = MagicMock()
tool_call = MagicMock()
tool_call.result = EXPECTED_TOOL_RESULT
mock_response.info = {"tool_calls": [tool_call]}
mock_agent.step.return_value = mock_response
with patch(
"app.controller.model_controller.validate_model_with_details"
) as mock_validate:
validation_result = ValidationResult()
validation_result.is_valid = True
validation_result.is_tool_calls = True
validation_result.error_type = None
validation_result.failed_stage = None
validation_result.successful_stages = [
ValidationStage.INITIALIZATION,
ValidationStage.MODEL_CREATION,
ValidationStage.AGENT_CREATION,
ValidationStage.MODEL_CALL,
ValidationStage.TOOL_CALL_EXECUTION,
]
validation_result.validation_stages = {
ValidationStage.INITIALIZATION: True,
ValidationStage.MODEL_CREATION: True,
ValidationStage.AGENT_CREATION: True,
ValidationStage.MODEL_CALL: True,
ValidationStage.TOOL_CALL_EXECUTION: True,
}
validation_result.diagnostic_info = {
"initialization": {"model_platform": "openai"},
"model_creation": {"model_created": True},
}
validation_result.model_response_info = {
"has_response": True,
"tool_calls_count": 1,
}
validation_result.tool_call_info = {
"tool_calls_count": 1,
"execution_successful": True,
}
mock_validate.return_value = validation_result
response = await validate_model(request_data)
assert isinstance(response, ValidateModelResponse)
assert response.is_valid is True
assert response.is_tool_calls is True
assert response.error_type is None
assert response.failed_stage is None
assert response.successful_stages == [
"initialization",
"model_creation",
"agent_creation",
"model_call",
"tool_call_execution",
]
assert response.diagnostic_info is not None
assert response.model_response_info is not None
assert response.tool_call_info is not None
assert response.validation_stages is not None
assert response.validation_stages["tool_call_execution"] is True
@pytest.mark.asyncio
async def test_validate_model_with_diagnostics_failure(self):
"""Test model validation failure with diagnostics enabled."""
request_data = ValidateModelRequest(
model_platform="openai",
model_type="gpt-4o",
api_key="invalid_key",
include_diagnostics=True,
)
with patch(
"app.controller.model_controller.validate_model_with_details"
) as mock_validate:
validation_result = ValidationResult()
validation_result.is_valid = False
validation_result.is_tool_calls = False
validation_result.error_type = (
ValidationErrorType.AUTHENTICATION_ERROR
)
validation_result.failed_stage = ValidationStage.MODEL_CREATION
validation_result.raw_error_message = "401 Unauthorized"
validation_result.error_message = "401 Unauthorized"
validation_result.successful_stages = [
ValidationStage.INITIALIZATION
]
validation_result.validation_stages = {
ValidationStage.INITIALIZATION: True,
ValidationStage.MODEL_CREATION: False,
}
validation_result.diagnostic_info = {
"initialization": {"model_platform": "openai"},
}
mock_validate.return_value = validation_result
response = await validate_model(request_data)
assert isinstance(response, ValidateModelResponse)
assert response.is_valid is False
assert response.is_tool_calls is False
assert response.error_type == "authentication_error"
assert response.failed_stage == "model_creation"
assert response.successful_stages == ["initialization"]
assert response.diagnostic_info is not None
assert response.message == "401 Unauthorized"
@pytest.mark.asyncio
async def test_validate_model_without_diagnostics(self):
"""Test model validation without diagnostics (default)."""
request_data = ValidateModelRequest(
model_platform="openai",
model_type="gpt-4o",
api_key="test_key",
include_diagnostics=False,
)
with patch(
"app.controller.model_controller.validate_model_with_details"
) as mock_validate:
validation_result = ValidationResult()
validation_result.is_valid = True
validation_result.is_tool_calls = True
mock_validate.return_value = validation_result
response = await validate_model(request_data)
assert isinstance(response, ValidateModelResponse)
assert response.is_valid is True
assert response.is_tool_calls is True
# Diagnostic fields should not be included when include_diagnostics=False
assert response.error_type is None
assert response.failed_stage is None
assert response.successful_stages is None
assert response.diagnostic_info is None
@pytest.mark.asyncio
async def test_validate_model_tool_call_not_supported(self):
"""Test model validation when tool calls are not supported."""
request_data = ValidateModelRequest(
model_platform="openai",
model_type="gpt-4o",
api_key="test_key",
include_diagnostics=True,
)
with patch(
"app.controller.model_controller.validate_model_with_details"
) as mock_validate:
validation_result = ValidationResult()
validation_result.is_valid = True
validation_result.is_tool_calls = False
validation_result.error_type = (
ValidationErrorType.TOOL_CALL_NOT_SUPPORTED
)
validation_result.failed_stage = (
ValidationStage.TOOL_CALL_EXECUTION
)
validation_result.successful_stages = [
ValidationStage.INITIALIZATION,
ValidationStage.MODEL_CREATION,
ValidationStage.AGENT_CREATION,
ValidationStage.MODEL_CALL,
]
mock_validate.return_value = validation_result
response = await validate_model(request_data)
assert isinstance(response, ValidateModelResponse)
assert response.is_valid is True
assert response.is_tool_calls is False
assert response.error_type == "tool_call_not_supported"
assert (
"does not support tool calling" in response.message
or "tool call" in response.message.lower()
)
@pytest.mark.asyncio
async def test_validate_model_tool_call_execution_failed(self):
"""Test model validation when tool call execution fails."""
request_data = ValidateModelRequest(
model_platform="openai",
model_type="gpt-4o",
api_key="test_key",
include_diagnostics=True,
)
with patch(
"app.controller.model_controller.validate_model_with_details"
) as mock_validate:
validation_result = ValidationResult()
validation_result.is_valid = False
validation_result.is_tool_calls = False
validation_result.error_type = (
ValidationErrorType.TOOL_CALL_EXECUTION_FAILED
)
validation_result.failed_stage = (
ValidationStage.TOOL_CALL_EXECUTION
)
validation_result.raw_error_message = "Tool execution failed"
validation_result.error_message = "Tool execution failed"
mock_validate.return_value = validation_result
response = await validate_model(request_data)
assert isinstance(response, ValidateModelResponse)
assert response.is_valid is False
assert response.is_tool_calls is False
assert response.error_type == "tool_call_execution_failed"
assert response.message == "Tool execution failed"
@pytest.mark.asyncio
async def test_validate_model_network_error(self):
"""Test model validation with network error."""
request_data = ValidateModelRequest(
model_platform="openai",
model_type="gpt-4o",
api_key="test_key",
include_diagnostics=True,
)
with patch(
"app.controller.model_controller.validate_model_with_details"
) as mock_validate:
validation_result = ValidationResult()
validation_result.is_valid = False
validation_result.is_tool_calls = False
validation_result.error_type = ValidationErrorType.NETWORK_ERROR
validation_result.failed_stage = ValidationStage.MODEL_CREATION
validation_result.raw_error_message = "Connection timeout"
validation_result.error_message = "Connection timeout"
mock_validate.return_value = validation_result
response = await validate_model(request_data)
assert isinstance(response, ValidateModelResponse)
assert response.is_valid is False
assert response.error_type == "network_error"
assert response.message == "Connection timeout"
@pytest.mark.asyncio
async def test_validate_model_rate_limit_error(self):
"""Test model validation with rate limit error."""
request_data = ValidateModelRequest(
model_platform="openai",
model_type="gpt-4o",
api_key="test_key",
include_diagnostics=True,
)
with patch(
"app.controller.model_controller.validate_model_with_details"
) as mock_validate:
validation_result = ValidationResult()
validation_result.is_valid = False
validation_result.is_tool_calls = False
validation_result.error_type = ValidationErrorType.RATE_LIMIT_ERROR
validation_result.failed_stage = ValidationStage.MODEL_CALL
validation_result.raw_error_message = "429 Rate limit exceeded"
validation_result.error_message = "429 Rate limit exceeded"
mock_validate.return_value = validation_result
response = await validate_model(request_data)
assert isinstance(response, ValidateModelResponse)
assert response.is_valid is False
assert response.error_type == "rate_limit_error"
assert response.message == "429 Rate limit exceeded"
@pytest.mark.asyncio
async def test_validate_model_empty_api_key(self):
"""Test model validation with empty API key."""
request_data = ValidateModelRequest(
model_platform="openai",
model_type="gpt-4o",
api_key="", # Empty API key
)
# Should raise HTTPException before calling validate_model_with_details
with pytest.raises(Exception): # HTTPException is raised
await validate_model(request_data)
@pytest.mark.asyncio
async def test_validate_model_error_response_structure(self):
"""Test that error response structure is correct."""
request_data = ValidateModelRequest(
model_platform="openai",
model_type="gpt-4o",
api_key="test_key",
include_diagnostics=True,
)
with patch(
"app.controller.model_controller.validate_model_with_details"
) as mock_validate:
validation_result = ValidationResult()
validation_result.is_valid = False
validation_result.is_tool_calls = False
validation_result.error_type = (
ValidationErrorType.AUTHENTICATION_ERROR
)
validation_result.error_message = "Invalid API key"
validation_result.error_details = {"code": "invalid_key"}
mock_validate.return_value = validation_result
response = await validate_model(request_data)
assert response.error_code == "authentication_error"
assert response.error is not None
assert response.error["type"] == "invalid_request_error"
assert response.error["code"] == "authentication_error"
assert response.error["message"] == "Invalid API key"
assert "details" in response.error
assert response.error["details"]["code"] == "invalid_key"
@pytest.mark.integration
class TestModelControllerIntegrationEnhanced:
"""Integration tests for enhanced model controller."""
def test_validate_model_endpoint_with_diagnostics(
self, client: TestClient
):
"""Test validate model endpoint with diagnostics through FastAPI test client."""
request_data = {
"model_platform": "openai",
"model_type": "gpt-4o",
"api_key": "test_key",
"include_diagnostics": True,
}
with patch(
"app.controller.model_controller.validate_model_with_details"
) as mock_validate:
validation_result = ValidationResult()
validation_result.is_valid = True
validation_result.is_tool_calls = True
validation_result.successful_stages = [
ValidationStage.INITIALIZATION,
ValidationStage.MODEL_CREATION,
ValidationStage.AGENT_CREATION,
ValidationStage.MODEL_CALL,
ValidationStage.TOOL_CALL_EXECUTION,
]
validation_result.validation_stages = {
ValidationStage.INITIALIZATION: True,
ValidationStage.MODEL_CREATION: True,
ValidationStage.AGENT_CREATION: True,
ValidationStage.MODEL_CALL: True,
ValidationStage.TOOL_CALL_EXECUTION: True,
}
validation_result.diagnostic_info = {"test": "info"}
validation_result.model_response_info = {"has_response": True}
validation_result.tool_call_info = {"tool_calls_count": 1}
mock_validate.return_value = validation_result
response = client.post("/model/validate", json=request_data)
assert response.status_code == 200
response_data = response.json()
assert response_data["is_valid"] is True
assert response_data["is_tool_calls"] is True
assert "error_type" in response_data
assert "failed_stage" in response_data
assert "successful_stages" in response_data
assert "diagnostic_info" in response_data
assert "model_response_info" in response_data
assert "tool_call_info" in response_data
assert "validation_stages" in response_data
def test_validate_model_endpoint_without_diagnostics(
self, client: TestClient
):
"""Test validate model endpoint without diagnostics through FastAPI test client."""
request_data = {
"model_platform": "openai",
"model_type": "gpt-4o",
"api_key": "test_key",
"include_diagnostics": False,
}
with patch(
"app.controller.model_controller.validate_model_with_details"
) as mock_validate:
validation_result = ValidationResult()
validation_result.is_valid = True
validation_result.is_tool_calls = True
mock_validate.return_value = validation_result
response = client.post("/model/validate", json=request_data)
assert response.status_code == 200
response_data = response.json()
assert response_data["is_valid"] is True
assert response_data["is_tool_calls"] is True
# Diagnostic fields should not be present when include_diagnostics=False
assert (
"error_type" not in response_data
or response_data.get("error_type") is None
)
assert (
"failed_stage" not in response_data
or response_data.get("failed_stage") is None
)