devElectronAppBundle.test.ts 9.9 KB

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