package tech.easyflow.ai.easyagentsflow.repository;

import com.easyagents.flow.core.chain.NodeState;
import com.easyagents.flow.core.chain.repository.NodeStateField;
import org.junit.Assert;
import org.junit.Test;
import tech.easyflow.ai.easyagentsflow.support.WorkflowExecutionStepKey;
import tech.easyflow.common.cache.VersionedFields;

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

/**
 * 节点字段化状态编码回归测试。
 */
public class WorkflowStateFieldsNodeTest {

    /**
     * 验证节点生命周期业务尝试键可跨 Redis 字段快照恢复。
     */
    @Test
    public void shouldPreserveExecutionAttemptKey() {
        NodeState state = new NodeState();
        state.setNodeId("loop");
        state.setChainInstanceId("instance");
        state.setExecutionAttemptKey(
                "instance:loop:trigger");
        state.setVersion(7L);

        Map<String, Object> encoded =
                new LinkedHashMap<>(
                        WorkflowStateFields
                                .allNodeFields(state));
        NodeState decoded =
                WorkflowStateFields.decodeNode(
                        new VersionedFields(
                                7L,
                                encoded));

        Assert.assertEquals(
                "instance:loop:trigger",
                decoded
                        .getExecutionAttemptKey());
        Assert.assertEquals(
                7L, decoded.getVersion());
    }

    /**
     * 验证升级前在途节点沿用 memory.executeId，避免结束审计关联到新键。
     */
    @Test
    public void shouldRestoreLegacyExecutionKey() {
        NodeState legacyState = new NodeState();
        legacyState.setNodeId("loop");
        legacyState.setChainInstanceId(
                "instance");
        legacyState.getMemory().put(
                "executeId",
                "legacy-step-key");

        Map<String, Object> encoded =
                new LinkedHashMap<>(
                        WorkflowStateFields
                                .allNodeFields(
                                        legacyState));
        encoded.remove(
                NodeStateField
                        .EXECUTION_ATTEMPT_KEY
                        .name());

        NodeState decoded =
                WorkflowStateFields.decodeNode(
                        new VersionedFields(
                                3L,
                                encoded));

        Assert.assertEquals(
                "legacy-step-key",
                WorkflowExecutionStepKey.resolve(
                        decoded
                                .getExecutionAttemptKey()));
    }

    /**
     * 验证旧对象快照同样补齐最终执行键。
     */
    @Test
    public void shouldNormalizeLegacyObjectState() {
        NodeState legacyState = new NodeState();
        legacyState.getMemory().put(
                "executeId",
                "legacy-object-step");

        WorkflowStateFields.normalizeNode(
                legacyState);

        Assert.assertEquals(
                "legacy-object-step",
                WorkflowExecutionStepKey.resolve(
                        legacyState
                                .getExecutionAttemptKey()));
    }
}
