build-time-config.test.mjs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import assert from "node:assert/strict";
  2. import { mkdtemp, rm, writeFile } from "node:fs/promises";
  3. import { tmpdir } from "node:os";
  4. import { join } from "node:path";
  5. import { after, test } from "node:test";
  6. import { createBuildTimeConfigDefines, resolveBuildTimeConfig } from "../build-time-config.mjs";
  7. const dir = await mkdtemp(join(tmpdir(), "zcode-build-time-config-"));
  8. after(() => rm(dir, { recursive: true, force: true }));
  9. async function writeDefaults(name, content) {
  10. const path = join(dir, name);
  11. await writeFile(path, typeof content === "string" ? content : JSON.stringify(content));
  12. return path;
  13. }
  14. test("没有私有默认配置也没有环境变量时三项能力都关闭", async () => {
  15. const config = await resolveBuildTimeConfig({}, { defaultsPath: join(dir, "missing.json") });
  16. assert.deepEqual(config, {
  17. armsRumEndpoint: "",
  18. telemetryReportEndpoint: "",
  19. autoUpdateEnabled: false,
  20. });
  21. });
  22. test("私有默认配置启用三项能力", async () => {
  23. const defaultsPath = await writeDefaults("defaults.json", {
  24. ZCODE_ARMS_RUM_ENDPOINT: " https://rum.example.com/x ",
  25. ZCODE_TELEMETRY_REPORT_ENDPOINT: "https://report.example.com/event",
  26. ZCODE_AUTO_UPDATE: "1",
  27. });
  28. assert.deepEqual(await resolveBuildTimeConfig({}, { defaultsPath }), {
  29. armsRumEndpoint: "https://rum.example.com/x",
  30. telemetryReportEndpoint: "https://report.example.com/event",
  31. autoUpdateEnabled: true,
  32. });
  33. });
  34. test("环境变量优先于私有默认配置,显式空字符串关闭单项能力", async () => {
  35. const defaultsPath = await writeDefaults("override.json", {
  36. ZCODE_ARMS_RUM_ENDPOINT: "https://rum.example.com/x",
  37. ZCODE_TELEMETRY_REPORT_ENDPOINT: "https://report.example.com/event",
  38. ZCODE_AUTO_UPDATE: "true",
  39. });
  40. const config = await resolveBuildTimeConfig(
  41. {
  42. ZCODE_ARMS_RUM_ENDPOINT: "",
  43. ZCODE_TELEMETRY_REPORT_ENDPOINT: "https://other.example.com/event",
  44. ZCODE_AUTO_UPDATE: "0",
  45. },
  46. { defaultsPath },
  47. );
  48. assert.deepEqual(config, {
  49. armsRumEndpoint: "",
  50. telemetryReportEndpoint: "https://other.example.com/event",
  51. autoUpdateEnabled: false,
  52. });
  53. });
  54. test("私有默认配置格式错误时直接报错,不静默关闭", async () => {
  55. const defaultsPath = await writeDefaults("broken.json", "[1]");
  56. await assert.rejects(resolveBuildTimeConfig({}, { defaultsPath }), /必须是 JSON 对象/);
  57. });
  58. test("define 以 JSON 字面量注入", () => {
  59. assert.deepEqual(
  60. createBuildTimeConfigDefines({
  61. armsRumEndpoint: "https://rum.example.com/x",
  62. telemetryReportEndpoint: "",
  63. autoUpdateEnabled: true,
  64. }),
  65. {
  66. __ZCODE_ARMS_RUM_ENDPOINT__: '"https://rum.example.com/x"',
  67. __ZCODE_TELEMETRY_REPORT_ENDPOINT__: '""',
  68. __ZCODE_AUTO_UPDATE_ENABLED__: "true",
  69. },
  70. );
  71. });