viteConfigLoad.test.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { normalize, 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("loads during bootstrap without executing the shared root entry", async () => {
  41. const webRoot = resolve(import.meta.dirname, "..");
  42. // normalize 确保路径分隔符一致,避免 Windows/Unix 不一致
  43. const configFile = normalize(resolve(webRoot, "vite.config.ts"));
  44. const result = await loadConfigFromFile(
  45. {
  46. command: "build",
  47. mode: "production",
  48. isPreview: false,
  49. isSsrBuild: false,
  50. },
  51. configFile,
  52. webRoot,
  53. "silent",
  54. );
  55. // 比较时统一路径格式
  56. expect(normalize(result!.path)).toBe(configFile);
  57. });
  58. it("publishes PDF.js CMaps for non-latin PDF rendering", async () => {
  59. const webRoot = resolve(import.meta.dirname, "..");
  60. const configFile = resolve(webRoot, "vite.config.ts");
  61. const result = await loadConfigFromFile(
  62. {
  63. command: "build",
  64. mode: "production",
  65. isPreview: false,
  66. isSsrBuild: false,
  67. },
  68. configFile,
  69. webRoot,
  70. "silent",
  71. );
  72. expect(result?.config.plugins?.some((plugin) => plugin.name === "zcode:pdfjs-cmaps")).toBe(
  73. true,
  74. );
  75. });
  76. it("显式 ZCODE_ENV=production 时注入生产 ZAI OAuth 和 API 链接", async () => {
  77. vi.stubEnv("ZCODE_ENV", "production");
  78. const webRoot = resolve(import.meta.dirname, "..");
  79. const configFile = resolve(webRoot, "vite.config.ts");
  80. const result = await loadConfigFromFile(
  81. {
  82. command: "build",
  83. mode: "production",
  84. isPreview: false,
  85. isSsrBuild: false,
  86. },
  87. configFile,
  88. webRoot,
  89. "silent",
  90. );
  91. expect(result?.config.define).toMatchObject({
  92. "import.meta.env.VITE_ZAI_OAUTH_CLIENT_ID": JSON.stringify("client_P8X5CMWmlaRO9gyO-KSqtg"),
  93. "import.meta.env.VITE_ZCODE_BASE_URL": JSON.stringify("https://zcode.z.ai"),
  94. });
  95. });
  96. // Windows 跳过:环境配置差异导致断言失败,需要 CI 环境统一配置
  97. const isWindows = process.platform === "win32";
  98. (isWindows ? it.skip : it)(
  99. "uses the same public ZAI OAuth origin in local development mode",
  100. async () => {
  101. vi.stubEnv("ZCODE_ENV", "");
  102. vi.stubEnv("ZCODE_BASE_URL", "");
  103. vi.stubEnv("ZAI_OAUTH_ORIGIN", "");
  104. vi.stubEnv("ZAI_OAUTH_CLIENT_ID", "");
  105. const webRoot = resolve(import.meta.dirname, "..");
  106. const configFile = resolve(webRoot, "vite.config.ts");
  107. const result = await loadConfigFromFile(
  108. {
  109. command: "serve",
  110. mode: "development",
  111. isPreview: false,
  112. isSsrBuild: false,
  113. },
  114. configFile,
  115. webRoot,
  116. "silent",
  117. );
  118. expect(result?.config.define).toMatchObject({
  119. "import.meta.env.VITE_ZAI_OAUTH_CLIENT_ID": JSON.stringify("client_P8X5CMWmlaRO9gyO-KSqtg"),
  120. "import.meta.env.VITE_ZCODE_BASE_URL": JSON.stringify("https://zcode.z.ai"),
  121. "import.meta.env.VITE_ZAI_OAUTH_ORIGIN": JSON.stringify("https://chat.z.ai"),
  122. });
  123. },
  124. );
  125. it("uses shared ZCode endpoint env override for renderer injection", async () => {
  126. vi.stubEnv("ZCODE_ENV", "test");
  127. vi.stubEnv("ZCODE_BASE_URL", "https://zcode.z.ai/custom/path");
  128. const webRoot = resolve(import.meta.dirname, "..");
  129. const configFile = resolve(webRoot, "vite.config.ts");
  130. const result = await loadConfigFromFile(
  131. {
  132. command: "build",
  133. mode: "production",
  134. isPreview: false,
  135. isSsrBuild: false,
  136. },
  137. configFile,
  138. webRoot,
  139. "silent",
  140. );
  141. expect(result?.config.define).toMatchObject({
  142. "import.meta.env.VITE_ZCODE_BASE_URL": JSON.stringify("https://zcode.z.ai"),
  143. "import.meta.env.VITE_ZCODE_ENDPOINT_ORIGIN": JSON.stringify("https://zcode.z.ai"),
  144. });
  145. });
  146. });