"""@api_endpoint 데코레이터 테스트.

Phase 2.5.
"""
import pytest

from app.api.deps import api_endpoint
from app.core.errors import AppError


def test_dict_response_gets_warnings_key():
    @api_endpoint
    def handler():
        return {"ok": True}

    result = handler()
    assert result == {"ok": True, "warnings": []}


def test_existing_warnings_preserved():
    @api_endpoint
    def handler():
        return {"ok": True, "warnings": ["preset"]}

    assert handler() == {"ok": True, "warnings": ["preset"]}


def test_non_dict_response_unchanged():
    @api_endpoint
    def handler():
        return [1, 2, 3]

    assert handler() == [1, 2, 3]


def test_app_error_reraised():
    @api_endpoint
    def handler():
        raise AppError(code="test.err", message="x", status_code=400)

    with pytest.raises(AppError) as exc_info:
        handler()
    assert exc_info.value.code == "test.err"


def test_unhandled_exception_wrapped_to_app_error():
    @api_endpoint
    def handler():
        raise ValueError("unexpected")

    with pytest.raises(AppError) as exc_info:
        handler()
    assert exc_info.value.code == "internal_error"
    assert exc_info.value.status_code == 500


def test_function_metadata_preserved():
    @api_endpoint
    def my_specific_name():
        """My docstring."""
        return {}

    assert my_specific_name.__name__ == "my_specific_name"
    assert my_specific_name.__doc__ == "My docstring."
