| 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 TestIsOpenAI(t *testing.T) { |
| 41 | for _, tc := range []struct { |
| 42 | baseURL string |
| 43 | want bool |
| 44 | }{ |
| 45 | {"https://api.openai.com/v1", true}, |
| 46 | {"https://API.OPENAI.COM/v1", true}, |
| 47 | {"https://platform.openai.com/v1", false}, |
| 48 | {"https://gateway.example/v1", false}, |
| 49 | {"not-a-url", false}, |
| 50 | } { |
| 51 | if got := IsOpenAI(tc.baseURL); got != tc.want { |
| 52 | t.Errorf("IsOpenAI(%q) = %v, want %v", tc.baseURL, got, tc.want) |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | func TestDeepSeekPrefixChatURL(t *testing.T) { |
| 58 | for _, tc := range []struct { |
| 59 | name string |
| 60 | in string |
| 61 | want string |
| 62 | }{ |
| 63 | {name: "canonical", in: "https://api.deepseek.com/chat/completions", want: "https://api.deepseek.com/beta/chat/completions"}, |
| 64 | {name: "v1 and query", in: "https://api.deepseek.com/v1/chat/completions?trace=1", want: "https://api.deepseek.com/beta/chat/completions"}, |
| 65 | {name: "regional port", in: "https://sg.deepseek.com:8443/v1/chat/completions", want: "https://sg.deepseek.com:8443/beta/chat/completions"}, |
| 66 | {name: "custom gateway", in: "https://gateway.example/v1/chat/completions", want: ""}, |
| 67 | {name: "apex", in: "https://deepseek.com/chat/completions", want: ""}, |
| 68 | {name: "garbage", in: "not-a-url", want: ""}, |
| 69 | } { |
| 70 | t.Run(tc.name, func(t *testing.T) { |
| 71 | if got := deepSeekPrefixChatURL(tc.in); got != tc.want { |
| 72 | t.Fatalf("deepSeekPrefixChatURL(%q) = %q, want %q", tc.in, got, tc.want) |
| 73 | } |
| 74 | }) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | func TestGeminiAPIModelNormalization(t *testing.T) { |
| 79 | for _, tc := range []struct { |
| 80 | baseURL string |
| 81 | model string |
| 82 | want string |
| 83 | }{ |
| 84 | {"https://generativelanguage.googleapis.com/v1beta/openai/", "models/gemini-3.6-flash", "gemini-3.6-flash"}, |
| 85 | {"https://generativelanguage.googleapis.com/v1beta/openai", "gemini-3.6-flash", "gemini-3.6-flash"}, |
| 86 | {"https://api.example.com/v1", "models/gemini-3.6-flash", "models/gemini-3.6-flash"}, |
| 87 | } { |
| 88 | if got := normalizeModelID(tc.baseURL, tc.model); got != tc.want { |
| 89 | t.Errorf("normalizeModelID(%q, %q) = %q, want %q", tc.baseURL, tc.model, got, tc.want) |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | func TestUsesGeminiThoughtSignatures(t *testing.T) { |
| 95 | for _, tc := range []struct { |
| 96 | name string |
| 97 | baseURL string |
| 98 | model string |
| 99 | want bool |
| 100 | }{ |
| 101 | {"official endpoint", "https://generativelanguage.googleapis.com/v1beta/openai", "custom-alias", true}, |
| 102 | {"compatible gateway", "https://openrouter.ai/api/v1", "google/gemini-3.1-pro", true}, |
| 103 | {"different provider", "https://api.deepseek.com/v1", "deepseek-chat", false}, |
| 104 | {"incidental model text", "https://api.example.com/v1", "not-gemini-compatible", false}, |
| 105 | } { |
| 106 | t.Run(tc.name, func(t *testing.T) { |
| 107 | if got := usesGeminiThoughtSignatures(tc.baseURL, tc.model); got != tc.want { |
| 108 | t.Fatalf("usesGeminiThoughtSignatures(%q, %q) = %v, want %v", tc.baseURL, tc.model, got, tc.want) |
| 109 | } |
| 110 | }) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // TestIsMiniMax pins the host-matching rule for MiniMax. The spelling is |
| 115 | // `minimaxi`, not `minimax` — the latter is reserved for any future |
| 116 | // minimax-branded gateway so the two never collide. |
| 117 | func TestIsMiniMax(t *testing.T) { |
| 118 | for _, tc := range []struct { |
| 119 | baseURL string |
| 120 | want bool |
| 121 | }{ |
| 122 | // Canonical |
| 123 | {"https://api.minimaxi.com", true}, |
| 124 | {"https://api.minimaxi.com/v1", true}, |
| 125 | {"https://api.minimaxi.com/anthropic", true}, |
| 126 | // Regional subdomains under the apex |
| 127 | {"https://eu.minimaxi.com/v1", true}, |
| 128 | {"https://us.minimaxi.com/v1", true}, |
| 129 | // Apex rejected |
| 130 | {"https://minimaxi.com/v1", false}, |
| 131 | {"https://minimaxi.com", false}, |
| 132 | // Other vendors must not match |
| 133 | {"https://api.deepseek.com", false}, |
| 134 | {"https://api.openai.com/v1", false}, |
| 135 | // Wrong spelling — minimax, not minimaxi — must not match |
| 136 | {"https://api.minimax.com/v1", false}, |
| 137 | {"https://api.minimax.example.com", false}, |
| 138 | // Garbage |
| 139 | {"", false}, |
| 140 | {"not-a-url", false}, |
| 141 | } { |
| 142 | if got := IsMiniMax(tc.baseURL); got != tc.want { |
| 143 | t.Errorf("IsMiniMax(%q) = %v, want %v", tc.baseURL, got, tc.want) |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | func TestIsMiMo(t *testing.T) { |
| 149 | for _, tc := range []struct { |
| 150 | baseURL string |
| 151 | want bool |
| 152 | }{ |
| 153 | {"https://api.xiaomimimo.com/v1", true}, |
| 154 | {"https://token-plan-cn.xiaomimimo.com/v1", true}, |
| 155 | {"https://token-plan-sgp.xiaomimimo.com/v1", true}, |
| 156 | {"https://token-plan-ams.xiaomimimo.com/v1", true}, |
| 157 | {"https://xiaomimimo.com/v1", false}, |
| 158 | {"https://api.deepseek.com", false}, |
| 159 | {"", false}, |
| 160 | {"not-a-url", false}, |
| 161 | } { |
| 162 | if got := IsMiMo(tc.baseURL); got != tc.want { |
| 163 | t.Errorf("IsMiMo(%q) = %v, want %v", tc.baseURL, got, tc.want) |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // TestIsZhipu pins the host-matching rule for Zhipu GLM across both the China |
| 169 | // (bigmodel.cn) and international (z.ai) endpoints. |
| 170 | func TestIsZhipu(t *testing.T) { |
| 171 | for _, tc := range []struct { |
| 172 | baseURL string |
| 173 | want bool |
| 174 | }{ |
| 175 | // Canonical China endpoint |
| 176 | {"https://open.bigmodel.cn/api/paas/v4", true}, |
| 177 | {"https://open.bigmodel.cn", true}, |
| 178 | // Subdomains under the China apex |
| 179 | {"https://api.bigmodel.cn/v1", true}, |
| 180 | // Canonical international endpoint |
| 181 | {"https://api.z.ai/api/paas/v4", true}, |
| 182 | {"https://api.z.ai", true}, |
| 183 | // Subdomain under the z.ai apex |
| 184 | {"https://open.z.ai/v1", true}, |
| 185 | // Bare apexes rejected (misconfiguration) |
| 186 | {"https://bigmodel.cn/v1", false}, |
| 187 | {"https://z.ai", false}, |
| 188 | // Other vendors must not match |
| 189 | {"https://api.deepseek.com", false}, |
| 190 | {"https://api.minimaxi.com/v1", false}, |
| 191 | {"https://api.openai.com/v1", false}, |
| 192 | // Garbage |
| 193 | {"", false}, |
| 194 | {"not-a-url", false}, |
| 195 | } { |
| 196 | if got := IsZhipu(tc.baseURL); got != tc.want { |
| 197 | t.Errorf("IsZhipu(%q) = %v, want %v", tc.baseURL, got, tc.want) |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | func TestIsTokenRhythm(t *testing.T) { |
| 203 | for _, tc := range []struct { |
| 204 | baseURL string |
| 205 | want bool |
| 206 | }{ |
| 207 | {"https://tokenrhythm.studio/v1", true}, |
| 208 | {"https://TOKENRHYTHM.STUDIO/v1", true}, |
| 209 | {"https://api.tokenrhythm.studio/v1", false}, |
| 210 | {"https://tokenrhythm.example.com/v1", false}, |
| 211 | {"https://opencode.ai/zen/go/v1", false}, |
| 212 | {"", false}, |
| 213 | } { |
| 214 | if got := IsTokenRhythm(tc.baseURL); got != tc.want { |
| 215 | t.Errorf("IsTokenRhythm(%q) = %v, want %v", tc.baseURL, got, tc.want) |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | func TestIsLongCat(t *testing.T) { |
| 221 | for _, tc := range []struct { |
| 222 | baseURL string |
| 223 | want bool |
| 224 | }{ |
| 225 | {"https://api.longcat.chat/openai/v1", true}, |
| 226 | {"https://api.longcat.chat/anthropic", true}, |
| 227 | {"https://gateway.longcat.chat/openai/v1", true}, |
| 228 | {"https://longcat.chat/openai/v1", false}, |
| 229 | {"https://api.deepseek.com", false}, |
| 230 | {"", false}, |
| 231 | {"not-a-url", false}, |
| 232 | } { |
| 233 | if got := IsLongCat(tc.baseURL); got != tc.want { |
| 234 | t.Errorf("IsLongCat(%q) = %v, want %v", tc.baseURL, got, tc.want) |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | func TestIsKimiAPI(t *testing.T) { |
| 240 | for _, tc := range []struct { |
| 241 | baseURL string |
| 242 | want bool |
| 243 | }{ |
| 244 | {"https://api.moonshot.cn/v1", true}, |
| 245 | {"https://api.moonshot.ai/v1", true}, |
| 246 | {"https://API.MOONSHOT.CN/v1/chat/completions", true}, |
| 247 | {"https://moonshot.cn/v1", false}, |
| 248 | {"https://gateway.moonshot.cn/v1", false}, |
| 249 | {"https://opencode.ai/zen/go/v1", false}, |
| 250 | {"", false}, |
| 251 | {"not-a-url", false}, |
| 252 | } { |
| 253 | if got := IsKimiAPI(tc.baseURL); got != tc.want { |
| 254 | t.Errorf("IsKimiAPI(%q) = %v, want %v", tc.baseURL, got, tc.want) |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | func TestIsOllamaCloud(t *testing.T) { |
| 260 | for _, tc := range []struct { |
| 261 | baseURL string |
| 262 | want bool |
| 263 | }{ |
| 264 | {"https://ollama.com/v1", true}, |
| 265 | {"https://api.ollama.com/v1", true}, |
| 266 | {"https://localhost:11434/v1", false}, |
| 267 | {"http://127.0.0.1:11434/v1", false}, |
| 268 | {"https://api.openai.com/v1", false}, |
| 269 | {"", false}, |
| 270 | {"not-a-url", false}, |
| 271 | } { |
| 272 | if got := IsOllamaCloud(tc.baseURL); got != tc.want { |
| 273 | t.Errorf("IsOllamaCloud(%q) = %v, want %v", tc.baseURL, got, tc.want) |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 |