package tech.easyflow.approval.support;

import org.junit.Assert;
import org.junit.Test;
import tech.easyflow.approval.enums.ApprovalActionType;
import tech.easyflow.common.web.exceptions.BusinessException;

/**
 * {@link ApprovalApplicationReasonPolicy} 测试。
 */
public class ApprovalApplicationReasonPolicyTest {

    /**
     * 验证命中发布审批时说明必填并去除首尾空白。
     */
    @Test
    public void shouldNormalizeRequiredPublishReason() {
        String result = ApprovalApplicationReasonPolicy.normalize(
                true,
                ApprovalActionType.PUBLISH.getCode(),
                "  修复校验逻辑并补充测试  "
        );

        Assert.assertEquals("修复校验逻辑并补充测试", result);
    }

    /**
     * 验证命中发布审批时拒绝空说明。
     */
    @Test(expected = BusinessException.class)
    public void shouldRejectBlankRequiredPublishReason() {
        ApprovalApplicationReasonPolicy.normalize(
                true,
                ApprovalActionType.PUBLISH.getCode(),
                "   "
        );
    }

    /**
     * 验证命中发布审批时拒绝超过 500 字的说明。
     */
    @Test(expected = BusinessException.class)
    public void shouldRejectOversizedRequiredPublishReason() {
        ApprovalApplicationReasonPolicy.normalize(
                true,
                ApprovalActionType.PUBLISH.getCode(),
                "a".repeat(501)
        );
    }

    /**
     * 验证未命中审批或非发布动作无需说明。
     */
    @Test
    public void shouldIgnoreReasonWhenItIsNotRequired() {
        Assert.assertNull(ApprovalApplicationReasonPolicy.normalize(
                false,
                ApprovalActionType.PUBLISH.getCode(),
                null
        ));
        Assert.assertNull(ApprovalApplicationReasonPolicy.normalize(
                true,
                ApprovalActionType.OFFLINE.getCode(),
                null
        ));
    }
}
