# Copyright 2026 Google LLC
#
# 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.

import base64
import json
import os
import re
import sys
from unittest import mock
from unittest.mock import AsyncMock
from unittest.mock import MagicMock

from anthropic import NOT_GIVEN
from anthropic import types as anthropic_types
from google.adk import version as adk_version
from google.adk.models import anthropic_llm
from google.adk.models import AnthropicGenerateContentConfig
from google.adk.models.anthropic_llm import AnthropicLlm
from google.adk.models.anthropic_llm import Claude
from google.adk.models.anthropic_llm import content_to_message_param
from google.adk.models.anthropic_llm import function_declaration_to_tool_param
from google.adk.models.anthropic_llm import part_to_message_block
from google.adk.models.llm_request import LlmRequest
from google.adk.models.llm_response import LlmResponse
from google.genai import types
from google.genai import version as genai_version
from google.genai.types import Content
from google.genai.types import Part
import pytest


@pytest.fixture
def generate_content_response():
  return anthropic_types.Message(
      id="msg_vrtx_testid",
      content=[
          anthropic_types.TextBlock(
              citations=None, text="Hi! How can I help you today?", type="text"
          )
      ],
      model="claude-3-5-sonnet-v2-20241022",
      role="assistant",
      stop_reason="end_turn",
      stop_sequence=None,
      type="message",
      usage=anthropic_types.Usage(
          cache_creation_input_tokens=0,
          cache_read_input_tokens=0,
          input_tokens=13,
          output_tokens=12,
          server_tool_use=None,
          service_tier=None,
      ),
  )


@pytest.fixture
def generate_llm_response():
  return LlmResponse.create(
      types.GenerateContentResponse(
          candidates=[
              types.Candidate(
                  content=Content(
                      role="model",
                      parts=[Part.from_text(text="Hello, how can I help you?")],
                  ),
                  finish_reason=types.FinishReason.STOP,
              )
          ]
      )
  )


@pytest.fixture
def claude_llm():
  return Claude(model="claude-3-5-sonnet-v2@20241022")


@pytest.fixture
def llm_request():
  return LlmRequest(
      model="claude-3-5-sonnet-v2@20241022",
      contents=[Content(role="user", parts=[Part.from_text(text="Hello")])],
      config=types.GenerateContentConfig(
          temperature=0.1,
          response_modalities=[types.Modality.TEXT],
          system_instruction="You are a helpful assistant",
      ),
  )


def test_claude_anthropic_client_creation():
  # Test with environment variables
  with mock.patch.dict(
      os.environ,
      {
          "GOOGLE_CLOUD_PROJECT": "env-project",
          "GOOGLE_CLOUD_LOCATION": "env-location",
      },
  ):
    model = Claude(model="claude-3-5-sonnet-v2@20241022")
    with mock.patch(
        "google.adk.models.anthropic_llm.AsyncAnthropicVertex", autospec=True
    ) as mock_client_class:
      _ = model._anthropic_client
      mock_client_class.assert_called_once()
      _, kwargs = mock_client_class.call_args
      assert kwargs["project_id"] == "env-project"
      assert kwargs["region"] == "env-location"


def test_claude_anthropic_client_creation_with_full_resource_name():
  # Test with full resource name in model string
  model = Claude(
      model="projects/test-project/locations/test-location/publishers/anthropic/models/claude-3-5-sonnet-v2@20241022"
  )
  with mock.patch(
      "google.adk.models.anthropic_llm.AsyncAnthropicVertex", autospec=True
  ) as mock_client_class:
    _ = model._anthropic_client
    mock_client_class.assert_called_once()
    _, kwargs = mock_client_class.call_args
    assert kwargs["project_id"] == "test-project"
    assert kwargs["region"] == "test-location"


def test_supported_models():
  models = Claude.supported_models()
  assert len(models) == 2
  assert models[0] == r"claude-3-.*"
  assert models[1] == r"claude-.*-4.*"


function_declaration_test_cases = [
    (
        "function_with_no_parameters",
        types.FunctionDeclaration(
            name="get_current_time",
            description="Gets the current time.",
        ),
        anthropic_types.ToolParam(
            name="get_current_time",
            description="Gets the current time.",
            input_schema={"type": "object", "properties": {}},
        ),
    ),
    (
        "function_with_one_optional_parameter",
        types.FunctionDeclaration(
            name="get_weather",
            description="Gets weather information for a given location.",
            parameters=types.Schema(
                type=types.Type.OBJECT,
                properties={
                    "location": types.Schema(
                        type=types.Type.STRING,
                        description="City and state, e.g., San Francisco, CA",
                    )
                },
            ),
        ),
        anthropic_types.ToolParam(
            name="get_weather",
            description="Gets weather information for a given location.",
            input_schema={
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": (
                            "City and state, e.g., San Francisco, CA"
                        ),
                    }
                },
            },
        ),
    ),
    (
        "function_with_one_required_parameter",
        types.FunctionDeclaration(
            name="get_stock_price",
            description="Gets the current price for a stock ticker.",
            parameters=types.Schema(
                type=types.Type.OBJECT,
                properties={
                    "ticker": types.Schema(
                        type=types.Type.STRING,
                        description="The stock ticker, e.g., AAPL",
                    )
                },
                required=["ticker"],
            ),
        ),
        anthropic_types.ToolParam(
            name="get_stock_price",
            description="Gets the current price for a stock ticker.",
            input_schema={
                "type": "object",
                "properties": {
                    "ticker": {
                        "type": "string",
                        "description": "The stock ticker, e.g., AAPL",
                    }
                },
                "required": ["ticker"],
            },
        ),
    ),
    (
        "function_with_multiple_mixed_parameters",
        types.FunctionDeclaration(
            name="submit_order",
            description="Submits a product order.",
            parameters=types.Schema(
                type=types.Type.OBJECT,
                properties={
                    "product_id": types.Schema(
                        type=types.Type.STRING, description="The product ID"
                    ),
                    "quantity": types.Schema(
                        type=types.Type.INTEGER,
                        description="The order quantity",
                    ),
                    "notes": types.Schema(
                        type=types.Type.STRING,
                        description="Optional order notes",
                    ),
                },
                required=["product_id", "quantity"],
            ),
        ),
        anthropic_types.ToolParam(
            name="submit_order",
            description="Submits a product order.",
            input_schema={
                "type": "object",
                "properties": {
                    "product_id": {
                        "type": "string",
                        "description": "The product ID",
                    },
                    "quantity": {
                        "type": "integer",
                        "description": "The order quantity",
                    },
                    "notes": {
                        "type": "string",
                        "description": "Optional order notes",
                    },
                },
                "required": ["product_id", "quantity"],
            },
        ),
    ),
    (
        "function_with_complex_nested_parameter",
        types.FunctionDeclaration(
            name="create_playlist",
            description="Creates a playlist from a list of songs.",
            parameters=types.Schema(
                type=types.Type.OBJECT,
                properties={
                    "playlist_name": types.Schema(
                        type=types.Type.STRING,
                        description="The name for the new playlist",
                    ),
                    "songs": types.Schema(
                        type=types.Type.ARRAY,
                        description="A list of songs to add to the playlist",
                        items=types.Schema(
                            type=types.Type.OBJECT,
                            properties={
                                "title": types.Schema(type=types.Type.STRING),
                                "artist": types.Schema(type=types.Type.STRING),
                            },
                            required=["title", "artist"],
                        ),
                    ),
                },
                required=["playlist_name", "songs"],
            ),
        ),
        anthropic_types.ToolParam(
            name="create_playlist",
            description="Creates a playlist from a list of songs.",
            input_schema={
                "type": "object",
                "properties": {
                    "playlist_name": {
                        "type": "string",
                        "description": "The name for the new playlist",
                    },
                    "songs": {
                        "type": "array",
                        "description": "A list of songs to add to the playlist",
                        "items": {
                            "type": "object",
                            "properties": {
                                "title": {"type": "string"},
                                "artist": {"type": "string"},
                            },
                            "required": ["title", "artist"],
                        },
                    },
                },
                "required": ["playlist_name", "songs"],
            },
        ),
    ),
    (
        "function_with_nested_object_parameter",
        types.FunctionDeclaration(
            name="update_profile",
            description="Updates a user profile.",
            parameters=types.Schema(
                type=types.Type.OBJECT,
                properties={
                    "profile": types.Schema(
                        type=types.Type.OBJECT,
                        description="The profile data",
                        properties={
                            "name": types.Schema(
                                type=types.Type.STRING,
                                description="Full name",
                            ),
                            "address": types.Schema(
                                type=types.Type.OBJECT,
                                description="Mailing address",
                                properties={
                                    "city": types.Schema(
                                        type=types.Type.STRING,
                                    ),
                                    "state": types.Schema(
                                        type=types.Type.STRING,
                                    ),
                                },
                            ),
                        },
                    ),
                },
                required=["profile"],
            ),
        ),
        anthropic_types.ToolParam(
            name="update_profile",
            description="Updates a user profile.",
            input_schema={
                "type": "object",
                "properties": {
                    "profile": {
                        "type": "object",
                        "description": "The profile data",
                        "properties": {
                            "name": {
                                "type": "string",
                                "description": "Full name",
                            },
                            "address": {
                                "type": "object",
                                "description": "Mailing address",
                                "properties": {
                                    "city": {"type": "string"},
                                    "state": {"type": "string"},
                                },
                            },
                        },
                    },
                },
                "required": ["profile"],
            },
        ),
    ),
    (
        "function_with_any_of_parameter",
        types.FunctionDeclaration(
            name="set_value",
            description="Sets a value that can be a string or integer.",
            parameters=types.Schema(
                type=types.Type.OBJECT,
                properties={
                    "value": types.Schema(
                        description="A string or integer value",
                        any_of=[
                            types.Schema(type=types.Type.STRING),
                            types.Schema(type=types.Type.INTEGER),
                        ],
                    ),
                },
                required=["value"],
            ),
        ),
        anthropic_types.ToolParam(
            name="set_value",
            description="Sets a value that can be a string or integer.",
            input_schema={
                "type": "object",
                "properties": {
                    "value": {
                        "description": "A string or integer value",
                        "anyOf": [
                            {"type": "string"},
                            {"type": "integer"},
                        ],
                    },
                },
                "required": ["value"],
            },
        ),
    ),
    (
        "function_with_additional_properties_parameter",
        types.FunctionDeclaration(
            name="store_metadata",
            description="Stores arbitrary key-value metadata.",
            parameters=types.Schema(
                type=types.Type.OBJECT,
                properties={
                    "metadata": types.Schema(
                        type=types.Type.OBJECT,
                        description="Arbitrary metadata",
                        additional_properties=types.Schema(
                            type=types.Type.STRING,
                        ),
                    ),
                },
                required=["metadata"],
            ),
        ),
        anthropic_types.ToolParam(
            name="store_metadata",
            description="Stores arbitrary key-value metadata.",
            input_schema={
                "type": "object",
                "properties": {
                    "metadata": {
                        "type": "object",
                        "description": "Arbitrary metadata",
                        "additionalProperties": {"type": "string"},
                    },
                },
                "required": ["metadata"],
            },
        ),
    ),
    (
        "function_with_parameters_json_schema_combinators",
        types.FunctionDeclaration(
            name="validate_payload",
            description="Validates a payload with schema combinators.",
            parameters_json_schema={
                "type": "OBJECT",
                "properties": {
                    "choice": {
                        "oneOf": [
                            {"type": "STRING"},
                            {"type": "INTEGER"},
                        ],
                    },
                    "config": {
                        "allOf": [
                            {
                                "type": "OBJECT",
                                "properties": {
                                    "enabled": {"type": "BOOLEAN"},
                                },
                            },
                        ],
                    },
                    "blocked": {
                        "not": {
                            "type": "NULL",
                        },
                    },
                    "tuple_value": {
                        "type": "ARRAY",
                        "items": [
                            {"type": "STRING"},
                            {"type": "INTEGER"},
                        ],
                    },
                },
                "required": ["choice"],
            },
        ),
        anthropic_types.ToolParam(
            name="validate_payload",
            description="Validates a payload with schema combinators.",
            input_schema={
                "type": "object",
                "properties": {
                    "choice": {
                        "oneOf": [
                            {"type": "string"},
                            {"type": "integer"},
                        ],
                    },
                    "config": {
                        "allOf": [
                            {
                                "type": "object",
                                "properties": {
                                    "enabled": {"type": "boolean"},
                                },
                            },
                        ],
                    },
                    "blocked": {
                        "not": {
                            "type": "null",
                        },
                    },
                    "tuple_value": {
                        "type": "array",
                        "items": [
                            {"type": "string"},
                            {"type": "integer"},
                        ],
                    },
                },
                "required": ["choice"],
            },
        ),
    ),
    (
        "function_with_parameters_json_schema",
        types.FunctionDeclaration(
            name="search_database",
            description="Searches a database with given criteria.",
            parameters_json_schema={
                "type": "object",
                "properties": {
                    "query": {
                        "type": "string",
                        "description": "The search query",
                    },
                    "limit": {
                        "type": "integer",
                        "description": "Maximum number of results",
                    },
                },
                "required": ["query"],
            },
        ),
        anthropic_types.ToolParam(
            name="search_database",
            description="Searches a database with given criteria.",
            input_schema={
                "type": "object",
                "properties": {
                    "query": {
                        "type": "string",
                        "description": "The search query",
                    },
                    "limit": {
                        "type": "integer",
                        "description": "Maximum number of results",
                    },
                },
                "required": ["query"],
            },
        ),
    ),
]


