package tech.easyflow.admin.controller.ai;

import com.mybatisflex.core.query.QueryWrapper;
import org.testng.Assert;
import org.testng.annotations.Test;
import tech.easyflow.ai.entity.Model;
import tech.easyflow.ai.mapper.ModelMapper;
import tech.easyflow.ai.service.ModelService;
import tech.easyflow.common.domain.Result;

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

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * 模型列表控制器测试。
 */
public class ModelControllerTest {

    /**
     * 验证模型列表使用包含目录能力元数据的服务查询。
     */
    @Test
    public void listShouldReturnCapabilityDecoratedModels() {
        ModelService service = mock(ModelService.class);
        Model query = new Model();
        Model decoratedModel = new Model();
        decoratedModel.setContextWindowTokens(1_000_000L);
        decoratedModel.setMaxOutputTokens(64_000L);
        when(service.listSelectableModels(query, false, "id", "desc"))
                .thenReturn(List.of(decoratedModel));
        ModelController controller = new ModelController(service);

        Result<List<Model>> result = controller.list(query, false, "id", "desc");

        Assert.assertSame(decoratedModel, result.getData().get(0));
        Assert.assertEquals(Long.valueOf(1_000_000L),
                result.getData().get(0).getContextWindowTokens());
        verify(service).listSelectableModels(query, false, "id", "desc");
    }

    /**
     * 验证仅包含空白字符的选择文本不会生成模糊查询条件。
     */
    @Test
    public void selectLlmShouldIgnoreBlankSearchText() {
        ModelService service = mock(ModelService.class);
        ModelMapper mapper = mock(ModelMapper.class);
        when(service.getMapper()).thenReturn(mapper);
        when(mapper.selectListWithRelationsByQuery(any(QueryWrapper.class)))
                .thenReturn(List.of());
        ModelController controller = new ModelController(service);

        Result<Map<String, List<Model>>> result = controller.selectLlmByProviderAndModelType(
                "chat", BigInteger.ONE, "   ");

        Assert.assertTrue(result.getData().isEmpty());
        verify(mapper).selectListWithRelationsByQuery(any(QueryWrapper.class));
    }
}
