package com.ruoyi.web.modules.content.facade;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.content.constant.MerchantEntryAgreementConstants;
import com.ruoyi.web.modules.content.domain.BizMerchantEntryAgreement;
import com.ruoyi.web.modules.content.facade.impl.MerchantEntryAgreementFacadeImpl;
import com.ruoyi.web.modules.content.mapper.BizMerchantEntryAgreementMapper;
import com.ruoyi.web.modules.content.service.IMerchantEntryAgreementService;

/**
 * 入驻协议 Facade 单元测试
 */
@ExtendWith(MockitoExtension.class)
class MerchantEntryAgreementFacadeImplTest
{
    @Mock
    private IMerchantEntryAgreementService agreementService;

    @Mock
    private BizMerchantEntryAgreementMapper agreementMapper;

    @InjectMocks
    private MerchantEntryAgreementFacadeImpl facade;

    @Test
    void isEnabled_delegatesToService()
    {
        when(agreementService.isEnabled()).thenReturn(true);

        assertTrue(facade.isEnabled());
    }

    @Test
    void assertAccepted_delegatesToService()
    {
        facade.assertAccepted(true);

        verify(agreementService).assertAccepted(true);
    }

    @Test
    void assertAccepted_notAccepted_throws()
    {
        org.mockito.Mockito.doThrow(new ServiceException(MerchantEntryAgreementConstants.MSG_AGREE_REQUIRED))
                .when(agreementService).assertAccepted(false);

        assertThrows(ServiceException.class, () -> facade.assertAccepted(false));
    }

    @Test
    void getCheckboxLabel_fromDb()
    {
        BizMerchantEntryAgreement row = new BizMerchantEntryAgreement();
        row.setAgreementTitle("标题");
        row.setVersionLabel("v2");
        when(agreementMapper.selectById(MerchantEntryAgreementConstants.CONFIG_ID)).thenReturn(row);
        when(agreementService.buildCheckboxLabel("标题", "v2"))
                .thenReturn("我已阅读并同意《标题》（v2）");

        assertEquals("我已阅读并同意《标题》（v2）", facade.getCheckboxLabel());
    }
}