@pytest.mark.parametrize(
    "_, function_declaration, expected_tool_param",
    function_declaration_test_cases,
    ids=[case[0] for case in function_declaration_test_cases],
)
def test_function_declaration_to_tool_param(
    _, function_declaration, expected_tool_param
):
  """Test function_declaration_to_tool_param."""
  assert (
      function_declaration_to_tool_param(function_declaration)
      == expected_tool_param
  )


@pytest.mark.asyncio
async def test_generate_content_async(
    claude_llm, llm_request, generate_content_response, generate_llm_response
):
  with mock.patch.object(claude_llm, "_anthropic_client") as mock_client:
    with mock.patch.object(
        anthropic_llm,
        "message_to_generate_content_response",
        return_value=generate_llm_response,
    ):
      # Create a mock coroutine that returns the generate_content_response.
      async def mock_coro():
        return generate_content_response

      # Assign the coroutine to the mocked method
      mock_client.messages.create.return_value = mock_coro()

      responses = [
          resp
          async for resp in claude_llm.generate_content_async(
              llm_request, stream=False
          )
      ]
      assert len(responses) == 1
      assert isinstance(responses[0], LlmResponse)
      assert responses[0].content.parts[0].text == "Hello, how can I help you?"


@pytest.mark.asyncio
async def test_anthropic_llm_generate_content_async(
    llm_request, generate_content_response, generate_llm_response
):
  anthropic_llm_instance = AnthropicLlm(model="claude-sonnet-4-20250514")
  with mock.patch.object(
      anthropic_llm_instance, "_anthropic_client"
  ) as mock_client:
    with mock.patch.object(
        anthropic_llm,
        "message_to_generate_content_response",
        return_value=generate_llm_response,
    ):
      # Create a mock coroutine that returns the generate_content_response.
      async def mock_coro():
        return generate_content_response

      # Assign the coroutine to the mocked method
      mock_client.messages.create.return_value = mock_coro()

      responses = [
          resp
          async for resp in anthropic_llm_instance.generate_content_async(
              llm_request, stream=False
          )
      ]
      assert len(responses) == 1
      assert isinstance(responses[0], LlmResponse)
      assert responses[0].content.parts[0].text == "Hello, how can I help you?"


def test_claude_vertex_client_uses_tracking_headers():
  """Tests that Claude vertex client is called with tracking headers."""
  with mock.patch.object(
      anthropic_llm, "AsyncAnthropicVertex", autospec=True
  ) as mock_anthropic_vertex:
    with mock.patch.dict(
        os.environ,
        {
            "GOOGLE_CLOUD_PROJECT": "test-project",
            "GOOGLE_CLOUD_LOCATION": "us-central1",
        },
    ):
      instance = Claude(model="claude-3-5-sonnet-v2@20241022")
      _ = instance._anthropic_client
      mock_anthropic_vertex.assert_called_once()
      _, kwargs = mock_anthropic_vertex.call_args
      assert "default_headers" in kwargs
      assert "x-goog-api-client" in kwargs["default_headers"]
      assert "user-agent" in kwargs["default_headers"]
      assert (
          f"google-adk/{adk_version.__version__}"
          in kwargs["default_headers"]["user-agent"]
      )


@pytest.mark.asyncio
async def test_generate_content_async_with_max_tokens(
    llm_request, generate_content_response, generate_llm_response
):
  claude_llm = Claude(model="claude-3-5-sonnet-v2@20241022", max_tokens=4096)
  with mock.patch.object(claude_llm, "_anthropic_client") as mock_client:
    with mock.patch.object(
        anthropic_llm,
        "message_to_generate_content_response",
        return_value=generate_llm_response,
    ):
      # Create a mock coroutine that returns the generate_content_response.
      async def mock_coro():
        return generate_content_response

      # Assign the coroutine to the mocked method
      mock_client.messages.create.return_value = mock_coro()

      _ = [
          resp
          async for resp in claude_llm.generate_content_async(
              llm_request, stream=False
          )
      ]
      mock_client.messages.create.assert_called_once()
      _, kwargs = mock_client.messages.create.call_args
      assert kwargs["max_tokens"] == 4096


def test_part_to_message_block_with_content():
  """Test that part_to_message_block handles content format."""
  from google.adk.models.anthropic_llm import part_to_message_block

  # Create a function response part with content array.
  mcp_response_part = types.Part.from_function_response(
      name="generate_sample_filesystem",
      response={
          "content": [{
              "type": "text",
              "text": '{"name":"root","node_type":"folder","children":[]}',
          }]
      },
  )
  mcp_response_part.function_response.id = "test_id_123"

  result = part_to_message_block(mcp_response_part)

  # ToolResultBlockParam is a TypedDict.
  assert isinstance(result, dict)
  assert result["tool_use_id"] == "test_id_123"
  assert result["type"] == "tool_result"
  assert not result["is_error"]
  # Verify the content was extracted from the content format.
  assert (
      '{"name":"root","node_type":"folder","children":[]}' in result["content"]
  )


