"""
通用 Schema 单元测试
=====================

验证 ApiResponse / ApiErrorResponse 的序列化与泛型行为。
"""

from app.schemas.common import (
    ApiErrorResponse,
    ApiResponse,
    error_response,
    success_response,
)


class TestSuccessResponse:
    """成功响应构造"""

    def test_success_response_with_data(self) -> None:
        resp = success_response(data={"id": 1}, message="创建成功")
        assert resp.code == 200
        assert resp.data == {"id": 1}
        assert resp.message == "创建成功"

    def test_success_response_default(self) -> None:
        resp = success_response()
        assert resp.code == 200
        assert resp.data is None
        assert resp.message == "success"


class TestErrorResponse:
    """错误响应构造"""

    def test_error_response_with_error_code(self) -> None:
        resp = error_response(
            code=402,
            message="积分不足",
            error_code="insufficient_points",
        )
        assert resp.code == 402
        assert resp.message == "积分不足"
        assert resp.error_code == "insufficient_points"
        assert resp.detail is None

    def test_error_response_with_detail(self) -> None:
        resp = error_response(
            code=422,
            message="参数错误",
            detail={"fields": {"name": "required"}},
        )
        assert resp.detail == {"fields": {"name": "required"}}


class TestApiResponseSerialization:
    """序列化"""

    def test_api_response_model_dump(self) -> None:
        resp = ApiResponse(code=200, data=[1, 2, 3], message="ok")
        dumped = resp.model_dump()
        assert dumped["code"] == 200
        assert dumped["data"] == [1, 2, 3]
        assert dumped["message"] == "ok"

    def test_api_error_response_model_dump(self) -> None:
        resp = ApiErrorResponse(
            code=402,
            message="积分不足",
            error_code="insufficient_points",
        )
        dumped = resp.model_dump()
        assert dumped["code"] == 402
        assert dumped["error_code"] == "insufficient_points"
