86 lines
2.8 KiB
Java
86 lines
2.8 KiB
Java
package com.rj.audio;
|
||
|
||
import com.rj.dto.SiliconFlowTtsRequest;
|
||
import com.rj.dto.SiliconFlowTtsResponse;
|
||
import com.rj.service.SiliconFlowTtsService;
|
||
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.TestPropertySource;
|
||
|
||
import java.util.Base64;
|
||
|
||
import static org.junit.jupiter.api.Assertions.*;
|
||
|
||
/**
|
||
* 文本转语音测试类
|
||
*
|
||
* @author rj
|
||
* @date 2025-01-02
|
||
*/
|
||
@SpringBootTest
|
||
@TestPropertySource(properties = {
|
||
"siliconflow.enabled=true",
|
||
"siliconflow.api-key=${SILICONFLOW_API_KEY:test-key}",
|
||
"siliconflow.base-url=https://api.siliconflow.cn/v1"
|
||
})
|
||
public class TextToSpeechTest {
|
||
|
||
@Autowired
|
||
private SiliconFlowTtsService ttsService;
|
||
|
||
@Test
|
||
public void testAudioDurationCalculation() {
|
||
// 测试音频时长计算
|
||
SiliconFlowTtsService service = new SiliconFlowTtsService();
|
||
|
||
// 模拟不同大小的音频文件
|
||
long smallAudio = 50000; // 50KB
|
||
long mediumAudio = 200000; // 200KB
|
||
long largeAudio = 500000; // 500KB
|
||
|
||
// 注意:由于calculateAudioDuration是private方法,我们通过反射或公共方法测试
|
||
// 这里我们主要测试服务的基本功能
|
||
assertNotNull(ttsService);
|
||
}
|
||
|
||
@Test
|
||
public void testTtsRequestWithVoice() {
|
||
SiliconFlowTtsRequest request = new SiliconFlowTtsRequest();
|
||
request.setModel("fnlp/MOSS-TTSD-v0.5");
|
||
request.setInput("Hello, this is a test message.");
|
||
request.setVoice("fnlp/MOSS-TTSD-v0.5:claire");
|
||
|
||
assertNotNull(request);
|
||
assertEquals("fnlp/MOSS-TTSD-v0.5", request.getModel());
|
||
assertEquals("fnlp/MOSS-TTSD-v0.5:claire", request.getVoice());
|
||
assertNotNull(request.getInput());
|
||
}
|
||
|
||
@Test
|
||
public void testBase64Encoding() {
|
||
// 测试Base64编码/解码
|
||
String testText = "Hello World";
|
||
String encoded = Base64.getEncoder().encodeToString(testText.getBytes());
|
||
String decoded = new String(Base64.getDecoder().decode(encoded));
|
||
|
||
assertEquals(testText, decoded);
|
||
assertNotNull(encoded);
|
||
assertTrue(encoded.length() > 0);
|
||
}
|
||
|
||
@Test
|
||
public void testTtsResponseCreation() {
|
||
// 测试TTS响应创建
|
||
String mockAudioData = "mockAudioBase64Data";
|
||
SiliconFlowTtsResponse response = SiliconFlowTtsResponse.success(
|
||
mockAudioData, "mp3", 44100, 5.5
|
||
);
|
||
|
||
assertTrue(response.isSuccess());
|
||
assertEquals(mockAudioData, response.getAudio());
|
||
assertEquals("mp3", response.getFormat());
|
||
assertEquals(44100, response.getSampleRate());
|
||
assertEquals(5.5, response.getDuration());
|
||
}
|
||
} |