package tech.easyflow.ai.node;

import com.alibaba.fastjson.JSONObject;
import com.easyagents.flow.core.node.BaseNode;
import org.junit.Assert;
import org.junit.Test;
import tech.easyflow.common.web.exceptions.BusinessException;

/**
 * {@link MakeFileNodeParser} 单元测试。
 *
 * @author Codex
 * @since 2026-04-18
 */
public class MakeFileNodeParserTest {

    @Test
    public void testShouldUseDefaults() {
        MakeFileNodeParser parser = new MakeFileNodeParser();
        MakeFileNode node = (MakeFileNode) parser.doParse(new JSONObject(), new JSONObject(), new JSONObject());

        Assert.assertEquals("docx", node.getTargetFormat());
        Assert.assertEquals("markdown", node.getSourceFormat());
        Assert.assertEquals("default", node.getTemplateStyle());
        Assert.assertNull(node.getFileName());
    }

    @Test
    public void testShouldPreferTargetFormatAndTrimFileName() {
        MakeFileNodeParser parser = new MakeFileNodeParser();
        JSONObject data = new JSONObject();
        data.put("targetFormat", "pdf");
        data.put("sourceFormat", "markdown");
        data.put("fileName", "  导出报告  ");
        data.put("templateStyle", "custom");

        BaseNode parsed = parser.doParse(new JSONObject(), data, new JSONObject());
        MakeFileNode node = (MakeFileNode) parsed;
        Assert.assertEquals("pdf", node.getTargetFormat());
        Assert.assertEquals("markdown", node.getSourceFormat());
        Assert.assertEquals("导出报告", node.getFileName());
        Assert.assertEquals("default", node.getTemplateStyle());
    }

    @Test
    public void testShouldRejectIllegalCombination() {
        MakeFileNodeParser parser = new MakeFileNodeParser();
        JSONObject data = new JSONObject();
        data.put("sourceFormat", "html");
        data.put("targetFormat", "md");

        BusinessException exception = Assert.assertThrows(
                BusinessException.class,
                () -> parser.doParse(new JSONObject(), data, new JSONObject())
        );
        Assert.assertTrue(exception.getMessage().contains("html -> md"));
    }
}
