| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- import { describe, expect, it } from "vitest";
- import {
- DEFAULT_BIGMODEL_API_ORIGIN,
- DEFAULT_WEB_REMOTE_CONTROL_RELAY_WS_URL,
- DEFAULT_ZAI_BUSINESS_BASE_URL,
- DEFAULT_ZAI_OAUTH_CLIENT_ID,
- DEFAULT_ZAI_OAUTH_ORIGIN,
- DEFAULT_ZCODE_ENDPOINT_ORIGIN,
- TEST_BIGMODEL_API_ORIGIN,
- TEST_ZAI_BUSINESS_BASE_URL,
- TEST_ZAI_OAUTH_CLIENT_ID,
- TEST_ZAI_OAUTH_ORIGIN,
- TEST_ZCODE_ENDPOINT_ORIGIN,
- buildBigModelCodingPlanPersonalManageUrl,
- buildBigModelCodingPlanTeamManageUrl,
- buildBigModelApiUrl,
- buildRuntimeZCodeApiUrl,
- buildRuntimeZaiBusinessUrl,
- buildRuntimeZaiOAuthUrl,
- buildRuntimeZCodeEndpointUrls,
- buildZCodeEndpointUrls,
- normalizeZCodeEndpointOrigin,
- resolveBigModelApiOrigin,
- resolveRuntimeProductEndpointConfig,
- resolveWebRemoteControlRelayWsUrl,
- resolveZaiBusinessBaseUrl,
- resolveZaiOAuthClientId,
- resolveZaiOAuthOrigin,
- resolveZCodeEndpointOrigin,
- } from "../src/zcodeEndpoint.js";
- import { appSettingsPatchSchema, appSettingsSchema } from "../src/validationAppSettings.js";
- describe("zcodeEndpoint", () => {
- it("defaults to the scoped origin in every env without env base url", () => {
- expect(resolveZCodeEndpointOrigin({ env: "test" })).toBe(TEST_ZCODE_ENDPOINT_ORIGIN);
- expect(resolveZCodeEndpointOrigin({ env: "production" })).toBe(DEFAULT_ZCODE_ENDPOINT_ORIGIN);
- });
- it("keeps the non-production settings override above env base url", () => {
- expect(
- resolveZCodeEndpointOrigin({
- env: "test",
- envBaseOrigin: "https://zcode.z.ai",
- overrideOrigin: "http://localhost:3030/api/v1",
- }),
- ).toBe("http://localhost:3030");
- });
- it("uses a non-production override after normalizing to origin", () => {
- expect(
- resolveZCodeEndpointOrigin({
- env: "test",
- overrideOrigin: "https://zcode.z.ai/api/v1/ignored?x=1",
- }),
- ).toBe(TEST_ZCODE_ENDPOINT_ORIGIN);
- });
- it("ignores overrides in production", () => {
- expect(
- resolveZCodeEndpointOrigin({
- env: "production",
- envBaseOrigin: "https://zcode.z.ai",
- overrideOrigin: "https://zcode.z.ai",
- }),
- ).toBe(DEFAULT_ZCODE_ENDPOINT_ORIGIN);
- });
- it("derives http and websocket urls from the same origin", () => {
- expect(buildZCodeEndpointUrls("http://localhost:3030")).toEqual({
- origin: "http://localhost:3030",
- apiBaseUrl: "http://localhost:3030/api/v1",
- remoteUrl: "http://localhost:3030/remote/v3",
- webRemoteCallbackUrl: "http://localhost:3030/remote/v4",
- webShareCallbackUrl: "http://localhost:3030/cn/share/callback",
- relayWsUrl: "ws://localhost:3030/api/v1/remote/ws2",
- zcodePlanOpenAiBaseUrl: "http://localhost:3030/api/v1/zcode-plan",
- zcodePlanAnthropicBaseUrl: "http://localhost:3030/api/v1/zcode-plan/anthropic",
- zcodePlanBillingCurrentUrl: "http://localhost:3030/api/v1/zcode-plan/billing/current",
- zcodePlanBillingBalanceUrl: "http://localhost:3030/api/v1/zcode-plan/billing/balance",
- });
- });
- it("uses remote v3 for app versions below 3.4.0", () => {
- expect(buildZCodeEndpointUrls("https://zcode.z.ai", { appVersion: "3.3.9" }).remoteUrl).toBe(
- "https://zcode.z.ai/remote/v3",
- );
- });
- it("uses remote v4 for app versions at or above 3.4.0", () => {
- expect(buildZCodeEndpointUrls("https://zcode.z.ai", { appVersion: "3.4.0" }).remoteUrl).toBe(
- "https://zcode.z.ai/remote/v4",
- );
- expect(buildZCodeEndpointUrls("https://zcode.z.ai", { appVersion: "3.4.1" }).remoteUrl).toBe(
- "https://zcode.z.ai/remote/v4",
- );
- expect(
- buildZCodeEndpointUrls("https://zcode.z.ai", { appVersion: "3.5.0-beta.1" }).remoteUrl,
- ).toBe("https://zcode.z.ai/remote/v4");
- });
- it("keeps prerelease and invalid app versions on remote v3", () => {
- expect(
- buildZCodeEndpointUrls("https://zcode.z.ai", { appVersion: "3.4.0-beta.1" }).remoteUrl,
- ).toBe("https://zcode.z.ai/remote/v3");
- expect(buildZCodeEndpointUrls("https://zcode.z.ai", { appVersion: "dev" }).remoteUrl).toBe(
- "https://zcode.z.ai/remote/v3",
- );
- expect(buildZCodeEndpointUrls("https://zcode.z.ai", { appVersion: "03.4.0" }).remoteUrl).toBe(
- "https://zcode.z.ai/remote/v3",
- );
- expect(
- buildZCodeEndpointUrls("https://zcode.z.ai", { appVersion: "3.4.1-alpha..1" }).remoteUrl,
- ).toBe("https://zcode.z.ai/remote/v3");
- expect(buildZCodeEndpointUrls("https://zcode.z.ai", { appVersion: "3.4.1-01" }).remoteUrl).toBe(
- "https://zcode.z.ai/remote/v3",
- );
- });
- it("derives runtime zcode-plan urls from ZCODE_ENV", () => {
- expect(buildRuntimeZCodeEndpointUrls({ ZCODE_ENV: "test" }).zcodePlanOpenAiBaseUrl).toBe(
- "https://zcode.z.ai/api/v1/zcode-plan",
- );
- expect(buildRuntimeZCodeEndpointUrls({ ZCODE_ENV: "production" }).zcodePlanOpenAiBaseUrl).toBe(
- "https://zcode.z.ai/api/v1/zcode-plan",
- );
- expect(buildRuntimeZCodeEndpointUrls({}).zcodePlanOpenAiBaseUrl).toBe(
- "https://zcode.z.ai/api/v1/zcode-plan",
- );
- expect(
- buildRuntimeZCodeEndpointUrls({
- ZCODE_ENV: "test",
- ZCODE_BASE_URL: "https://zcode.z.ai/ignored/path",
- }).zcodePlanAnthropicBaseUrl,
- ).toBe("https://zcode.z.ai/api/v1/zcode-plan/anthropic");
- });
- it("derives V2 relay from each endpoint origin", () => {
- expect(
- resolveWebRemoteControlRelayWsUrl({
- endpointOrigin: TEST_ZCODE_ENDPOINT_ORIGIN,
- }),
- ).toBe("wss://zcode.z.ai/api/v1/remote/ws2");
- expect(
- resolveWebRemoteControlRelayWsUrl({
- endpointOrigin: DEFAULT_ZCODE_ENDPOINT_ORIGIN,
- }),
- ).toBe(DEFAULT_WEB_REMOTE_CONTROL_RELAY_WS_URL);
- expect(
- resolveWebRemoteControlRelayWsUrl({
- endpointOrigin: "http://localhost:3030",
- }),
- ).toBe("ws://localhost:3030/api/v1/remote/ws2");
- });
- it("allows an explicit web remote control relay url override", () => {
- expect(
- resolveWebRemoteControlRelayWsUrl({
- endpointOrigin: TEST_ZCODE_ENDPOINT_ORIGIN,
- overrideUrl: " ws://localhost:8787/api/v1/remote/ws2 ",
- }),
- ).toBe("ws://localhost:8787/api/v1/remote/ws2");
- });
- it("derives BigModel API origin from ZCODE_ENV", () => {
- expect(resolveBigModelApiOrigin({ ZCODE_ENV: "production" })).toBe("https://bigmodel.cn");
- expect(resolveBigModelApiOrigin({ ZCODE_ENV: "test" })).toBe("https://bigmodel.cn");
- expect(buildBigModelApiUrl({ ZCODE_ENV: "test" }, "/api/biz/subscription/list")).toBe(
- "https://bigmodel.cn/api/biz/subscription/list",
- );
- });
- it("derives BigModel Coding Plan manage urls from ZCODE_ENV", () => {
- expect(buildBigModelCodingPlanPersonalManageUrl({ ZCODE_ENV: "test" })).toBe(
- "https://bigmodel.cn/coding-plan/personal/overview",
- );
- expect(buildBigModelCodingPlanPersonalManageUrl({ ZCODE_ENV: "production" })).toBe(
- "https://bigmodel.cn/coding-plan/personal/overview",
- );
- expect(buildBigModelCodingPlanTeamManageUrl({ ZCODE_ENV: "test" })).toBe(
- "https://bigmodel.cn/coding-plan/team/plans",
- );
- expect(buildBigModelCodingPlanTeamManageUrl({ ZCODE_ENV: "production" })).toBe(
- "https://bigmodel.cn/coding-plan/team/plans",
- );
- });
- it("resolves all product endpoints for the test product environment", () => {
- expect(resolveRuntimeProductEndpointConfig({ ZCODE_ENV: "test" })).toMatchObject({
- zcodeEnv: "test",
- zcodeEndpointOrigin: TEST_ZCODE_ENDPOINT_ORIGIN,
- zaiOAuthOrigin: TEST_ZAI_OAUTH_ORIGIN,
- zaiBusinessBaseUrl: TEST_ZAI_BUSINESS_BASE_URL,
- zaiOAuthClientId: TEST_ZAI_OAUTH_CLIENT_ID,
- bigModelApiOrigin: TEST_BIGMODEL_API_ORIGIN,
- });
- });
- it("resolves all product endpoints for the production product environment", () => {
- expect(resolveRuntimeProductEndpointConfig({ ZCODE_ENV: "production" })).toMatchObject({
- zcodeEnv: "production",
- zcodeEndpointOrigin: DEFAULT_ZCODE_ENDPOINT_ORIGIN,
- zaiOAuthOrigin: DEFAULT_ZAI_OAUTH_ORIGIN,
- zaiBusinessBaseUrl: DEFAULT_ZAI_BUSINESS_BASE_URL,
- zaiOAuthClientId: DEFAULT_ZAI_OAUTH_CLIENT_ID,
- bigModelApiOrigin: DEFAULT_BIGMODEL_API_ORIGIN,
- });
- });
- it("builds product endpoint URLs from the selected environment", () => {
- expect(buildRuntimeZCodeApiUrl({ ZCODE_ENV: "test" }, "/api/v1/oauth/token")).toBe(
- "https://zcode.z.ai/api/v1/oauth/token",
- );
- expect(buildRuntimeZaiOAuthUrl({ ZCODE_ENV: "test" }, "/api/oauth/authorize")).toBe(
- "https://chat.z.ai/api/oauth/authorize",
- );
- expect(buildRuntimeZaiBusinessUrl({ ZCODE_ENV: "test" }, "/api/auth/z/login")).toBe(
- "https://api.z.ai/api/auth/z/login",
- );
- expect(buildBigModelApiUrl({ ZCODE_ENV: "test" }, "/login")).toBe(
- "https://bigmodel.cn/login",
- );
- });
- it("keeps environment-scoped endpoint overrides centralized", () => {
- const env = {
- ZCODE_ENV: "test",
- ZCODE_TEST_BASE_URL: "https://test-zcode.example.com/path",
- ZAI_TEST_OAUTH_ORIGIN: "https://test-oauth.example.com/oauth",
- ZAI_TEST_BUSINESS_BASE_URL: "https://test-business.example.com/api",
- ZAI_TEST_OAUTH_CLIENT_ID: "client_test_override",
- BIGMODEL_TEST_API_BASE_URL: "https://test-bigmodel.example.com/base",
- };
- expect(resolveRuntimeProductEndpointConfig(env)).toMatchObject({
- zcodeEndpointOrigin: "https://test-zcode.example.com",
- zaiOAuthOrigin: "https://test-oauth.example.com",
- zaiBusinessBaseUrl: "https://test-business.example.com",
- zaiOAuthClientId: "client_test_override",
- bigModelApiOrigin: "https://test-bigmodel.example.com",
- });
- });
- it("keeps unscoped endpoint overrides above scoped endpoint overrides", () => {
- const env = {
- ZCODE_ENV: "test",
- ZAI_OAUTH_ORIGIN: "https://oauth-global.example.com",
- ZAI_TEST_OAUTH_ORIGIN: "https://oauth-test.example.com",
- ZAI_BUSINESS_BASE_URL: "https://business-global.example.com",
- ZAI_TEST_BUSINESS_BASE_URL: "https://business-test.example.com",
- ZAI_OAUTH_CLIENT_ID: "client_global",
- ZAI_TEST_OAUTH_CLIENT_ID: "client_test",
- BIGMODEL_API_BASE_URL: "https://bigmodel-global.example.com",
- BIGMODEL_TEST_API_BASE_URL: "https://bigmodel-test.example.com",
- };
- expect(resolveZaiOAuthOrigin(env)).toBe("https://oauth-global.example.com");
- expect(resolveZaiBusinessBaseUrl(env)).toBe("https://business-global.example.com");
- expect(resolveZaiOAuthClientId(env)).toBe("client_global");
- expect(resolveBigModelApiOrigin(env)).toBe("https://bigmodel-global.example.com");
- });
- it("ignores blank runtime endpoint overrides", () => {
- const env = {
- ZCODE_ENV: "test",
- ZCODE_BASE_URL: " ",
- ZCODE_TEST_BASE_URL: "\t",
- ZAI_OAUTH_ORIGIN: "",
- ZAI_TEST_OAUTH_ORIGIN: " ",
- ZAI_BUSINESS_BASE_URL: " ",
- ZAI_TEST_BUSINESS_BASE_URL: "",
- ZAI_OAUTH_CLIENT_ID: "",
- ZAI_TEST_OAUTH_CLIENT_ID: " ",
- BIGMODEL_API_BASE_URL: " ",
- BIGMODEL_TEST_API_BASE_URL: "",
- };
- expect(resolveRuntimeProductEndpointConfig(env)).toMatchObject({
- zcodeEndpointOrigin: TEST_ZCODE_ENDPOINT_ORIGIN,
- zaiOAuthOrigin: TEST_ZAI_OAUTH_ORIGIN,
- zaiBusinessBaseUrl: TEST_ZAI_BUSINESS_BASE_URL,
- zaiOAuthClientId: TEST_ZAI_OAUTH_CLIENT_ID,
- bigModelApiOrigin: TEST_BIGMODEL_API_ORIGIN,
- });
- });
- it("rejects non-http endpoint origins", () => {
- expect(() => normalizeZCodeEndpointOrigin("file:///tmp/zcode")).toThrow(/http or https/);
- });
- it("persists optional endpoint override in app settings", () => {
- expect(
- appSettingsSchema.parse({ zcodeEndpointOrigin: "https://zcode.z.ai/path" })
- .zcodeEndpointOrigin,
- ).toBe(TEST_ZCODE_ENDPOINT_ORIGIN);
- expect(
- appSettingsPatchSchema.parse({ zcodeEndpointOrigin: "http://localhost:3030/foo" })
- .zcodeEndpointOrigin,
- ).toBe("http://localhost:3030");
- });
- });
|