package tech.easyflow.ai.service.capability;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import tech.easyflow.ai.entity.Model;

/**
 * 静态模型能力库与命名规则解析测试。
 */
public class ModelCapabilityResolverTest {

    /** 待测试能力解析器。 */
    private ModelCapabilityResolver resolver;

    /**
     * 加载真实静态模型目录。
     */
    @Before
    public void setUp() {
        resolver = new ModelCapabilityResolver(new ModelCapabilityCatalog(new ObjectMapper()));
    }

    /**
     * 验证 DashScope 模型短 ID 能命中 Alibaba 目录条目。
     */
    @Test
    public void shouldResolveCatalogCapabilitiesByProviderAlias() {
        ModelCapabilityResolution result = resolver.resolve("dashscope", "qwen3.7-plus");

        Assert.assertEquals(ModelCapabilitySource.CATALOG, result.getSource());
        Assert.assertEquals(Model.MODEL_TYPES[0], result.getModelType());
        Assert.assertEquals(Boolean.TRUE, result.getSupportImage());
        Assert.assertEquals(Boolean.TRUE, result.getSupportThinking());
        Assert.assertEquals(Boolean.TRUE, result.getSupportTool());
    }

    /**
     * 验证本地补充的 BAAI 嵌入与重排模型类型互斥。
     */
    @Test
    public void shouldResolveBaaiEmbeddingAndRerankModels() {
        ModelCapabilityResolution embedding = resolver.resolve(null, "BAAI/bge-m3");
        ModelCapabilityResolution rerank = resolver.resolve(null, "bge-reranker-v2-m3");

        Assert.assertEquals(Model.MODEL_TYPES[1], embedding.getModelType());
        Assert.assertEquals(Model.MODEL_TYPES[2], rerank.getModelType());
        Assert.assertEquals(Boolean.FALSE, embedding.getSupportTool());
        Assert.assertEquals(Boolean.FALSE, rerank.getSupportImage());
    }

    /**
     * 验证自定义部署名称仍能通过严格关键词识别视觉模型。
     */
    @Test
    public void shouldInferVisionForCustomDeploymentName() {
        ModelCapabilityResolution result = resolver.resolve(
                "gpustack", "team-a/qwen2.5-vl-7b-instruct-awq");

        Assert.assertEquals(ModelCapabilitySource.RULE, result.getSource());
        Assert.assertEquals(Model.MODEL_TYPES[0], result.getModelType());
        Assert.assertEquals(Boolean.TRUE, result.getSupportImage());
        Assert.assertNull(result.getSupportTool());
    }

    /**
     * 验证无法识别的自定义模型保留未知能力，不误判为不支持工具。
     */
    @Test
    public void shouldKeepCapabilitiesUnknownForCustomModel() {
        ModelCapabilityResolution result = resolver.resolve("custom", "team-production-model");

        Assert.assertEquals(ModelCapabilitySource.DEFAULT, result.getSource());
        Assert.assertEquals(Model.MODEL_TYPES[0], result.getModelType());
        Assert.assertNull(result.getSupportImage());
        Assert.assertNull(result.getSupportThinking());
        Assert.assertNull(result.getSupportTool());
    }
}
