Enterprise agent platform for building, deploying, and operating AI teammates.
"""REST agent-preview endpoint must scope tools to the request's
``tool_categories`` (issues #798 / #117).
Before the fix, ``preview_agent`` built its ``WebToolConfig`` without a
``ToolSelectionSpec`` (and with the default ``include_mcp_tools=True``),
so a preview loaded every Custom API / MCP server the *user* had
configured — regardless of what the previewed agent actually selected.
These tests pin the spec handed to ``WebToolConfig`` for each
``tool_categories`` shape, mirroring the runtime chat-path semantics.
"""
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from xagent.web.api.agents import AgentPreviewRequest, preview_agent
from xagent.web.models.user import User
def _make_user() -> User:
user = User()
user.id = 7
user.is_admin = False
return user
async def _run_preview(tool_categories, *, execute_error=None):
"""Run preview_agent with a stubbed LLM/AgentService and return the
WebToolConfig it was constructed with."""
current_user = _make_user()
db = MagicMock()
model_record = MagicMock()
model_record.model_id = "test-model"
db.query.return_value.filter.return_value.first.return_value = model_record
request = AgentPreviewRequest(
instructions="preview instructions",
execution_mode="balanced",
models={"general": 1},
knowledge_bases=[],
skills=[],
tool_categories=tool_categories,
message="hello",
)
with (
patch("xagent.web.api.agents.UserAwareModelStorage") as mock_storage_class,
patch("xagent.web.api.agents.InMemoryMemoryStore"),
patch("xagent.web.api.agents.AgentService") as mock_agent_service_class,
):
mock_storage = MagicMock()
mock_storage.get_llm_by_name_with_access.return_value = MagicMock()
mock_storage_class.return_value = mock_storage
mock_agent_service = mock_agent_service_class.return_value
mock_agent_service.execute_task = AsyncMock(
return_value={"output": "preview response", "status": "completed"},
side_effect=execute_error,
)
await preview_agent(request=request, current_user=current_user, db=db)
return mock_agent_service_class.call_args.kwargs["tool_config"]
@pytest.mark.asyncio
async def test_preview_scoped_categories_exclude_custom_api_and_mcp():
"""A preview for an agent that selected only ``basic`` must not load
the user's Custom API registry nor initialize MCP servers."""
tool_config = await _run_preview(["basic_tools"])
spec = tool_config.get_tool_selection_spec()
assert spec.is_by_categories()
assert spec.includes_category("basic_tools") is True
assert spec.includes_custom_api() is False
assert spec.includes_mcp() is False
assert tool_config._include_mcp_tools is False
@pytest.mark.asyncio
async def test_preview_mcp_server_scope_loads_only_that_connector():
"""``mcp:<server>`` selection opts into MCP config loading and admits
the matching Custom API wrapper only."""
tool_config = await _run_preview(["mcp:LinkedIn"])
spec = tool_config.get_tool_selection_spec()
assert spec.is_by_categories()
assert spec.mcp_servers == frozenset({"linkedin"})
assert spec.includes_custom_api() is True
assert tool_config._include_mcp_tools is True
@pytest.mark.asyncio
async def test_preview_empty_categories_yield_zero_tools():
"""Explicit ``[]`` mirrors a saved agent with zero tools selected."""
tool_config = await _run_preview([])
spec = tool_config.get_tool_selection_spec()
assert spec.is_none()
assert spec.includes_custom_api() is False
assert tool_config._include_mcp_tools is False
@pytest.mark.asyncio
async def test_preview_omitted_categories_keep_builtins_but_not_custom_apis():
"""Field omitted (None, legacy "unconfigured") keeps the full built-in
tool set, but must NOT bulk-load the user-level Custom API registry —
the same opt-out the delegated-agent path applies (#798 / #117)."""
tool_config = await _run_preview(None)
spec = tool_config.get_tool_selection_spec()
assert spec.is_all()
assert spec.includes_custom_api() is False
# Legacy ALL mode does not pay MCP server init on the preview path.
assert tool_config._include_mcp_tools is False
@pytest.mark.asyncio
@pytest.mark.parametrize("category", ["basic", "code_execution"])
@pytest.mark.parametrize("execute_error", [None, RuntimeError("execution failed")])
async def test_preview_binds_task_sandbox_and_cleans_it_on_all_outcomes(
monkeypatch, tmp_path, execute_error, category
):
from fastapi import HTTPException
monkeypatch.setenv("SANDBOX_ENABLED", "true")
monkeypatch.setenv("XAGENT_UPLOADS_DIR", str(tmp_path))
manager = MagicMock()
provider = object()
manager.get_or_create_lease_provider = AsyncMock(return_value=provider)
manager.delete_sandbox = AsyncMock()
with patch("xagent.web.sandbox_manager.get_sandbox_manager", return_value=manager):
if execute_error:
with pytest.raises(HTTPException, match="execution failed"):
await _run_preview([category], execute_error=execute_error)
else:
config = await _run_preview([category])
assert config.get_sandbox() is provider
call = manager.get_or_create_lease_provider.await_args
assert call.args[0] == "task"
task_id = call.args[1]
assert call.kwargs["mount_intent"].mount_root == str(tmp_path / "preview" / task_id)
manager.delete_sandbox.assert_awaited_once_with("task", task_id)
@pytest.mark.asyncio
@pytest.mark.parametrize("categories", [[], ["basic_tools"], ["web_tools"]])
async def test_non_code_preview_does_not_allocate_sandbox(monkeypatch, categories):
monkeypatch.setenv("SANDBOX_ENABLED", "true")
with patch("xagent.web.sandbox_manager.get_sandbox_manager") as get_manager:
config = await _run_preview(categories)
assert config.get_sandbox() is None
get_manager.assert_not_called()