def test_part_to_message_block_with_traditional_result():
  """Test that part_to_message_block handles traditional result format."""
  from google.adk.models.anthropic_llm import part_to_message_block

  # Create a function response part with traditional result format
  traditional_response_part = types.Part.from_function_response(
      name="some_tool",
      response={
          "result": "This is the result from the tool",
      },
  )
  traditional_response_part.function_response.id = "test_id_456"

  result = part_to_message_block(traditional_response_part)

  # ToolResultBlockParam is a TypedDict.
  assert isinstance(result, dict)
  assert result["tool_use_id"] == "test_id_456"
  assert result["type"] == "tool_result"
  assert not result["is_error"]
  # Verify the content was extracted from the traditional format
  assert "This is the result from the tool" in result["content"]


def test_part_to_message_block_with_multiple_content_items():
  """Test content with multiple items."""
  from google.adk.models.anthropic_llm import part_to_message_block

  # Create a function response with multiple content items
  multi_content_part = types.Part.from_function_response(
      name="multi_response_tool",
      response={
          "content": [
              {"type": "text", "text": "First part"},
              {"type": "text", "text": "Second part"},
          ]
      },
  )
  multi_content_part.function_response.id = "test_id_789"

  result = part_to_message_block(multi_content_part)

  # ToolResultBlockParam is a TypedDict.
  assert isinstance(result, dict)
  # Multiple text items should be joined with newlines
  assert result["content"] == "First part\nSecond part"


def test_part_to_message_block_with_pdf_document():
  """Test that part_to_message_block handles PDF document parts."""
  pdf_data = b"%PDF-1.4 fake pdf content"
  part = Part(
      inline_data=types.Blob(mime_type="application/pdf", data=pdf_data)
  )

  result = part_to_message_block(part)

  assert isinstance(result, dict)
  assert result["type"] == "document"
  assert result["source"]["type"] == "base64"
  assert result["source"]["media_type"] == "application/pdf"
  assert result["source"]["data"] == base64.b64encode(pdf_data).decode()


def test_part_to_message_block_with_pdf_mime_type_parameters():
  """Test that PDF parts with MIME type parameters are handled correctly."""
  pdf_data = b"%PDF-1.4 fake pdf content"
  part = Part(
      inline_data=types.Blob(
          mime_type="application/pdf; name=doc.pdf", data=pdf_data
      )
  )

  result = part_to_message_block(part)

  assert isinstance(result, dict)
  assert result["type"] == "document"
  assert result["source"]["type"] == "base64"
  assert result["source"]["media_type"] == "application/pdf; name=doc.pdf"
  assert result["source"]["data"] == base64.b64encode(pdf_data).decode()


content_to_message_param_test_cases = [
    (
        "user_role_with_text_and_image",
        Content(
            role="user",
            parts=[
                Part.from_text(text="What's in this image?"),
                Part(
                    inline_data=types.Blob(
                        mime_type="image/jpeg", data=b"fake_image_data"
                    )
                ),
            ],
        ),
        "user",
        2,  # Expected content length
        None,  # No warning expected
    ),
    (
        "model_role_with_text_and_image",
        Content(
            role="model",
            parts=[
                Part.from_text(text="I see a cat."),
                Part(
                    inline_data=types.Blob(
                        mime_type="image/png", data=b"fake_image_data"
                    )
                ),
            ],
        ),
        "assistant",
        1,  # Image filtered out, only text remains
        "Image data is not supported in Claude for assistant turns.",
    ),
    (
        "assistant_role_with_text_and_image",
        Content(
            role="assistant",
            parts=[
                Part.from_text(text="Here's what I found."),
                Part(
                    inline_data=types.Blob(
                        mime_type="image/webp", data=b"fake_image_data"
                    )
                ),
            ],
        ),
        "assistant",
        1,  # Image filtered out, only text remains
        "Image data is not supported in Claude for assistant turns.",
    ),
    (
        "user_role_with_text_and_document",
        Content(
            role="user",
            parts=[
                Part.from_text(text="Summarize this document."),
                Part(
                    inline_data=types.Blob(
                        mime_type="application/pdf", data=b"fake_pdf_data"
                    )
                ),
            ],
        ),
        "user",
        2,  # Both text and document included
        None,  # No warning expected
    ),
    (
        "model_role_with_text_and_document",
        Content(
            role="model",
            parts=[
                Part.from_text(text="Here is the summary."),
                Part(
                    inline_data=types.Blob(
                        mime_type="application/pdf", data=b"fake_pdf_data"
                    )
                ),
            ],
        ),
        "assistant",
        1,  # Document filtered out, only text remains
        "PDF data is not supported in Claude for assistant turns.",
    ),
]


@pytest.mark.parametrize(
    "_, content, expected_role, expected_content_length, expected_warning",
    content_to_message_param_test_cases,
    ids=[case[0] for case in content_to_message_param_test_cases],
)
def test_content_to_message_param(
    _, content, expected_role, expected_content_length, expected_warning
):
  """Test content_to_message_param handles images and documents based on role."""
  with mock.patch("google.adk.models.anthropic_llm.logger") as mock_logger:
    result = content_to_message_param(content)

    assert result["role"] == expected_role
    assert len(result["content"]) == expected_content_length

    if expected_warning:
      mock_logger.warning.assert_called_once_with(expected_warning)
    else:
      mock_logger.warning.assert_not_called()


# --- Tests for Bug #2: json.dumps for dict/list function results ---


def test_part_to_message_block_dict_result_serialized_as_json():
  """Dict results should be serialized with json.dumps, not str()."""
  response_part = types.Part.from_function_response(
      name="get_topic",
      response={"result": {"topic": "travel", "active": True, "count": None}},
  )
  response_part.function_response.id = "test_id"

  result = part_to_message_block(response_part)
  content = result["content"]

  # Must be valid JSON (json.dumps produces "true"/"null", not "True"/"None")
  parsed = json.loads(content)
  assert parsed["topic"] == "travel"
  assert parsed["active"] is True
  assert parsed["count"] is None


def test_part_to_message_block_list_result_serialized_as_json():
  """List results should be serialized with json.dumps."""
  response_part = types.Part.from_function_response(
      name="get_items",
      response={"result": ["item1", "item2", "item3"]},
  )
  response_part.function_response.id = "test_id"

  result = part_to_message_block(response_part)
  content = result["content"]

  parsed = json.loads(content)
  assert parsed == ["item1", "item2", "item3"]


def test_part_to_message_block_empty_dict_result_not_dropped():
  """Empty dict results should produce '{}', not empty string."""
  response_part = types.Part.from_function_response(
      name="some_tool",
      response={"result": {}},
  )
  response_part.function_response.id = "test_id"

  result = part_to_message_block(response_part)
  assert result["content"] == "{}"


def test_part_to_message_block_empty_list_result_not_dropped():
  """Empty list results should produce '[]', not empty string."""
  response_part = types.Part.from_function_response(
      name="some_tool",
      response={"result": []},
  )
  response_part.function_response.id = "test_id"

  result = part_to_message_block(response_part)
  assert result["content"] == "[]"


def test_part_to_message_block_string_result_unchanged():
  """String results should still work as before (backward compat)."""
  response_part = types.Part.from_function_response(
      name="simple_tool",
      response={"result": "plain text result"},
  )
  response_part.function_response.id = "test_id"

  result = part_to_message_block(response_part)
  assert result["content"] == "plain text result"


def test_part_to_message_block_nested_dict_result():
  """Nested dict with arrays should produce valid JSON."""
  response_part = types.Part.from_function_response(
      name="search",
      response={
          "result": {
              "results": [
                  {"id": 1, "tags": ["a", "b"]},
                  {"id": 2, "meta": {"key": "val"}},
              ],
              "has_more": False,
          }
      },
  )
  response_part.function_response.id = "test_id"

  result = part_to_message_block(response_part)
  parsed = json.loads(result["content"])
  assert parsed["has_more"] is False
  assert parsed["results"][0]["tags"] == ["a", "b"]


# --- Tests for arbitrary dict fallback (e.g. SkillToolset load_skill) ---


def test_part_to_message_block_arbitrary_dict_serialized_as_json():
  """Dicts with keys other than 'content'/'result' should be JSON-serialized.

  This covers tools like load_skill that return arbitrary key structures
  such as {"skill_name": ..., "instructions": ..., "frontmatter": ...}.
  """
  response_part = types.Part.from_function_response(
      name="load_skill",
      response={
          "skill_name": "my_skill",
          "instructions": "Step 1: do this. Step 2: do that.",
          "frontmatter": {"version": "1.0", "tags": ["a", "b"]},
      },
  )
  response_part.function_response.id = "test_id"

  result = part_to_message_block(response_part)

  assert result["type"] == "tool_result"
  assert result["tool_use_id"] == "test_id"
  assert not result["is_error"]
  parsed = json.loads(result["content"])
  assert parsed["skill_name"] == "my_skill"
  assert parsed["instructions"] == "Step 1: do this. Step 2: do that."
  assert parsed["frontmatter"]["version"] == "1.0"


def test_part_to_message_block_run_skill_script_response():
  """run_skill_script response keys (stdout/stderr/status) should not be dropped."""
  response_part = types.Part.from_function_response(
      name="run_skill_script",
      response={
          "skill_name": "my_skill",
          "file_path": "scripts/setup.py",
          "stdout": "Done.",
          "stderr": "",
          "status": "success",
      },
  )
  response_part.function_response.id = "test_id_2"

  result = part_to_message_block(response_part)

  parsed = json.loads(result["content"])
  assert parsed["status"] == "success"
  assert parsed["stdout"] == "Done."


