package com.easyagents.skill.model;

import com.easyagents.skill.exception.SkillValidationException;
import org.junit.Assert;
import org.junit.Test;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * SkillMetadata 防御性复制与键约束测试。
 */
public class SkillMetadataTest {

    /**
     * 嵌套 Map/List 在读写边界上执行防御性复制。
     */
    @Test
    @SuppressWarnings("unchecked")
    public void deeplyCopiesNestedValues() {
        LinkedHashMap<String, Object> nested = new LinkedHashMap<>();
        nested.put("items", new java.util.ArrayList<>(List.of("a")));
        SkillMetadata metadata = new SkillMetadata(Map.of("nested", nested));

        Map<String, Object> read = metadata.getValues();
        ((Map<String, Object>) read.get("nested")).put("changed", true);

        Assert.assertNull(((Map<?, ?>) metadata.get("nested")).get("changed"));
    }

    /**
     * 空白键写入会显式失败。
     */
    @Test(expected = SkillValidationException.class)
    public void rejectBlankKey() {
        new SkillMetadata().put(" ", "value");
    }

    /**
     * 嵌套非字符串键会显式失败且不会静默转换。
     */
    @Test(expected = SkillValidationException.class)
    @SuppressWarnings({"rawtypes", "unchecked"})
    public void rejectNestedNonStringKey() {
        Map nested = new LinkedHashMap();
        nested.put(1, "number");
        new SkillMetadata(Map.of("nested", nested));
    }
}
