package store import "testing" func TestChannelUpstreamURL(t *testing.T) { cases := []struct { base, path, want string }{ // 无版本前缀 → 默认补 /v1(OpenAI 纯域名) {"https://api.openai.com", "/chat/completions", "https://api.openai.com/v1/chat/completions"}, // 已含 /v1 → 直接拼资源路径 {"https://api.openai.com/v1", "/chat/completions", "https://api.openai.com/v1/chat/completions"}, {"https://api.anthropic.com/v1", "/messages", "https://api.anthropic.com/v1/messages"}, // 自定义版本前缀(如 BigModel 的 /v4) → 直接拼资源路径 {"https://open.bigmodel.cn/api/paas/v4", "/chat/completions", "https://open.bigmodel.cn/api/paas/v4/chat/completions"}, {"https://open.bigmodel.cn/api/paas/v4", "/models", "https://open.bigmodel.cn/api/paas/v4/models"}, // 本地 mock:无版本前缀补 /v1 {"http://localhost:9000", "/chat/completions", "http://localhost:9000/v1/chat/completions"}, {"http://localhost:9000/v1", "/models", "http://localhost:9000/v1/models"}, // 完整端点原样 {"https://api.openai.com/v1/chat/completions", "/chat/completions", "https://api.openai.com/v1/chat/completions"}, // 尾斜杠 {"https://api.openai.com/v1/", "/chat/completions", "https://api.openai.com/v1/chat/completions"}, } for _, c := range cases { ch := &Channel{BaseURL: c.base} got := ch.UpstreamURL("", c.path) if got != c.want { t.Errorf("UpstreamURL(%q, %q) = %q, want %q", c.base, c.path, got, c.want) } } // 分协议 base_url 覆盖 ch := &Channel{ BaseURL: "https://open.bigmodel.cn/api/paas/v4", BaseURLs: map[string]string{"responses": "https://resp.example.com/v4", "messages": "https://msg.example.com/v1"}, } cases2 := []struct{ proto, path, want string }{ {"chat", "/chat/completions", "https://open.bigmodel.cn/api/paas/v4/chat/completions"}, // 用主 base {"responses", "/responses", "https://resp.example.com/v4/responses"}, // 用覆盖 base {"messages", "/messages", "https://msg.example.com/v1/messages"}, // 用覆盖 base {"", "/models", "https://open.bigmodel.cn/api/paas/v4/models"}, // 空协议用主 base } for _, c := range cases2 { got := ch.UpstreamURL(c.proto, c.path) if got != c.want { t.Errorf("UpstreamURL(%q, %q) = %q, want %q", c.proto, c.path, got, c.want) } } }