desktopProviderConfig.test.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { afterEach, describe, expect, it, vi } from "vitest";
  2. import { resolveZCodeBuiltinProviderConfigFilePath } from "../src/main/desktopProviderConfig.js";
  3. // 实现用 node:path join 产出本机分隔符(运行时语义正确),Windows 单测下需按
  4. // POSIX 口径归一后再比较,期望值才能跨平台成立。
  5. function toPosixPath(value: string) {
  6. return value.replaceAll("\\", "/");
  7. }
  8. describe("resolveZCodeBuiltinProviderConfigFilePath", () => {
  9. it("正式包读取 resources,开发态读取仓库 config", () => {
  10. expect(
  11. toPosixPath(
  12. resolveZCodeBuiltinProviderConfigFilePath({
  13. isPackaged: true,
  14. resourcesPath: "/Applications/ZCode.app/Contents/Resources",
  15. appPath: "/Applications/ZCode.app/Contents/Resources/app.asar",
  16. // 隔离宿主环境变量:ZCode 开发运行时会注入 ZCODE_BUILTIN_PROVIDER_CONFIG_FILE,
  17. // 不显式清空会让本机跑单测时短路进 explicitPath 分支(CI 无此变量所以一直是绿的)。
  18. env: {},
  19. }),
  20. ),
  21. ).toBe("/Applications/ZCode.app/Contents/Resources/config/provider/zcode-builtin.json");
  22. expect(
  23. toPosixPath(
  24. resolveZCodeBuiltinProviderConfigFilePath({
  25. isPackaged: false,
  26. appPath: "/repo/packages/desktop",
  27. env: {},
  28. }),
  29. ),
  30. ).toBe("/repo/config/provider/zcode-builtin.test.json");
  31. });
  32. it("显式 ZCode Built-in Config Source 优先于安装布局", () => {
  33. expect(
  34. resolveZCodeBuiltinProviderConfigFilePath({
  35. env: { ZCODE_BUILTIN_PROVIDER_CONFIG_FILE: "/fixtures/zcode-builtin.json" },
  36. isPackaged: true,
  37. resourcesPath: "/Applications/ZCode.app/Contents/Resources",
  38. }),
  39. ).toBe("/fixtures/zcode-builtin.json");
  40. });
  41. });
  42. describe("开发态按编译环境选取配置", () => {
  43. afterEach(() => {
  44. vi.unstubAllGlobals();
  45. vi.resetModules();
  46. });
  47. it.each(["test", "production"])("%s 不受启动时的环境变量反向覆盖", async (environment) => {
  48. vi.resetModules();
  49. vi.stubGlobal("__ZCODE_ENV__", environment);
  50. const { resolveZCodeBuiltinProviderConfigFilePath: resolvePath } =
  51. await import("../src/main/desktopProviderConfig.js");
  52. expect(
  53. toPosixPath(
  54. resolvePath({
  55. isPackaged: false,
  56. appPath: "/repo/packages/desktop",
  57. env: { ZCODE_ENV: environment === "test" ? "production" : "test" },
  58. }),
  59. ),
  60. ).toBe(
  61. `/repo/config/provider/${environment === "production" ? "zcode-builtin.json" : "zcode-builtin.test.json"}`,
  62. );
  63. });
  64. });