package tech.easyflow.publicapi.controller;

import jakarta.servlet.http.HttpServletRequest;
import org.junit.Assert;
import org.junit.Test;
import tech.easyflow.ai.documentimport.PublicDocumentImportDtos;
import tech.easyflow.common.web.exceptions.BusinessException;
import tech.easyflow.publicapi.interceptor.PublicApiInterceptor;
import tech.easyflow.system.entity.SysApiKey;

import java.lang.reflect.Proxy;
import java.math.BigInteger;

/**
 * {@link PublicKnowledgeDocumentImportController} 请求边界测试。
 *
 * @author Codex
 * @since 2026-08-02
 */
public class PublicKnowledgeDocumentImportControllerTest {

    /**
     * 验证重试请求缺少 taskId 时返回明确参数错误。
     */
    @Test
    public void retryShouldRejectMissingTaskId() {
        SysApiKey token = new SysApiKey();
        token.setId(BigInteger.ONE);
        HttpServletRequest servletRequest = (HttpServletRequest) Proxy.newProxyInstance(
            HttpServletRequest.class.getClassLoader(),
            new Class<?>[]{HttpServletRequest.class},
            (instance, method, args) -> {
                if ("getAttribute".equals(method.getName())) {
                    Assert.assertEquals(
                        PublicApiInterceptor.AUTHENTICATED_API_KEY_ATTRIBUTE,
                        args[0]
                    );
                    return token;
                }
                throw new AssertionError(
                    "测试路径不应调用 HttpServletRequest." + method.getName()
                );
            }
        );
        PublicKnowledgeDocumentImportController controller =
            new PublicKnowledgeDocumentImportController(
                null,
                null,
                null,
                null,
                null
            );
        PublicDocumentImportDtos.RetryRequest request =
            new PublicDocumentImportDtos.RetryRequest();

        try {
            controller.retry("test-key", request, servletRequest);
            Assert.fail("Expected missing taskId rejection");
        } catch (BusinessException expected) {
            Assert.assertEquals(400, expected.getHttpStatus());
            Assert.assertTrue(expected.getMessage().contains("taskId 不能为空"));
        }
    }
}
