embeddedBrowserJavaScriptDialogPreload.test.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
  2. import { PlatformChannels } from "@zcode/shared";
  3. const h = vi.hoisted(() => ({
  4. executeInMainWorld: vi.fn(),
  5. exposeInMainWorld: vi.fn(),
  6. sendSync: vi.fn(),
  7. }));
  8. vi.mock("electron", () => ({
  9. contextBridge: {
  10. executeInMainWorld: h.executeInMainWorld,
  11. exposeInMainWorld: h.exposeInMainWorld,
  12. },
  13. ipcRenderer: {
  14. sendSync: h.sendSync,
  15. },
  16. }));
  17. async function installPreloadBridge() {
  18. await import("../src/preload/embeddedBrowserJavaScriptDialog.js");
  19. const [bridgeKey, bridge] = h.exposeInMainWorld.mock.calls[0] as [
  20. string,
  21. { show: (type: "alert" | "confirm", message: string) => unknown },
  22. ];
  23. const execution = h.executeInMainWorld.mock.calls[0]?.[0] as {
  24. func: (key: string) => void;
  25. args: [string];
  26. };
  27. return { bridge, bridgeKey, execution };
  28. }
  29. /** 第二个主世界注入:Notification.permission 状态 override。 */
  30. async function installNotificationOverride(): Promise<() => void> {
  31. await import("../src/preload/embeddedBrowserJavaScriptDialog.js");
  32. const execution = h.executeInMainWorld.mock.calls[1]?.[0] as {
  33. func: () => void;
  34. args: [];
  35. };
  36. return execution.func;
  37. }
  38. describe("embedded browser JavaScript Dialog preload", () => {
  39. beforeEach(() => {
  40. vi.resetModules();
  41. h.executeInMainWorld.mockReset();
  42. h.exposeInMainWorld.mockReset();
  43. h.sendSync.mockReset();
  44. });
  45. afterEach(() => {
  46. vi.unstubAllGlobals();
  47. });
  48. describe("Notification.permission 状态 override", () => {
  49. // node 测试环境没有 Notification 全局,构造一个可 override 的替身。
  50. const stubNotificationGlobal = () => {
  51. vi.stubGlobal(
  52. "Notification",
  53. class Notification {
  54. static get permission(): NotificationPermission {
  55. return "denied";
  56. }
  57. static requestPermission(): Promise<NotificationPermission> {
  58. return Promise.resolve("denied");
  59. }
  60. },
  61. );
  62. };
  63. it("初始为 default(不误导为 denied),requestPermission 结果回写状态", async () => {
  64. stubNotificationGlobal();
  65. const install = await installNotificationOverride();
  66. const requestMock = vi.fn(() => Promise.resolve("granted" as NotificationPermission));
  67. Object.defineProperty(Notification, "requestPermission", {
  68. value: requestMock,
  69. configurable: true,
  70. writable: true,
  71. });
  72. install();
  73. expect(Notification.permission).toBe("default");
  74. await Notification.requestPermission();
  75. expect(Notification.permission).toBe("granted");
  76. expect(requestMock).toHaveBeenCalledTimes(1);
  77. });
  78. it("denied 结果同样回写;旧回调签名仍被调用", async () => {
  79. stubNotificationGlobal();
  80. const install = await installNotificationOverride();
  81. const requestMock = vi.fn(() => Promise.resolve("denied" as NotificationPermission));
  82. Object.defineProperty(Notification, "requestPermission", {
  83. value: requestMock,
  84. configurable: true,
  85. writable: true,
  86. });
  87. install();
  88. const legacyCallback = vi.fn();
  89. await Notification.requestPermission(legacyCallback);
  90. expect(Notification.permission).toBe("denied");
  91. expect(legacyCallback).toHaveBeenCalledWith("denied");
  92. });
  93. });
  94. it("returns the main-process choice without invoking Chromium's native dialog", async () => {
  95. h.sendSync.mockReturnValue({ handled: true, value: false });
  96. const { bridge, bridgeKey, execution } = await installPreloadBridge();
  97. const nativeAlert = vi.fn();
  98. const nativeConfirm = vi.fn(() => true);
  99. const pageWindow = { alert: nativeAlert, confirm: nativeConfirm } as unknown as Window;
  100. Object.assign(pageWindow, { [bridgeKey]: bridge });
  101. vi.stubGlobal("window", pageWindow);
  102. execution.func(...execution.args);
  103. expect(window.confirm("delete")).toBe(false);
  104. window.alert("notice");
  105. expect(h.sendSync).toHaveBeenNthCalledWith(
  106. 1,
  107. PlatformChannels.EmbeddedBrowserJavaScriptDialog,
  108. { type: "confirm", message: "delete" },
  109. );
  110. expect(h.sendSync).toHaveBeenNthCalledWith(
  111. 2,
  112. PlatformChannels.EmbeddedBrowserJavaScriptDialog,
  113. { type: "alert", message: "notice" },
  114. );
  115. expect(nativeConfirm).not.toHaveBeenCalled();
  116. expect(nativeAlert).not.toHaveBeenCalled();
  117. });
  118. it("calls the original API when main requests automation passthrough", async () => {
  119. h.sendSync.mockReturnValue({ handled: false });
  120. const { bridge, bridgeKey, execution } = await installPreloadBridge();
  121. const nativeAlert = vi.fn();
  122. const nativeConfirm = vi.fn(() => true);
  123. const pageWindow = { alert: nativeAlert, confirm: nativeConfirm } as unknown as Window;
  124. Object.assign(pageWindow, { [bridgeKey]: bridge });
  125. vi.stubGlobal("window", pageWindow);
  126. execution.func(...execution.args);
  127. expect(window.confirm("automation")).toBe(true);
  128. window.alert("automation-alert");
  129. expect(nativeConfirm).toHaveBeenCalledWith("automation");
  130. expect(nativeAlert).toHaveBeenCalledWith("automation-alert");
  131. });
  132. it("installs the same wrapper in an inherited same-origin blank iframe", async () => {
  133. h.sendSync.mockReturnValue({ handled: true, value: false });
  134. const { bridge, bridgeKey, execution } = await installPreloadBridge();
  135. const childNativeAlert = vi.fn();
  136. const childNativeConfirm = vi.fn(() => true);
  137. const childWindow = {
  138. alert: childNativeAlert,
  139. confirm: childNativeConfirm,
  140. document: { querySelectorAll: vi.fn(() => []) },
  141. } as unknown as Window;
  142. const frame = {
  143. addEventListener: vi.fn(),
  144. contentWindow: childWindow,
  145. } as unknown as HTMLIFrameElement;
  146. const observe = vi.fn();
  147. const pageWindow = {
  148. alert: vi.fn(),
  149. confirm: vi.fn(() => true),
  150. document: {
  151. documentElement: {},
  152. querySelectorAll: vi.fn(() => [frame]),
  153. },
  154. MutationObserver: class {
  155. observe = observe;
  156. },
  157. } as unknown as Window;
  158. Object.assign(pageWindow, { [bridgeKey]: bridge });
  159. vi.stubGlobal("window", pageWindow);
  160. execution.func(...execution.args);
  161. expect(childWindow.confirm("iframe-confirm")).toBe(false);
  162. expect(childNativeConfirm).not.toHaveBeenCalled();
  163. expect(observe).toHaveBeenCalledWith(
  164. pageWindow.document.documentElement,
  165. expect.objectContaining({ childList: true, subtree: true }),
  166. );
  167. });
  168. });