package tech.easyflow.ai.document.support;

import com.easyagents.document.core.entity.ParseRequest;
import com.easyagents.document.core.entity.PdfParseRequest;
import com.easyagents.document.core.entity.PptxParseRequest;
import com.easyagents.document.core.entity.XlsxParseRequest;
import org.junit.Assert;
import org.junit.Test;
import tech.easyflow.ai.document.model.DocumentParseScenario;

/**
 * {@link DocumentParseRequestFactory} 单元测试。
 *
 * @author Codex
 * @since 2026-04-14
 */
public class DocumentParseRequestFactoryTest {

    /**
     * 验证工作流文本场景只请求最小文本结果。
     */
    @Test
    public void shouldBuildWorkflowTextScenarioRequest() {
        DocumentParseRequestFactory factory = new DocumentParseRequestFactory();

        ParseRequest request = factory.build(buildSource(), DocumentParseScenario.WORKFLOW_TEXT);

        Assert.assertTrue(request.getReturnMarkdown());
        Assert.assertFalse(request.getReturnMiddleJson());
        Assert.assertFalse(request.getReturnContentList());
        Assert.assertFalse(request.getReturnImages());
        Assert.assertTrue(request instanceof PdfParseRequest);
    }

    /**
     * 验证知识库导入场景保留结构化工件。
     */
    @Test
    public void shouldBuildKnowledgeImportScenarioRequest() {
        DocumentParseRequestFactory factory = new DocumentParseRequestFactory();

        ParseRequest request = factory.build(buildSource(), DocumentParseScenario.KNOWLEDGE_IMPORT);

        Assert.assertTrue(request.getReturnMarkdown());
        Assert.assertTrue(request.getReturnMiddleJson());
        Assert.assertTrue(request.getReturnContentList());
        Assert.assertTrue(request.getReturnImages());
        Assert.assertTrue(request instanceof PdfParseRequest);
    }

    /**
     * 验证 PPTX / XLSX 会构建对应的强类型请求。
     */
    @Test
    public void shouldBuildOfficeTypedRequests() {
        DocumentParseRequestFactory factory = new DocumentParseRequestFactory();

        ParseRequest pptxRequest = factory.build(buildSource("slides.pptx",
            "application/vnd.openxmlformats-officedocument.presentationml.presentation"), DocumentParseScenario.KNOWLEDGE_IMPORT);
        ParseRequest xlsxRequest = factory.build(buildSource("table.xlsx",
            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"), DocumentParseScenario.KNOWLEDGE_IMPORT);

        Assert.assertTrue(pptxRequest instanceof PptxParseRequest);
        Assert.assertTrue(xlsxRequest instanceof XlsxParseRequest);
    }

    private LoadedDocumentSource buildSource() {
        return buildSource("demo.pdf", "application/pdf");
    }

    private LoadedDocumentSource buildSource(String fileName, String contentType) {
        LoadedDocumentSource source = new LoadedDocumentSource();
        source.setFileName(fileName);
        source.setContentType(contentType);
        source.setContentBytes("pdf-data".getBytes());
        source.setSize(8L);
        return source;
    }
}
