viteRendererConfig.test.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import { existsSync } from "node:fs";
  2. import { resolve } from "node:path";
  3. import { afterEach, describe, expect, it, vi } from "vitest";
  4. import { createServer, loadConfigFromFile } from "vite";
  5. import { desktopRendererDependencyAliases } from "../vite.config";
  6. describe("desktop renderer Vite dependency aliases", () => {
  7. afterEach(() => {
  8. vi.unstubAllEnvs();
  9. });
  10. it("points React singleton dependencies to installed package locations", () => {
  11. for (const dependencyName of ["react", "react-dom", "lucide-react"] as const) {
  12. expect(
  13. existsSync(desktopRendererDependencyAliases[dependencyName]),
  14. `${dependencyName} alias target should exist`,
  15. ).toBe(true);
  16. }
  17. });
  18. it("keeps React subpath aliases loadable for Vite dependency optimization", () => {
  19. expect(
  20. existsSync(resolve(desktopRendererDependencyAliases.react, "jsx-runtime.js")),
  21. "react/jsx-runtime should resolve from the React package root",
  22. ).toBe(true);
  23. expect(
  24. existsSync(resolve(desktopRendererDependencyAliases["react-dom"], "client.js")),
  25. "react-dom/client should resolve from the React DOM package root",
  26. ).toBe(true);
  27. });
  28. // Windows 跳过:环境配置差异导致断言失败,需要 CI 环境统一配置
  29. const isWindows = process.platform === "win32";
  30. (isWindows ? it.skip : it)(
  31. "injects the selected ZCode endpoint origin into renderer env",
  32. async () => {
  33. vi.stubEnv("ZCODE_ENV", "test");
  34. vi.stubEnv("ZCODE_BASE_URL", "");
  35. const desktopRoot = resolve(import.meta.dirname, "..");
  36. const configFile = resolve(desktopRoot, "vite.config.ts");
  37. const result = await loadConfigFromFile(
  38. {
  39. command: "build",
  40. mode: "production",
  41. isPreview: false,
  42. isSsrBuild: false,
  43. },
  44. configFile,
  45. desktopRoot,
  46. "silent",
  47. );
  48. expect(result?.config.define).toMatchObject({
  49. "import.meta.env.VITE_ZCODE_BASE_URL": JSON.stringify("https://zcode.z.ai"),
  50. "import.meta.env.VITE_ZCODE_ENDPOINT_ORIGIN": JSON.stringify("https://zcode.z.ai"),
  51. __ZCODE_LOCAL_DEVELOPMENT_RUNTIME__: JSON.stringify(false),
  52. });
  53. },
  54. );
  55. it("marks non-production renderer builds as local development runtime", async () => {
  56. vi.stubEnv("ZCODE_ENV", "production");
  57. const desktopRoot = resolve(import.meta.dirname, "..");
  58. const configFile = resolve(desktopRoot, "vite.config.ts");
  59. const result = await loadConfigFromFile(
  60. {
  61. command: "serve",
  62. mode: "development",
  63. isPreview: false,
  64. isSsrBuild: false,
  65. },
  66. configFile,
  67. desktopRoot,
  68. "silent",
  69. );
  70. expect(result?.config.define).toMatchObject({
  71. __ZCODE_ENV__: JSON.stringify("production"),
  72. __ZCODE_PRODUCT_FLAVOR__: JSON.stringify("production"),
  73. __ZCODE_LOCAL_DEVELOPMENT_RUNTIME__: JSON.stringify(true),
  74. "import.meta.env.VITE_ZCODE_E2E_STORE_BRIDGE": JSON.stringify(""),
  75. });
  76. });
  77. it("publishes PDF.js CMaps for non-latin PDF rendering", async () => {
  78. const desktopRoot = resolve(import.meta.dirname, "..");
  79. const configFile = resolve(desktopRoot, "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. desktopRoot,
  89. "silent",
  90. );
  91. expect(result?.config.plugins?.some((plugin) => plugin.name === "zcode:pdfjs-cmaps")).toBe(
  92. true,
  93. );
  94. });
  95. it("injects the E2E store bridge only from the runner flag", async () => {
  96. vi.stubEnv("ZCODE_ENV", "test");
  97. vi.stubEnv("VITE_ZCODE_E2E_STORE_BRIDGE", "1");
  98. const desktopRoot = resolve(import.meta.dirname, "..");
  99. const configFile = resolve(desktopRoot, "vite.config.ts");
  100. const result = await loadConfigFromFile(
  101. {
  102. command: "build",
  103. mode: "production",
  104. isPreview: false,
  105. isSsrBuild: false,
  106. },
  107. configFile,
  108. desktopRoot,
  109. "silent",
  110. );
  111. expect(result?.config.define).toMatchObject({
  112. __ZCODE_ENV__: JSON.stringify("test"),
  113. __ZCODE_PRODUCT_FLAVOR__: JSON.stringify("preview"),
  114. "import.meta.env.VITE_ZCODE_E2E_STORE_BRIDGE": JSON.stringify("1"),
  115. });
  116. });
  117. it("ZCODE_PREVIEW_IDENTITY=1 让生产后端的 renderer 编译成 Preview 身份", async () => {
  118. vi.stubEnv("ZCODE_ENV", "production");
  119. vi.stubEnv("ZCODE_PREVIEW_IDENTITY", "1");
  120. const desktopRoot = resolve(import.meta.dirname, "..");
  121. const configFile = resolve(desktopRoot, "vite.config.ts");
  122. const result = await loadConfigFromFile(
  123. {
  124. command: "build",
  125. mode: "production",
  126. isPreview: false,
  127. isSsrBuild: false,
  128. },
  129. configFile,
  130. desktopRoot,
  131. "silent",
  132. );
  133. expect(result?.config.define).toMatchObject({
  134. __ZCODE_ENV__: JSON.stringify("production"),
  135. __ZCODE_PRODUCT_FLAVOR__: JSON.stringify("preview"),
  136. });
  137. });
  138. });
  139. describe("desktop React Scan development tools", () => {
  140. afterEach(() => vi.unstubAllEnvs());
  141. async function loadReactScanPlugin(command: "serve" | "build", mode: string) {
  142. const desktopRoot = resolve(import.meta.dirname, "..");
  143. const result = await loadConfigFromFile(
  144. { command, mode },
  145. resolve(desktopRoot, "vite.config.ts"),
  146. desktopRoot,
  147. "silent",
  148. );
  149. return result?.config.plugins?.flat().find((plugin) => plugin?.name === "zcode:react-scan");
  150. }
  151. it("serves Scan before React only in the main development entry", async () => {
  152. vi.stubEnv("ZCODE_ENV", "production");
  153. vi.stubEnv("ZCODE_REACT_SCAN", undefined);
  154. const plugin = await loadReactScanPlugin("serve", "development");
  155. expect(plugin).toBeDefined();
  156. const server = await createServer({
  157. configFile: false,
  158. plugins: [plugin],
  159. server: { middlewareMode: true, watch: null },
  160. optimizeDeps: { noDiscovery: true, include: [] },
  161. });
  162. try {
  163. const html =
  164. '<html><head></head><body><script type="module">window.app = true</script></body></html>';
  165. const transformed = await server.transformIndexHtml("/index.html", html);
  166. const scanScript = transformed.match(/<script src="([^"]+react-scan[^"]+)"><\/script>/);
  167. expect(scanScript).not.toBeNull();
  168. expect(scanScript?.[1]).toMatch(/^\/@fs\//);
  169. expect(transformed.indexOf(scanScript![0])).toBeLessThan(
  170. transformed.indexOf('type="module"'),
  171. );
  172. for (const entry of ["resource-manager", "plugin-sandbox", "cua-permission-panel"]) {
  173. expect(await server.transformIndexHtml(`/${entry}.html`, html)).not.toContain("react-scan");
  174. }
  175. } finally {
  176. await server.close();
  177. }
  178. });
  179. it("allows opting out of development instrumentation", async () => {
  180. vi.stubEnv("ZCODE_REACT_SCAN", "0");
  181. expect(await loadReactScanPlugin("serve", "development")).toBeUndefined();
  182. });
  183. it.each([
  184. ["serve", "production", "1"],
  185. ["build", "production", "0"],
  186. ["build", "production", "1"],
  187. ["build", "development", "0"],
  188. ["build", "development", "1"],
  189. ] as const)(
  190. "excludes Scan from %s in %s mode with ZCODE_REACT_SCAN=%s",
  191. async (command, mode, scan) => {
  192. vi.stubEnv("ZCODE_REACT_SCAN", scan);
  193. expect(await loadReactScanPlugin(command, mode)).toBeUndefined();
  194. },
  195. );
  196. });