package com.rj.controller;

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.mock.web.MockMultipartFile;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

/**
 * 音频上传功能测试类
 * 
 * @author rj
 * @date 2025-01-30
 */
@SpringBootTest
@ActiveProfiles("test")
public class AudioUploadTest {

    @Autowired
    private WebApplicationContext webApplicationContext;

    @Test
    public void testAudioUpload() throws Exception {
        MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
        
        // 创建模拟音频文件
        MockMultipartFile file = new MockMultipartFile(
            "file", 
            "test-audio.mp3", 
            "audio/mpeg", 
            "fake audio content".getBytes()
        );
        
        // 执行上传请求
        mockMvc.perform(MockMvcRequestBuilders.multipart("/api/tts/upload-audio")
                .file(file)
                .param("audioName", "测试音频文件")
                .param("creatorName", "张三")
                .param("creatorPhone", "13800138000")
                .contentType(MediaType.MULTIPART_FORM_DATA))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$.status").value("SUCCESS"))
                .andExpect(MockMvcResultMatchers.jsonPath("$.audioName").value("测试音频文件"))
                .andExpect(MockMvcResultMatchers.jsonPath("$.creatorName").value("张三"))
                .andExpect(MockMvcResultMatchers.jsonPath("$.creatorPhone").value("13800138000"));
    }

    @Test
    public void testAudioUploadWithInvalidFile() throws Exception {
        MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
        
        // 创建模拟非音频文件
        MockMultipartFile file = new MockMultipartFile(
            "file", 
            "test.txt", 
            "text/plain", 
            "fake text content".getBytes()
        );
        
        // 执行上传请求，应该返回400错误
        mockMvc.perform(MockMvcRequestBuilders.multipart("/api/tts/upload-audio")
                .file(file)
                .param("audioName", "测试文件")
                .param("creatorName", "张三")
                .param("creatorPhone", "13800138000")
                .contentType(MediaType.MULTIPART_FORM_DATA))
                .andExpect(MockMvcResultMatchers.status().isBadRequest());
    }

    @Test
    public void testAudioUploadWithEmptyFile() throws Exception {
        MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
        
        // 创建空文件
        MockMultipartFile file = new MockMultipartFile(
            "file", 
            "empty.mp3", 
            "audio/mpeg", 
            new byte[0]
        );
        
        // 执行上传请求，应该返回400错误
        mockMvc.perform(MockMvcRequestBuilders.multipart("/api/tts/upload-audio")
                .file(file)
                .param("audioName", "空文件")
                .param("creatorName", "张三")
                .param("creatorPhone", "13800138000")
                .contentType(MediaType.MULTIPART_FORM_DATA))
                .andExpect(MockMvcResultMatchers.status().isBadRequest());
    }
}