def test_part_to_message_block_error_response_not_dropped():
  """Error dicts like {"error": ..., "error_code": ...} should be serialized."""
  response_part = types.Part.from_function_response(
      name="load_skill",
      response={
          "error": "Skill 'missing' not found.",
          "error_code": "SKILL_NOT_FOUND",
      },
  )
  response_part.function_response.id = "test_id_3"

  result = part_to_message_block(response_part)

  parsed = json.loads(result["content"])
  assert parsed["error_code"] == "SKILL_NOT_FOUND"


def test_part_to_message_block_empty_response_stays_empty():
  """An empty response dict should still produce an empty content string."""
  response_part = types.Part.from_function_response(
      name="some_tool",
      response={},
  )
  response_part.function_response.id = "test_id_4"

  result = part_to_message_block(response_part)

  assert result["content"] == ""


def test_part_to_message_block_string_content_passes_through():
  """A scalar string `content` value must not be iterated char-by-char."""
  response_part = types.Part.from_function_response(
      name="some_tool",
      response={"content": "Hello"},
  )
  response_part.function_response.id = "test_id_str_content"

  result = part_to_message_block(response_part)

  assert result["content"] == "Hello"


def test_part_to_message_block_load_skill_resource_response():
  """LoadSkillResourceTool returns {content: <file text>} as a string."""
  file_text = "Line one\nLine two\nLine three"
  response_part = types.Part.from_function_response(
      name="load_skill_resource",
      response={
          "skill_name": "my-skill",
          "file_path": "references/doc.md",
          "content": file_text,
      },
  )
  response_part.function_response.id = "test_id_load_skill"

  result = part_to_message_block(response_part)

  assert result["content"] == file_text


def test_part_to_message_block_empty_string_content_falls_through():
  """`{"content": ""}` falls through to the JSON-dump fallback, not a crash."""
  response_part = types.Part.from_function_response(
      name="some_tool",
      response={"content": ""},
  )
  response_part.function_response.id = "test_id_empty_content_only"

  result = part_to_message_block(response_part)

  assert json.loads(result["content"]) == {"content": ""}


def test_part_to_message_block_empty_content_with_metadata_keeps_metadata():
  """`content: ""` is falsy; sibling keys still reach the model via JSON dump."""
  response_part = types.Part.from_function_response(
      name="some_tool",
      response={"content": "", "extra": "keep me"},
  )
  response_part.function_response.id = "test_id_empty_content_with_meta"

  result = part_to_message_block(response_part)

  parsed = json.loads(result["content"])
  assert parsed["content"] == ""
  assert parsed["extra"] == "keep me"


# --- Tests for Bug #1: Streaming support ---


def _make_mock_stream_events(events):
  """Helper to create an async iterable from a list of events."""

  async def _stream():
    for event in events:
      yield event

  return _stream()


@pytest.mark.asyncio
async def test_streaming_text_yields_partial_and_final():
  """Streaming text should yield partial chunks then a final response."""
  llm = AnthropicLlm(model="claude-sonnet-4-20250514")

  events = [
      MagicMock(
          type="message_start",
          message=MagicMock(usage=MagicMock(input_tokens=10, output_tokens=0)),
      ),
      MagicMock(
          type="content_block_start",
          index=0,
          content_block=anthropic_types.TextBlock(text="", type="text"),
      ),
      MagicMock(
          type="content_block_delta",
          index=0,
          delta=anthropic_types.TextDelta(text="Hello ", type="text_delta"),
      ),
      MagicMock(
          type="content_block_delta",
          index=0,
          delta=anthropic_types.TextDelta(text="world!", type="text_delta"),
      ),
      MagicMock(type="content_block_stop", index=0),
      MagicMock(
          type="message_delta",
          delta=MagicMock(stop_reason="end_turn"),
          usage=MagicMock(output_tokens=5),
      ),
      MagicMock(type="message_stop"),
  ]

  mock_client = MagicMock()
  mock_client.messages.create = AsyncMock(
      return_value=_make_mock_stream_events(events)
  )

  llm_request = LlmRequest(
      model="claude-sonnet-4-20250514",
      contents=[Content(role="user", parts=[Part.from_text(text="Hi")])],
      config=types.GenerateContentConfig(
          system_instruction="You are helpful",
      ),
  )

  with mock.patch.object(llm, "_anthropic_client", mock_client):
    responses = [
        r async for r in llm.generate_content_async(llm_request, stream=True)
    ]

  # 2 partial text chunks + 1 final aggregated
  assert len(responses) == 3
  assert responses[0].partial is True
  assert responses[0].content.parts[0].text == "Hello "
  assert responses[1].partial is True
  assert responses[1].content.parts[0].text == "world!"
  assert responses[2].partial is False
  assert responses[2].content.parts[0].text == "Hello world!"
  assert responses[2].usage_metadata.prompt_token_count == 10
  assert responses[2].usage_metadata.candidates_token_count == 5


@pytest.mark.asyncio
async def test_streaming_tool_use_yields_function_call():
  """Streaming tool_use should accumulate args and yield in final."""
  llm = AnthropicLlm(model="claude-sonnet-4-20250514")

  events = [
      MagicMock(
          type="message_start",
          message=MagicMock(usage=MagicMock(input_tokens=20, output_tokens=0)),
      ),
      MagicMock(
          type="content_block_start",
          index=0,
          content_block=anthropic_types.TextBlock(text="", type="text"),
      ),
      MagicMock(
          type="content_block_delta",
          index=0,
          delta=anthropic_types.TextDelta(text="Checking.", type="text_delta"),
      ),
      MagicMock(type="content_block_stop", index=0),
      MagicMock(
          type="content_block_start",
          index=1,
          content_block=anthropic_types.ToolUseBlock(
              id="toolu_abc",
              name="get_weather",
              input={},
              type="tool_use",
          ),
      ),
      MagicMock(
          type="content_block_delta",
          index=1,
          delta=anthropic_types.InputJSONDelta(
              partial_json='{"city": "Paris"}',
              type="input_json_delta",
          ),
      ),
      MagicMock(type="content_block_stop", index=1),
      MagicMock(
          type="message_delta",
          delta=MagicMock(stop_reason="tool_use"),
          usage=MagicMock(output_tokens=12),
      ),
      MagicMock(type="message_stop"),
  ]

  mock_client = MagicMock()
  mock_client.messages.create = AsyncMock(
      return_value=_make_mock_stream_events(events)
  )

  llm_request = LlmRequest(
      model="claude-sonnet-4-20250514",
      contents=[
          Content(
              role="user",
              parts=[Part.from_text(text="Weather?")],
          )
      ],
      config=types.GenerateContentConfig(
          system_instruction="You are helpful",
      ),
  )

  with mock.patch.object(llm, "_anthropic_client", mock_client):
    responses = [
        r async for r in llm.generate_content_async(llm_request, stream=True)
    ]

  # 1 text partial + 1 final
  assert len(responses) == 2

  final = responses[-1]
  assert final.partial is False
  assert len(final.content.parts) == 2
  assert final.content.parts[0].text == "Checking."
  assert final.content.parts[1].function_call.name == "get_weather"
  assert final.content.parts[1].function_call.args == {"city": "Paris"}
  assert final.content.parts[1].function_call.id == "toolu_abc"


@pytest.mark.asyncio
async def test_streaming_passes_stream_true_to_create():
  """When stream=True, messages.create should be called with stream=True."""
  llm = AnthropicLlm(model="claude-sonnet-4-20250514")

  events = [
      MagicMock(
          type="message_start",
          message=MagicMock(usage=MagicMock(input_tokens=5, output_tokens=0)),
      ),
      MagicMock(
          type="content_block_start",
          index=0,
          content_block=anthropic_types.TextBlock(text="", type="text"),
      ),
      MagicMock(
          type="content_block_delta",
          index=0,
          delta=anthropic_types.TextDelta(text="Hi", type="text_delta"),
      ),
      MagicMock(type="content_block_stop", index=0),
      MagicMock(
          type="message_delta",
          delta=MagicMock(stop_reason="end_turn"),
          usage=MagicMock(output_tokens=1),
      ),
      MagicMock(type="message_stop"),
  ]

  mock_client = MagicMock()
  mock_client.messages.create = AsyncMock(
      return_value=_make_mock_stream_events(events)
  )

  llm_request = LlmRequest(
      model="claude-sonnet-4-20250514",
      contents=[Content(role="user", parts=[Part.from_text(text="Hi")])],
      config=types.GenerateContentConfig(
          system_instruction="Test",
      ),
  )

  with mock.patch.object(llm, "_anthropic_client", mock_client):
    _ = [r async for r in llm.generate_content_async(llm_request, stream=True)]

  mock_client.messages.create.assert_called_once()
  _, kwargs = mock_client.messages.create.call_args
  assert kwargs["stream"] is True


