package com.easyagents.flow.core.test;

import com.easyagents.flow.core.chain.ChainDefinition;
import com.easyagents.flow.core.chain.repository.InMemoryChainStateRepository;
import com.easyagents.flow.core.chain.repository.InMemoryNodeStateRepository;
import com.easyagents.flow.core.chain.runtime.ChainExecutor;
import com.easyagents.flow.core.chain.runtime.InMemoryTriggerStore;
import com.easyagents.flow.core.chain.runtime.TriggerScheduler;
import com.easyagents.flow.core.node.StartNode;
import org.junit.Assert;
import org.junit.Test;

import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

/**
 * {@link ChainExecutor} 实例定义元数据查询测试。
 */
public class ChainExecutorInstanceMetadataTest {

    /**
     * 验证节点名称来自实例启动时可恢复的定义快照。
     */
    @Test
    public void shouldResolveNodeNamesFromInstanceDefinition() {
        ChainDefinition definition = new ChainDefinition();
        definition.setId("metadata-definition");
        StartNode start = new StartNode();
        start.setId("start");
        start.setName("开始节点");
        definition.setNodes(Collections.singletonList(start));
        definition.setEdges(Collections.emptyList());

        InMemoryChainStateRepository stateRepository =
                new InMemoryChainStateRepository();
        stateRepository.create("metadata-instance")
                .setChainDefinitionId(definition.getId());
        ScheduledExecutorService schedulerPool =
                Executors.newSingleThreadScheduledExecutor();
        ExecutorService workerPool =
                Executors.newSingleThreadExecutor();
        TriggerScheduler scheduler = new TriggerScheduler(
                new InMemoryTriggerStore(),
                schedulerPool,
                workerPool,
                1_000L);
        ChainExecutor executor = new ChainExecutor(
                ignored -> definition,
                stateRepository,
                new InMemoryNodeStateRepository(),
                scheduler);

        try {
            Map<String, String> nodeNames =
                    executor.getInstanceNodeNames(
                            "metadata-instance");

            Assert.assertEquals(
                    Map.of("start", "开始节点"),
                    nodeNames);
        } finally {
            scheduler.shutdown();
        }
    }
}
