package tech.easyflow.ai.documentimport.task;

import org.junit.Test;
import org.mockito.Mockito;

/**
 * {@link DocumentImportPendingTaskMonitor} 调度退避回归测试。
 *
 * @author Codex
 * @since 2026-08-07
 */
public class DocumentImportPendingTaskMonitorTest {

    /**
     * 验证任务投递异常后立即进入冷却期，避免短周期重复扫描。
     */
    @Test
    public void dispatchFailureShouldEnterCooldown() {
        KnowledgeDocumentImportTaskAppService appService =
            Mockito.mock(KnowledgeDocumentImportTaskAppService.class);
        DocumentImportBatchAppService batchAppService =
            Mockito.mock(DocumentImportBatchAppService.class);
        Mockito.doThrow(new IllegalStateException("Redis unavailable"))
            .when(appService)
            .dispatchPendingTasks();
        DocumentImportPendingTaskMonitor monitor =
            new DocumentImportPendingTaskMonitor(appService, batchAppService);

        monitor.dispatchPendingTasks();
        monitor.dispatchPendingTasks();

        Mockito.verify(batchAppService, Mockito.times(1))
            .recoverPendingBatchRetries();
        Mockito.verify(appService, Mockito.times(1)).dispatchPendingTasks();
    }
}