@pytest.mark.asyncio
async def test_non_streaming_does_not_pass_stream_param():
  """When stream=False, messages.create should NOT get stream param."""
  llm = AnthropicLlm(model="claude-sonnet-4-20250514")

  mock_message = anthropic_types.Message(
      id="msg_test",
      content=[
          anthropic_types.TextBlock(text="Hello!", type="text", citations=None)
      ],
      model="claude-sonnet-4-20250514",
      role="assistant",
      stop_reason="end_turn",
      stop_sequence=None,
      type="message",
      usage=anthropic_types.Usage(
          input_tokens=5,
          output_tokens=2,
          cache_creation_input_tokens=0,
          cache_read_input_tokens=0,
          server_tool_use=None,
          service_tier=None,
      ),
  )

  mock_client = MagicMock()
  mock_client.messages.create = AsyncMock(return_value=mock_message)

  llm_request = LlmRequest(
      model="claude-sonnet-4-20250514",
      contents=[Content(role="user", parts=[Part.from_text(text="Hi")])],
      config=types.GenerateContentConfig(
          system_instruction="Test",
      ),
  )

  with mock.patch.object(llm, "_anthropic_client", mock_client):
    responses = [
        r async for r in llm.generate_content_async(llm_request, stream=False)
    ]

  assert len(responses) == 1
  mock_client.messages.create.assert_called_once()
  _, kwargs = mock_client.messages.create.call_args
  assert "stream" not in kwargs


def test_part_to_message_block_function_call_preserves_valid_id():
  """Valid Anthropic ids must round-trip byte-for-byte."""
  part = types.Part.from_function_call(name="test_tool", args={"k": "v"})
  part.function_call.id = "toolu_01abc"

  result = part_to_message_block(part)

  assert result["id"] == "toolu_01abc"


def test_part_to_message_block_function_response_preserves_valid_id():
  """function_response ids must round-trip byte-for-byte to tool_use_id."""
  part = types.Part.from_function_response(
      name="test_tool", response={"result": "ok"}
  )
  part.function_response.id = "toolu_01abc"

  result = part_to_message_block(part)

  assert result["tool_use_id"] == "toolu_01abc"


def test_part_to_message_block_preserves_adk_fallback_id():
  """ADK-generated ``adk-<uuid>`` ids match Anthropic's regex and round-trip.

  This is the path exercised by the contents.py fix: when Vertex Claude
  returns id=None, ``populate_client_function_call_id`` writes ``adk-<uuid>``,
  and contents.py preserves it through replay. ``part_to_message_block`` must
  pass it through to Anthropic unchanged so call/response stay paired.
  """
  call_part = types.Part.from_function_call(name="t", args={"a": 1})
  call_part.function_call.id = "adk-12345678-1234-1234-1234-123456789012"
  response_part = types.Part.from_function_response(
      name="t", response={"result": "ok"}
  )
  response_part.function_response.id = (
      "adk-12345678-1234-1234-1234-123456789012"
  )

  call_result = part_to_message_block(call_part)
  response_result = part_to_message_block(response_part)

  assert call_result["id"] == "adk-12345678-1234-1234-1234-123456789012"
  assert (
      response_result["tool_use_id"]
      == "adk-12345678-1234-1234-1234-123456789012"
  )
  # The pair must remain matched after conversion.
  assert call_result["id"] == response_result["tool_use_id"]


# --- Tests for extended thinking support ---


def test_build_anthropic_thinking_param_with_config():
  """When thinking_config has a positive budget, return ThinkingConfigEnabledParam."""
  from google.adk.models.anthropic_llm import _build_anthropic_thinking_param

  config = types.GenerateContentConfig(
      thinking_config=types.ThinkingConfig(thinking_budget=5000),
  )
  result = _build_anthropic_thinking_param(config)
  assert result == anthropic_types.ThinkingConfigEnabledParam(
      type="enabled", budget_tokens=5000
  )


def test_build_anthropic_thinking_param_zero_budget_disabled():
  """thinking_budget=0 maps to ThinkingConfigDisabledParam (genai DISABLED)."""
  from google.adk.models.anthropic_llm import _build_anthropic_thinking_param

  config = types.GenerateContentConfig(
      thinking_config=types.ThinkingConfig(thinking_budget=0),
  )
  result = _build_anthropic_thinking_param(config)
  assert result == anthropic_types.ThinkingConfigDisabledParam(type="disabled")


def test_build_anthropic_thinking_param_none_budget_raises():
  """thinking_budget=None must be set explicitly; raises ValueError."""
  from google.adk.models.anthropic_llm import _build_anthropic_thinking_param

  config = types.GenerateContentConfig(
      thinking_config=types.ThinkingConfig(),
  )
  with pytest.raises(
      ValueError, match="thinking_budget must be set explicitly"
  ):
    _build_anthropic_thinking_param(config)


def test_build_anthropic_thinking_param_automatic_budget_uses_adaptive():
  """thinking_budget=-1 (genai AUTOMATIC) maps to Anthropic adaptive thinking.

  Required for Claude Opus 4.7 (which rejects ``"enabled"`` with a 400 error)
  and recommended for Opus 4.6 / Sonnet 4.6 where ``"enabled"`` is deprecated.
  """
  from google.adk.models.anthropic_llm import _build_anthropic_thinking_param

  config = types.GenerateContentConfig(
      thinking_config=types.ThinkingConfig(thinking_budget=-1),
  )
  result = _build_anthropic_thinking_param(config)
  assert result == anthropic_types.ThinkingConfigAdaptiveParam(type="adaptive")


def test_build_anthropic_thinking_param_other_negative_uses_adaptive():
  """Any negative thinking_budget (not just -1) maps to adaptive thinking."""
  from google.adk.models.anthropic_llm import _build_anthropic_thinking_param

  config = types.GenerateContentConfig(
      thinking_config=types.ThinkingConfig(thinking_budget=-5),
  )
  result = _build_anthropic_thinking_param(config)
  assert result == anthropic_types.ThinkingConfigAdaptiveParam(type="adaptive")


def test_build_anthropic_thinking_param_no_config():
  """Returns NOT_GIVEN when no thinking config is set."""
  from anthropic import NOT_GIVEN
  from google.adk.models.anthropic_llm import _build_anthropic_thinking_param

  result_none = _build_anthropic_thinking_param(None)
  assert result_none is NOT_GIVEN

  config_no_thinking = types.GenerateContentConfig(
      system_instruction="test",
  )
  result_no_thinking = _build_anthropic_thinking_param(config_no_thinking)
  assert result_no_thinking is NOT_GIVEN


def test_content_block_to_part_thinking_block():
  """ThinkingBlock should produce Part with thought=True and signature."""
  from google.adk.models.anthropic_llm import content_block_to_part

  block = anthropic_types.ThinkingBlock(
      thinking="Let me reason about this.",
      signature="sig_abc123",
      type="thinking",
  )
  part = content_block_to_part(block)

  assert part is not None
  assert part.text == "Let me reason about this."
  assert part.thought is True
  assert part.thought_signature == b"sig_abc123"


def test_content_block_to_part_redacted_thinking():
  """RedactedThinkingBlock should preserve the encrypted blob for round-trip."""
  from google.adk.models.anthropic_llm import content_block_to_part

  block = anthropic_types.RedactedThinkingBlock(
      data="redacted_data",
      type="redacted_thinking",
  )
  part = content_block_to_part(block)

  assert part.thought is True
  assert part.text is None
  assert part.thought_signature == b"redacted_data"


def test_message_to_generate_content_response_with_thinking():
  """Message with ThinkingBlock + TextBlock yields both parts."""
  from google.adk.models.anthropic_llm import message_to_generate_content_response

  message = anthropic_types.Message(
      id="msg_test_thinking",
      content=[
          anthropic_types.ThinkingBlock(
              thinking="I need to think about this.",
              signature="sig_xyz",
              type="thinking",
          ),
          anthropic_types.RedactedThinkingBlock(
              data="hidden",
              type="redacted_thinking",
          ),
          anthropic_types.TextBlock(
              text="Here is my answer.",
              type="text",
              citations=None,
          ),
      ],
      model="claude-sonnet-4-20250514",
      role="assistant",
      stop_reason="end_turn",
      stop_sequence=None,
      type="message",
      usage=anthropic_types.Usage(
          input_tokens=10,
          output_tokens=20,
          cache_creation_input_tokens=0,
          cache_read_input_tokens=0,
          server_tool_use=None,
          service_tier=None,
      ),
  )

  response = message_to_generate_content_response(message)

  assert len(response.content.parts) == 3

  thinking_part = response.content.parts[0]
  assert thinking_part.text == "I need to think about this."
  assert thinking_part.thought is True
  assert thinking_part.thought_signature == b"sig_xyz"

  redacted_part = response.content.parts[1]
  assert redacted_part.thought is True
  assert redacted_part.text is None
  assert redacted_part.thought_signature == b"hidden"

  text_part = response.content.parts[2]
  assert text_part.text == "Here is my answer."
  assert text_part.thought is not True


def test_message_to_generate_content_response_reports_cache_read_tokens():
  """cache_read_input_tokens maps to usage_metadata.cached_content_token_count."""
  from google.adk.models.anthropic_llm import message_to_generate_content_response

  message = anthropic_types.Message(
      id="msg_cache_read",
      content=[
          anthropic_types.TextBlock(text="hi", type="text", citations=None)
      ],
      model="claude-sonnet-4-20250514",
      role="assistant",
      stop_reason="end_turn",
      stop_sequence=None,
      type="message",
      usage=anthropic_types.Usage(
          input_tokens=100,
          output_tokens=20,
          cache_creation_input_tokens=0,
          cache_read_input_tokens=75,
          server_tool_use=None,
          service_tier=None,
      ),
  )

  response = message_to_generate_content_response(message)

  assert response.usage_metadata.cached_content_token_count == 75


