tsupChunkIsolation.test.ts 7.9 KB

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