"""Tests for Throw Error node."""

import pytest
from flowire_sdk.exceptions import FlowFailureError

from fw_nodes_core.nodes.throw_error import ThrowErrorNode


@pytest.mark.asyncio
async def test_throw_error_raises_flow_failure(node_execution_context):
    """Test that Throw Error raises FlowFailureError with user message."""
    node = ThrowErrorNode()
    inputs = {"error_message": "Payment failed"}

    with pytest.raises(FlowFailureError, match="Payment failed"):
        await node.execute(inputs, node_execution_context)


@pytest.mark.asyncio
async def test_throw_error_resolves_expressions(mock_context_with_results):
    """Test that Throw Error resolves expressions in the error message."""
    node = ThrowErrorNode()
    inputs = {"error_message": "HTTP status: {{http_node.status_code}}"}

    with pytest.raises(FlowFailureError, match="HTTP status: 200"):
        await node.execute(inputs, mock_context_with_results)


@pytest.mark.asyncio
async def test_throw_error_empty_message(node_execution_context):
    """Test that Throw Error works with empty message."""
    node = ThrowErrorNode()
    inputs = {"error_message": ""}

    with pytest.raises(FlowFailureError):
        await node.execute(inputs, node_execution_context)


@pytest.mark.asyncio
async def test_throw_error_metadata():
    """Test Throw Error node metadata is correct."""
    node = ThrowErrorNode()
    metadata = node.get_metadata()

    assert metadata.name == "Throw Error"
    assert metadata.category == "logic"
    assert metadata.handles.outputs == []  # Terminal node
    assert len(metadata.handles.inputs) == 1