def test_message_to_generate_content_response_no_cache_read_tokens():
  """Absent cache_read_input_tokens yields cached_content_token_count=None."""
  from google.adk.models.anthropic_llm import message_to_generate_content_response

  message = anthropic_types.Message(
      id="msg_no_cache",
      content=[
          anthropic_types.TextBlock(text="hi", type="text", citations=None)
      ],
      model="claude-sonnet-4-20250514",
      role="assistant",
      stop_reason="end_turn",
      stop_sequence=None,
      type="message",
      usage=anthropic_types.Usage(
          input_tokens=100,
          output_tokens=20,
          cache_creation_input_tokens=0,
          cache_read_input_tokens=None,
          server_tool_use=None,
          service_tier=None,
      ),
  )

  response = message_to_generate_content_response(message)

  assert response.usage_metadata.cached_content_token_count is None


def test_part_to_message_block_thinking_roundtrip():
  """Part with thought=True and signature creates ThinkingBlockParam."""
  part = Part(
      text="My reasoning steps.",
      thought=True,
      thought_signature=b"roundtrip_sig",
  )

  result = part_to_message_block(part)

  assert isinstance(result, dict)
  assert result["type"] == "thinking"
  assert result["thinking"] == "My reasoning steps."
  assert result["signature"] == "roundtrip_sig"


def test_part_to_message_block_redacted_thinking_roundtrip():
  """Part with thought=True, no text, signature -> RedactedThinkingBlockParam."""
  part = Part(thought=True, thought_signature=b"encrypted_blob")

  result = part_to_message_block(part)

  assert isinstance(result, dict)
  assert result["type"] == "redacted_thinking"
  assert result["data"] == "encrypted_blob"


@pytest.mark.asyncio
async def test_non_streaming_passes_thinking_param():
  """When thinking_config is set, messages.create gets thinking kwarg."""
  llm = AnthropicLlm(model="claude-sonnet-4-20250514")

  mock_message = anthropic_types.Message(
      id="msg_think",
      content=[
          anthropic_types.TextBlock(text="Answer.", type="text", citations=None)
      ],
      model="claude-sonnet-4-20250514",
      role="assistant",
      stop_reason="end_turn",
      stop_sequence=None,
      type="message",
      usage=anthropic_types.Usage(
          input_tokens=5,
          output_tokens=2,
          cache_creation_input_tokens=0,
          cache_read_input_tokens=0,
          server_tool_use=None,
          service_tier=None,
      ),
  )

  mock_client = MagicMock()
  mock_client.messages.create = AsyncMock(return_value=mock_message)

  request = LlmRequest(
      model="claude-sonnet-4-20250514",
      contents=[Content(role="user", parts=[Part.from_text(text="Think")])],
      config=types.GenerateContentConfig(
          system_instruction="Test",
          thinking_config=types.ThinkingConfig(thinking_budget=8000),
      ),
  )

  with mock.patch.object(llm, "_anthropic_client", mock_client):
    _ = [r async for r in llm.generate_content_async(request, stream=False)]

  mock_client.messages.create.assert_called_once()
  _, kwargs = mock_client.messages.create.call_args
  assert kwargs["thinking"] == anthropic_types.ThinkingConfigEnabledParam(
      type="enabled", budget_tokens=8000
  )


@pytest.mark.asyncio
async def test_non_streaming_no_thinking_param_without_config():
  """Without thinking_config, thinking kwarg should be NOT_GIVEN."""
  from anthropic import NOT_GIVEN

  llm = AnthropicLlm(model="claude-sonnet-4-20250514")

  mock_message = anthropic_types.Message(
      id="msg_no_think",
      content=[
          anthropic_types.TextBlock(text="Hello!", type="text", citations=None)
      ],
      model="claude-sonnet-4-20250514",
      role="assistant",
      stop_reason="end_turn",
      stop_sequence=None,
      type="message",
      usage=anthropic_types.Usage(
          input_tokens=5,
          output_tokens=2,
          cache_creation_input_tokens=0,
          cache_read_input_tokens=0,
          server_tool_use=None,
          service_tier=None,
      ),
  )

  mock_client = MagicMock()
  mock_client.messages.create = AsyncMock(return_value=mock_message)

  request = LlmRequest(
      model="claude-sonnet-4-20250514",
      contents=[Content(role="user", parts=[Part.from_text(text="Hi")])],
      config=types.GenerateContentConfig(
          system_instruction="Test",
      ),
  )

  with mock.patch.object(llm, "_anthropic_client", mock_client):
    _ = [r async for r in llm.generate_content_async(request, stream=False)]

  mock_client.messages.create.assert_called_once()
  _, kwargs = mock_client.messages.create.call_args
  assert kwargs["thinking"] is NOT_GIVEN


@pytest.mark.asyncio
async def test_streaming_thinking_yields_partial_and_final():
  """Streaming with thinking blocks yields partial thought then final."""
  llm = AnthropicLlm(model="claude-sonnet-4-20250514")

  events = [
      MagicMock(
          type="message_start",
          message=MagicMock(usage=MagicMock(input_tokens=15, output_tokens=0)),
      ),
      # Thinking block start
      MagicMock(
          type="content_block_start",
          index=0,
          content_block=anthropic_types.ThinkingBlock(
              thinking="", signature="", type="thinking"
          ),
      ),
      # Thinking deltas
      MagicMock(
          type="content_block_delta",
          index=0,
          delta=anthropic_types.ThinkingDelta(
              thinking="Step 1: ", type="thinking_delta"
          ),
      ),
      MagicMock(
          type="content_block_delta",
          index=0,
          delta=anthropic_types.ThinkingDelta(
              thinking="analyze.", type="thinking_delta"
          ),
      ),
      MagicMock(type="content_block_stop", index=0),
      # Text block start
      MagicMock(
          type="content_block_start",
          index=1,
          content_block=anthropic_types.TextBlock(text="", type="text"),
      ),
      MagicMock(
          type="content_block_delta",
          index=1,
          delta=anthropic_types.TextDelta(
              text="The answer is 42.", type="text_delta"
          ),
      ),
      MagicMock(type="content_block_stop", index=1),
      MagicMock(
          type="message_delta",
          delta=MagicMock(stop_reason="end_turn"),
          usage=MagicMock(output_tokens=10),
      ),
      MagicMock(type="message_stop"),
  ]

  mock_client = MagicMock()
  mock_client.messages.create = AsyncMock(
      return_value=_make_mock_stream_events(events)
  )

  request = LlmRequest(
      model="claude-sonnet-4-20250514",
      contents=[Content(role="user", parts=[Part.from_text(text="What?")])],
      config=types.GenerateContentConfig(
          system_instruction="Think carefully",
          thinking_config=types.ThinkingConfig(thinking_budget=5000),
      ),
  )

  with mock.patch.object(llm, "_anthropic_client", mock_client):
    responses = [
        r async for r in llm.generate_content_async(request, stream=True)
    ]

  # 2 thinking partials + 1 text partial + 1 final = 4 responses
  assert len(responses) == 4

  # First two partials are thinking chunks.
  assert responses[0].partial is True
  assert responses[0].content.parts[0].thought is True
  assert responses[0].content.parts[0].text == "Step 1: "

  assert responses[1].partial is True
  assert responses[1].content.parts[0].thought is True
  assert responses[1].content.parts[0].text == "analyze."

  # Third partial is text.
  assert responses[2].partial is True
  assert responses[2].content.parts[0].text == "The answer is 42."

  # Final aggregated response has both thinking and text parts.
  final = responses[3]
  assert final.partial is False
  assert len(final.content.parts) == 2

  thinking_part = final.content.parts[0]
  assert thinking_part.thought is True
  assert thinking_part.text == "Step 1: analyze."

  text_part = final.content.parts[1]
  assert text_part.text == "The answer is 42."

  assert final.usage_metadata.prompt_token_count == 15
  assert final.usage_metadata.candidates_token_count == 10


@pytest.mark.asyncio
async def test_streaming_passes_thinking_param():
  """When thinking_config is set and stream=True, thinking kwarg is passed."""
  llm = AnthropicLlm(model="claude-sonnet-4-20250514")

  events = [
      MagicMock(
          type="message_start",
          message=MagicMock(usage=MagicMock(input_tokens=5, output_tokens=0)),
      ),
      MagicMock(
          type="content_block_start",
          index=0,
          content_block=anthropic_types.TextBlock(text="", type="text"),
      ),
      MagicMock(
          type="content_block_delta",
          index=0,
          delta=anthropic_types.TextDelta(text="Ok", type="text_delta"),
      ),
      MagicMock(type="content_block_stop", index=0),
      MagicMock(
          type="message_delta",
          delta=MagicMock(stop_reason="end_turn"),
          usage=MagicMock(output_tokens=1),
      ),
      MagicMock(type="message_stop"),
  ]

  mock_client = MagicMock()
  mock_client.messages.create = AsyncMock(
      return_value=_make_mock_stream_events(events)
  )

  request = LlmRequest(
      model="claude-sonnet-4-20250514",
      contents=[Content(role="user", parts=[Part.from_text(text="Hi")])],
      config=types.GenerateContentConfig(
          system_instruction="Test",
          thinking_config=types.ThinkingConfig(thinking_budget=3000),
      ),
  )

  with mock.patch.object(llm, "_anthropic_client", mock_client):
    _ = [r async for r in llm.generate_content_async(request, stream=True)]

  mock_client.messages.create.assert_called_once()
  _, kwargs = mock_client.messages.create.call_args
  assert kwargs["thinking"] == anthropic_types.ThinkingConfigEnabledParam(
      type="enabled", budget_tokens=3000
  )
  assert kwargs["stream"] is True


