package com.easyagents.document.core.async;

import com.easyagents.document.core.entity.ParseResponse;
import com.easyagents.document.core.entity.ParseResult;
import com.easyagents.document.core.entity.ParseTaskInfo;
import com.easyagents.document.core.entity.ParseTaskStatus;
import org.junit.Assert;
import org.junit.Test;

import java.util.Collections;
import java.util.concurrent.Executor;

/**
 * 异步任务管理器测试。
 *
 * @author Codex
 * @since 2026-04-16
 */
public class DocumentAsyncTaskManagerTest {

    @Test
    public void shouldTrackTaskLifecycleAndResult() {
        Executor directExecutor = new Executor() {
            @Override
            public void execute(Runnable command) {
                command.run();
            }
        };
        DocumentAsyncTaskManager manager = new DocumentAsyncTaskManager(new InMemoryDocumentAsyncTaskRepository(), directExecutor);

        ParseTaskStatus status = manager.submit("mineru", Collections.singletonList("demo.pptx"), updater -> {
            updater.update("ocr", 50, 1, 2, "处理中");
            ParseResponse response = new ParseResponse();
            ParseResult result = new ParseResult();
            result.setFileName("demo.pptx");
            result.setMarkdown("# Slide 1");
            response.setResults(Collections.singletonList(result));
            return response;
        });

        ParseTaskInfo taskInfo = manager.queryTaskInfo(status.getTaskId());

        Assert.assertEquals("completed", taskInfo.getStatus());
        Assert.assertEquals(Integer.valueOf(100), taskInfo.getProgressPercent());
        Assert.assertEquals("completed", taskInfo.getCurrentStage());
        Assert.assertNotNull(taskInfo.getResult());
        Assert.assertEquals(1, taskInfo.getResult().getResults().size());
    }
}
