|
| 1 | +# (C) Datadog, Inc. 2026-present |
| 2 | +# All rights reserved |
| 3 | +# Licensed under a 3-clause BSD style license (see LICENSE) |
| 4 | + |
| 5 | +from unittest.mock import MagicMock |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from ddev.ai.agent.anthropic_client import AnthropicAgent |
| 10 | +from ddev.ai.agent.build import build_agent, make_agent_builder |
| 11 | +from ddev.ai.phases.config import AgentConfig |
| 12 | +from ddev.ai.tools.fs.file_access_policy import FileAccessPolicy |
| 13 | +from ddev.ai.tools.fs.file_registry import FileRegistry |
| 14 | +from ddev.ai.tools.registry import ToolRegistry |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture |
| 18 | +def file_registry(tmp_path) -> FileRegistry: |
| 19 | + return FileRegistry(policy=FileAccessPolicy(write_root=tmp_path)) |
| 20 | + |
| 21 | + |
| 22 | +def test_build_agent_anthropic_returns_agent_and_registry(file_registry): |
| 23 | + agent_config = AgentConfig(provider="anthropic", model="claude-test", max_tokens=1024, tools=[]) |
| 24 | + agent_clients = {"anthropic": MagicMock()} |
| 25 | + |
| 26 | + agent, registry = build_agent( |
| 27 | + agent_config=agent_config, |
| 28 | + agent_clients=agent_clients, |
| 29 | + system_prompt="hello", |
| 30 | + owner_id="p1", |
| 31 | + file_registry=file_registry, |
| 32 | + ) |
| 33 | + |
| 34 | + assert isinstance(agent, AnthropicAgent) |
| 35 | + assert isinstance(registry, ToolRegistry) |
| 36 | + assert agent.name == "p1" |
| 37 | + |
| 38 | + |
| 39 | +def test_build_agent_missing_client_raises(file_registry): |
| 40 | + agent_config = AgentConfig(provider="anthropic", tools=[]) |
| 41 | + with pytest.raises(ValueError, match="No client provided for agent provider 'anthropic'"): |
| 42 | + build_agent( |
| 43 | + agent_config=agent_config, |
| 44 | + agent_clients={}, |
| 45 | + system_prompt="hello", |
| 46 | + owner_id="p1", |
| 47 | + file_registry=file_registry, |
| 48 | + ) |
| 49 | + |
| 50 | + |
| 51 | +def test_build_agent_unknown_provider_raises(file_registry): |
| 52 | + agent_config = AgentConfig(provider="openai", tools=[]) |
| 53 | + with pytest.raises(ValueError, match="Unknown agent provider: 'openai'"): |
| 54 | + build_agent( |
| 55 | + agent_config=agent_config, |
| 56 | + agent_clients={"openai": MagicMock()}, |
| 57 | + system_prompt="hello", |
| 58 | + owner_id="p1", |
| 59 | + file_registry=file_registry, |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +def test_make_agent_builder_returns_callable_that_delegates_to_build_agent(file_registry): |
| 64 | + agent_config = AgentConfig(provider="anthropic", tools=[]) |
| 65 | + agent_clients = {"anthropic": MagicMock()} |
| 66 | + |
| 67 | + builder = make_agent_builder( |
| 68 | + agent_config=agent_config, |
| 69 | + agent_clients=agent_clients, |
| 70 | + file_registry=file_registry, |
| 71 | + ) |
| 72 | + |
| 73 | + agent, registry = builder("system prompt", "p2") |
| 74 | + assert isinstance(agent, AnthropicAgent) |
| 75 | + assert isinstance(registry, ToolRegistry) |
| 76 | + assert agent.name == "p2" |
0 commit comments