tsupChunkIsolation.test.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import { describe, expect, it } from "vitest";
  2. import tsupConfig, {
  3. resolvePreinstalledToolsTestBuild,
  4. resolveBuildPackagedAgentTelemetryEnv,
  5. resolveBuildRemoteCdnReleaseRoots,
  6. resolveDesktopTsupBundleSecurityOptions,
  7. } from "../tsup.config";
  8. type EsbuildLikeOptions = {
  9. chunkNames?: string;
  10. legalComments?: string;
  11. };
  12. function getChunkNames(configName: string): string | undefined {
  13. const configs = Array.isArray(tsupConfig) ? tsupConfig : [tsupConfig];
  14. const target = configs.find((config) => config.name === configName);
  15. expect(target).toBeDefined();
  16. const esbuildOptions: EsbuildLikeOptions = {};
  17. target?.esbuildOptions?.(esbuildOptions);
  18. return esbuildOptions.chunkNames;
  19. }
  20. function getExternal(configName: string): string[] {
  21. const configs = Array.isArray(tsupConfig) ? tsupConfig : [tsupConfig];
  22. const target = configs.find((config) => config.name === configName);
  23. expect(target).toBeDefined();
  24. expect(Array.isArray(target?.external)).toBe(true);
  25. return target?.external as string[];
  26. }
  27. function getNoExternal(configName: string): string[] {
  28. const configs = Array.isArray(tsupConfig) ? tsupConfig : [tsupConfig];
  29. const target = configs.find((config) => config.name === configName);
  30. expect(target).toBeDefined();
  31. expect(Array.isArray(target?.noExternal)).toBe(true);
  32. return target?.noExternal as string[];
  33. }
  34. function getDefines(configName: string): Record<string, string> {
  35. const configs = Array.isArray(tsupConfig) ? tsupConfig : [tsupConfig];
  36. const target = configs.find((config) => config.name === configName);
  37. expect(target).toBeDefined();
  38. return target?.define ?? {};
  39. }
  40. describe("desktop tsup chunk isolation", () => {
  41. it("预装清单覆盖只能由显式 E2E 构建开启,运行时变量和 test 后端不构成权限", () => {
  42. expect(
  43. resolvePreinstalledToolsTestBuild({
  44. ZCODE_ENV: "test",
  45. ZCODE_E2E_PREINSTALLED_TOOLS_CATALOG: "/tmp/fixture.json",
  46. }),
  47. ).toBe(false);
  48. expect(resolvePreinstalledToolsTestBuild({ NODE_ENV: "production" })).toBe(false);
  49. expect(
  50. resolvePreinstalledToolsTestBuild({
  51. NODE_ENV: "production",
  52. VITE_ZCODE_E2E_STORE_BRIDGE: "1",
  53. }),
  54. ).toBe(true);
  55. });
  56. it("ZIP 解包器应外置,避免 Electron ESM 启动时动态 require fs 崩溃", () => {
  57. for (const name of ["main", "host", "scheduler"]) expect(getExternal(name)).toContain("yauzl");
  58. });
  59. it("main 构建应将 chunk 输出到 main 子目录,避免覆盖 host 构建产物", () => {
  60. expect(getChunkNames("main")).toBe("main/chunk-[hash]");
  61. });
  62. it("host 构建应将 chunk 输出到 host 子目录,避免覆盖 main 构建产物", () => {
  63. expect(getChunkNames("host")).toBe("host/chunk-[hash]");
  64. });
  65. it("main/host 构建应外置飞书 SDK,避免 Electron ESM 动态 require 崩溃", () => {
  66. expect(getExternal("main")).toContain("@larksuiteoapi/node-sdk");
  67. expect(getExternal("host")).toContain("@larksuiteoapi/node-sdk");
  68. });
  69. it("main/host 构建应外置 yaml,避免 Electron ESM 动态 require 崩溃", () => {
  70. expect(getExternal("main")).toContain("yaml");
  71. expect(getExternal("host")).toContain("yaml");
  72. });
  73. it("main/host/scheduler 应内联 CUA producer,安装包不能依赖被排除的 @zcode node_modules", () => {
  74. for (const configName of ["main", "host", "scheduler"]) {
  75. expect(getNoExternal(configName)).toContain("@zcode/zcode-cua");
  76. }
  77. });
  78. it("main/host/scheduler 应内联 Provider workspace 包,Electron 不能直接加载 TypeScript 源码", () => {
  79. for (const configName of ["main", "host", "scheduler"]) {
  80. expect(getNoExternal(configName)).toContain("@zcode/provider");
  81. expect(getNoExternal(configName)).toContain("@zcode/provider-node");
  82. }
  83. });
  84. it("生产构建应压缩 main/host/preload 且不生成 sourcemap", () => {
  85. expect(resolveDesktopTsupBundleSecurityOptions({ NODE_ENV: "production" })).toEqual({
  86. keepNames: true,
  87. minify: true,
  88. sourcemap: false,
  89. });
  90. });
  91. it("开发构建应保留 sourcemap 方便本地调试", () => {
  92. expect(resolveDesktopTsupBundleSecurityOptions({ NODE_ENV: "development" })).toEqual({
  93. keepNames: false,
  94. minify: false,
  95. sourcemap: true,
  96. });
  97. });
  98. it("E2E coverage 构建应关闭压缩并保留 sourcemap", () => {
  99. expect(
  100. resolveDesktopTsupBundleSecurityOptions({
  101. NODE_ENV: "production",
  102. ZCODE_E2E_COVERAGE: "1",
  103. }),
  104. ).toEqual({
  105. keepNames: false,
  106. minify: false,
  107. sourcemap: true,
  108. });
  109. });
  110. it("remote runtime CDN 根应可由 CI 的 CDN_DOMAIN/OSS_PATH_PREFIX 多值注入", () => {
  111. expect(
  112. resolveBuildRemoteCdnReleaseRoots({
  113. CDN_DOMAIN: "cdn-zcode.z.ai,cdn.zcode-ai.com",
  114. OSS_PATH_PREFIX: "zcode/electron/releases",
  115. }),
  116. ).toEqual([
  117. "https://cdn-zcode.z.ai/zcode/electron/releases",
  118. "https://cdn.zcode-ai.com/zcode/electron/releases",
  119. ]);
  120. });
  121. it("remote runtime CDN 根的路径前缀数量不匹配时应失败", () => {
  122. expect(() =>
  123. resolveBuildRemoteCdnReleaseRoots({
  124. CDN_DOMAIN: "cdn-zcode.z.ai,cdn.zcode-ai.com",
  125. OSS_PATH_PREFIX: "new-prefix,old-prefix,extra-prefix",
  126. }),
  127. ).toThrow("OSS_PATH_PREFIX must contain one value or match CDN_DOMAIN count");
  128. });
  129. it("正式安装包 Agent OTLP 配置应映射为 Main 使用的标准环境字段", () => {
  130. expect(
  131. resolveBuildPackagedAgentTelemetryEnv({
  132. ZCODE_ENV: "production",
  133. ZCODE_PACKAGED_AGENT_OTEL_ENDPOINT: "https://arms.example.com/otlp",
  134. ZCODE_PACKAGED_AGENT_OTEL_HEADERS: "x-arms-license-key=write-only",
  135. ZCODE_PACKAGED_AGENT_OTEL_SERVICE_NAME: "zcode-cli-agent",
  136. }),
  137. ).toEqual({
  138. OTEL_EXPORTER_OTLP_ENDPOINT: "https://arms.example.com/otlp",
  139. OTEL_EXPORTER_OTLP_HEADERS: "x-arms-license-key=write-only",
  140. OTEL_SERVICE_NAME: "zcode-cli-agent",
  141. ZCODE_TELEMETRY_RUNTIME_DISTRIBUTION: "packaged",
  142. });
  143. });
  144. it("正式安装包 Agent OTLP 配置不完整或生产环境使用 HTTP 时应阻止构建", () => {
  145. expect(() =>
  146. resolveBuildPackagedAgentTelemetryEnv({
  147. ZCODE_ENV: "production",
  148. ZCODE_PACKAGED_AGENT_OTEL_ENDPOINT: "https://arms.example.com/otlp",
  149. }),
  150. ).toThrow("must be configured together");
  151. expect(() =>
  152. resolveBuildPackagedAgentTelemetryEnv({
  153. ZCODE_ENV: "production",
  154. ZCODE_PACKAGED_AGENT_OTEL_ENDPOINT: "http://arms.example.com/otlp",
  155. ZCODE_PACKAGED_AGENT_OTEL_HEADERS: "x-arms-license-key=write-only",
  156. }),
  157. ).toThrow("must use HTTPS in production");
  158. });
  159. it("release/current 与 Tag 构建缺少 Agent OTLP 配置时应失败,普通开发构建保持 Noop", () => {
  160. expect(resolveBuildPackagedAgentTelemetryEnv({})).toEqual({});
  161. expect(() =>
  162. resolveBuildPackagedAgentTelemetryEnv({
  163. CI: "true",
  164. CI_COMMIT_BRANCH: "release/current",
  165. }),
  166. ).toThrow("required for release builds");
  167. expect(() =>
  168. resolveBuildPackagedAgentTelemetryEnv({
  169. CI: "true",
  170. CI_COMMIT_TAG: "v3.6.0",
  171. }),
  172. ).toThrow("required for release builds");
  173. });
  174. it("安装包身份与后端环境作为两个编译期常量注入每个 bundle", () => {
  175. // 打包后的应用不能依赖机器环境变量:ZCODE_PREVIEW_IDENTITY 在构建时折叠成 __ZCODE_PRODUCT_FLAVOR__。
  176. for (const entry of ["main", "host", "preload"] as const) {
  177. const defines = getDefines(entry);
  178. expect(defines).toHaveProperty("__ZCODE_ENV__");
  179. expect(defines).toHaveProperty("__ZCODE_PRODUCT_FLAVOR__");
  180. expect(['"production"', '"preview"']).toContain(defines.__ZCODE_PRODUCT_FLAVOR__);
  181. }
  182. });
  183. it("Agent OTLP 部署默认值只应编译进 Main bundle", () => {
  184. expect(getDefines("main")).toHaveProperty("__ZCODE_PACKAGED_AGENT_TELEMETRY_ENV__");
  185. expect(getDefines("host")).not.toHaveProperty("__ZCODE_PACKAGED_AGENT_TELEMETRY_ENV__");
  186. expect(getDefines("preload")).not.toHaveProperty("__ZCODE_PACKAGED_AGENT_TELEMETRY_ENV__");
  187. });
  188. it("所有 tsup 子构建都应关闭 esbuild legal comments 输出", () => {
  189. for (const configName of ["main", "host", "preload"]) {
  190. const configs = Array.isArray(tsupConfig) ? tsupConfig : [tsupConfig];
  191. const target = configs.find((config) => config.name === configName);
  192. expect(target).toBeDefined();
  193. const esbuildOptions: EsbuildLikeOptions = {};
  194. target?.esbuildOptions?.(esbuildOptions);
  195. expect(esbuildOptions.legalComments).toBe("none");
  196. }
  197. });
  198. });