package tech.easyflow.agent.config;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource;
import org.springframework.util.unit.DataSize;

import java.time.Duration;
import java.util.Map;

/**
 * {@link AgentDocumentProperties} 默认值、局部绑定与约束测试。
 */
public class AgentDocumentPropertiesTest {

    /**
     * 验证未显式配置时全部 SPEC 默认值可直接使用。
     */
    @Test
    public void shouldProvideSpecDefaults() {
        AgentDocumentProperties properties = new AgentDocumentProperties();

        Assert.assertTrue(properties.isEnabled());
        Assert.assertEquals(3, properties.getMaxDocumentsPerTurn());
        Assert.assertEquals(DataSize.ofMegabytes(30), properties.getMaxTotalBytesPerTurn());
        Assert.assertEquals(Duration.ofSeconds(30), properties.getReadTimeout());
        Assert.assertEquals(Duration.ofHours(24), properties.getTempRetention());
        Assert.assertEquals(2, properties.getReader().getCoreSize());
        Assert.assertEquals(4, properties.getReader().getMaxSize());
        Assert.assertEquals(32, properties.getReader().getQueueCapacity());
        Assert.assertEquals(DataSize.ofMegabytes(20), properties.getLimits().getOfficeMaxBytes());
        Assert.assertEquals(DataSize.ofMegabytes(10), properties.getLimits().getExcelMaxBytes());
        Assert.assertEquals(DataSize.ofMegabytes(5), properties.getLimits().getTextMaxBytes());
        Assert.assertEquals(200, properties.getLimits().getMaxPdfPages());
        Assert.assertEquals(200, properties.getLimits().getMaxSlides());
        Assert.assertEquals(20, properties.getLimits().getMaxSheets());
        Assert.assertEquals(50_000, properties.getLimits().getMaxNonEmptyCells());
        Assert.assertEquals(DataSize.ofMegabytes(150), properties.getLimits().getMaxExpandedBytes());
    }

    /**
     * 验证局部覆盖配置时未指定项仍保留代码默认值。
     */
    @Test
    public void shouldKeepDefaultsWhenSingleValueIsOverridden() {
        AgentDocumentProperties properties = new AgentDocumentProperties();
        MapConfigurationPropertySource source = new MapConfigurationPropertySource(Map.of(
                "easyflow.agent.document.max-documents-per-turn", "5"));

        new Binder(source).bind(
                "easyflow.agent.document",
                Bindable.ofInstance(properties));

        Assert.assertEquals(5, properties.getMaxDocumentsPerTurn());
        Assert.assertEquals(DataSize.ofMegabytes(30), properties.getMaxTotalBytesPerTurn());
        Assert.assertEquals(2, properties.getReader().getCoreSize());
        Assert.assertEquals(4, properties.getReader().getMaxSize());
        Assert.assertEquals(32, properties.getReader().getQueueCapacity());
        Assert.assertEquals(DataSize.ofMegabytes(20), properties.getLimits().getOfficeMaxBytes());
    }

    /**
     * 验证最大线程数小于核心线程数时配置约束会明确失败。
     */
    @Test
    public void shouldRejectInvalidReaderRange() {
        AgentDocumentProperties properties = new AgentDocumentProperties();
        properties.getReader().setCoreSize(4);
        properties.getReader().setMaxSize(2);

        Assert.assertFalse(properties.getReader().isThreadRangeValid());
    }
}
