Add chat client Agent typing tests (#6950)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
81e425b44f
commit
07981847ed
@@ -7,6 +7,7 @@ from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from agent_framework import (
|
||||
Agent,
|
||||
ChatMiddlewareLayer,
|
||||
ChatOptions,
|
||||
ChatResponseUpdate,
|
||||
@@ -136,6 +137,16 @@ def test_anthropic_client_wraps_raw_client_with_standard_layer_order() -> None:
|
||||
assert not issubclass(RawAnthropicClient, ChatTelemetryLayer)
|
||||
|
||||
|
||||
def test_agent_accepts_anthropic_clients() -> None:
|
||||
raw_client = RawAnthropicClient(api_key="test-api-key", model="claude-3-5-sonnet-20241022")
|
||||
raw_agent = Agent(client=raw_client, instructions="test agent")
|
||||
assert raw_agent.client is raw_client
|
||||
|
||||
client = AnthropicClient(api_key="test-api-key", model="claude-3-5-sonnet-20241022")
|
||||
agent = Agent(client=client, instructions="test agent")
|
||||
assert agent.client is client
|
||||
|
||||
|
||||
def test_anthropic_client_init_auto_create_client(
|
||||
anthropic_unit_test_env: dict[str, str],
|
||||
) -> None:
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from agent_framework import ChatMiddlewareLayer, FunctionInvocationLayer
|
||||
from agent_framework import Agent, ChatMiddlewareLayer, FunctionInvocationLayer
|
||||
from agent_framework._telemetry import get_user_agent
|
||||
from agent_framework.observability import ChatTelemetryLayer
|
||||
|
||||
@@ -42,6 +42,71 @@ def test_provider_client_wraps_raw_client_with_standard_layer_order(public_clien
|
||||
assert mro.index(ChatTelemetryLayer) < mro.index(raw_client)
|
||||
|
||||
|
||||
def test_agent_accepts_anthropic_foundry_clients() -> None:
|
||||
mock_transport = _create_mock_transport("https://test-resource.services.ai.azure.com/anthropic/")
|
||||
with patch("agent_framework_anthropic._foundry_client.AsyncAnthropicFoundry", return_value=mock_transport):
|
||||
raw_client = RawAnthropicFoundryClient(
|
||||
model="claude-foundry-test",
|
||||
resource="test-resource",
|
||||
api_key="test-key",
|
||||
)
|
||||
raw_agent = Agent(client=raw_client, instructions="test agent")
|
||||
assert raw_agent.client is raw_client
|
||||
|
||||
with patch("agent_framework_anthropic._foundry_client.AsyncAnthropicFoundry", return_value=mock_transport):
|
||||
client = AnthropicFoundryClient(
|
||||
model="claude-foundry-test",
|
||||
resource="test-resource",
|
||||
api_key="test-key",
|
||||
)
|
||||
agent = Agent(client=client, instructions="test agent")
|
||||
assert agent.client is client
|
||||
|
||||
|
||||
def test_agent_accepts_anthropic_bedrock_clients() -> None:
|
||||
mock_transport = _create_mock_transport("https://bedrock-runtime.us-east-1.amazonaws.com")
|
||||
with patch("agent_framework_anthropic._bedrock_client.AsyncAnthropicBedrock", return_value=mock_transport):
|
||||
raw_client = RawAnthropicBedrockClient(
|
||||
model="claude-bedrock-test",
|
||||
aws_access_key="access-key",
|
||||
aws_secret_key="secret-key",
|
||||
aws_region="us-east-1",
|
||||
)
|
||||
raw_agent = Agent(client=raw_client, instructions="test agent")
|
||||
assert raw_agent.client is raw_client
|
||||
|
||||
with patch("agent_framework_anthropic._bedrock_client.AsyncAnthropicBedrock", return_value=mock_transport):
|
||||
client = AnthropicBedrockClient(
|
||||
model="claude-bedrock-test",
|
||||
aws_access_key="access-key",
|
||||
aws_secret_key="secret-key",
|
||||
aws_region="us-east-1",
|
||||
)
|
||||
agent = Agent(client=client, instructions="test agent")
|
||||
assert agent.client is client
|
||||
|
||||
|
||||
def test_agent_accepts_anthropic_vertex_clients() -> None:
|
||||
mock_transport = _create_mock_transport("https://us-central1-aiplatform.googleapis.com/v1")
|
||||
with patch("agent_framework_anthropic._vertex_client.AsyncAnthropicVertex", return_value=mock_transport):
|
||||
raw_client = RawAnthropicVertexClient(
|
||||
model="claude-vertex-test",
|
||||
region="us-central1",
|
||||
project_id="test-project",
|
||||
)
|
||||
raw_agent = Agent(client=raw_client, instructions="test agent")
|
||||
assert raw_agent.client is raw_client
|
||||
|
||||
with patch("agent_framework_anthropic._vertex_client.AsyncAnthropicVertex", return_value=mock_transport):
|
||||
client = AnthropicVertexClient(
|
||||
model="claude-vertex-test",
|
||||
region="us-central1",
|
||||
project_id="test-project",
|
||||
)
|
||||
agent = Agent(client=client, instructions="test agent")
|
||||
assert agent.client is client
|
||||
|
||||
|
||||
def test_raw_anthropic_foundry_client_creates_sdk_client_from_settings(tmp_path) -> None:
|
||||
env_file = tmp_path / ".env"
|
||||
env_file.write_text(
|
||||
|
||||
@@ -5,7 +5,7 @@ from __future__ import annotations
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
from agent_framework import Content, Message
|
||||
from agent_framework import Agent, Content, Message
|
||||
|
||||
from agent_framework_bedrock import BedrockChatClient
|
||||
|
||||
@@ -40,6 +40,12 @@ def _make_client() -> BedrockChatClient:
|
||||
)
|
||||
|
||||
|
||||
def test_agent_accepts_bedrock_chat_client() -> None:
|
||||
client = _make_client()
|
||||
agent = Agent(client=client, instructions="test agent")
|
||||
assert agent.client is client
|
||||
|
||||
|
||||
async def test_get_response_invokes_bedrock_runtime() -> None:
|
||||
stub = _StubBedrockRuntime()
|
||||
client = BedrockChatClient(
|
||||
|
||||
@@ -12,6 +12,7 @@ from uuid import uuid4
|
||||
|
||||
import pytest
|
||||
from agent_framework import (
|
||||
Agent,
|
||||
AgentResponse,
|
||||
AgentSession,
|
||||
ChatContext,
|
||||
@@ -109,6 +110,20 @@ def test_raw_foundry_agent_chat_client_init_with_agent_name() -> None:
|
||||
mock_project.get_openai_client.assert_called_once_with()
|
||||
|
||||
|
||||
def test_agent_accepts_raw_foundry_agent_chat_client() -> None:
|
||||
mock_project = MagicMock()
|
||||
mock_project.get_openai_client.return_value = MagicMock()
|
||||
|
||||
client = RawFoundryAgentChatClient(
|
||||
project_client=mock_project,
|
||||
agent_name="test-agent",
|
||||
agent_version="1.0",
|
||||
)
|
||||
|
||||
agent = Agent(client=client, instructions="test agent")
|
||||
assert agent.client is client
|
||||
|
||||
|
||||
def test_raw_foundry_agent_chat_client_init_passes_agent_name_when_preview_enabled() -> None:
|
||||
"""Test preview-enabled clients bind the OpenAI client to the agent endpoint."""
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ from typing import Annotated, Any, cast
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from agent_framework import ChatResponse, Content, Message, SupportsChatGetResponse, tool
|
||||
from agent_framework import Agent, ChatResponse, Content, Message, SupportsChatGetResponse, tool
|
||||
from agent_framework._telemetry import get_user_agent
|
||||
from agent_framework.exceptions import ChatClientException, ChatClientInvalidRequestException
|
||||
from agent_framework_openai import OpenAIContentFilterException
|
||||
@@ -1412,3 +1412,17 @@ def test_parse_chunk_surfaces_oauth_consent_requested_event() -> None:
|
||||
assert consent_contents[0].consent_link == "https://consent-host.example.com/authorize?code=xyz"
|
||||
assert update.role == "assistant"
|
||||
assert update.raw_representation is mock_event
|
||||
|
||||
|
||||
def test_agent_accepts_foundry_chat_clients() -> None:
|
||||
mock_project = MagicMock()
|
||||
mock_openai = _make_mock_openai_client()
|
||||
mock_project.get_openai_client.return_value = mock_openai
|
||||
|
||||
raw_client = RawFoundryChatClient(project_client=mock_project, model="test-model")
|
||||
raw_agent = Agent(client=raw_client, instructions="test agent")
|
||||
assert raw_agent.client is raw_client
|
||||
|
||||
client = FoundryChatClient(project_client=mock_project, model="test-model")
|
||||
agent = Agent(client=client, instructions="test agent")
|
||||
assert agent.client is client
|
||||
|
||||
@@ -4,7 +4,7 @@ import inspect
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from agent_framework import SupportsChatGetResponse
|
||||
from agent_framework import Agent, SupportsChatGetResponse
|
||||
from agent_framework._settings import load_settings
|
||||
from agent_framework.exceptions import SettingNotFoundError
|
||||
from agent_framework.foundry import FoundryLocalClient
|
||||
@@ -67,6 +67,17 @@ def test_foundry_local_client_init(mock_foundry_local_manager: MagicMock) -> Non
|
||||
assert isinstance(client, SupportsChatGetResponse)
|
||||
|
||||
|
||||
def test_agent_accepts_foundry_local_client(mock_foundry_local_manager: MagicMock) -> None:
|
||||
with patch(
|
||||
"agent_framework_foundry_local._foundry_local_client.FoundryLocalManager",
|
||||
return_value=mock_foundry_local_manager,
|
||||
):
|
||||
client = FoundryLocalClient(model="test-model-id")
|
||||
|
||||
agent = Agent(client=client, instructions="test agent")
|
||||
assert agent.client is client
|
||||
|
||||
|
||||
def test_foundry_local_client_get_response_uses_explicit_runtime_buckets() -> None:
|
||||
"""Foundry Local should expose explicit runtime buckets instead of raw kwargs."""
|
||||
signature = inspect.signature(FoundryLocalClient.get_response)
|
||||
|
||||
@@ -13,7 +13,7 @@ from agent_framework import Agent, Content, FunctionTool, Message
|
||||
from google.genai import types
|
||||
from pydantic import BaseModel
|
||||
|
||||
from agent_framework_gemini import GeminiChatClient, GeminiChatOptions, ThinkingConfig
|
||||
from agent_framework_gemini import GeminiChatClient, GeminiChatOptions, RawGeminiChatClient, ThinkingConfig
|
||||
|
||||
|
||||
def _has_gemini_integration_credentials() -> bool:
|
||||
@@ -142,6 +142,20 @@ def _make_gemini_client(
|
||||
return client, mock
|
||||
|
||||
|
||||
def test_agent_accepts_gemini_chat_clients() -> None:
|
||||
mock = MagicMock()
|
||||
mock._api_client.vertexai = False
|
||||
mock._api_client._http_options.base_url = "https://generativelanguage.googleapis.com/"
|
||||
|
||||
raw_client = RawGeminiChatClient(client=mock, model="gemini-2.5-flash")
|
||||
raw_agent = Agent(client=raw_client, instructions="test agent")
|
||||
assert raw_agent.client is raw_client
|
||||
|
||||
client, _ = _make_gemini_client(model="gemini-2.5-flash")
|
||||
agent = Agent(client=client, instructions="test agent")
|
||||
assert agent.client is client
|
||||
|
||||
|
||||
def _parts(content: types.Content) -> list[types.Part]:
|
||||
assert content.parts is not None
|
||||
return content.parts
|
||||
|
||||
@@ -7,6 +7,7 @@ from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from agent_framework import (
|
||||
Agent,
|
||||
BaseChatClient,
|
||||
ChatResponseUpdate,
|
||||
Content,
|
||||
@@ -74,6 +75,12 @@ def chat_history() -> list[Message]:
|
||||
return []
|
||||
|
||||
|
||||
def test_agent_accepts_ollama_chat_client(ollama_unit_test_env: dict[str, str]) -> None:
|
||||
client = OllamaChatClient()
|
||||
agent = Agent(client=client, instructions="test agent")
|
||||
assert agent.client is client
|
||||
|
||||
|
||||
@fixture
|
||||
def mock_streaming_chat_completion_response() -> AsyncStream[OllamaChatResponse]:
|
||||
response = OllamaChatResponse(
|
||||
|
||||
@@ -227,6 +227,16 @@ def test_raw_openai_chat_client_accepts_preconfigured_client_with_timeout() -> N
|
||||
assert client is not None
|
||||
|
||||
|
||||
def test_agent_accepts_openai_chat_clients() -> None:
|
||||
raw_client = RawOpenAIChatClient(api_key="test-api-key", model="test-model")
|
||||
raw_agent = Agent(client=raw_client, instructions="test agent")
|
||||
assert raw_agent.client is raw_client
|
||||
|
||||
client = OpenAIChatClient(api_key="test-api-key", model="test-model")
|
||||
agent = Agent(client=client, instructions="test agent")
|
||||
assert agent.client is client
|
||||
|
||||
|
||||
def test_openai_chat_client_supports_all_tool_protocols() -> None:
|
||||
assert isinstance(OpenAIChatClient, SupportsCodeInterpreterTool) # pyrefly: ignore[unsafe-overlap]
|
||||
assert isinstance(OpenAIChatClient, SupportsWebSearchTool) # pyrefly: ignore[unsafe-overlap]
|
||||
|
||||
@@ -8,6 +8,7 @@ from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from agent_framework import (
|
||||
Agent,
|
||||
ChatResponse,
|
||||
Content,
|
||||
Message,
|
||||
@@ -70,6 +71,16 @@ def test_init_uses_explicit_parameters() -> None:
|
||||
assert all(parameter.kind != inspect.Parameter.VAR_KEYWORD for parameter in signature.parameters.values())
|
||||
|
||||
|
||||
def test_agent_accepts_openai_chat_completion_clients() -> None:
|
||||
raw_client = RawOpenAIChatCompletionClient(api_key="test-api-key", model="test-model")
|
||||
raw_agent = Agent(client=raw_client, instructions="test agent")
|
||||
assert raw_agent.client is raw_client
|
||||
|
||||
client = OpenAIChatCompletionClient(api_key="test-api-key", model="test-model")
|
||||
agent = Agent(client=client, instructions="test agent")
|
||||
assert agent.client is client
|
||||
|
||||
|
||||
def test_supports_web_search_only() -> None:
|
||||
assert not isinstance(OpenAIChatCompletionClient, SupportsCodeInterpreterTool)
|
||||
assert isinstance(OpenAIChatCompletionClient, SupportsWebSearchTool) # pyrefly: ignore[unsafe-overlap]
|
||||
|
||||
Reference in New Issue
Block a user