Files
smartDriveEE/src/test/java/com/rj/service/SiliconFlowTtsServiceTest.java
2025-10-04 19:52:06 +08:00

79 lines
2.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.rj.service;
import com.rj.config.SiliconFlowConfig;
import com.rj.dto.SiliconFlowTtsRequest;
import com.rj.dto.SiliconFlowTtsResponse;
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 static org.junit.jupiter.api.Assertions.*;
/**
* SiliconFlow TTS 服务测试类
*
* @author rj
* @date 2025-01-02
*/
@SpringBootTest
@TestPropertySource(properties = {
"siliconflow.enabled=true",
"siliconflow.api-key=test-api-key",
"siliconflow.base-url=https://api.siliconflow.cn/v1"
})
public class SiliconFlowTtsServiceTest {
@Autowired
private SiliconFlowTtsService ttsService;
@Autowired
private SiliconFlowConfig config;
@Test
public void testServiceConfiguration() {
assertNotNull(config);
assertTrue(config.isEnabled());
assertEquals("https://api.siliconflow.cn/v1", config.getBaseUrl());
assertEquals("fnlp/MOSS-TTSD-v0.5", config.getDefaultTtsModel());
}
@Test
public void testServiceAvailability() {
// 注意这个测试需要真实的API Key才能通过
// 在测试环境中,我们主要测试配置是否正确
assertNotNull(ttsService);
}
@Test
public void testTtsRequestCreation() {
SiliconFlowTtsRequest request = new SiliconFlowTtsRequest();
request.setModel("fnlp/MOSS-TTSD-v0.5");
request.setInput("[S1]Hello, how are you today?[S2]I'm doing great, thanks for asking![S1]That's wonderful to hear");
// voice参数有默认值但也可以手动设置
assertNotNull(request);
assertEquals("fnlp/MOSS-TTSD-v0.5", request.getModel());
assertNotNull(request.getInput());
assertEquals("fnlp/MOSS-TTSD-v0.5:claire", request.getVoice()); // 验证默认voice参数
}
@Test
public void testTtsResponseCreation() {
SiliconFlowTtsResponse successResponse = SiliconFlowTtsResponse.success(
"base64AudioData", "wav", 22050, 5.5
);
assertTrue(successResponse.isSuccess());
assertEquals("base64AudioData", successResponse.getAudio());
assertEquals("wav", successResponse.getFormat());
assertEquals(22050, successResponse.getSampleRate());
assertEquals(5.5, successResponse.getDuration());
SiliconFlowTtsResponse errorResponse = SiliconFlowTtsResponse.error("API调用失败");
assertFalse(errorResponse.isSuccess());
assertEquals("API调用失败", errorResponse.getMessage());
}
}