"""Step definitions for features/tdd_mcp_infer_resource_slots_null_properties.feature.

TDD issue-capture scenario for #10470: MCPToolAdapter.infer_resource_slots()
raises TypeError when the input schema has a null "properties" value.

The bug: input_schema.get("properties", {}) returns None (not {}) when the
key exists with a null value, causing TypeError on iteration.
"""

from __future__ import annotations

from behave import given, then, when
from behave.runner import Context

from cleveragents.mcp.adapter import MCPToolAdapter


@given("an MCP tool input schema where properties key is null")
def step_given_null_properties_schema(context: Context) -> None:
    """Set up an input schema where 'properties' key exists but has a null value."""
    context.mcp_tool_name = "test_tool"
    context.mcp_input_schema = {"properties": None}
    context.infer_type_error = None
    context.inferred_slots_result = None


@when("I call infer_resource_slots with the null properties schema")
def step_when_infer_slots_null_properties(context: Context) -> None:
    """Call infer_resource_slots() and capture any TypeError raised."""
    try:
        context.inferred_slots_result = MCPToolAdapter.infer_resource_slots(
            context.mcp_tool_name,
            context.mcp_input_schema,
        )
    except TypeError as exc:
        context.infer_type_error = exc


@then("no TypeError should have been raised during slot inference")
def step_then_no_type_error(context: Context) -> None:
    """Assert that no TypeError was raised during infer_resource_slots()."""
    assert context.infer_type_error is None, (
        f"Expected no TypeError, but got: {context.infer_type_error!r}\n"
        "Bug: infer_resource_slots() raises TypeError when 'properties' is None. "
        "Fix: use `input_schema.get('properties') or {}` instead of "
        "`input_schema.get('properties', {})`."
    )


@then("the inferred slots result should be an empty list")
def step_then_slots_empty_list(context: Context) -> None:
    """Assert that the result of infer_resource_slots() is an empty list."""
    result = context.inferred_slots_result
    assert result is not None, (
        "inferred_slots_result is None — infer_resource_slots() likely raised "
        "an exception before returning."
    )
    assert result == [], (
        f"Expected infer_resource_slots() to return [], got: {result!r}"
    )
