viteRendererConfig.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { existsSync } from "node:fs";
  2. import { resolve } from "node:path";
  3. import { afterEach, describe, expect, it, vi } from "vitest";
  4. import { 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)("injects the selected ZCode endpoint origin into renderer env", async () => {
  31. vi.stubEnv("ZCODE_ENV", "test");
  32. vi.stubEnv("ZCODE_BASE_URL", "");
  33. const desktopRoot = resolve(import.meta.dirname, "..");
  34. const configFile = resolve(desktopRoot, "vite.config.ts");
  35. const result = await loadConfigFromFile(
  36. {
  37. command: "build",
  38. mode: "production",
  39. isPreview: false,
  40. isSsrBuild: false,
  41. },
  42. configFile,
  43. desktopRoot,
  44. "silent",
  45. );
  46. expect(result?.config.define).toMatchObject({
  47. "import.meta.env.VITE_ZCODE_BASE_URL": JSON.stringify("https://zcode.z.ai"),
  48. "import.meta.env.VITE_ZCODE_ENDPOINT_ORIGIN": JSON.stringify(
  49. "https://zcode.z.ai",
  50. ),
  51. __ZCODE_LOCAL_DEVELOPMENT_RUNTIME__: JSON.stringify(false),
  52. });
  53. });
  54. it("marks non-production renderer builds as local development runtime", async () => {
  55. vi.stubEnv("ZCODE_ENV", "production");
  56. const desktopRoot = resolve(import.meta.dirname, "..");
  57. const configFile = resolve(desktopRoot, "vite.config.ts");
  58. const result = await loadConfigFromFile(
  59. {
  60. command: "serve",
  61. mode: "development",
  62. isPreview: false,
  63. isSsrBuild: false,
  64. },
  65. configFile,
  66. desktopRoot,
  67. "silent",
  68. );
  69. expect(result?.config.define).toMatchObject({
  70. __ZCODE_ENV__: JSON.stringify("production"),
  71. __ZCODE_PRODUCT_FLAVOR__: JSON.stringify("production"),
  72. __ZCODE_LOCAL_DEVELOPMENT_RUNTIME__: JSON.stringify(true),
  73. "import.meta.env.VITE_ZCODE_E2E_STORE_BRIDGE": JSON.stringify(""),
  74. });
  75. });
  76. it("publishes PDF.js CMaps for non-latin PDF rendering", async () => {
  77. const desktopRoot = resolve(import.meta.dirname, "..");
  78. const configFile = resolve(desktopRoot, "vite.config.ts");
  79. const result = await loadConfigFromFile(
  80. {
  81. command: "build",
  82. mode: "production",
  83. isPreview: false,
  84. isSsrBuild: false,
  85. },
  86. configFile,
  87. desktopRoot,
  88. "silent",
  89. );
  90. expect(result?.config.plugins?.some((plugin) => plugin.name === "zcode:pdfjs-cmaps")).toBe(
  91. true,
  92. );
  93. });
  94. it("injects the E2E store bridge only from the runner flag", async () => {
  95. vi.stubEnv("ZCODE_ENV", "test");
  96. vi.stubEnv("VITE_ZCODE_E2E_STORE_BRIDGE", "1");
  97. const desktopRoot = resolve(import.meta.dirname, "..");
  98. const configFile = resolve(desktopRoot, "vite.config.ts");
  99. const result = await loadConfigFromFile(
  100. {
  101. command: "build",
  102. mode: "production",
  103. isPreview: false,
  104. isSsrBuild: false,
  105. },
  106. configFile,
  107. desktopRoot,
  108. "silent",
  109. );
  110. expect(result?.config.define).toMatchObject({
  111. __ZCODE_ENV__: JSON.stringify("test"),
  112. __ZCODE_PRODUCT_FLAVOR__: JSON.stringify("preview"),
  113. "import.meta.env.VITE_ZCODE_E2E_STORE_BRIDGE": JSON.stringify("1"),
  114. });
  115. });
  116. it("ZCODE_PREVIEW_IDENTITY=1 让生产后端的 renderer 编译成 Preview 身份", async () => {
  117. vi.stubEnv("ZCODE_ENV", "production");
  118. vi.stubEnv("ZCODE_PREVIEW_IDENTITY", "1");
  119. const desktopRoot = resolve(import.meta.dirname, "..");
  120. const configFile = resolve(desktopRoot, "vite.config.ts");
  121. const result = await loadConfigFromFile(
  122. {
  123. command: "build",
  124. mode: "production",
  125. isPreview: false,
  126. isSsrBuild: false,
  127. },
  128. configFile,
  129. desktopRoot,
  130. "silent",
  131. );
  132. expect(result?.config.define).toMatchObject({
  133. __ZCODE_ENV__: JSON.stringify("production"),
  134. __ZCODE_PRODUCT_FLAVOR__: JSON.stringify("preview"),
  135. });
  136. });
  137. });