zcodeEndpoint.test.ts 12 KB

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