"""
{{ display_name }} - Automation tests.

This module contains tests for the webhook handler automation.
"""

import pytest

from torivers_sdk.testing import LocalSandbox
from main import {{ class_name }}


@pytest.fixture
def sandbox():
    """Create a local sandbox for testing."""
    return LocalSandbox()


@pytest.fixture
def automation():
    """Create an automation instance."""
    return {{ class_name }}()


@pytest.mark.asyncio
async def test_valid_created_event(sandbox, automation):
    """Test handling of valid created event."""
    result = await sandbox.run(
        automation,
        input_data={
            "event_type": "created",
            "payload": {"id": "123", "name": "test"},
        },
    )

    assert result.success
    assert "_blocks" in result.output_data
    blocks = result.output_data["_blocks"]
    assert len(blocks) > 0
    assert blocks[0]["type"] == "json_data"
    data = blocks[0]["data"]
    assert data["processed"] is True
    assert "event_id" in data
    assert len(data["actions_taken"]) > 0


@pytest.mark.asyncio
async def test_valid_updated_event(sandbox, automation):
    """Test handling of valid updated event."""
    result = await sandbox.run(
        automation,
        input_data={
            "event_type": "updated",
            "payload": {"id": "123", "changes": {"name": "new_name"}},
        },
    )

    assert result.success
    assert "_blocks" in result.output_data
    blocks = result.output_data["_blocks"]
    data = blocks[0]["data"]
    assert data["processed"] is True


@pytest.mark.asyncio
async def test_invalid_event_type(sandbox, automation):
    """Test handling of invalid event type."""
    result = await sandbox.run(
        automation,
        input_data={
            "event_type": "invalid_type",
            "payload": {"data": "test"},
        },
    )

    assert result.success  # Handler should complete, but mark as not processed
    blocks = result.output_data["_blocks"]
    data = blocks[0]["data"]
    assert data["processed"] is False


@pytest.mark.asyncio
async def test_empty_payload(sandbox, automation):
    """Test handling of empty payload."""
    result = await sandbox.run(
        automation,
        input_data={
            "event_type": "created",
            "payload": {},
        },
    )

    assert result.success
    blocks = result.output_data["_blocks"]
    data = blocks[0]["data"]
    assert data["processed"] is False


@pytest.mark.asyncio
async def test_response_format(sandbox, automation):
    """Test that response matches expected format."""
    result = await sandbox.run(
        automation,
        input_data={
            "event_type": "triggered",
            "payload": {"trigger_id": "abc"},
        },
    )

    assert result.success
    assert result.output_data.get("_version") == 1
    blocks = result.output_data["_blocks"]
    assert isinstance(blocks, list)
    data = blocks[0]["data"]
    assert "response" in data
    assert "status" in data["response"]
    assert "code" in data["response"]
