test: cover SSW AI proxy routing
Co-Authored-By: Claude <[email protected]>
This commit is contained in:
1 parent
f5ce7d925b
commit
98baa512ca
1 file changed
+198
@@ -0,0 +1,198 @@
|
||||
package ssw.common.util;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* SswLLMClient URL resolver + OpenAI 响应解析单元测试.
|
||||
*
|
||||
* 覆盖 BS-eventhub 修复 (2026-07-22):
|
||||
* 1. SSW internal proxy URL 精确不追加 /chat/completions (核心回归)
|
||||
* 2. OpenAI baseUrl 追加 /chat/completions
|
||||
* 3. Anthropic baseUrl 追加 /v1/messages
|
||||
* 4. 业务错误 JSON {code:600, msg:'Login expired'} 在 choices 解析前给出含 code/msg 的错误
|
||||
*/
|
||||
class SswLLMClientTest {
|
||||
|
||||
// --- 1. SSW internal proxy 精确匹配, 不追加后缀 ---
|
||||
|
||||
@Test
|
||||
@DisplayName("internal proxy URL 精确使用, 不追加 /chat/completions")
|
||||
void internalProxyUrlNotAppended() {
|
||||
String base = "http://nginx/api/ssw/ai/openai-proxy";
|
||||
assertEquals(base, SswLLMClient.resolveChatUrl(base),
|
||||
"internal proxy 追加 /chat/completions 会被 JNPF AuthFilter 误判 credential 为用户 token");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("internal proxy URL 带 trailing slash 也精确使用")
|
||||
void internalProxyUrlTrailingSlashNotAppended() {
|
||||
String base = "http://nginx/api/ssw/ai/openai-proxy/";
|
||||
assertEquals("http://nginx/api/ssw/ai/openai-proxy",
|
||||
SswLLMClient.resolveChatUrl(base));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("internal proxy 不是 Anthropic 协议")
|
||||
void internalProxyIsNotAnthropic() {
|
||||
assertTrue(!SswLLMClient.isAnthropicBase("http://nginx/api/ssw/ai/openai-proxy"));
|
||||
}
|
||||
|
||||
// --- 2. OpenAI baseUrl 追加 /chat/completions ---
|
||||
|
||||
@Test
|
||||
@DisplayName("OpenAI baseUrl 追加 /chat/completions")
|
||||
void openAiUrlAppendsChatCompletions() {
|
||||
assertEquals("https://api.deepseek.com/v1/chat/completions",
|
||||
SswLLMClient.resolveChatUrl("https://api.deepseek.com/v1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("OpenAI baseUrl trailing slash 也追加 /chat/completions")
|
||||
void openAiUrlTrailingSlashAppendsChatCompletions() {
|
||||
assertEquals("https://token-plan-cn.xiaomimimo.com/v1/chat/completions",
|
||||
SswLLMClient.resolveChatUrl("https://token-plan-cn.xiaomimimo.com/v1/"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("OpenAI baseUrl 已含 /chat/completions 不重复追加")
|
||||
void openAiUrlIdempotent() {
|
||||
String base = "https://api.deepseek.com/v1/chat/completions";
|
||||
assertEquals(base, SswLLMClient.resolveChatUrl(base));
|
||||
}
|
||||
|
||||
// --- 3. Anthropic baseUrl 追加 /v1/messages ---
|
||||
|
||||
@Test
|
||||
@DisplayName("Anthropic baseUrl 追加 /v1/messages")
|
||||
void anthropicUrlAppendsV1Messages() {
|
||||
assertTrue(SswLLMClient.isAnthropicBase("https://api.minimaxi.com/anthropic"));
|
||||
assertEquals("https://api.minimaxi.com/anthropic/v1/messages",
|
||||
SswLLMClient.resolveChatUrl("https://api.minimaxi.com/anthropic"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Anthropic baseUrl 已含 /v1/messages 不重复追加")
|
||||
void anthropicUrlIdempotent() {
|
||||
String base = "https://api.minimaxi.com/anthropic/v1/messages";
|
||||
assertEquals(base, SswLLMClient.resolveChatUrl(base));
|
||||
}
|
||||
|
||||
// --- 4. 业务错误 JSON 在 choices 解析前给出含 code/msg 的错误 ---
|
||||
|
||||
@Test
|
||||
@DisplayName("业务错误 JSON {code:600, msg:'Login expired'} 抛出含 code/msg 的错误, 而非 '无 choices'")
|
||||
void businessErrorEnvelopeReportsCodeAndMsg() {
|
||||
JSONObject resp = new JSONObject();
|
||||
resp.put("code", 600);
|
||||
resp.put("msg", "Login expired, please log in again");
|
||||
|
||||
RuntimeException ex = assertThrows(RuntimeException.class,
|
||||
() -> SswLLMClient.parseOpenAiContent(resp));
|
||||
|
||||
String m = ex.getMessage();
|
||||
assertTrue(m.contains("600"), "错误信息应含业务 code 600, 实际: " + m);
|
||||
assertTrue(m.contains("Login expired"), "错误信息应含业务 msg, 实际: " + m);
|
||||
assertTrue(!m.contains("无 choices"),
|
||||
"业务错误不应被误报为 'OpenAI 响应无 choices', 实际: " + m);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("正常 OpenAI 响应正确解析 content")
|
||||
void parsesNormalOpenAiContent() {
|
||||
JSONObject resp = JSONObject.parseObject(
|
||||
"{\"choices\":[{\"message\":{\"role\":\"assistant\",\"content\":\"hello\"}}]}");
|
||||
assertEquals("hello", SswLLMClient.parseOpenAiContent(resp));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("无 choices 且非业务错误信封仍报 '无 choices'")
|
||||
void missingChoicesStillReportsNoChoices() {
|
||||
JSONObject resp = new JSONObject();
|
||||
resp.put("object", "chat.completion");
|
||||
RuntimeException ex = assertThrows(RuntimeException.class,
|
||||
() -> SswLLMClient.parseOpenAiContent(resp));
|
||||
assertTrue(ex.getMessage().contains("无 choices"), ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("BS-eventhub 强化: 业务错误即使带空 choices 也被识别 (混合信封)")
|
||||
void businessErrorWithEmptyChoicesStillReported() {
|
||||
JSONObject resp = new JSONObject();
|
||||
resp.put("code", 600);
|
||||
resp.put("msg", "Login expired, please log in again");
|
||||
resp.put("choices", new com.alibaba.fastjson.JSONArray());
|
||||
RuntimeException ex = assertThrows(RuntimeException.class,
|
||||
() -> SswLLMClient.parseOpenAiContent(resp));
|
||||
String m = ex.getMessage();
|
||||
assertTrue(m.contains("600"), "错误信息应含业务 code 600, 实际: " + m);
|
||||
assertTrue(m.contains("Login expired"), "错误信息应含业务 msg, 实际: " + m);
|
||||
assertTrue(!m.contains("无 choices"),
|
||||
"混合信封不应被误报为 'OpenAI 响应无 choices', 实际: " + m);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("BS-eventhub 强化: 业务错误即使 choices.content=null 也被识别 (混合信封)")
|
||||
void businessErrorWithContentNullStillReported() {
|
||||
JSONObject resp = new JSONObject();
|
||||
resp.put("code", 600);
|
||||
resp.put("msg", "Login expired");
|
||||
com.alibaba.fastjson.JSONObject choice = new com.alibaba.fastjson.JSONObject();
|
||||
com.alibaba.fastjson.JSONObject message = new com.alibaba.fastjson.JSONObject();
|
||||
message.put("content", (String) null);
|
||||
choice.put("message", message);
|
||||
com.alibaba.fastjson.JSONArray choices = new com.alibaba.fastjson.JSONArray();
|
||||
choices.add(choice);
|
||||
resp.put("choices", choices);
|
||||
RuntimeException ex = assertThrows(RuntimeException.class,
|
||||
() -> SswLLMClient.parseOpenAiContent(resp));
|
||||
String m = ex.getMessage();
|
||||
assertTrue(m.contains("600"), "错误信息应含业务 code 600, 实际: " + m);
|
||||
assertTrue(!m.contains("content 为空"),
|
||||
"混合信封不应被误报为 'OpenAI choices[0].message.content 为空', 实际: " + m);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("code=200 即使带 msg 也不视为业务错误 (正常 OpenAI 也可能带 msg 字段)")
|
||||
void code200WithMsgIsNotBusinessError() {
|
||||
JSONObject resp = JSONObject.parseObject(
|
||||
"{\"code\":200,\"msg\":\"ok\",\"choices\":[{\"message\":{\"content\":\"hi\"}}]}");
|
||||
assertEquals("hi", SswLLMClient.parseOpenAiContent(resp));
|
||||
}
|
||||
|
||||
// --- 5. query/fragment 变体的 SSW internal proxy 识别 (0.21.x) ---
|
||||
|
||||
@Test
|
||||
@DisplayName("internal proxy URL 带 query string 仍精确使用, 不追加 /chat/completions")
|
||||
void internalProxyUrlWithQueryStringNotAppended() {
|
||||
String base = "http://nginx/api/ssw/ai/openai-proxy?app=testspace";
|
||||
assertEquals(base, SswLLMClient.resolveChatUrl(base),
|
||||
"internal proxy 带 query 应原样返回, 不追加 /chat/completions? 等");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("internal proxy URL 带 fragment 仍精确使用")
|
||||
void internalProxyUrlWithFragmentNotAppended() {
|
||||
String base = "http://nginx/api/ssw/ai/openai-proxy#default";
|
||||
assertEquals(base, SswLLMClient.resolveChatUrl(base));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("internal proxy URL 带 query + trailing slash 仍精确使用 (不加 /chat/completions)")
|
||||
void internalProxyUrlQueryAndTrailingSlashNotAppended() {
|
||||
String base = "http://nginx/api/ssw/ai/openai-proxy/?app=testspace";
|
||||
String resolved = SswLLMClient.resolveChatUrl(base);
|
||||
// 行为契约: 不加 /chat/completions, 可能是 .../openai-proxy?app=testspace 或 .../openai-proxy/?app=testspace,
|
||||
// 两种均正确. 这里接受任一等价写法, 绝不附加路径后缀.
|
||||
assertTrue(!resolved.contains("/chat/completions"),
|
||||
"internal proxy 不应追加 /chat/completions, 实际: " + resolved);
|
||||
assertTrue(resolved.equals("http://nginx/api/ssw/ai/openai-proxy/?app=testspace")
|
||||
|| resolved.equals("http://nginx/api/ssw/ai/openai-proxy?app=testspace"),
|
||||
"URL 应保持 internal proxy 原结构, 实际: " + resolved);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user