account_base_url_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. //go:build unit
  2. package service
  3. import (
  4. "testing"
  5. "github.com/Wei-Shaw/sub2api/internal/pkg/xai"
  6. "github.com/stretchr/testify/require"
  7. )
  8. func TestGetBaseURL(t *testing.T) {
  9. tests := []struct {
  10. name string
  11. account Account
  12. expected string
  13. }{
  14. {
  15. name: "non-apikey type returns empty",
  16. account: Account{
  17. Type: AccountTypeOAuth,
  18. Platform: PlatformAnthropic,
  19. },
  20. expected: "",
  21. },
  22. {
  23. name: "apikey without base_url returns default anthropic",
  24. account: Account{
  25. Type: AccountTypeAPIKey,
  26. Platform: PlatformAnthropic,
  27. Credentials: map[string]any{},
  28. },
  29. expected: "https://api.anthropic.com",
  30. },
  31. {
  32. name: "apikey with custom base_url",
  33. account: Account{
  34. Type: AccountTypeAPIKey,
  35. Platform: PlatformAnthropic,
  36. Credentials: map[string]any{"base_url": "https://custom.example.com"},
  37. },
  38. expected: "https://custom.example.com",
  39. },
  40. {
  41. name: "antigravity apikey auto-appends /antigravity",
  42. account: Account{
  43. Type: AccountTypeAPIKey,
  44. Platform: PlatformAntigravity,
  45. Credentials: map[string]any{"base_url": "https://upstream.example.com"},
  46. },
  47. expected: "https://upstream.example.com/antigravity",
  48. },
  49. {
  50. name: "antigravity apikey trims trailing slash before appending",
  51. account: Account{
  52. Type: AccountTypeAPIKey,
  53. Platform: PlatformAntigravity,
  54. Credentials: map[string]any{"base_url": "https://upstream.example.com/"},
  55. },
  56. expected: "https://upstream.example.com/antigravity",
  57. },
  58. {
  59. name: "antigravity non-apikey returns empty",
  60. account: Account{
  61. Type: AccountTypeOAuth,
  62. Platform: PlatformAntigravity,
  63. Credentials: map[string]any{"base_url": "https://upstream.example.com"},
  64. },
  65. expected: "",
  66. },
  67. }
  68. for _, tt := range tests {
  69. t.Run(tt.name, func(t *testing.T) {
  70. result := tt.account.GetBaseURL()
  71. if result != tt.expected {
  72. t.Errorf("GetBaseURL() = %q, want %q", result, tt.expected)
  73. }
  74. })
  75. }
  76. }
  77. func TestGetGeminiBaseURL(t *testing.T) {
  78. const defaultGeminiURL = "https://generativelanguage.googleapis.com"
  79. tests := []struct {
  80. name string
  81. account Account
  82. expected string
  83. }{
  84. {
  85. name: "apikey without base_url returns default",
  86. account: Account{
  87. Type: AccountTypeAPIKey,
  88. Platform: PlatformGemini,
  89. Credentials: map[string]any{},
  90. },
  91. expected: defaultGeminiURL,
  92. },
  93. {
  94. name: "apikey with custom base_url",
  95. account: Account{
  96. Type: AccountTypeAPIKey,
  97. Platform: PlatformGemini,
  98. Credentials: map[string]any{"base_url": "https://custom-gemini.example.com"},
  99. },
  100. expected: "https://custom-gemini.example.com",
  101. },
  102. {
  103. name: "antigravity apikey auto-appends /antigravity",
  104. account: Account{
  105. Type: AccountTypeAPIKey,
  106. Platform: PlatformAntigravity,
  107. Credentials: map[string]any{"base_url": "https://upstream.example.com"},
  108. },
  109. expected: "https://upstream.example.com/antigravity",
  110. },
  111. {
  112. name: "antigravity apikey trims trailing slash",
  113. account: Account{
  114. Type: AccountTypeAPIKey,
  115. Platform: PlatformAntigravity,
  116. Credentials: map[string]any{"base_url": "https://upstream.example.com/"},
  117. },
  118. expected: "https://upstream.example.com/antigravity",
  119. },
  120. {
  121. name: "antigravity oauth does NOT append /antigravity",
  122. account: Account{
  123. Type: AccountTypeOAuth,
  124. Platform: PlatformAntigravity,
  125. Credentials: map[string]any{"base_url": "https://upstream.example.com"},
  126. },
  127. expected: "https://upstream.example.com",
  128. },
  129. {
  130. name: "oauth without base_url returns default",
  131. account: Account{
  132. Type: AccountTypeOAuth,
  133. Platform: PlatformAntigravity,
  134. Credentials: map[string]any{},
  135. },
  136. expected: defaultGeminiURL,
  137. },
  138. {
  139. name: "nil credentials returns default",
  140. account: Account{
  141. Type: AccountTypeAPIKey,
  142. Platform: PlatformGemini,
  143. },
  144. expected: defaultGeminiURL,
  145. },
  146. }
  147. for _, tt := range tests {
  148. t.Run(tt.name, func(t *testing.T) {
  149. result := tt.account.GetGeminiBaseURL(defaultGeminiURL)
  150. if result != tt.expected {
  151. t.Errorf("GetGeminiBaseURL() = %q, want %q", result, tt.expected)
  152. }
  153. })
  154. }
  155. }
  156. func TestGetGrokBaseURLUsesSubscriptionProxyForOAuth(t *testing.T) {
  157. tests := []struct {
  158. name string
  159. account Account
  160. expected string
  161. }{
  162. {
  163. name: "oauth without base_url uses CLI subscription proxy",
  164. account: Account{
  165. Type: AccountTypeOAuth,
  166. Platform: PlatformGrok,
  167. Credentials: map[string]any{},
  168. },
  169. expected: xai.DefaultCLIBaseURL,
  170. },
  171. {
  172. name: "oauth stored official API endpoint is honored (manual endpoint switch)",
  173. account: Account{
  174. Type: AccountTypeOAuth,
  175. Platform: PlatformGrok,
  176. Credentials: map[string]any{
  177. "base_url": xai.DefaultBaseURL,
  178. },
  179. },
  180. expected: xai.DefaultBaseURL,
  181. },
  182. {
  183. name: "oauth stored regional API endpoint is honored",
  184. account: Account{
  185. Type: AccountTypeOAuth,
  186. Platform: PlatformGrok,
  187. Credentials: map[string]any{
  188. "base_url": "https://us-west-2.api.x.ai/v1",
  189. },
  190. },
  191. expected: "https://us-west-2.api.x.ai/v1",
  192. },
  193. {
  194. name: "oauth stored CLI proxy is honored verbatim",
  195. account: Account{
  196. Type: AccountTypeOAuth,
  197. Platform: PlatformGrok,
  198. Credentials: map[string]any{
  199. "base_url": xai.DefaultCLIBaseURL,
  200. },
  201. },
  202. expected: xai.DefaultCLIBaseURL,
  203. },
  204. {
  205. name: "oauth unparseable base_url falls back to CLI proxy",
  206. account: Account{
  207. Type: AccountTypeOAuth,
  208. Platform: PlatformGrok,
  209. Credentials: map[string]any{
  210. "base_url": "not a url",
  211. },
  212. },
  213. expected: xai.DefaultCLIBaseURL,
  214. },
  215. {
  216. name: "oauth explicit custom base_url redirects forwarding traffic",
  217. account: Account{
  218. Type: AccountTypeOAuth,
  219. Platform: PlatformGrok,
  220. Credentials: map[string]any{
  221. "base_url": "https://custom.example.com/v1",
  222. },
  223. },
  224. expected: "https://custom.example.com/v1",
  225. },
  226. {
  227. name: "oauth custom base_url with path prefix redirects forwarding traffic",
  228. account: Account{
  229. Type: AccountTypeOAuth,
  230. Platform: PlatformGrok,
  231. Credentials: map[string]any{
  232. "base_url": "https://relay.example.com/xai/v1",
  233. },
  234. },
  235. expected: "https://relay.example.com/xai/v1",
  236. },
  237. {
  238. name: "API key without base_url uses official credit-backed API",
  239. account: Account{
  240. Type: AccountTypeAPIKey,
  241. Platform: PlatformGrok,
  242. Credentials: map[string]any{},
  243. },
  244. expected: xai.DefaultBaseURL,
  245. },
  246. }
  247. for _, tt := range tests {
  248. t.Run(tt.name, func(t *testing.T) {
  249. require.Equal(t, tt.expected, tt.account.GetGrokBaseURL())
  250. })
  251. }
  252. }
  253. func TestGetGrokBaseURLHonorsOAuthCustomRegardlessOfUnsafeOverrides(t *testing.T) {
  254. t.Setenv(xai.EnvAllowUnsafeURLOverrides, "true")
  255. account := Account{
  256. Type: AccountTypeOAuth,
  257. Platform: PlatformGrok,
  258. Credentials: map[string]any{
  259. "base_url": "https://custom.example.com/v1",
  260. },
  261. }
  262. require.Equal(t, "https://custom.example.com/v1", account.GetGrokBaseURL())
  263. }
  264. func TestGetGrokMediaBaseURLRedirectsCLIGatewayToOfficialAPI(t *testing.T) {
  265. tests := []struct {
  266. name string
  267. account Account
  268. expected string
  269. }{
  270. {
  271. name: "oauth without base_url uses official media API",
  272. account: Account{
  273. Type: AccountTypeOAuth,
  274. Platform: PlatformGrok,
  275. Credentials: map[string]any{},
  276. },
  277. expected: xai.DefaultBaseURL,
  278. },
  279. {
  280. name: "oauth stored CLI proxy is separated from the media API",
  281. account: Account{
  282. Type: AccountTypeOAuth,
  283. Platform: PlatformGrok,
  284. Credentials: map[string]any{
  285. "base_url": xai.DefaultCLIBaseURL,
  286. },
  287. },
  288. expected: xai.DefaultBaseURL,
  289. },
  290. {
  291. name: "oauth stored CLI proxy variant is canonicalized to the media API",
  292. account: Account{
  293. Type: AccountTypeOAuth,
  294. Platform: PlatformGrok,
  295. Credentials: map[string]any{
  296. "base_url": "HTTPS://CLI-CHAT-PROXY.GROK.COM:443/%76%31/",
  297. },
  298. },
  299. expected: xai.DefaultBaseURL,
  300. },
  301. {
  302. name: "oauth unparseable base_url falls back to official media API",
  303. account: Account{
  304. Type: AccountTypeOAuth,
  305. Platform: PlatformGrok,
  306. Credentials: map[string]any{
  307. "base_url": "not a url",
  308. },
  309. },
  310. expected: xai.DefaultBaseURL,
  311. },
  312. {
  313. name: "oauth stored official API endpoint is honored (manual endpoint switch)",
  314. account: Account{
  315. Type: AccountTypeOAuth,
  316. Platform: PlatformGrok,
  317. Credentials: map[string]any{
  318. "base_url": xai.DefaultBaseURL,
  319. },
  320. },
  321. expected: xai.DefaultBaseURL,
  322. },
  323. {
  324. name: "oauth stored regional API endpoint is honored for media",
  325. account: Account{
  326. Type: AccountTypeOAuth,
  327. Platform: PlatformGrok,
  328. Credentials: map[string]any{
  329. "base_url": "https://us-west-2.api.x.ai/v1",
  330. },
  331. },
  332. expected: "https://us-west-2.api.x.ai/v1",
  333. },
  334. {
  335. name: "oauth custom base_url redirects media traffic",
  336. account: Account{
  337. Type: AccountTypeOAuth,
  338. Platform: PlatformGrok,
  339. Credentials: map[string]any{
  340. "base_url": "https://custom.example.com/v1",
  341. },
  342. },
  343. expected: "https://custom.example.com/v1",
  344. },
  345. {
  346. name: "API key retains its configured media API",
  347. account: Account{
  348. Type: AccountTypeAPIKey,
  349. Platform: PlatformGrok,
  350. Credentials: map[string]any{
  351. "base_url": "https://grok.example.com/v1",
  352. },
  353. },
  354. expected: "https://grok.example.com/v1",
  355. },
  356. {
  357. name: "non-Grok account has no Grok media base URL",
  358. account: Account{
  359. Type: AccountTypeOAuth,
  360. Platform: PlatformOpenAI,
  361. Credentials: map[string]any{},
  362. },
  363. expected: "",
  364. },
  365. }
  366. for _, tt := range tests {
  367. t.Run(tt.name, func(t *testing.T) {
  368. require.Equal(t, tt.expected, tt.account.GetGrokMediaBaseURL())
  369. })
  370. }
  371. }
  372. func TestGetGrokMediaBaseURLHonorsOAuthCustomRegardlessOfUnsafeOverrides(t *testing.T) {
  373. t.Setenv(xai.EnvAllowUnsafeURLOverrides, "true")
  374. account := Account{
  375. Type: AccountTypeOAuth,
  376. Platform: PlatformGrok,
  377. Credentials: map[string]any{
  378. "base_url": "https://custom.example.com/v1",
  379. },
  380. }
  381. require.Equal(t, "https://custom.example.com/v1", account.GetGrokMediaBaseURL())
  382. }