ensureElectronRuntime.test.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { mkdtemp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import { ensureElectronRuntime } from "../scripts/ensure-electron-runtime.mjs";
  6. const roots: string[] = [];
  7. afterEach(async () => {
  8. vi.unstubAllEnvs();
  9. await Promise.all(roots.splice(0).map((root) => rm(root, { recursive: true, force: true })));
  10. });
  11. async function fixture() {
  12. const packageRoot = await mkdtemp(join(tmpdir(), "zcode-electron-runtime-"));
  13. roots.push(packageRoot);
  14. await writeFile(join(packageRoot, "install.js"), "");
  15. const binary = join(packageRoot, "dist", "electron");
  16. const install = async () => {
  17. await mkdir(join(packageRoot, "dist"), { recursive: true });
  18. await writeFile(join(packageRoot, "path.txt"), "electron\n");
  19. await writeFile(binary, "fixture binary");
  20. };
  21. return { packageRoot, binary, install };
  22. }
  23. describe("Electron runtime readiness", () => {
  24. it.each([
  25. [undefined, "https://npmmirror.com/mirrors/electron/"],
  26. ["", "https://npmmirror.com/mirrors/electron/"],
  27. [" ", "https://npmmirror.com/mirrors/electron/"],
  28. ["https://electron.example.com/", "https://electron.example.com/"],
  29. ])("安装子进程镜像为 %s 时使用 %s,且不改写父进程环境", async (mirror, expected) => {
  30. vi.stubEnv("ELECTRON_MIRROR", mirror);
  31. const { packageRoot, binary } = await fixture();
  32. await writeFile(
  33. join(packageRoot, "install.js"),
  34. `
  35. const { mkdir, writeFile } = require('node:fs/promises');
  36. const { join } = require('node:path');
  37. (async () => {
  38. await mkdir(join(__dirname, 'dist'), { recursive: true });
  39. await writeFile(join(__dirname, 'dist', 'electron'), process.env.ELECTRON_MIRROR || 'missing');
  40. await writeFile(join(__dirname, 'path.txt'), 'electron');
  41. })();
  42. `,
  43. );
  44. expect(await ensureElectronRuntime({ packageRoot })).toBe(binary);
  45. expect(await readFile(binary, "utf8")).toBe(expected);
  46. expect(process.env.ELECTRON_MIRROR).toBe(mirror);
  47. });
  48. it("保留 npm 显式镜像配置及其他安装环境变量", async () => {
  49. vi.stubEnv("npm_config_electron_mirror", "https://electron.example.com/");
  50. const { packageRoot, binary } = await fixture();
  51. await writeFile(
  52. join(packageRoot, "install.js"),
  53. `
  54. const { mkdir, writeFile } = require('node:fs/promises');
  55. const { join } = require('node:path');
  56. (async () => {
  57. await mkdir(join(__dirname, 'dist'), { recursive: true });
  58. await writeFile(join(__dirname, 'dist', 'electron'), process.env.npm_config_electron_mirror);
  59. await writeFile(join(__dirname, 'path.txt'), 'electron');
  60. })();
  61. `,
  62. );
  63. expect(await ensureElectronRuntime({ packageRoot })).toBe(binary);
  64. expect(await readFile(binary, "utf8")).toBe("https://electron.example.com/");
  65. });
  66. it("复用官方 path.txt 中已存在的二进制,不重复安装", async () => {
  67. const { packageRoot, binary, install } = await fixture();
  68. await install();
  69. const installRuntime = vi.fn();
  70. expect(await ensureElectronRuntime({ packageRoot, installRuntime })).toBe(binary);
  71. expect(installRuntime).not.toHaveBeenCalled();
  72. });
  73. it("无 postinstall 的干净安装等待官方安装脚本完成后才返回路径", async () => {
  74. const { packageRoot, binary, install } = await fixture();
  75. const installation = Promise.withResolvers<void>();
  76. const installRuntime = vi.fn(async () => {
  77. await installation.promise;
  78. await install();
  79. });
  80. const completed = vi.fn();
  81. const pending = ensureElectronRuntime({ packageRoot, installRuntime }).then(completed);
  82. await vi.waitFor(() => expect(installRuntime).toHaveBeenCalledOnce());
  83. expect(completed).not.toHaveBeenCalled();
  84. installation.resolve();
  85. await pending;
  86. expect(completed).toHaveBeenCalledWith(binary);
  87. });
  88. it("path.txt 残留但二进制缺失时重新准备", async () => {
  89. const { packageRoot, binary, install } = await fixture();
  90. await writeFile(join(packageRoot, "path.txt"), "electron");
  91. const installRuntime = vi.fn(install);
  92. expect(await ensureElectronRuntime({ packageRoot, installRuntime })).toBe(binary);
  93. expect(installRuntime).toHaveBeenCalledOnce();
  94. });
  95. it("安装失败直接传播;成功退出但缺失二进制也不返回旧路径", async () => {
  96. const { packageRoot } = await fixture();
  97. const failure = new Error("installer failed");
  98. await expect(
  99. ensureElectronRuntime({
  100. packageRoot,
  101. installRuntime: async () => {
  102. throw failure;
  103. },
  104. }),
  105. ).rejects.toBe(failure);
  106. await expect(
  107. ensureElectronRuntime({ packageRoot, installRuntime: async () => {} }),
  108. ).rejects.toThrow("Electron binary unavailable");
  109. });
  110. it("实际异步子进程安装脚本产出可执行路径", async () => {
  111. const { packageRoot, binary } = await fixture();
  112. await writeFile(
  113. join(packageRoot, "install.js"),
  114. `
  115. const { mkdir, writeFile } = require('node:fs/promises');
  116. const { join } = require('node:path');
  117. (async () => {
  118. await mkdir(join(__dirname, 'dist'), { recursive: true });
  119. await writeFile(join(__dirname, 'dist', 'electron'), 'binary');
  120. await writeFile(join(__dirname, 'path.txt'), 'electron');
  121. })();
  122. `,
  123. );
  124. expect(await ensureElectronRuntime({ packageRoot })).toBe(binary);
  125. });
  126. });