devElectronAppBundle.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. import { mkdtemp, readFile, readlink, rm, mkdir, symlink, 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 {
  6. DEV_ELECTRON_APP_BUNDLE_ID,
  7. DEV_ELECTRON_APP_NAME,
  8. DEV_ELECTRON_PROTOCOL_SCHEME,
  9. DEV_ELECTRON_BUNDLE_FORMAT,
  10. patchDevElectronInfoPlist,
  11. prepareDevElectronAppBundle,
  12. } from "../scripts/devElectronAppBundle.mjs";
  13. const tempRoots: string[] = [];
  14. const signApp = vi.fn(async (_appPath: string) => {});
  15. afterEach(async () => {
  16. signApp.mockClear();
  17. await Promise.all(tempRoots.splice(0).map((root) => rm(root, { recursive: true, force: true })));
  18. });
  19. describe("dev Electron app bundle", () => {
  20. it("adds a product identity and zcode URL scheme to the raw Electron plist", () => {
  21. const source = `<?xml version="1.0" encoding="UTF-8"?>
  22. <plist version="1.0">
  23. <dict>
  24. \t<key>CFBundleDisplayName</key>
  25. \t<string>Electron</string>
  26. \t<key>CFBundleIdentifier</key>
  27. \t<string>com.github.Electron</string>
  28. \t<key>CFBundleName</key>
  29. \t<string>Electron</string>
  30. </dict>
  31. </plist>
  32. `;
  33. const patched = patchDevElectronInfoPlist(source);
  34. expect(patched).toContain(`<string>${DEV_ELECTRON_APP_NAME}</string>`);
  35. expect(patched).toContain(`<string>${DEV_ELECTRON_APP_BUNDLE_ID}</string>`);
  36. expect(patched).toContain("<key>CFBundleURLTypes</key>");
  37. expect(patched).toContain(`<string>${DEV_ELECTRON_PROTOCOL_SCHEME}</string>`);
  38. expect((patched.match(/<key>CFBundleURLTypes<\/key>/gu) ?? []).length).toBe(1);
  39. });
  40. it("makes the macOS dev launcher use the prepared bundle executable", async () => {
  41. const devScript = await readFile(new URL("../scripts/dev.mjs", import.meta.url), "utf8");
  42. expect(devScript).toContain('process.platform === "darwin" && existsSync(electronBinary)');
  43. expect(devScript).toContain("prepareDevElectronAppBundle");
  44. expect(devScript).toContain("electronCommand = devBundle.executablePath");
  45. });
  46. it("copies the runtime and patches only the Dev bundle plist", async () => {
  47. const root = await mkdtemp(join(tmpdir(), "zcode-dev-bundle-test-"));
  48. tempRoots.push(root);
  49. const sourceApp = join(root, "source", "Electron.app");
  50. const sourceContents = join(sourceApp, "Contents");
  51. await mkdir(sourceContents, { recursive: true });
  52. await writeFile(
  53. join(sourceContents, "Info.plist"),
  54. `<?xml version="1.0"?><plist version="1.0"><dict>
  55. <key>CFBundleDisplayName</key><string>Electron</string>
  56. <key>CFBundleIdentifier</key><string>com.github.Electron</string>
  57. <key>CFBundleName</key><string>Electron</string>
  58. </dict></plist>\n`,
  59. "utf8",
  60. );
  61. const outputRoot = join(root, "runtime");
  62. const result = await prepareDevElectronAppBundle({
  63. signApp,
  64. electronAppPath: sourceApp,
  65. runtimeRoot: outputRoot,
  66. electronVersion: "41.0.3",
  67. arch: "arm64",
  68. });
  69. expect(result.appPath).toBe(`${outputRoot}/41.0.3-arm64/ZCode Dev.app`);
  70. expect(result.executablePath).toBe(
  71. `${outputRoot}/41.0.3-arm64/ZCode Dev.app/Contents/MacOS/Electron`,
  72. );
  73. const plist = await readFile(join(result.appPath, "Contents", "Info.plist"), "utf8");
  74. expect(plist).toContain("dev.zcode.app.development");
  75. expect(plist).toContain("<string>zcode</string>");
  76. });
  77. // Windows 普通用户无符号链接权限(需开发者模式),fs.symlink EPERM;覆盖由 CI Linux 承担。
  78. it.skipIf(process.platform === "win32")(
  79. "keeps framework symlinks relative so sandboxed helpers stay inside the bundle",
  80. async () => {
  81. const root = await mkdtemp(join(tmpdir(), "zcode-dev-bundle-symlink-test-"));
  82. tempRoots.push(root);
  83. const sourceApp = join(root, "source", "Electron.app");
  84. const sourceContents = join(sourceApp, "Contents");
  85. const framework = join(sourceContents, "Frameworks", "Electron Framework.framework");
  86. const versionA = join(framework, "Versions", "A", "Resources");
  87. await mkdir(join(sourceContents, "MacOS"), { recursive: true });
  88. await mkdir(versionA, { recursive: true });
  89. await writeFile(
  90. join(sourceContents, "Info.plist"),
  91. `<?xml version="1.0"?><plist version="1.0"><dict>
  92. <key>CFBundleDisplayName</key><string>Electron</string>
  93. <key>CFBundleIdentifier</key><string>com.github.Electron</string>
  94. <key>CFBundleName</key><string>Electron</string>
  95. </dict></plist>\n`,
  96. "utf8",
  97. );
  98. await writeFile(join(sourceContents, "MacOS", "Electron"), "source-v1", "utf8");
  99. await writeFile(join(versionA, "icudtl.dat"), "icu", "utf8");
  100. await symlink("A", join(framework, "Versions", "Current"));
  101. await symlink("Versions/Current/Resources", join(framework, "Resources"));
  102. const result = await prepareDevElectronAppBundle({
  103. signApp,
  104. electronAppPath: sourceApp,
  105. runtimeRoot: join(root, "runtime"),
  106. electronVersion: "41.0.3",
  107. arch: "arm64",
  108. });
  109. const copiedFramework = join(
  110. result.appPath,
  111. "Contents",
  112. "Frameworks",
  113. "Electron Framework.framework",
  114. );
  115. expect(await readlink(join(copiedFramework, "Versions", "Current"))).toBe("A");
  116. expect(await readlink(join(copiedFramework, "Resources"))).toBe("Versions/Current/Resources");
  117. expect(await readFile(join(copiedFramework, "Resources", "icudtl.dat"), "utf8")).toBe("icu");
  118. },
  119. );
  120. it("rebuilds a cached bundle whose stamp predates the current layout format", async () => {
  121. const root = await mkdtemp(join(tmpdir(), "zcode-dev-bundle-format-test-"));
  122. tempRoots.push(root);
  123. const sourceApp = join(root, "source", "Electron.app");
  124. const sourceContents = join(sourceApp, "Contents");
  125. const sourceMacOS = join(sourceContents, "MacOS");
  126. await mkdir(sourceMacOS, { recursive: true });
  127. await writeFile(
  128. join(sourceContents, "Info.plist"),
  129. `<?xml version="1.0"?><plist version="1.0"><dict>
  130. <key>CFBundleDisplayName</key><string>Electron</string>
  131. <key>CFBundleIdentifier</key><string>com.github.Electron</string>
  132. <key>CFBundleName</key><string>Electron</string>
  133. </dict></plist>\n`,
  134. "utf8",
  135. );
  136. await writeFile(join(sourceMacOS, "Electron"), "source-v1", "utf8");
  137. const outputRoot = join(root, "runtime");
  138. const first = await prepareDevElectronAppBundle({
  139. signApp,
  140. electronAppPath: sourceApp,
  141. runtimeRoot: outputRoot,
  142. electronVersion: "41.0.3",
  143. arch: "arm64",
  144. });
  145. const stampPath = join(outputRoot, "41.0.3-arm64", ".zcode-dev-electron-source.json");
  146. const stamp = JSON.parse(await readFile(stampPath, "utf8"));
  147. expect(stamp.format).toBe(DEV_ELECTRON_BUNDLE_FORMAT);
  148. // 模拟旧版脚本留下的缓存:没有 format 字段,源二进制指纹却仍然匹配。
  149. await writeFile(
  150. stampPath,
  151. JSON.stringify({ size: stamp.size, mtimeMs: stamp.mtimeMs }),
  152. "utf8",
  153. );
  154. await writeFile(join(first.appPath, "Contents", "MacOS", "Electron"), "stale-copy", "utf8");
  155. await prepareDevElectronAppBundle({
  156. signApp,
  157. electronAppPath: sourceApp,
  158. runtimeRoot: outputRoot,
  159. electronVersion: "41.0.3",
  160. arch: "arm64",
  161. });
  162. expect(await readFile(join(first.appPath, "Contents", "MacOS", "Electron"), "utf8")).toBe(
  163. "source-v1",
  164. );
  165. });
  166. it("rebuilds the ignored bundle when the source Electron launcher changes", async () => {
  167. const root = await mkdtemp(join(tmpdir(), "zcode-dev-bundle-refresh-test-"));
  168. tempRoots.push(root);
  169. const sourceApp = join(root, "source", "Electron.app");
  170. const sourceContents = join(sourceApp, "Contents");
  171. const sourceMacOS = join(sourceContents, "MacOS");
  172. await mkdir(sourceMacOS, { recursive: true });
  173. await writeFile(
  174. join(sourceContents, "Info.plist"),
  175. `<?xml version="1.0"?><plist version="1.0"><dict>
  176. <key>CFBundleDisplayName</key><string>Electron</string>
  177. <key>CFBundleIdentifier</key><string>com.github.Electron</string>
  178. <key>CFBundleName</key><string>Electron</string>
  179. </dict></plist>\n`,
  180. "utf8",
  181. );
  182. await writeFile(join(sourceMacOS, "Electron"), "source-v1", "utf8");
  183. const outputRoot = join(root, "runtime");
  184. const first = await prepareDevElectronAppBundle({
  185. signApp,
  186. electronAppPath: sourceApp,
  187. runtimeRoot: outputRoot,
  188. electronVersion: "41.0.3",
  189. arch: "arm64",
  190. });
  191. await writeFile(join(sourceMacOS, "Electron"), "source-v2", "utf8");
  192. await prepareDevElectronAppBundle({
  193. signApp,
  194. electronAppPath: sourceApp,
  195. runtimeRoot: outputRoot,
  196. electronVersion: "41.0.3",
  197. arch: "arm64",
  198. });
  199. expect(await readFile(join(first.appPath, "Contents", "MacOS", "Electron"), "utf8")).toBe(
  200. "source-v2",
  201. );
  202. });
  203. it("reuses the ignored bundle when the source Electron launcher is unchanged", async () => {
  204. const root = await mkdtemp(join(tmpdir(), "zcode-dev-bundle-reuse-test-"));
  205. tempRoots.push(root);
  206. const sourceApp = join(root, "source", "Electron.app");
  207. const sourceContents = join(sourceApp, "Contents");
  208. const sourceMacOS = join(sourceContents, "MacOS");
  209. await mkdir(sourceMacOS, { recursive: true });
  210. await writeFile(
  211. join(sourceContents, "Info.plist"),
  212. `<?xml version="1.0"?><plist version="1.0"><dict>
  213. <key>CFBundleDisplayName</key><string>Electron</string>
  214. <key>CFBundleIdentifier</key><string>com.github.Electron</string>
  215. <key>CFBundleName</key><string>Electron</string>
  216. </dict></plist>\n`,
  217. "utf8",
  218. );
  219. await writeFile(join(sourceMacOS, "Electron"), "source-v1", "utf8");
  220. const outputRoot = join(root, "runtime");
  221. const first = await prepareDevElectronAppBundle({
  222. signApp,
  223. electronAppPath: sourceApp,
  224. runtimeRoot: outputRoot,
  225. electronVersion: "41.0.3",
  226. arch: "arm64",
  227. });
  228. // 重建会先 rm 整个 bundle 目录;用一个标记文件证明第二次确实走了缓存命中。
  229. const marker = join(first.appPath, "Contents", "cache-witness");
  230. await writeFile(marker, "kept", "utf8");
  231. await prepareDevElectronAppBundle({
  232. signApp,
  233. electronAppPath: sourceApp,
  234. runtimeRoot: outputRoot,
  235. electronVersion: "41.0.3",
  236. arch: "arm64",
  237. });
  238. expect(await readFile(marker, "utf8")).toBe("kept");
  239. });
  240. });
  241. async function signingFixture() {
  242. const root = await mkdtemp(join(tmpdir(), "zcode-dev-signing-"));
  243. tempRoots.push(root);
  244. const electronAppPath = join(root, "Electron.app");
  245. await mkdir(join(electronAppPath, "Contents", "MacOS"), { recursive: true });
  246. await writeFile(join(electronAppPath, "Contents", "MacOS", "Electron"), "fixture binary");
  247. await writeFile(
  248. join(electronAppPath, "Contents", "Info.plist"),
  249. `<plist><dict>
  250. <key>CFBundleDisplayName</key><string>Electron</string>
  251. <key>CFBundleIdentifier</key><string>com.github.Electron</string>
  252. <key>CFBundleName</key><string>Electron</string>
  253. </dict></plist>`,
  254. );
  255. return {
  256. electronAppPath,
  257. runtimeRoot: join(root, "runtime"),
  258. electronVersion: "44.7.0",
  259. arch: "arm64",
  260. };
  261. }
  262. it("开发副本在写入身份后签名,缓存命中不重复签名", async () => {
  263. const input = await signingFixture();
  264. const sign = vi.fn(async (path: string) => {
  265. expect(await readFile(join(path, "Contents", "Info.plist"), "utf8")).toContain(
  266. DEV_ELECTRON_APP_BUNDLE_ID,
  267. );
  268. });
  269. const first = await prepareDevElectronAppBundle({ ...input, signApp: sign });
  270. await prepareDevElectronAppBundle({ ...input, signApp: sign });
  271. expect(sign).toHaveBeenCalledExactlyOnceWith(first.appPath);
  272. });
  273. it("签名失败不发布缓存指纹;下次重新准备可恢复", async () => {
  274. const input = await signingFixture();
  275. await expect(
  276. prepareDevElectronAppBundle({
  277. ...input,
  278. signApp: async () => {
  279. throw new Error("signer failed");
  280. },
  281. }),
  282. ).rejects.toThrow("signer failed");
  283. const stamp = join(input.runtimeRoot, "44.7.0-arm64", ".zcode-dev-electron-source.json");
  284. await expect(readFile(stamp, "utf8")).rejects.toMatchObject({ code: "ENOENT" });
  285. const recovered = vi.fn(async () => {});
  286. await prepareDevElectronAppBundle({ ...input, signApp: recovered });
  287. expect(recovered).toHaveBeenCalledOnce();
  288. expect(JSON.parse(await readFile(stamp, "utf8")).format).toBe(DEV_ELECTRON_BUNDLE_FORMAT);
  289. });