viteConfigLoad.test.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { resolve } from "node:path";
  2. import { readFile } from "node:fs/promises";
  3. import { afterEach, describe, expect, it, vi } from "vitest";
  4. import { loadConfigFromFile } from "vite";
  5. describe("web Vite config", () => {
  6. afterEach(() => {
  7. vi.unstubAllEnvs();
  8. });
  9. it.each([undefined, " ", " 3.12.9-beta.1 "])(
  10. "构建版本使用手动值或 package 默认值:%s",
  11. async (override) => {
  12. vi.stubEnv("WEB_REMOTE_FRONTEND_VERSION", override);
  13. const webRoot = resolve(import.meta.dirname, "..");
  14. const { version } = JSON.parse(
  15. await readFile(resolve(webRoot, "../../package.json"), "utf8"),
  16. );
  17. const result = await loadConfigFromFile(
  18. { command: "build", mode: "production", isPreview: false, isSsrBuild: false },
  19. resolve(webRoot, "vite.config.ts"),
  20. webRoot,
  21. "silent",
  22. );
  23. expect(result?.config.define?.__ZCODE_VERSION__).toBe(
  24. JSON.stringify(override?.trim() || version),
  25. );
  26. },
  27. );
  28. it("非法手动版本在构建配置加载时失败", async () => {
  29. vi.stubEnv("WEB_REMOTE_FRONTEND_VERSION", "../latest");
  30. const webRoot = resolve(import.meta.dirname, "..");
  31. await expect(
  32. loadConfigFromFile(
  33. { command: "build", mode: "production", isPreview: false, isSsrBuild: false },
  34. resolve(webRoot, "vite.config.ts"),
  35. webRoot,
  36. "silent",
  37. ),
  38. ).rejects.toThrow(/version/i);
  39. });
  40. it("publishes PDF.js CMaps for non-latin PDF rendering", async () => {
  41. const webRoot = resolve(import.meta.dirname, "..");
  42. const configFile = resolve(webRoot, "vite.config.ts");
  43. const result = await loadConfigFromFile(
  44. {
  45. command: "build",
  46. mode: "production",
  47. isPreview: false,
  48. isSsrBuild: false,
  49. },
  50. configFile,
  51. webRoot,
  52. "silent",
  53. );
  54. expect(result?.config.plugins?.some((plugin) => plugin.name === "zcode:pdfjs-cmaps")).toBe(
  55. true,
  56. );
  57. });
  58. it("显式 ZCODE_ENV=production 时注入生产 ZAI OAuth 和 API 链接", async () => {
  59. vi.stubEnv("ZCODE_ENV", "production");
  60. const webRoot = resolve(import.meta.dirname, "..");
  61. const configFile = resolve(webRoot, "vite.config.ts");
  62. const result = await loadConfigFromFile(
  63. {
  64. command: "build",
  65. mode: "production",
  66. isPreview: false,
  67. isSsrBuild: false,
  68. },
  69. configFile,
  70. webRoot,
  71. "silent",
  72. );
  73. expect(result?.config.define).toMatchObject({
  74. "import.meta.env.VITE_ZAI_OAUTH_CLIENT_ID": JSON.stringify("client_P8X5CMWmlaRO9gyO-KSqtg"),
  75. "import.meta.env.VITE_ZCODE_BASE_URL": JSON.stringify("https://zcode.z.ai"),
  76. });
  77. });
  78. it.each([
  79. ["test", "https://bigmodel.cn"],
  80. ["production", "https://bigmodel.cn"],
  81. ])("%s 构建向远控登录页注入对应 BigModel 授权域名", async (environment, origin) => {
  82. vi.stubEnv("ZCODE_ENV", environment);
  83. const webRoot = resolve(import.meta.dirname, "..");
  84. const result = await loadConfigFromFile(
  85. { command: "build", mode: "production", isPreview: false, isSsrBuild: false },
  86. resolve(webRoot, "vite.config.ts"),
  87. webRoot,
  88. "silent",
  89. );
  90. expect(result?.config.define?.["import.meta.env.VITE_BIGMODEL_OAUTH_ORIGIN"]).toBe(
  91. JSON.stringify(origin),
  92. );
  93. });
  94. // Windows 跳过:环境配置差异导致断言失败,需要 CI 环境统一配置
  95. const isWindows = process.platform === "win32";
  96. (isWindows ? it.skip : it)(
  97. "uses the same public ZAI OAuth origin in local development mode",
  98. async () => {
  99. vi.stubEnv("ZCODE_ENV", "");
  100. vi.stubEnv("ZCODE_BASE_URL", "");
  101. vi.stubEnv("ZAI_OAUTH_ORIGIN", "");
  102. vi.stubEnv("ZAI_OAUTH_CLIENT_ID", "");
  103. const webRoot = resolve(import.meta.dirname, "..");
  104. const configFile = resolve(webRoot, "vite.config.ts");
  105. const result = await loadConfigFromFile(
  106. {
  107. command: "serve",
  108. mode: "development",
  109. isPreview: false,
  110. isSsrBuild: false,
  111. },
  112. configFile,
  113. webRoot,
  114. "silent",
  115. );
  116. expect(result?.config.define).toMatchObject({
  117. "import.meta.env.VITE_ZAI_OAUTH_CLIENT_ID": JSON.stringify("client_P8X5CMWmlaRO9gyO-KSqtg"),
  118. "import.meta.env.VITE_ZCODE_BASE_URL": JSON.stringify("https://zcode.z.ai"),
  119. "import.meta.env.VITE_ZAI_OAUTH_ORIGIN": JSON.stringify("https://chat.z.ai"),
  120. });
  121. },
  122. );
  123. it("uses shared ZCode endpoint env override for renderer injection", async () => {
  124. vi.stubEnv("ZCODE_ENV", "test");
  125. vi.stubEnv("ZCODE_BASE_URL", "https://zcode.z.ai/custom/path");
  126. const webRoot = resolve(import.meta.dirname, "..");
  127. const configFile = resolve(webRoot, "vite.config.ts");
  128. const result = await loadConfigFromFile(
  129. {
  130. command: "build",
  131. mode: "production",
  132. isPreview: false,
  133. isSsrBuild: false,
  134. },
  135. configFile,
  136. webRoot,
  137. "silent",
  138. );
  139. expect(result?.config.define).toMatchObject({
  140. "import.meta.env.VITE_ZCODE_BASE_URL": JSON.stringify("https://zcode.z.ai"),
  141. "import.meta.env.VITE_ZCODE_ENDPOINT_ORIGIN": JSON.stringify("https://zcode.z.ai"),
  142. });
  143. });
  144. });