devElectronAppBundle.test.ts 10 KB

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