package cn.novalon.gym.manage.app.integration;

import cn.novalon.gym.manage.app.ManageApplication;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.reactive.server.WebTestClient;

/**
 * 操作日志导出功能集成测试
 * 
 * 注意：此测试存在超时问题，暂时禁用。
 * TODO: 修复Excel导出的超时问题
 * 
 * @author 张翔
 * @date 2026-04-03
 */
@Disabled("暂时禁用：Excel导出功能存在超时问题，需要优化")
@SpringBootTest(
    classes = ManageApplication.class,
    webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT
)
@ActiveProfiles("test")
class OperationLogExportIntegrationTest {

    @Autowired
    private WebTestClient webTestClient;

    @Test
    @WithMockUser(username = "admin", roles = {"ADMIN"})
    void testExportOperationLogs_ShouldReturnExcelFile() {
        webTestClient.get()
                .uri("/api/logs/operation/export")
                .accept(MediaType.APPLICATION_OCTET_STREAM)
                .exchange()
                .expectStatus().isOk()
                .expectHeader().contentType(MediaType.APPLICATION_OCTET_STREAM)
                .expectHeader().valueMatches("Content-Disposition", "attachment; filename=\"operation_logs_.*\\.xlsx\"")
                .expectBody(byte[].class)
                .value(bytes -> {
                    assert bytes != null;
                    assert bytes.length > 0;
                    assert bytes[0] == 0x50;
                    assert bytes[1] == 0x4B;
                });
    }

    @Test
    @WithMockUser(username = "admin", roles = {"ADMIN"})
    void testExportOperationLogsWithKeyword_ShouldReturnFilteredExcel() {
        webTestClient.get()
                .uri(uriBuilder -> uriBuilder
                        .path("/api/logs/operation/export")
                        .queryParam("keyword", "test")
                        .build())
                .accept(MediaType.APPLICATION_OCTET_STREAM)
                .exchange()
                .expectStatus().isOk()
                .expectHeader().contentType(MediaType.APPLICATION_OCTET_STREAM)
                .expectBody(byte[].class)
                .value(bytes -> {
                    assert bytes != null;
                    assert bytes.length > 0;
                });
    }
}
