package com.agent.management.engine;

import org.junit.jupiter.api.Test;

import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

class TemplateRendererTest {

    @Test
    void rendersFlatVariable() {
        String result = TemplateRenderer.render("Hello {{name}}", Map.of("name", "World"));
        assertThat(result).isEqualTo("Hello World");
    }

    @Test
    void rendersNodeScopedVariableWithDotSyntax() {
        Map<String, Object> vars = Map.of(
                "nodeA__result", "value-from-A"
        );
        assertThat(TemplateRenderer.render("{{nodeA.result}}", vars)).isEqualTo("value-from-A");
    }

    @Test
    void rendersNestedScopedVariable() {
        Map<String, Object> vars = Map.of(
                "nodeA__data", Map.of("total", 42)
        );
        assertThat(TemplateRenderer.render("{{nodeA.data.total}}", vars)).isEqualTo("42");
    }

    @Test
    void keepsMissingPlaceholder() {
        assertThat(TemplateRenderer.render("{{missing}}", Map.of())).isEqualTo("{{missing}}");
    }

    @Test
    void returnsEmptyForNullOrEmptyTemplate() {
        assertThat(TemplateRenderer.render(null, Map.of("x", 1))).isEmpty();
        assertThat(TemplateRenderer.render("", Map.of("x", 1))).isEmpty();
    }

    @Test
    void supportsLegacyDoubleUnderscoreKey() {
        Map<String, Object> vars = Map.of("node_1__output", 99);
        assertThat(TemplateRenderer.render("{{node_1__output}}", vars)).isEqualTo("99");
    }
}
