| 1 | package openai |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | // TestIsDeepSeek pins the host-matching rule for DeepSeek: the canonical |
| 6 | // api.deepseek.com, any *.deepseek.com subdomain, but NOT the apex |
| 7 | // deepseek.com (a misconfiguration we explicitly reject). |
| 8 | func TestIsDeepSeek(t *testing.T) { |
| 9 | for _, tc := range []struct { |
| 10 | baseURL string |
| 11 | want bool |
| 12 | }{ |
| 13 | // Canonical |
| 14 | {"https://api.deepseek.com", true}, |
| 15 | {"https://api.deepseek.com/v1", true}, |
| 16 | {"https://api.deepseek.com/anthropic", true}, |
| 17 | // Regional subdomains under the apex |
| 18 | {"https://eu.deepseek.com/v1", true}, |
| 19 | {"https://us.deepseek.com/v1", true}, |
| 20 | // Apex rejected (would require a user pointing their base_url at |
| 21 | // the apex domain, which is a misconfiguration) |
| 22 | {"https://deepseek.com/v1", false}, |
| 23 | {"https://deepseek.com", false}, |
| 24 | // Other vendors must not match |
| 25 | {"https://api.minimaxi.com/v1", false}, |
| 26 | {"https://api.openai.com/v1", false}, |
| 27 | // Wrong-spelling TLDs (e.g. "deepseek.io") must not match |
| 28 | {"https://api.deepseek.io", false}, |
| 29 | {"https://deepseek.io", false}, |
| 30 | // Garbage |
| 31 | {"", false}, |
| 32 | {"not-a-url", false}, |
| 33 | } { |
| 34 | if got := IsDeepSeek(tc.baseURL); got != tc.want { |
| 35 | t.Errorf("IsDeepSeek(%q) = %v, want %v", tc.baseURL, got, tc.want) |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func TestIsOfficialDeepSeekVisionModel(t *testing.T) { |
| 41 | for _, tc := range []struct { |
| 42 | model string |
| 43 | want bool |
| 44 | }{ |
| 45 | {OfficialDeepSeekVisionModel, true}, |
| 46 | {"DEEPSEEK-V4-FLASH-VISION-EXP", true}, |
| 47 | {" deepseek-v4-flash-vision-exp ", true}, |
| 48 | {"deepseek-v4-flash", false}, |
| 49 | {"deepseek-v4-pro", false}, |
| 50 | {"deepseek-v5-vision", false}, |
| 51 | {"", false}, |
| 52 | } { |
| 53 | if got := IsOfficialDeepSeekVisionModel(tc.model); got != tc.want { |
| 54 | t.Errorf("IsOfficialDeepSeekVisionModel(%q) = %v, want %v", tc.model, got, tc.want) |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func TestOfficialDeepSeekAllowsVision(t *testing.T) { |
| 60 | for _, tc := range []struct { |
| 61 | baseURL string |
| 62 | model string |
| 63 | want bool |
| 64 | }{ |
| 65 | {"https://api.deepseek.com", OfficialDeepSeekVisionModel, true}, |
| 66 | {"https://api.deepseek.com/anthropic", OfficialDeepSeekVisionModel, true}, |
| 67 | {"https://eu.deepseek.com/v1", OfficialDeepSeekVisionModel, true}, |
| 68 | {"https://api.deepseek.com", "deepseek-flash", true}, |
| 69 | {"https://api.deepseek.com", "deepseek-v4-flash", true}, |
| 70 | {"https://api.deepseek.com", "deepseek-v4.1-flash-expires-on-0910", true}, |
| 71 | {"https://api.deepseek.com", "deepseek-v4-pro", false}, |
| 72 | {"https://api.deepseek.com", "deepseek-v5-vision", false}, |
| 73 | {"https://gateway.example/v1", OfficialDeepSeekVisionModel, false}, |
| 74 | } { |
| 75 | if got := OfficialDeepSeekAllowsVision(tc.baseURL, tc.model); got != tc.want { |
| 76 | t.Errorf("OfficialDeepSeekAllowsVision(%q, %q) = %v, want %v", tc.baseURL, tc.model, got, tc.want) |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func TestIsOpenAI(t *testing.T) { |
| 82 | for _, tc := range []struct { |
| 83 | baseURL string |
| 84 | want bool |
| 85 | }{ |
| 86 | {"https://api.openai.com/v1", true}, |
| 87 | {"https://API.OPENAI.COM/v1", true}, |
| 88 | {"https://platform.openai.com/v1", false}, |
| 89 | {"https://gateway.example/v1", false}, |
| 90 | {"not-a-url", false}, |
| 91 | } { |
| 92 | if got := IsOpenAI(tc.baseURL); got != tc.want { |
| 93 | t.Errorf("IsOpenAI(%q) = %v, want %v", tc.baseURL, got, tc.want) |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | func TestDeepSeekPrefixChatURL(t *testing.T) { |
| 99 | for _, tc := range []struct { |
| 100 | name string |
| 101 | in string |
| 102 | want string |
| 103 | }{ |
| 104 | {name: "canonical", in: "https://api.deepseek.com/chat/completions", want: "https://api.deepseek.com/beta/chat/completions"}, |
| 105 | {name: "v1 and query", in: "https://api.deepseek.com/v1/chat/completions?trace=1", want: "https://api.deepseek.com/beta/chat/completions"}, |
| 106 | {name: "regional port", in: "https://sg.deepseek.com:8443/v1/chat/completions", want: "https://sg.deepseek.com:8443/beta/chat/completions"}, |
| 107 | {name: "custom gateway", in: "https://gateway.example/v1/chat/completions", want: ""}, |
| 108 | {name: "apex", in: "https://deepseek.com/chat/completions", want: ""}, |
| 109 | {name: "garbage", in: "not-a-url", want: ""}, |
| 110 | } { |
| 111 | t.Run(tc.name, func(t *testing.T) { |
| 112 | if got := deepSeekPrefixChatURL(tc.in); got != tc.want { |
| 113 | t.Fatalf("deepSeekPrefixChatURL(%q) = %q, want %q", tc.in, got, tc.want) |
| 114 | } |
| 115 | }) |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | func TestGeminiAPIModelNormalization(t *testing.T) { |
| 120 | for _, tc := range []struct { |
| 121 | baseURL string |
| 122 | model string |
| 123 | want string |
| 124 | }{ |
| 125 | {"https://generativelanguage.googleapis.com/v1beta/openai/", "models/gemini-3.6-flash", "gemini-3.6-flash"}, |
| 126 | {"https://generativelanguage.googleapis.com/v1beta/openai", "gemini-3.6-flash", "gemini-3.6-flash"}, |
| 127 | {"https://api.example.com/v1", "models/gemini-3.6-flash", "models/gemini-3.6-flash"}, |
| 128 | } { |
| 129 | if got := normalizeModelID(tc.baseURL, tc.model); got != tc.want { |
| 130 | t.Errorf("normalizeModelID(%q, %q) = %q, want %q", tc.baseURL, tc.model, got, tc.want) |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | func TestUsesGeminiThoughtSignatures(t *testing.T) { |
| 136 | for _, tc := range []struct { |
| 137 | name string |
| 138 | baseURL string |
| 139 | model string |
| 140 | want bool |
| 141 | }{ |
| 142 | {"official endpoint", "https://generativelanguage.googleapis.com/v1beta/openai", "custom-alias", true}, |
| 143 | {"compatible gateway", "https://openrouter.ai/api/v1", "google/gemini-3.1-pro", true}, |
| 144 | {"different provider", "https://api.deepseek.com/v1", "deepseek-chat", false}, |
| 145 | {"incidental model text", "https://api.example.com/v1", "not-gemini-compatible", false}, |
| 146 | } { |
| 147 | t.Run(tc.name, func(t *testing.T) { |
| 148 | if got := usesGeminiThoughtSignatures(tc.baseURL, tc.model); got != tc.want { |
| 149 | t.Fatalf("usesGeminiThoughtSignatures(%q, %q) = %v, want %v", tc.baseURL, tc.model, got, tc.want) |
| 150 | } |
| 151 | }) |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | // TestIsMiniMax pins the host-matching rule for MiniMax. The spelling is |
| 156 | // `minimaxi`, not `minimax` — the latter is reserved for any future |
| 157 | // minimax-branded gateway so the two never collide. |
| 158 | func TestIsMiniMax(t *testing.T) { |
| 159 | for _, tc := range []struct { |
| 160 | baseURL string |
| 161 | want bool |
| 162 | }{ |
| 163 | // Canonical |
| 164 | {"https://api.minimaxi.com", true}, |
| 165 | {"https://api.minimaxi.com/v1", true}, |
| 166 | {"https://api.minimaxi.com/anthropic", true}, |
| 167 | // Regional subdomains under the apex |
| 168 | {"https://eu.minimaxi.com/v1", true}, |
| 169 | {"https://us.minimaxi.com/v1", true}, |
| 170 | // Apex rejected |
| 171 | {"https://minimaxi.com/v1", false}, |
| 172 | {"https://minimaxi.com", false}, |
| 173 | // Other vendors must not match |
| 174 | {"https://api.deepseek.com", false}, |
| 175 | {"https://api.openai.com/v1", false}, |
| 176 | // Wrong spelling — minimax, not minimaxi — must not match |
| 177 | {"https://api.minimax.com/v1", false}, |
| 178 | {"https://api.minimax.example.com", false}, |
| 179 | // Garbage |
| 180 | {"", false}, |
| 181 | {"not-a-url", false}, |
| 182 | } { |
| 183 | if got := IsMiniMax(tc.baseURL); got != tc.want { |
| 184 | t.Errorf("IsMiniMax(%q) = %v, want %v", tc.baseURL, got, tc.want) |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | func TestIsMiMo(t *testing.T) { |
| 190 | for _, tc := range []struct { |
| 191 | baseURL string |
| 192 | want bool |
| 193 | }{ |
| 194 | {"https://api.xiaomimimo.com/v1", true}, |
| 195 | {"https://token-plan-cn.xiaomimimo.com/v1", true}, |
| 196 | {"https://token-plan-sgp.xiaomimimo.com/v1", true}, |
| 197 | {"https://token-plan-ams.xiaomimimo.com/v1", true}, |
| 198 | {"https://xiaomimimo.com/v1", false}, |
| 199 | {"https://api.deepseek.com", false}, |
| 200 | {"", false}, |
| 201 | {"not-a-url", false}, |
| 202 | } { |
| 203 | if got := IsMiMo(tc.baseURL); got != tc.want { |
| 204 | t.Errorf("IsMiMo(%q) = %v, want %v", tc.baseURL, got, tc.want) |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | // TestIsZhipu pins the host-matching rule for Zhipu GLM across both the China |
| 210 | // (bigmodel.cn) and international (z.ai) endpoints. |
| 211 | func TestIsZhipu(t *testing.T) { |
| 212 | for _, tc := range []struct { |
| 213 | baseURL string |
| 214 | want bool |
| 215 | }{ |
| 216 | // Canonical China endpoint |
| 217 | {"https://open.bigmodel.cn/api/paas/v4", true}, |
| 218 | {"https://open.bigmodel.cn", true}, |
| 219 | // Subdomains under the China apex |
| 220 | {"https://api.bigmodel.cn/v1", true}, |
| 221 | // Canonical international endpoint |
| 222 | {"https://api.z.ai/api/paas/v4", true}, |
| 223 | {"https://api.z.ai", true}, |
| 224 | // Subdomain under the z.ai apex |
| 225 | {"https://open.z.ai/v1", true}, |
| 226 | // Bare apexes rejected (misconfiguration) |
| 227 | {"https://bigmodel.cn/v1", false}, |
| 228 | {"https://z.ai", false}, |
| 229 | // Other vendors must not match |
| 230 | {"https://api.deepseek.com", false}, |
| 231 | {"https://api.minimaxi.com/v1", false}, |
| 232 | {"https://api.openai.com/v1", false}, |
| 233 | // Garbage |
| 234 | {"", false}, |
| 235 | {"not-a-url", false}, |
| 236 | } { |
| 237 | if got := IsZhipu(tc.baseURL); got != tc.want { |
| 238 | t.Errorf("IsZhipu(%q) = %v, want %v", tc.baseURL, got, tc.want) |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | func TestIsTokenRhythm(t *testing.T) { |
| 244 | for _, tc := range []struct { |
| 245 | baseURL string |
| 246 | want bool |
| 247 | }{ |
| 248 | {"https://tokenrhythm.studio/v1", true}, |
| 249 | {"https://TOKENRHYTHM.STUDIO/v1", true}, |
| 250 | {"https://api.tokenrhythm.studio/v1", false}, |
| 251 | {"https://tokenrhythm.example.com/v1", false}, |
| 252 | {"https://opencode.ai/zen/go/v1", false}, |
| 253 | {"", false}, |
| 254 | } { |
| 255 | if got := IsTokenRhythm(tc.baseURL); got != tc.want { |
| 256 | t.Errorf("IsTokenRhythm(%q) = %v, want %v", tc.baseURL, got, tc.want) |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | func TestIsLongCat(t *testing.T) { |
| 262 | for _, tc := range []struct { |
| 263 | baseURL string |
| 264 | want bool |
| 265 | }{ |
| 266 | {"https://api.longcat.chat/openai/v1", true}, |
| 267 | {"https://api.longcat.chat/anthropic", true}, |
| 268 | {"https://gateway.longcat.chat/openai/v1", true}, |
| 269 | {"https://longcat.chat/openai/v1", false}, |
| 270 | {"https://api.deepseek.com", false}, |
| 271 | {"", false}, |
| 272 | {"not-a-url", false}, |
| 273 | } { |
| 274 | if got := IsLongCat(tc.baseURL); got != tc.want { |
| 275 | t.Errorf("IsLongCat(%q) = %v, want %v", tc.baseURL, got, tc.want) |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | func TestIsKimiAPI(t *testing.T) { |
| 281 | for _, tc := range []struct { |
| 282 | baseURL string |
| 283 | want bool |
| 284 | }{ |
| 285 | {"https://api.moonshot.cn/v1", true}, |
| 286 | {"https://api.moonshot.ai/v1", true}, |
| 287 | {"https://API.MOONSHOT.CN/v1/chat/completions", true}, |
| 288 | {"https://moonshot.cn/v1", false}, |
| 289 | {"https://gateway.moonshot.cn/v1", false}, |
| 290 | {"https://opencode.ai/zen/go/v1", false}, |
| 291 | {"", false}, |
| 292 | {"not-a-url", false}, |
| 293 | } { |
| 294 | if got := IsKimiAPI(tc.baseURL); got != tc.want { |
| 295 | t.Errorf("IsKimiAPI(%q) = %v, want %v", tc.baseURL, got, tc.want) |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | func TestIsOllamaCloud(t *testing.T) { |
| 301 | for _, tc := range []struct { |
| 302 | baseURL string |
| 303 | want bool |
| 304 | }{ |
| 305 | {"https://ollama.com/v1", true}, |
| 306 | {"https://api.ollama.com/v1", true}, |
| 307 | {"https://localhost:11434/v1", false}, |
| 308 | {"http://127.0.0.1:11434/v1", false}, |
| 309 | {"https://api.openai.com/v1", false}, |
| 310 | {"", false}, |
| 311 | {"not-a-url", false}, |
| 312 | } { |
| 313 | if got := IsOllamaCloud(tc.baseURL); got != tc.want { |
| 314 | t.Errorf("IsOllamaCloud(%q) = %v, want %v", tc.baseURL, got, tc.want) |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 |