tsupConfig.test.ts 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { execFile } from "node:child_process";
  2. import { readFileSync } from "node:fs";
  3. import { mkdtemp, readFile, rm, symlink, writeFile } from "node:fs/promises";
  4. import { createRequire } from "node:module";
  5. import { tmpdir } from "node:os";
  6. import { dirname, join, resolve } from "node:path";
  7. import { promisify } from "node:util";
  8. import { afterAll, beforeAll, describe, expect, it } from "vitest";
  9. import { collectBareModuleSpecifiers } from "../src/packaging/stageDependencies.js";
  10. import { SERVER_CLI_DEFINES } from "../tsup.config.js";
  11. import { loadBuiltinProviderConfig } from "../../../scripts/builtin-provider-config.mjs";
  12. describe("server cli tsup config", () => {
  13. it("embeds the selected environment's Built-in config used by Server Core", async () => {
  14. const expected = await loadBuiltinProviderConfig();
  15. expect(JSON.parse(SERVER_CLI_DEFINES.__ZCODE_BUILTIN_PROVIDER_CONFIG_JSON__)).toBe(
  16. expected.content,
  17. );
  18. });
  19. it("embeds the product version and the Source Server compatibility floor", () => {
  20. const rootPackageJson = JSON.parse(
  21. readFileSync(resolve(import.meta.dirname, "../../../package.json"), "utf8"),
  22. ) as { version: string; zcodeCompatibility?: { minCompatibleVersion?: string } };
  23. expect(JSON.parse(SERVER_CLI_DEFINES.__ZCODE_VERSION__)).toBe(rootPackageJson.version);
  24. // 缺少这个 define 时 shared 的 ZCODE_MIN_COMPATIBLE_VERSION 会回退为产品版本,
  25. // Desktop 与远端 Server 的兼容性判定就失去了独立的下限。
  26. expect(JSON.parse(SERVER_CLI_DEFINES.__ZCODE_MIN_COMPATIBLE_VERSION__)).toBe(
  27. rootPackageJson.zcodeCompatibility?.minCompatibleVersion ?? rootPackageJson.version,
  28. );
  29. });
  30. });
  31. const execFileAsync = promisify(execFile);
  32. const packageRoot = resolve(import.meta.dirname, "..");
  33. const require = createRequire(import.meta.url);
  34. const tsupCli = join(dirname(require.resolve("tsup/package.json")), "dist/cli-default.js");
  35. describe("real ESM Server bundle", () => {
  36. let runtimeDir = "";
  37. beforeAll(async () => {
  38. runtimeDir = await mkdtemp(join(tmpdir(), "zcode-server-bundle-"));
  39. await execFileAsync(process.execPath, [tsupCli, "--out-dir", runtimeDir, "--silent"], {
  40. cwd: packageRoot,
  41. timeout: 25_000,
  42. });
  43. await writeFile(join(runtimeDir, "package.json"), '{"type":"module"}\n', "utf8");
  44. await symlink(
  45. resolve(packageRoot, "../../node_modules"),
  46. join(runtimeDir, "node_modules"),
  47. process.platform === "win32" ? "junction" : "dir",
  48. );
  49. }, 45_000);
  50. afterAll(async () => {
  51. if (runtimeDir) await rm(runtimeDir, { recursive: true, force: true });
  52. });
  53. it("loads the real ESM Server CLI bundle before handling a version probe", async () => {
  54. // Bug 原因:源码测试不会经过真实 ESM 打包,模拟发行包也不会加载真实 services。
  55. // 必须执行实际 ESM 产物,才能捕获间接依赖 yazl 内的 require("fs") 启动崩溃。
  56. const { stdout } = await execFileAsync(
  57. process.execPath,
  58. [join(runtimeDir, "server-cli.js"), "--version", "--json"],
  59. {
  60. cwd: runtimeDir,
  61. // 不继承测试加载器;发行入口需要依靠 Node 自身的模块加载规则启动。
  62. env: { ...process.env, NODE_OPTIONS: "" },
  63. timeout: 10_000,
  64. },
  65. );
  66. expect(JSON.parse(stdout)).toEqual({
  67. product: "zcode-server",
  68. version: expect.any(String),
  69. });
  70. });
  71. it("keeps the Feishu SDK as a staged runtime dependency of Server Core", async () => {
  72. // Bug 原因:SDK 内联进 ESM 后,其 CJS 依赖 require Node 内置模块会抛 Dynamic require;
  73. // esbuild 懒初始化只跑一次,重试拿到半初始化模块,远端飞书长连接报
  74. // "EventDispatcher is not a constructor"。SDK 必须保留为裸导入,由 staging 随包携带。
  75. const source = await readFile(join(runtimeDir, "server-core.js"), "utf8");
  76. expect(source).not.toContain("@larksuiteoapi/node-sdk/es/index.js");
  77. expect(source).not.toContain("@larksuiteoapi/node-sdk/lib/index.js");
  78. expect(collectBareModuleSpecifiers(source)).toContain("@larksuiteoapi/node-sdk");
  79. });
  80. });