@pytest.mark.asyncio
async def test_streaming_redacted_thinking_block_preserved_in_final():
  """Streaming RedactedThinkingBlock arrives at start and ends up in final."""
  llm = AnthropicLlm(model="claude-sonnet-4-20250514")

  events = [
      MagicMock(
          type="message_start",
          message=MagicMock(usage=MagicMock(input_tokens=8, output_tokens=0)),
      ),
      MagicMock(
          type="content_block_start",
          index=0,
          content_block=anthropic_types.RedactedThinkingBlock(
              data="encrypted_blob", type="redacted_thinking"
          ),
      ),
      MagicMock(type="content_block_stop", index=0),
      MagicMock(
          type="content_block_start",
          index=1,
          content_block=anthropic_types.TextBlock(text="", type="text"),
      ),
      MagicMock(
          type="content_block_delta",
          index=1,
          delta=anthropic_types.TextDelta(text="Done.", type="text_delta"),
      ),
      MagicMock(type="content_block_stop", index=1),
      MagicMock(
          type="message_delta",
          delta=MagicMock(stop_reason="end_turn"),
          usage=MagicMock(output_tokens=4),
      ),
      MagicMock(type="message_stop"),
  ]

  mock_client = MagicMock()
  mock_client.messages.create = AsyncMock(
      return_value=_make_mock_stream_events(events)
  )

  request = LlmRequest(
      model="claude-sonnet-4-20250514",
      contents=[Content(role="user", parts=[Part.from_text(text="Hi")])],
      config=types.GenerateContentConfig(
          system_instruction="Test",
          thinking_config=types.ThinkingConfig(thinking_budget=3000),
      ),
  )

  with mock.patch.object(llm, "_anthropic_client", mock_client):
    responses = [
        r async for r in llm.generate_content_async(request, stream=True)
    ]

  final = responses[-1]
  assert final.partial is False
  assert len(final.content.parts) == 2

  redacted_part = final.content.parts[0]
  assert redacted_part.thought is True
  assert redacted_part.text is None
  assert redacted_part.thought_signature == b"encrypted_blob"

  text_part = final.content.parts[1]
  assert text_part.text == "Done."


def test_part_to_message_block_function_call_none_id():
  """Function call with None ID should get a valid generated ID."""
  part = types.Part.from_function_call(name="test_tool", args={"key": "value"})
  part.function_call.id = None

  result = part_to_message_block(part)
  assert result["id"].startswith("toolu_")
  assert re.fullmatch(r"[a-zA-Z0-9_-]+", result["id"])


def test_part_to_message_block_function_call_empty_id():
  """Function call with empty string ID should get a valid generated ID."""
  part = types.Part.from_function_call(name="test_tool", args={"key": "value"})
  part.function_call.id = ""

  result = part_to_message_block(part)
  assert result["id"].startswith("toolu_")
  assert re.fullmatch(r"[a-zA-Z0-9_-]+", result["id"])


def test_part_to_message_block_function_call_invalid_chars_id():
  """Function call with invalid chars in ID should get a valid generated ID."""
  part = types.Part.from_function_call(name="test_tool", args={"key": "value"})
  part.function_call.id = "invalid id with spaces!"

  result = part_to_message_block(part)
  assert result["id"].startswith("toolu_")
  assert re.fullmatch(r"[a-zA-Z0-9_-]+", result["id"])


def test_part_to_message_block_function_response_none_id():
  """Function response with None ID should get a valid generated ID."""
  part = types.Part.from_function_response(
      name="test_tool", response={"result": "ok"}
  )
  part.function_response.id = None

  result = part_to_message_block(part)
  assert result["tool_use_id"].startswith("toolu_")
  assert re.fullmatch(r"[a-zA-Z0-9_-]+", result["tool_use_id"])


def test_part_to_message_block_function_response_empty_id():
  """Function response with empty ID should get a valid generated ID."""
  part = types.Part.from_function_response(
      name="test_tool", response={"result": "ok"}
  )
  part.function_response.id = ""

  result = part_to_message_block(part)
  assert result["tool_use_id"].startswith("toolu_")
  assert re.fullmatch(r"[a-zA-Z0-9_-]+", result["tool_use_id"])


def _make_tool_call_part(name: str, call_id: str | None) -> Part:
  part = types.Part.from_function_call(name=name, args={})
  part.function_call.id = call_id
  return part


def _make_tool_response_part(name: str, response_id: str | None) -> Part:
  part = types.Part.from_function_response(name=name, response={"result": "ok"})
  part.function_response.id = response_id
  return part


async def _capture_anthropic_messages(
    llm: AnthropicLlm,
    contents: list[Content],
    generate_content_response,
    generate_llm_response,
) -> list[dict]:
  llm_request = LlmRequest(
      model="claude-sonnet-4-20250514",
      contents=contents,
      config=types.GenerateContentConfig(system_instruction="You are helpful"),
  )
  with mock.patch.object(llm, "_anthropic_client") as mock_client:
    with mock.patch.object(
        anthropic_llm,
        "message_to_generate_content_response",
        return_value=generate_llm_response,
    ):

      async def mock_coro():
        return generate_content_response

      mock_client.messages.create.return_value = mock_coro()
      _ = [
          r async for r in llm.generate_content_async(llm_request, stream=False)
      ]

  _, kwargs = mock_client.messages.create.call_args
  return kwargs["messages"]


@pytest.mark.parametrize(
    "case_id,call_ids,response_ids,expected_unique",
    [
        (
            "distinct_invalid_pair_uniquely",
            ["bad A!", "bad B!"],
            ["bad A!", "bad B!"],
            2,
        ),
        ("matching_empty_ids_pair", [""], [""], 1),
        ("none_and_empty_collapse", [None], [""], 1),
        ("repeated_invalid_id_consistent", ["bad!"], ["bad!"], 1),
    ],
    ids=lambda v: v if isinstance(v, str) else None,
)
@pytest.mark.asyncio
async def test_generate_content_async_pairs_invalid_tool_ids(
    case_id,
    call_ids,
    response_ids,
    expected_unique,
    generate_content_response,
    generate_llm_response,
):
  """Anthropic requests have matching, properly-counted tool_use/tool_result IDs."""
  llm = AnthropicLlm(model="claude-sonnet-4-20250514")
  contents = [
      Content(role="user", parts=[Part.from_text(text="Hi")]),
      Content(
          role="model",
          parts=[
              _make_tool_call_part(f"tool_{i}", cid)
              for i, cid in enumerate(call_ids)
          ],
      ),
      Content(
          role="user",
          parts=[
              _make_tool_response_part(f"tool_{i}", rid)
              for i, rid in enumerate(response_ids)
          ],
      ),
  ]

  messages = await _capture_anthropic_messages(
      llm, contents, generate_content_response, generate_llm_response
  )

  use_ids = [b["id"] for b in messages[1]["content"] if b["type"] == "tool_use"]
  result_ids = [
      b["tool_use_id"]
      for b in messages[2]["content"]
      if b["type"] == "tool_result"
  ]
  assert len(set(use_ids)) == expected_unique
  assert set(use_ids) == set(result_ids)


@pytest.mark.asyncio
async def test_non_streaming_no_system_instruction_passes_not_given():
  """system=NOT_GIVEN when LlmRequest has no system_instruction."""
  llm = AnthropicLlm(model="claude-sonnet-4-20250514")

  mock_message = anthropic_types.Message(
      id="msg_test",
      content=[
          anthropic_types.TextBlock(text="ok", type="text", citations=None)
      ],
      model="claude-sonnet-4-20250514",
      role="assistant",
      stop_reason="end_turn",
      stop_sequence=None,
      type="message",
      usage=anthropic_types.Usage(
          input_tokens=1,
          output_tokens=1,
          cache_creation_input_tokens=0,
          cache_read_input_tokens=0,
          server_tool_use=None,
          service_tier=None,
      ),
  )

  mock_client = MagicMock()
  mock_client.messages.create = AsyncMock(return_value=mock_message)

  request = LlmRequest(
      model="claude-sonnet-4-20250514",
      contents=[Content(role="user", parts=[Part.from_text(text="Hi")])],
  )
  assert request.config.system_instruction is None

  with mock.patch.object(llm, "_anthropic_client", mock_client):
    _ = [r async for r in llm.generate_content_async(request, stream=False)]

  mock_client.messages.create.assert_called_once()
  _, kwargs = mock_client.messages.create.call_args
  assert kwargs["system"] is NOT_GIVEN


@pytest.mark.asyncio
async def test_streaming_no_system_instruction_passes_not_given():
  """system=NOT_GIVEN on the streaming path when no system_instruction."""
  llm = AnthropicLlm(model="claude-sonnet-4-20250514")

  events = [
      MagicMock(
          type="message_start",
          message=MagicMock(usage=MagicMock(input_tokens=1, output_tokens=0)),
      ),
      MagicMock(
          type="content_block_start",
          index=0,
          content_block=anthropic_types.TextBlock(text="", type="text"),
      ),
      MagicMock(
          type="content_block_delta",
          index=0,
          delta=anthropic_types.TextDelta(text="ok", type="text_delta"),
      ),
      MagicMock(type="content_block_stop", index=0),
      MagicMock(
          type="message_delta",
          delta=MagicMock(stop_reason="end_turn"),
          usage=MagicMock(output_tokens=1),
      ),
      MagicMock(type="message_stop"),
  ]

  mock_client = MagicMock()
  mock_client.messages.create = AsyncMock(
      return_value=_make_mock_stream_events(events)
  )

  request = LlmRequest(
      model="claude-sonnet-4-20250514",
      contents=[Content(role="user", parts=[Part.from_text(text="Hi")])],
  )
  assert request.config.system_instruction is None

  with mock.patch.object(llm, "_anthropic_client", mock_client):
    _ = [r async for r in llm.generate_content_async(request, stream=True)]

  mock_client.messages.create.assert_called_once()
  _, kwargs = mock_client.messages.create.call_args
  assert kwargs["system"] is NOT_GIVEN


