package com.ruoyi.web.modules.account.service;

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.web.modules.account.constant.MemberConstants;
import com.ruoyi.web.modules.account.domain.BizMemberAddress;
import com.ruoyi.web.modules.account.mapper.BizMemberAddressMapper;
import com.ruoyi.web.modules.account.service.impl.MemberAddressServiceImpl;

@ExtendWith(MockitoExtension.class)
class MemberAddressServiceImplTest
{
    @Mock
    private BizMemberAddressMapper addressMapper;

    @InjectMocks
    private MemberAddressServiceImpl addressService;

    @Test
    void setDefaultAddress_clearsOthers()
    {
        BizMemberAddress existing = new BizMemberAddress();
        existing.setAddressId(2L);
        when(addressMapper.selectById(2L, 1L)).thenReturn(existing);

        addressService.setDefaultAddress(1L, 2L);

        verify(addressMapper).clearDefault(1L);
        verify(addressMapper).setDefault(2L, 1L);
    }

    @Test
    void addAddress_withDefault_clearsPrevious()
    {
        BizMemberAddress addr = new BizMemberAddress();
        addr.setConsigneeName("张三");
        addr.setMobile("17700001111");
        addr.setRegionCode("110101");
        addr.setRegionName("北京市/市辖区/东城区");
        addr.setDetailAddress("详细地址");
        addr.setIsDefault(MemberConstants.DEFAULT_YES);

        addressService.addAddress(1L, addr);

        verify(addressMapper).clearDefault(1L);
        verify(addressMapper).insert(addr);
    }
}
