|
|
@@ -0,0 +1,207 @@
|
|
|
+package com.zsjz.ai.module.ai.core.agent;
|
|
|
+
|
|
|
+import io.agentscope.core.model.GenerateOptions;
|
|
|
+import io.agentscope.core.model.Model;
|
|
|
+import com.zsjz.ai.module.ai.domain.AiExpertAgent;
|
|
|
+import com.zsjz.ai.module.ai.domain.AiModelConfig;
|
|
|
+import com.zsjz.ai.module.ai.mapper.AiModelConfigMapper;
|
|
|
+import com.zsjz.ai.module.ai.service.IAiModelConfigService;
|
|
|
+import com.zsjz.ai.common.compat.ServiceException;
|
|
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNull;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertSame;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * {@link AgentModelFactory} 单元测试:模型槽位解析与模型构造(对齐上游 a20d2f3d)。
|
|
|
+ *
|
|
|
+ * <p>覆盖压缩摘要模型的回退与思考关闭、上下文窗口写入模型实例,以及槽位与校验逻辑。</p>
|
|
|
+ *
|
|
|
+ * @author BizBuddy
|
|
|
+ */
|
|
|
+@DisplayName("智能体模型构造工厂")
|
|
|
+class AgentModelFactoryTest {
|
|
|
+
|
|
|
+ private final AiModelConfigMapper mapper = mock(AiModelConfigMapper.class);
|
|
|
+ private final IAiModelConfigService modelConfigService = mock(IAiModelConfigService.class);
|
|
|
+ private final AgentModelFactory factory = new AgentModelFactory(mapper, modelConfigService);
|
|
|
+
|
|
|
+ private static AiModelConfig config(Long id, String code, String baseUrl, String status) {
|
|
|
+ AiModelConfig config = new AiModelConfig();
|
|
|
+ config.setId(id);
|
|
|
+ config.setModelCode(code);
|
|
|
+ config.setBaseUrl(baseUrl);
|
|
|
+ config.setProvider(null);
|
|
|
+ config.setStatus(status);
|
|
|
+ return config;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static AiExpertAgent definition() {
|
|
|
+ AiExpertAgent agent = new AiExpertAgent();
|
|
|
+ agent.setAgentCode("a1");
|
|
|
+ agent.setModelId(1L);
|
|
|
+ return agent;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("摘要模型:槽位未配置或与主模型相同时回退主模型配置")
|
|
|
+ void shouldFallbackToMainConfigForSummary() {
|
|
|
+ AiExpertAgent def = definition();
|
|
|
+ when(mapper.selectById(1L)).thenReturn(config(1L, "deepseek-v4.1-flash", "https://x", "0"));
|
|
|
+ when(modelConfigService.decryptApiKey(1L)).thenReturn("sk");
|
|
|
+
|
|
|
+ def.setSummaryModelId(null);
|
|
|
+ assertNotNull(factory.resolveSummaryModel(def));
|
|
|
+ def.setSummaryModelId(1L);
|
|
|
+ assertNotNull(factory.resolveSummaryModel(def));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("摘要模型:槽位不可用时回退主模型配置")
|
|
|
+ void shouldFallbackToMainWhenSummarySlotUnusable() {
|
|
|
+ AiExpertAgent def = definition();
|
|
|
+ def.setSummaryModelId(2L);
|
|
|
+ when(mapper.selectById(2L)).thenReturn(null);
|
|
|
+ when(mapper.selectById(1L)).thenReturn(config(1L, "deepseek-v4.1-flash", "https://x", "0"));
|
|
|
+ when(modelConfigService.decryptApiKey(1L)).thenReturn("sk");
|
|
|
+ assertNotNull(factory.resolveSummaryModel(def));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("摘要调用:DashScope 兼容端点关闭思考")
|
|
|
+ void shouldDisableThinkingForDashScopeSummary() {
|
|
|
+ AiModelConfig cfg = config(9L, "deepseek-v4.1-flash", "https://dashscope.aliyuncs.com/compatible-mode/v1", "0");
|
|
|
+ cfg.setProvider("dashscope");
|
|
|
+
|
|
|
+ GenerateOptions options = factory.buildGenerateOptions(cfg, true);
|
|
|
+
|
|
|
+ assertNotNull(options);
|
|
|
+ assertEquals(Boolean.FALSE, options.getAdditionalBodyParams().get("enable_thinking"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("摘要调用:DeepSeek 官方端点关闭思考")
|
|
|
+ void shouldDisableThinkingForDeepSeekSummary() {
|
|
|
+ AiModelConfig cfg = config(9L, "deepseek-chat", "https://api.deepseek.com/v1", "0");
|
|
|
+ cfg.setProvider("deepseek");
|
|
|
+
|
|
|
+ GenerateOptions options = factory.buildGenerateOptions(cfg, true);
|
|
|
+
|
|
|
+ assertNotNull(options);
|
|
|
+ assertEquals(Map.of("type", "disabled"), options.getAdditionalBodyParams().get("thinking"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("摘要调用:未适配的提供商不注入思考参数")
|
|
|
+ void shouldNotInjectThinkingParamForUnknownProvider() {
|
|
|
+ AiModelConfig cfg = config(9L, "gpt-4", "https://api.example.com/v1", "0");
|
|
|
+ cfg.setProvider("openai");
|
|
|
+
|
|
|
+ assertNull(factory.buildGenerateOptions(cfg, true));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("槽位模型不可用时回退空")
|
|
|
+ void shouldReturnNullWhenSlotConfigUnusable() {
|
|
|
+ AiExpertAgent def = definition();
|
|
|
+ def.setMemoryModelId(2L);
|
|
|
+ when(mapper.selectById(2L)).thenReturn(null);
|
|
|
+ assertNull(factory.resolveMemoryModel(def));
|
|
|
+
|
|
|
+ when(mapper.selectById(2L)).thenReturn(config(2L, " ", "https://x", "0"));
|
|
|
+ assertNull(factory.resolveMemoryModel(def));
|
|
|
+
|
|
|
+ when(mapper.selectById(2L)).thenReturn(config(2L, "m", "https://x/{id}", "0"));
|
|
|
+ assertNull(factory.resolveMemoryModel(def));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("槽位模型可用时构造实例")
|
|
|
+ void shouldBuildModelForUsableSlot() {
|
|
|
+ AiExpertAgent def = definition();
|
|
|
+ def.setMemoryModelId(2L);
|
|
|
+ when(mapper.selectById(2L)).thenReturn(config(2L, "gpt-4", "https://api.example.com/v1", "0"));
|
|
|
+ when(modelConfigService.decryptApiKey(2L)).thenReturn("sk-x");
|
|
|
+ Model model = factory.resolveMemoryModel(def);
|
|
|
+ assertNotNull(model);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("模型上下文窗口按配置写入模型实例")
|
|
|
+ void shouldApplyConfiguredContextWindow() {
|
|
|
+ AiModelConfig cfg = config(9L, "qwen-plus", "https://api.example.com/v1", "0");
|
|
|
+ cfg.setContextWindow(131072);
|
|
|
+ when(modelConfigService.decryptApiKey(9L)).thenReturn("sk-x");
|
|
|
+
|
|
|
+ Model model = factory.buildChatModel(cfg);
|
|
|
+
|
|
|
+ assertEquals(131072, model.getContextWindowSize());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("取槽位模型标识")
|
|
|
+ void shouldResolveSlotModelCode() {
|
|
|
+ assertNull(factory.slotModelCode(null));
|
|
|
+ when(mapper.selectById(8L)).thenReturn(null);
|
|
|
+ assertNull(factory.slotModelCode(8L));
|
|
|
+ AiModelConfig cfg = config(8L, "gpt-8", "https://x", "0");
|
|
|
+ when(mapper.selectById(8L)).thenReturn(cfg);
|
|
|
+ assertEquals("gpt-8", factory.slotModelCode(8L));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("校验对话模型配置并抛出明确异常")
|
|
|
+ void shouldValidateChatModel() {
|
|
|
+ assertThrows(ServiceException.class, () -> factory.resolveChatModel(null));
|
|
|
+ when(mapper.selectById(1L)).thenReturn(null);
|
|
|
+ assertThrows(ServiceException.class, () -> factory.resolveChatModel(1L));
|
|
|
+
|
|
|
+ AiModelConfig blankCode = config(1L, " ", "https://x", "0");
|
|
|
+ when(mapper.selectById(1L)).thenReturn(blankCode);
|
|
|
+ assertThrows(ServiceException.class, () -> factory.resolveChatModel(1L));
|
|
|
+
|
|
|
+ AiModelConfig badUrl = config(1L, "m", "https://x/{id}", "0");
|
|
|
+ when(mapper.selectById(1L)).thenReturn(badUrl);
|
|
|
+ assertThrows(ServiceException.class, () -> factory.resolveChatModel(1L));
|
|
|
+
|
|
|
+ AiModelConfig ok = config(1L, "m", "https://api.example.com/v1", "0");
|
|
|
+ when(mapper.selectById(1L)).thenReturn(ok);
|
|
|
+ assertSame(ok, factory.resolveChatModel(1L));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("构造模型:无提供商直连 OpenAI 兼容客户端")
|
|
|
+ void shouldBuildChatModelWithoutProvider() {
|
|
|
+ AiModelConfig cfg = config(9L, "gpt-4", "https://api.example.com/v1/chat/completions", "0");
|
|
|
+ cfg.setTimeoutSeconds(30);
|
|
|
+ cfg.setMaxRetries(2);
|
|
|
+ when(modelConfigService.decryptApiKey(9L)).thenReturn("sk");
|
|
|
+ assertNotNull(factory.buildChatModel(cfg));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("构造模型:提供商无扩展时回退显式客户端")
|
|
|
+ void shouldFallbackWhenProviderUnknown() {
|
|
|
+ AiModelConfig cfg = config(9L, "gpt-4", "https://api.example.com/v1", "0");
|
|
|
+ cfg.setProvider("no-such-provider");
|
|
|
+ when(modelConfigService.decryptApiKey(9L)).thenReturn("sk");
|
|
|
+ assertNotNull(factory.buildChatModel(cfg));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("构造模型:密钥为空时回退显式客户端")
|
|
|
+ void shouldFallbackWhenApiKeyBlank() {
|
|
|
+ AiModelConfig cfg = config(9L, "gpt-4", "https://api.example.com/v1", "0");
|
|
|
+ cfg.setProvider("openai");
|
|
|
+ when(modelConfigService.decryptApiKey(9L)).thenReturn(" ");
|
|
|
+ assertNotNull(factory.buildChatModel(cfg));
|
|
|
+ }
|
|
|
+}
|