@pytest.mark.asyncio
async def test_generate_content_async_with_generation_config(
    generate_content_response, generate_llm_response
):
  claude_llm = Claude(model="claude-3-5-sonnet-v2@20241022")
  llm_request = LlmRequest(
      model="claude-3-5-sonnet-v2@20241022",
      contents=[Content(role="user", parts=[Part.from_text(text="Hello")])],
      config=types.GenerateContentConfig(
          temperature=0.7,
          top_p=0.9,
          top_k=50,
          stop_sequences=["##"],
          max_output_tokens=1024,
      ),
  )
  with mock.patch.object(claude_llm, "_anthropic_client") as mock_client:
    with mock.patch.object(
        anthropic_llm,
        "message_to_generate_content_response",
        return_value=generate_llm_response,
    ):

      async def mock_coro():
        return generate_content_response

      mock_client.messages.create.return_value = mock_coro()

      _ = [
          resp
          async for resp in claude_llm.generate_content_async(
              llm_request, stream=False
          )
      ]
      mock_client.messages.create.assert_called_once()
      _, kwargs = mock_client.messages.create.call_args
      assert kwargs["temperature"] == 0.7
      assert kwargs["top_p"] == 0.9
      assert kwargs["top_k"] == 50
      assert kwargs["stop_sequences"] == ["##"]
      assert kwargs["max_tokens"] == 1024


@pytest.mark.asyncio
async def test_generate_content_streaming_with_generation_config(
    generate_content_response,
):
  claude_llm = Claude(model="claude-3-5-sonnet-v2@20241022")
  llm_request = LlmRequest(
      model="claude-3-5-sonnet-v2@20241022",
      contents=[Content(role="user", parts=[Part.from_text(text="Hello")])],
      config=types.GenerateContentConfig(
          temperature=0.7,
          top_p=0.9,
          top_k=50,
          stop_sequences=["##"],
          max_output_tokens=1024,
      ),
  )
  with mock.patch.object(claude_llm, "_anthropic_client") as mock_client:

    async def mock_coro(*args, **kwargs):
      async def async_gen():
        if False:
          yield None

      return async_gen()

    mock_client.messages.create.side_effect = mock_coro

    _ = [
        resp
        async for resp in claude_llm.generate_content_async(
            llm_request, stream=True
        )
    ]
    mock_client.messages.create.assert_called_once()
    _, kwargs = mock_client.messages.create.call_args
    assert kwargs["temperature"] == 0.7
    assert kwargs["top_p"] == 0.9
    assert kwargs["top_k"] == 50
    assert kwargs["stop_sequences"] == ["##"]
    assert kwargs["max_tokens"] == 1024
    assert kwargs["stream"]


@pytest.mark.asyncio
async def test_generate_content_async_with_thinking_level_warns_and_ignores(
    generate_content_response,
    generate_llm_response,
):
  """Tests that generate_content_async with standard thinking_level warns and ignores it."""
  claude_llm = AnthropicLlm(model="claude-sonnet-4-20250514")
  llm_request = LlmRequest(
      model="claude-sonnet-4-20250514",
      contents=[Content(role="user", parts=[Part.from_text(text="Hello")])],
      config=types.GenerateContentConfig(
          thinking_config=types.ThinkingConfig(
              thinking_budget=-1,
              thinking_level=types.ThinkingLevel.MINIMAL,
          )
      ),
  )
  with mock.patch.object(claude_llm, "_anthropic_client") as mock_client:
    with mock.patch.object(
        anthropic_llm,
        "message_to_generate_content_response",
        return_value=generate_llm_response,
    ):

      async def mock_coro():
        return generate_content_response

      mock_client.messages.create.return_value = mock_coro()

      with pytest.warns(
          UserWarning,
          match="Standard thinking_config.thinking_level is not supported",
      ):
        _ = [
            resp
            async for resp in claude_llm.generate_content_async(
                llm_request, stream=False
            )
        ]
      mock_client.messages.create.assert_called_once()
      _, kwargs = mock_client.messages.create.call_args
      # Verify that thinking_level was ignored (but budget -1 still enabled adaptive thinking).
      assert kwargs["thinking"] == {"type": "adaptive"}
      assert "output_config" not in kwargs


@pytest.mark.asyncio
async def test_generate_content_async_anthropic_config_with_thinking_level_raises_error():
  """Tests that AnthropicGenerateContentConfig with standard thinking_level raises ValueError."""
  with pytest.raises(
      ValueError,
      match="thinking_level is not supported in AnthropicGenerateContentConfig",
  ):
    _ = AnthropicGenerateContentConfig(
        effort="xhigh",
        thinking_config=types.ThinkingConfig(
            thinking_budget=-1,
            thinking_level=types.ThinkingLevel.MINIMAL,
        ),
    )


@pytest.mark.asyncio
async def test_generate_content_async_with_anthropic_config_effort(
    generate_content_response,
    generate_llm_response,
):
  """Tests generate_content_async with Anthropic-specific effort configuration."""
  claude_llm = AnthropicLlm(model="claude-sonnet-4-20250514")
  llm_request = LlmRequest(
      model="claude-sonnet-4-20250514",
      contents=[Content(role="user", parts=[Part.from_text(text="Hello")])],
      config=AnthropicGenerateContentConfig(
          effort="xhigh",
      ),
  )
  with mock.patch.object(claude_llm, "_anthropic_client") as mock_client:
    with mock.patch.object(
        anthropic_llm,
        "message_to_generate_content_response",
        return_value=generate_llm_response,
    ):

      async def mock_coro():
        return generate_content_response

      mock_client.messages.create.return_value = mock_coro()

      _ = [
          resp
          async for resp in claude_llm.generate_content_async(
              llm_request, stream=False
          )
      ]
      mock_client.messages.create.assert_called_once()
      _, kwargs = mock_client.messages.create.call_args
      assert kwargs["output_config"] == {"effort": "xhigh"}
      assert kwargs["thinking"] is NOT_GIVEN


@pytest.mark.asyncio
async def test_generate_content_async_excludes_sampling_when_thinking(
    generate_content_response,
    generate_llm_response,
):
  """Tests that sampling parameters are excluded when thinking is enabled."""
  claude_llm = AnthropicLlm(model="claude-sonnet-4-20250514")
  llm_request = LlmRequest(
      model="claude-sonnet-4-20250514",
      contents=[Content(role="user", parts=[Part.from_text(text="Hello")])],
      config=types.GenerateContentConfig(
          temperature=0.7,
          top_p=0.9,
          top_k=50,
          thinking_config=types.ThinkingConfig(
              thinking_budget=1024,
          ),
      ),
  )
  with mock.patch.object(claude_llm, "_anthropic_client") as mock_client:
    with mock.patch.object(
        anthropic_llm,
        "message_to_generate_content_response",
        return_value=generate_llm_response,
    ):

      async def mock_coro():
        return generate_content_response

      mock_client.messages.create.return_value = mock_coro()

      with pytest.warns(
          UserWarning, match="Sampling parameters .* are ignored"
      ):
        _ = [
            resp
            async for resp in claude_llm.generate_content_async(
                llm_request, stream=False
            )
        ]
      mock_client.messages.create.assert_called_once()
      _, kwargs = mock_client.messages.create.call_args
      assert "temperature" not in kwargs
      assert "top_p" not in kwargs
      assert "top_k" not in kwargs
      assert kwargs["max_tokens"] == 8192
      assert kwargs["thinking"] == {"type": "enabled", "budget_tokens": 1024}


@pytest.mark.asyncio
async def test_generate_content_async_excludes_sampling_when_effort(
    generate_content_response,
    generate_llm_response,
):
  """Tests that sampling parameters are excluded when effort is enabled."""
  claude_llm = AnthropicLlm(model="claude-sonnet-4-20250514")
  llm_request = LlmRequest(
      model="claude-sonnet-4-20250514",
      contents=[Content(role="user", parts=[Part.from_text(text="Hello")])],
      config=AnthropicGenerateContentConfig(
          temperature=0.7,
          top_p=0.9,
          top_k=50,
          effort="xhigh",
      ),
  )
  with mock.patch.object(claude_llm, "_anthropic_client") as mock_client:
    with mock.patch.object(
        anthropic_llm,
        "message_to_generate_content_response",
        return_value=generate_llm_response,
    ):

      async def mock_coro():
        return generate_content_response

      mock_client.messages.create.return_value = mock_coro()

      with pytest.warns(
          UserWarning, match="Sampling parameters .* are ignored"
      ):
        _ = [
            resp
            async for resp in claude_llm.generate_content_async(
                llm_request, stream=False
            )
        ]
      mock_client.messages.create.assert_called_once()
      _, kwargs = mock_client.messages.create.call_args
      assert "temperature" not in kwargs
      assert "top_p" not in kwargs
      assert "top_k" not in kwargs
      assert kwargs["output_config"] == {"effort": "xhigh"}
