package tech.easyflow.agent.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Assert;
import org.junit.Test;
import tech.easyflow.agent.vo.AgentResourceOptionsView;
import tech.easyflow.skill.entity.Skill;

import java.lang.reflect.Method;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

/**
 * Agent 设计器安全资源选项测试。
 */
public class AgentOptionQueryServiceTest {

    /**
     * Skill 候选必须使用已发布展示字段，并按发布资源真实统计文本与二进制摘要。
     *
     * @throws Exception 反射调用失败时抛出
     */
    @Test
    public void skillOptionShouldUsePublishedMetadataAndResourceKinds() throws Exception {
        AgentOptionQueryService service = new AgentOptionQueryService(
                null, null, null, null, null, null, null, null,
                null, null, null, new ObjectMapper());
        Skill skill = new Skill();
        skill.setId(BigInteger.ONE);
        skill.setDisplayName("未发布草稿名称");
        skill.setDescription("未发布草稿描述");
        skill.setVisibilityScope("ALL");
        skill.setSnapshotHash("aggregate-hash");
        skill.setPublishedSnapshotJson(Map.of(
                "displayName", "线上名称",
                "description", "线上描述",
                "visibilityScope", "PRIVATE",
                "skillContent", "主",
                "resources", List.of(
                        Map.of("path", "references/a.md", "text", true, "textContent", "参考"),
                        Map.of("path", "assets/a.bin", "text", false, "contentRef", "sha256:a"))));
        skill.setPublishedToolBindingsJson(Map.of(
                "bindings", List.of(Map.of("toolCount", 3))));

        Method method = AgentOptionQueryService.class.getDeclaredMethod("toSkillOption", Skill.class);
        method.setAccessible(true);
        AgentResourceOptionsView.SkillOption option =
                (AgentResourceOptionsView.SkillOption) method.invoke(service, skill);

        Assert.assertEquals("线上名称", option.displayName());
        Assert.assertEquals("线上描述", option.description());
        Assert.assertEquals("PRIVATE", option.visibilityScope());
        Assert.assertEquals(3, option.toolCount());
        Assert.assertEquals(1, option.textResourceCount());
        Assert.assertEquals(1, option.binaryResourceCount());
        Assert.assertEquals(("主" + "参考").getBytes(StandardCharsets.UTF_8).length, option.textBytes());
    }
}
