package com.easyagents.flow.core.test;

import com.easyagents.flow.core.chain.Chain;
import com.easyagents.flow.core.chain.ChainDefinition;
import com.easyagents.flow.core.chain.Parameter;
import com.easyagents.flow.core.chain.RefType;
import com.easyagents.flow.core.node.StartNode;
import org.junit.Assert;
import org.junit.Test;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * 验证开始节点输入参数在运行时会同时写入裸参数名与 `nodeId.paramName` 别名。
 */
public class ChainStartParameterAliasTest {

    @Test
    public void shouldCreateScopedAliasFromPlainStartInput() {
        Chain chain = createChain();
        Map<String, Object> memory = new HashMap<>();
        Map<String, Object> variables = new HashMap<>();
        variables.put("user_input", "hello");

        chain.applyStartParameterAliases(memory, variables);

        Assert.assertEquals("hello", memory.get("start_1.user_input"));
        Assert.assertNull(memory.get("user_input"));
    }

    @Test
    public void shouldCreatePlainAliasFromScopedStartInput() {
        Chain chain = createChain();
        Map<String, Object> memory = new HashMap<>();
        Map<String, Object> variables = new HashMap<>();
        variables.put("start_1.user_input", "hello");

        chain.applyStartParameterAliases(memory, variables);

        Assert.assertEquals("hello", memory.get("user_input"));
        Assert.assertNull(memory.get("start_1.user_input"));
    }

    private Chain createChain() {
        ChainDefinition definition = new ChainDefinition();
        StartNode startNode = new StartNode();
        startNode.setId("start_1");

        Parameter parameter = new Parameter();
        parameter.setName("user_input");
        parameter.setRefType(RefType.INPUT);
        startNode.setParameters(java.util.Collections.singletonList(parameter));

        definition.addNode(startNode);
        definition.setEdges(Collections.emptyList());
        return new Chain(definition, "state_1");
    }
}
