tsupConfig.test.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { execFile } from "node:child_process";
  2. import { readFileSync } from "node:fs";
  3. import { mkdtemp, 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 { describe, expect, it } from "vitest";
  9. import { SERVER_CLI_DEFINES } from "../tsup.config.js";
  10. import { loadBuiltinProviderConfig } from "../../../scripts/builtin-provider-config.mjs";
  11. describe("server cli tsup config", () => {
  12. it("embeds the selected environment's Built-in config used by Server Core", async () => {
  13. const expected = await loadBuiltinProviderConfig();
  14. expect(JSON.parse(SERVER_CLI_DEFINES.__ZCODE_BUILTIN_PROVIDER_CONFIG_JSON__)).toBe(
  15. expected.content,
  16. );
  17. });
  18. it("embeds the product version and the Source Server compatibility floor", () => {
  19. const rootPackageJson = JSON.parse(
  20. readFileSync(resolve(import.meta.dirname, "../../../package.json"), "utf8"),
  21. ) as { version: string; zcodeCompatibility?: { minCompatibleVersion?: string } };
  22. expect(JSON.parse(SERVER_CLI_DEFINES.__ZCODE_VERSION__)).toBe(rootPackageJson.version);
  23. // 缺少这个 define 时 shared 的 ZCODE_MIN_COMPATIBLE_VERSION 会回退为产品版本,
  24. // Desktop 与远端 Server 的兼容性判定就失去了独立的下限。
  25. expect(JSON.parse(SERVER_CLI_DEFINES.__ZCODE_MIN_COMPATIBLE_VERSION__)).toBe(
  26. rootPackageJson.zcodeCompatibility?.minCompatibleVersion ?? rootPackageJson.version,
  27. );
  28. });
  29. });
  30. const execFileAsync = promisify(execFile);
  31. const packageRoot = resolve(import.meta.dirname, "..");
  32. const require = createRequire(import.meta.url);
  33. const tsupCli = join(dirname(require.resolve("tsup/package.json")), "dist/cli-default.js");
  34. it("loads the real ESM Server CLI bundle before handling a version probe", async () => {
  35. const runtimeDir = await mkdtemp(join(tmpdir(), "zcode-server-bundle-"));
  36. try {
  37. // Bug 原因:源码测试不会经过真实 ESM 打包,模拟发行包也不会加载真实 services。
  38. // 必须执行实际 ESM 产物,才能捕获间接依赖 yazl 内的 require("fs") 启动崩溃。
  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. const { stdout } = await execFileAsync(
  50. process.execPath,
  51. [join(runtimeDir, "server-cli.js"), "--version", "--json"],
  52. {
  53. cwd: runtimeDir,
  54. // 不继承测试加载器;发行入口需要依靠 Node 自身的模块加载规则启动。
  55. env: { ...process.env, NODE_OPTIONS: "" },
  56. timeout: 10_000,
  57. },
  58. );
  59. expect(JSON.parse(stdout)).toEqual({
  60. product: "zcode-server",
  61. version: expect.any(String),
  62. });
  63. } finally {
  64. await rm(runtimeDir, { recursive: true, force: true });
  65. }
  66. }, 45_000);