package withdrawal

import (
	"net/http/httptest"
	"strings"
	"testing"

	"github.com/gin-gonic/gin"
)

func TestReviewWithdrawalRequestBindApprovedFalse(t *testing.T) {
	gin.SetMode(gin.TestMode)

	c, _ := gin.CreateTestContext(httptest.NewRecorder())
	c.Request = httptest.NewRequest("POST", "/admin/withdrawals/1/review", strings.NewReader(`{"approved":false,"remark":"资料不符"}`))
	c.Request.Header.Set("Content-Type", "application/json")

	var req ReviewWithdrawalRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		t.Fatalf("approved=false 应该通过参数绑定: %v", err)
	}
	if req.Approved == nil {
		t.Fatal("approved 字段应该被识别为已传入")
	}
	if *req.Approved {
		t.Fatal("approved=false 不应该被绑定成 true")
	}
}

func TestReviewWithdrawalRequestBindMissingApproved(t *testing.T) {
	gin.SetMode(gin.TestMode)

	c, _ := gin.CreateTestContext(httptest.NewRecorder())
	c.Request = httptest.NewRequest("POST", "/admin/withdrawals/1/review", strings.NewReader(`{"remark":"资料不符"}`))
	c.Request.Header.Set("Content-Type", "application/json")

	var req ReviewWithdrawalRequest
	if err := c.ShouldBindJSON(&req); err == nil {
		t.Fatal("缺少 approved 字段时应该参数绑定失败")
	}
}
