zcodeEndpoint.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. DEFAULT_BIGMODEL_API_ORIGIN,
  4. DEFAULT_WEB_REMOTE_CONTROL_RELAY_WS_URL,
  5. DEFAULT_ZAI_BUSINESS_BASE_URL,
  6. DEFAULT_ZAI_OAUTH_CLIENT_ID,
  7. DEFAULT_ZAI_OAUTH_ORIGIN,
  8. DEFAULT_ZCODE_ENDPOINT_ORIGIN,
  9. TEST_BIGMODEL_API_ORIGIN,
  10. TEST_ZAI_BUSINESS_BASE_URL,
  11. TEST_ZAI_OAUTH_CLIENT_ID,
  12. TEST_ZAI_OAUTH_ORIGIN,
  13. TEST_ZCODE_ENDPOINT_ORIGIN,
  14. buildBigModelCodingPlanPersonalManageUrl,
  15. buildBigModelCodingPlanTeamManageUrl,
  16. buildBigModelApiUrl,
  17. buildRuntimeZCodeApiUrl,
  18. buildRuntimeZaiBusinessUrl,
  19. buildRuntimeZaiOAuthUrl,
  20. buildRuntimeZCodeEndpointUrls,
  21. buildZCodeEndpointUrls,
  22. normalizeZCodeEndpointOrigin,
  23. resolveBigModelApiOrigin,
  24. resolveRuntimeProductEndpointConfig,
  25. resolveWebRemoteControlRelayWsUrl,
  26. resolveZaiBusinessBaseUrl,
  27. resolveZaiOAuthClientId,
  28. resolveZaiOAuthOrigin,
  29. resolveZCodeEndpointOrigin,
  30. } from "../src/zcodeEndpoint.js";
  31. import { appSettingsPatchSchema, appSettingsSchema } from "../src/validationAppSettings.js";
  32. describe("zcodeEndpoint", () => {
  33. it("defaults to the scoped origin in every env without env base url", () => {
  34. expect(resolveZCodeEndpointOrigin({ env: "test" })).toBe(TEST_ZCODE_ENDPOINT_ORIGIN);
  35. expect(resolveZCodeEndpointOrigin({ env: "production" })).toBe(DEFAULT_ZCODE_ENDPOINT_ORIGIN);
  36. });
  37. it("uses ZCODE_BASE_URL style env base as the default endpoint", () => {
  38. expect(
  39. resolveZCodeEndpointOrigin({
  40. env: "test",
  41. envBaseOrigin: "https://zcode.z.ai/api/v1",
  42. }),
  43. ).toBe(TEST_ZCODE_ENDPOINT_ORIGIN);
  44. expect(
  45. resolveZCodeEndpointOrigin({
  46. env: "production",
  47. envBaseOrigin: "https://zcode.z.ai/api/v1",
  48. }),
  49. ).toBe(DEFAULT_ZCODE_ENDPOINT_ORIGIN);
  50. });
  51. it("keeps the non-production settings override above env base url", () => {
  52. expect(
  53. resolveZCodeEndpointOrigin({
  54. env: "test",
  55. envBaseOrigin: "https://zcode.z.ai",
  56. overrideOrigin: "http://localhost:3030/api/v1",
  57. }),
  58. ).toBe("http://localhost:3030");
  59. });
  60. it("uses a non-production override after normalizing to origin", () => {
  61. expect(
  62. resolveZCodeEndpointOrigin({
  63. env: "test",
  64. overrideOrigin: "https://zcode.z.ai/api/v1/ignored?x=1",
  65. }),
  66. ).toBe(TEST_ZCODE_ENDPOINT_ORIGIN);
  67. });
  68. it("ignores overrides in production", () => {
  69. expect(
  70. resolveZCodeEndpointOrigin({
  71. env: "production",
  72. envBaseOrigin: "https://zcode.z.ai",
  73. overrideOrigin: "https://zcode.z.ai",
  74. }),
  75. ).toBe(DEFAULT_ZCODE_ENDPOINT_ORIGIN);
  76. });
  77. it("derives http and websocket urls from the same origin", () => {
  78. expect(buildZCodeEndpointUrls("http://localhost:3030")).toEqual({
  79. origin: "http://localhost:3030",
  80. apiBaseUrl: "http://localhost:3030/api/v1",
  81. remoteUrl: "http://localhost:3030/remote/v3",
  82. webRemoteCallbackUrl: "http://localhost:3030/web-remote/callback",
  83. webShareCallbackUrl: "http://localhost:3030/cn/share/callback",
  84. relayWsUrl: "ws://localhost:3030/ws",
  85. zcodePlanOpenAiBaseUrl: "http://localhost:3030/api/v1/zcode-plan",
  86. zcodePlanAnthropicBaseUrl: "http://localhost:3030/api/v1/zcode-plan/anthropic",
  87. zcodePlanBillingCurrentUrl: "http://localhost:3030/api/v1/zcode-plan/billing/current",
  88. zcodePlanBillingBalanceUrl: "http://localhost:3030/api/v1/zcode-plan/billing/balance",
  89. });
  90. });
  91. it("uses remote v3 for app versions below 3.4.0", () => {
  92. expect(buildZCodeEndpointUrls("https://zcode.z.ai", { appVersion: "3.3.9" }).remoteUrl).toBe(
  93. "https://zcode.z.ai/remote/v3",
  94. );
  95. });
  96. it("uses remote v4 for app versions at or above 3.4.0", () => {
  97. expect(buildZCodeEndpointUrls("https://zcode.z.ai", { appVersion: "3.4.0" }).remoteUrl).toBe(
  98. "https://zcode.z.ai/remote/v4",
  99. );
  100. expect(buildZCodeEndpointUrls("https://zcode.z.ai", { appVersion: "3.4.1" }).remoteUrl).toBe(
  101. "https://zcode.z.ai/remote/v4",
  102. );
  103. expect(
  104. buildZCodeEndpointUrls("https://zcode.z.ai", { appVersion: "3.5.0-beta.1" }).remoteUrl,
  105. ).toBe("https://zcode.z.ai/remote/v4");
  106. });
  107. it("keeps prerelease and invalid app versions on remote v3", () => {
  108. expect(
  109. buildZCodeEndpointUrls("https://zcode.z.ai", { appVersion: "3.4.0-beta.1" }).remoteUrl,
  110. ).toBe("https://zcode.z.ai/remote/v3");
  111. expect(buildZCodeEndpointUrls("https://zcode.z.ai", { appVersion: "dev" }).remoteUrl).toBe(
  112. "https://zcode.z.ai/remote/v3",
  113. );
  114. expect(buildZCodeEndpointUrls("https://zcode.z.ai", { appVersion: "03.4.0" }).remoteUrl).toBe(
  115. "https://zcode.z.ai/remote/v3",
  116. );
  117. expect(
  118. buildZCodeEndpointUrls("https://zcode.z.ai", { appVersion: "3.4.1-alpha..1" }).remoteUrl,
  119. ).toBe("https://zcode.z.ai/remote/v3");
  120. expect(buildZCodeEndpointUrls("https://zcode.z.ai", { appVersion: "3.4.1-01" }).remoteUrl).toBe(
  121. "https://zcode.z.ai/remote/v3",
  122. );
  123. });
  124. it("derives runtime zcode-plan urls from ZCODE_ENV", () => {
  125. expect(buildRuntimeZCodeEndpointUrls({ ZCODE_ENV: "test" }).zcodePlanOpenAiBaseUrl).toBe(
  126. "https://zcode.z.ai/api/v1/zcode-plan",
  127. );
  128. expect(buildRuntimeZCodeEndpointUrls({ ZCODE_ENV: "production" }).zcodePlanOpenAiBaseUrl).toBe(
  129. "https://zcode.z.ai/api/v1/zcode-plan",
  130. );
  131. expect(buildRuntimeZCodeEndpointUrls({}).zcodePlanOpenAiBaseUrl).toBe(
  132. "https://zcode.z.ai/api/v1/zcode-plan",
  133. );
  134. expect(
  135. buildRuntimeZCodeEndpointUrls({
  136. ZCODE_ENV: "test",
  137. ZCODE_BASE_URL: "https://zcode.z.ai/ignored/path",
  138. }).zcodePlanAnthropicBaseUrl,
  139. ).toBe("https://zcode.z.ai/api/v1/zcode-plan/anthropic");
  140. });
  141. it("uses the test relay only for the standard test endpoint", () => {
  142. expect(
  143. resolveWebRemoteControlRelayWsUrl({
  144. endpointOrigin: TEST_ZCODE_ENDPOINT_ORIGIN,
  145. }),
  146. ).toBe("wss://zcode.z.ai/ws");
  147. expect(
  148. resolveWebRemoteControlRelayWsUrl({
  149. endpointOrigin: DEFAULT_ZCODE_ENDPOINT_ORIGIN,
  150. }),
  151. ).toBe(DEFAULT_WEB_REMOTE_CONTROL_RELAY_WS_URL);
  152. expect(
  153. resolveWebRemoteControlRelayWsUrl({
  154. endpointOrigin: "http://localhost:3030",
  155. }),
  156. ).toBe(DEFAULT_WEB_REMOTE_CONTROL_RELAY_WS_URL);
  157. });
  158. it("allows an explicit web remote control relay url override", () => {
  159. expect(
  160. resolveWebRemoteControlRelayWsUrl({
  161. endpointOrigin: TEST_ZCODE_ENDPOINT_ORIGIN,
  162. overrideUrl: " ws://localhost:8787/ws ",
  163. }),
  164. ).toBe("ws://localhost:8787/ws");
  165. });
  166. it("derives BigModel API origin from ZCODE_ENV", () => {
  167. expect(resolveBigModelApiOrigin({ ZCODE_ENV: "production" })).toBe("https://bigmodel.cn");
  168. expect(resolveBigModelApiOrigin({ ZCODE_ENV: "test" })).toBe("https://bigmodel.cn");
  169. expect(buildBigModelApiUrl({ ZCODE_ENV: "test" }, "/api/biz/subscription/list")).toBe(
  170. "https://bigmodel.cn/api/biz/subscription/list",
  171. );
  172. });
  173. it("derives BigModel Coding Plan manage urls from ZCODE_ENV", () => {
  174. expect(buildBigModelCodingPlanPersonalManageUrl({ ZCODE_ENV: "test" })).toBe(
  175. "https://bigmodel.cn/coding-plan/personal/overview",
  176. );
  177. expect(buildBigModelCodingPlanPersonalManageUrl({ ZCODE_ENV: "production" })).toBe(
  178. "https://bigmodel.cn/coding-plan/personal/overview",
  179. );
  180. expect(buildBigModelCodingPlanTeamManageUrl({ ZCODE_ENV: "test" })).toBe(
  181. "https://bigmodel.cn/coding-plan/team/plans",
  182. );
  183. expect(buildBigModelCodingPlanTeamManageUrl({ ZCODE_ENV: "production" })).toBe(
  184. "https://bigmodel.cn/coding-plan/team/plans",
  185. );
  186. });
  187. it("resolves all product endpoints for the test product environment", () => {
  188. expect(resolveRuntimeProductEndpointConfig({ ZCODE_ENV: "test" })).toMatchObject({
  189. zcodeEnv: "test",
  190. zcodeEndpointOrigin: TEST_ZCODE_ENDPOINT_ORIGIN,
  191. zaiOAuthOrigin: TEST_ZAI_OAUTH_ORIGIN,
  192. zaiBusinessBaseUrl: TEST_ZAI_BUSINESS_BASE_URL,
  193. zaiOAuthClientId: TEST_ZAI_OAUTH_CLIENT_ID,
  194. bigModelApiOrigin: TEST_BIGMODEL_API_ORIGIN,
  195. });
  196. });
  197. it("resolves all product endpoints for the production product environment", () => {
  198. expect(resolveRuntimeProductEndpointConfig({ ZCODE_ENV: "production" })).toMatchObject({
  199. zcodeEnv: "production",
  200. zcodeEndpointOrigin: DEFAULT_ZCODE_ENDPOINT_ORIGIN,
  201. zaiOAuthOrigin: DEFAULT_ZAI_OAUTH_ORIGIN,
  202. zaiBusinessBaseUrl: DEFAULT_ZAI_BUSINESS_BASE_URL,
  203. zaiOAuthClientId: DEFAULT_ZAI_OAUTH_CLIENT_ID,
  204. bigModelApiOrigin: DEFAULT_BIGMODEL_API_ORIGIN,
  205. });
  206. });
  207. it("builds product endpoint URLs from the selected environment", () => {
  208. expect(buildRuntimeZCodeApiUrl({ ZCODE_ENV: "test" }, "/api/v1/oauth/token")).toBe(
  209. "https://zcode.z.ai/api/v1/oauth/token",
  210. );
  211. expect(buildRuntimeZaiOAuthUrl({ ZCODE_ENV: "test" }, "/api/oauth/authorize")).toBe(
  212. "https://chat.z.ai/api/oauth/authorize",
  213. );
  214. expect(buildRuntimeZaiBusinessUrl({ ZCODE_ENV: "test" }, "/api/auth/z/login")).toBe(
  215. "https://api.z.ai/api/auth/z/login",
  216. );
  217. expect(buildBigModelApiUrl({ ZCODE_ENV: "test" }, "/login")).toBe(
  218. "https://bigmodel.cn/login",
  219. );
  220. });
  221. it("keeps environment-scoped endpoint overrides centralized", () => {
  222. const env = {
  223. ZCODE_ENV: "test",
  224. ZCODE_TEST_BASE_URL: "https://test-zcode.example.com/path",
  225. ZAI_TEST_OAUTH_ORIGIN: "https://test-oauth.example.com/oauth",
  226. ZAI_TEST_BUSINESS_BASE_URL: "https://test-business.example.com/api",
  227. ZAI_TEST_OAUTH_CLIENT_ID: "client_test_override",
  228. BIGMODEL_TEST_API_BASE_URL: "https://test-bigmodel.example.com/base",
  229. };
  230. expect(resolveRuntimeProductEndpointConfig(env)).toMatchObject({
  231. zcodeEndpointOrigin: "https://test-zcode.example.com",
  232. zaiOAuthOrigin: "https://test-oauth.example.com",
  233. zaiBusinessBaseUrl: "https://test-business.example.com",
  234. zaiOAuthClientId: "client_test_override",
  235. bigModelApiOrigin: "https://test-bigmodel.example.com",
  236. });
  237. });
  238. it("keeps unscoped endpoint overrides above scoped endpoint overrides", () => {
  239. const env = {
  240. ZCODE_ENV: "test",
  241. ZAI_OAUTH_ORIGIN: "https://oauth-global.example.com",
  242. ZAI_TEST_OAUTH_ORIGIN: "https://oauth-test.example.com",
  243. ZAI_BUSINESS_BASE_URL: "https://business-global.example.com",
  244. ZAI_TEST_BUSINESS_BASE_URL: "https://business-test.example.com",
  245. ZAI_OAUTH_CLIENT_ID: "client_global",
  246. ZAI_TEST_OAUTH_CLIENT_ID: "client_test",
  247. BIGMODEL_API_BASE_URL: "https://bigmodel-global.example.com",
  248. BIGMODEL_TEST_API_BASE_URL: "https://bigmodel-test.example.com",
  249. };
  250. expect(resolveZaiOAuthOrigin(env)).toBe("https://oauth-global.example.com");
  251. expect(resolveZaiBusinessBaseUrl(env)).toBe("https://business-global.example.com");
  252. expect(resolveZaiOAuthClientId(env)).toBe("client_global");
  253. expect(resolveBigModelApiOrigin(env)).toBe("https://bigmodel-global.example.com");
  254. });
  255. it("ignores blank runtime endpoint overrides", () => {
  256. const env = {
  257. ZCODE_ENV: "test",
  258. ZCODE_BASE_URL: " ",
  259. ZCODE_TEST_BASE_URL: "\t",
  260. ZAI_OAUTH_ORIGIN: "",
  261. ZAI_TEST_OAUTH_ORIGIN: " ",
  262. ZAI_BUSINESS_BASE_URL: " ",
  263. ZAI_TEST_BUSINESS_BASE_URL: "",
  264. ZAI_OAUTH_CLIENT_ID: "",
  265. ZAI_TEST_OAUTH_CLIENT_ID: " ",
  266. BIGMODEL_API_BASE_URL: " ",
  267. BIGMODEL_TEST_API_BASE_URL: "",
  268. };
  269. expect(resolveRuntimeProductEndpointConfig(env)).toMatchObject({
  270. zcodeEndpointOrigin: TEST_ZCODE_ENDPOINT_ORIGIN,
  271. zaiOAuthOrigin: TEST_ZAI_OAUTH_ORIGIN,
  272. zaiBusinessBaseUrl: TEST_ZAI_BUSINESS_BASE_URL,
  273. zaiOAuthClientId: TEST_ZAI_OAUTH_CLIENT_ID,
  274. bigModelApiOrigin: TEST_BIGMODEL_API_ORIGIN,
  275. });
  276. });
  277. it("rejects non-http endpoint origins", () => {
  278. expect(() => normalizeZCodeEndpointOrigin("file:///tmp/zcode")).toThrow(/http or https/);
  279. });
  280. it("persists optional endpoint override in app settings", () => {
  281. expect(
  282. appSettingsSchema.parse({ zcodeEndpointOrigin: "https://zcode.z.ai/path" })
  283. .zcodeEndpointOrigin,
  284. ).toBe(TEST_ZCODE_ENDPOINT_ORIGIN);
  285. expect(
  286. appSettingsPatchSchema.parse({ zcodeEndpointOrigin: "http://localhost:3030/foo" })
  287. .zcodeEndpointOrigin,
  288. ).toBe("http://localhost:3030");
  289. });
  290. });