package tech.easyflow.ai.node.filegeneration;

import org.junit.Assert;
import org.junit.Test;
import tech.easyflow.common.web.exceptions.BusinessException;

import java.util.List;

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

    @Test
    public void testShouldSanitizeFileNameAndOverrideExtension() {
        FileGenerationService service = new FileGenerationService(List.of(new MarkdownFileRenderer()));
        FileGenerationResult result = service.generate(new FileGenerationRequest(
                "demo",
                "plain_text",
                "md",
                "  a/b.docx  ",
                "custom"
        ));

        Assert.assertEquals("ab.md", result.getFileName());
        Assert.assertEquals("md", result.getTargetFormat());
        Assert.assertEquals("text/markdown", result.getContentType());
    }

    @Test
    public void testShouldFallbackToGeneratedFileName() {
        FileGenerationService service = new FileGenerationService(List.of(new MarkdownFileRenderer()));
        FileGenerationResult result = service.generate(new FileGenerationRequest(
                "",
                "markdown",
                "md",
                "  /  ",
                "default"
        ));

        Assert.assertTrue(result.getFileName().startsWith("generated-file-"));
        Assert.assertTrue(result.getFileName().endsWith(".md"));
        Assert.assertEquals(0, result.getSize());
    }

    @Test
    public void testShouldRejectHtmlToDocx() {
        FileGenerationService service = new FileGenerationService(List.of(new DocxFileRenderer(new MarkdownSupport())));

        BusinessException exception = Assert.assertThrows(
                BusinessException.class,
                () -> service.generate(new FileGenerationRequest("html", "html", "docx", "demo", "default"))
        );

        Assert.assertTrue(exception.getMessage().contains("html -> docx"));
    }
}
