package tech.easyflow.system.service.impl;

import com.mybatisflex.core.query.QueryWrapper;
import org.junit.Test;

import tech.easyflow.system.entity.SysAccount;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

/**
 * {@link SysAccountServiceImpl} 测试。
 */
public class SysAccountServiceImplTest {

    /**
     * 验证展示名解析优先级与缺失数据回退。
     */
    @Test
    public void shouldResolveDisplayNameByNicknameThenLoginNameThenId() {
        SysAccountServiceImpl service = spy(new SysAccountServiceImpl());

        SysAccount nicknameAccount = new SysAccount();
        nicknameAccount.setId(BigInteger.ONE);
        nicknameAccount.setNickname("管理员");
        nicknameAccount.setLoginName("admin");

        SysAccount loginNameAccount = new SysAccount();
        loginNameAccount.setId(BigInteger.valueOf(2));
        loginNameAccount.setNickname("   ");
        loginNameAccount.setLoginName("operator");

        doReturn(List.of(nicknameAccount, loginNameAccount))
                .when(service)
                .list(any(QueryWrapper.class));

        Map<BigInteger, String> displayNameMap = service.resolveDisplayNameMap(
                List.of(BigInteger.ONE, BigInteger.valueOf(2), BigInteger.valueOf(3))
        );

        assertEquals("管理员", displayNameMap.get(BigInteger.ONE));
        assertEquals("operator", displayNameMap.get(BigInteger.valueOf(2)));
        assertEquals("3", displayNameMap.get(BigInteger.valueOf(3)));
        verify(service, times(1)).list(any(QueryWrapper.class));
    }
}
