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.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.MallServiceAgreementConstants;
import com.ruoyi.web.modules.content.domain.BizMallServiceAgreement;
import com.ruoyi.web.modules.content.facade.impl.MallServiceAgreementFacadeImpl;
import com.ruoyi.web.modules.content.mapper.BizMallServiceAgreementMapper;
import com.ruoyi.web.modules.content.service.IMallServiceAgreementService;

/**
 * 商城服务协议 Facade 单元测试
 */
@ExtendWith(MockitoExtension.class)
class MallServiceAgreementFacadeImplTest
{
    @Mock
    private IMallServiceAgreementService agreementService;

    @Mock
    private BizMallServiceAgreementMapper agreementMapper;

    @InjectMocks
    private MallServiceAgreementFacadeImpl facade;

    @Test
    void assertAcceptedForLogin_delegates()
    {
        facade.assertAcceptedForLogin(true);
        verify(agreementService).assertAcceptedForLogin(true);
    }

    @Test
    void assertAcceptedForRegister_delegates()
    {
        facade.assertAcceptedForRegister(true);
        verify(agreementService).assertAcceptedForRegister(true);
    }

    @Test
    void getCheckboxLabel_fromDb()
    {
        BizMallServiceAgreement row = new BizMallServiceAgreement();
        row.setAgreementTitle("用户协议");
        row.setVersionLabel("v2");
        when(agreementMapper.selectById(MallServiceAgreementConstants.CONFIG_ID)).thenReturn(row);
        when(agreementService.buildCheckboxLabel("用户协议", "v2"))
                .thenReturn("我已阅读并同意《用户协议》（v2）");

        assertEquals("我已阅读并同意《用户协议》（v2）", facade.getCheckboxLabel());
    }

    @Test
    void assertAcceptedForRegister_notAccepted_throws()
    {
        org.mockito.Mockito.doThrow(new ServiceException(MallServiceAgreementConstants.MSG_AGREE_REQUIRED))
                .when(agreementService).assertAcceptedForRegister(false);

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