package tech.easyflow.ai.service.impl;

import org.junit.Assert;
import org.junit.Test;
import tech.easyflow.ai.entity.Model;
import tech.easyflow.ai.service.capability.ModelCapabilityResolution;
import tech.easyflow.ai.service.capability.ModelCapabilitySource;

import java.lang.reflect.Method;

/**
 * 模型能力自动识别与手动覆盖合并规则测试。
 */
public class ModelServiceImplCapabilityOverrideTest {

    /**
     * 验证用户明确关闭的能力不会被模型库重新打开。
     *
     * @throws ReflectiveOperationException 无法调用待测试方法时抛出
     */
    @Test
    public void shouldPreserveExplicitCapabilityOverrides() throws ReflectiveOperationException {
        Model model = new Model();
        model.setSupportImage(Boolean.FALSE);
        model.setSupportThinking(Boolean.FALSE);
        model.setSupportTool(Boolean.FALSE);

        applyResolvedCapabilities(model, detectedCapabilities());

        Assert.assertEquals(Boolean.FALSE, model.getSupportImage());
        Assert.assertEquals(Boolean.FALSE, model.getSupportThinking());
        Assert.assertEquals(Boolean.FALSE, model.getSupportTool());
    }

    /**
     * 验证空能力值会由模型库自动补齐。
     *
     * @throws ReflectiveOperationException 无法调用待测试方法时抛出
     */
    @Test
    public void shouldFillCapabilitiesWhenNotConfigured() throws ReflectiveOperationException {
        Model model = new Model();

        applyResolvedCapabilities(model, detectedCapabilities());

        Assert.assertEquals(Boolean.TRUE, model.getSupportImage());
        Assert.assertEquals(Boolean.TRUE, model.getSupportThinking());
        Assert.assertEquals(Boolean.TRUE, model.getSupportTool());
    }

    /**
     * 创建模型库已确认的对话能力。
     *
     * @return 全部开启的模型能力
     */
    private ModelCapabilityResolution detectedCapabilities() {
        return new ModelCapabilityResolution(
                Model.MODEL_TYPES[0],
                Boolean.TRUE,
                Boolean.TRUE,
                Boolean.TRUE,
                ModelCapabilitySource.CATALOG);
    }

    /**
     * 调用服务内部的能力合并逻辑。
     *
     * @param model 待合并模型
     * @param resolution 自动识别结果
     * @throws ReflectiveOperationException 无法调用待测试方法时抛出
     */
    private void applyResolvedCapabilities(Model model, ModelCapabilityResolution resolution)
            throws ReflectiveOperationException {
        ModelServiceImpl service = new ModelServiceImpl();
        Method method = ModelServiceImpl.class.getDeclaredMethod(
                "applyResolvedChatCapabilities", Model.class, ModelCapabilityResolution.class);
        method.setAccessible(true);
        method.invoke(service, model, resolution);
    }
}
