package com.rj.service;

import com.rj.dto.VideoSynthesisRequestDto;
import com.rj.entity.VideoSynthesisLog;
import com.rj.service.IVideoSynthesisService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

import static org.junit.jupiter.api.Assertions.*;

/**
 * 视频合成临时URL功能测试类
 * 
 * @author rj
 * @date 2025-01-30
 */
@SpringBootTest
@ActiveProfiles("test")
public class VideoSynthesisTempUrlTest {

    @Autowired
    private IVideoSynthesisService videoSynthesisService;

    /**
     * 测试临时URL生成
     */
    @Test
    public void testTempUrlGeneration() {
        VideoSynthesisRequestDto request = new VideoSynthesisRequestDto();
        request.setVideoName("测试视频");
        request.setImageUrl("http://test.com/image.jpg");
        request.setAudioUrl("http://test.com/audio.mp3");
        request.setModel("liveportrait");
        
        VideoSynthesisLog result = videoSynthesisService.synthesizeVideoAndSave(request);
        
        assertNotNull(result);
        assertNotNull(result.getVideoTempUrl());
        assertTrue(result.getVideoTempUrl().contains("video/temp/"));
        assertTrue(result.getVideoTempUrl().contains("name="));
        System.out.println("临时URL生成测试通过: " + result.getVideoTempUrl());
    }

    /**
     * 测试临时URL格式
     */
    @Test
    public void testTempUrlFormat() {
        VideoSynthesisRequestDto request = new VideoSynthesisRequestDto();
        request.setVideoName("我的产品视频");
        request.setImageUrl("http://test.com/image.jpg");
        request.setAudioUrl("http://test.com/audio.mp3");
        request.setModel("liveportrait");
        
        VideoSynthesisLog result = videoSynthesisService.synthesizeVideoAndSave(request);
        
        assertNotNull(result);
        String tempUrl = result.getVideoTempUrl();
        
        // 验证URL格式
        assertTrue(tempUrl.startsWith("https://"));
        assertTrue(tempUrl.contains("/video/temp/"));
        assertTrue(tempUrl.contains("?name="));
        
        // 验证请求ID在URL中
        assertTrue(tempUrl.contains(result.getRequestId()));
        
        // 验证视频名称被编码
        assertTrue(tempUrl.contains("name="));
        
        System.out.println("临时URL格式测试通过: " + tempUrl);
    }

    /**
     * 测试中文视频名称的URL编码
     */
    @Test
    public void testChineseVideoNameEncoding() {
        VideoSynthesisRequestDto request = new VideoSynthesisRequestDto();
        request.setVideoName("我的中文视频名称");
        request.setImageUrl("http://test.com/image.jpg");
        request.setAudioUrl("http://test.com/audio.mp3");
        request.setModel("liveportrait");
        
        VideoSynthesisLog result = videoSynthesisService.synthesizeVideoAndSave(request);
        
        assertNotNull(result);
        String tempUrl = result.getVideoTempUrl();
        
        // 验证中文名称被正确编码
        assertTrue(tempUrl.contains("name="));
        assertFalse(tempUrl.contains("我的中文视频名称")); // 应该被编码
        assertTrue(tempUrl.contains("%")); // 应该包含编码字符
        
        System.out.println("中文视频名称编码测试通过: " + tempUrl);
    }

    /**
     * 测试特殊字符视频名称的URL编码
     */
    @Test
    public void testSpecialCharacterVideoNameEncoding() {
        VideoSynthesisRequestDto request = new VideoSynthesisRequestDto();
        request.setVideoName("Video with Spaces & Special Characters!@#$%");
        request.setImageUrl("http://test.com/image.jpg");
        request.setAudioUrl("http://test.com/audio.mp3");
        request.setModel("liveportrait");
        
        VideoSynthesisLog result = videoSynthesisService.synthesizeVideoAndSave(request);
        
        assertNotNull(result);
        String tempUrl = result.getVideoTempUrl();
        
        // 验证特殊字符被正确编码
        assertTrue(tempUrl.contains("name="));
        assertFalse(tempUrl.contains(" ")); // 空格应该被编码
        assertFalse(tempUrl.contains("&")); // &符号应该被编码
        assertFalse(tempUrl.contains("!")); // 感叹号应该被编码
        
        System.out.println("特殊字符视频名称编码测试通过: " + tempUrl);
    }
}

























































