interaction-main.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { app, protocol } from "electron";
  2. import assert from "node:assert/strict";
  3. import { readFile, writeFile } from "node:fs/promises";
  4. import { join } from "node:path";
  5. import { PLUGIN_SANDBOX_PRIVILEGED_SCHEME } from "../../src/main/pluginSandbox/index.js";
  6. import { managedEnvironment } from "./managed-environment.js";
  7. import { clickHostPoint, checkPagePointerInput } from "./pointer-input.js";
  8. const [root, profile, node, cli] = process.argv.slice(2) as [string, string, string, string];
  9. app.setPath("userData", profile);
  10. protocol.registerSchemesAsPrivileged([PLUGIN_SANDBOX_PRIVILEGED_SCHEME]);
  11. app.on("window-all-closed", () => {});
  12. app
  13. .whenReady()
  14. .then(async () => {
  15. const htmlPath = join(root, "widget.html");
  16. const goodHtml = await readFile(htmlPath, "utf8");
  17. const env = await managedEnvironment(root, node, cli);
  18. try {
  19. // 真实等待握手时限;不伪造 controller 的错误态或直接卸载 webview。
  20. await writeFile(
  21. htmlPath,
  22. '<!doctype html><script>throw new Error("fixture startup failure")</script>',
  23. );
  24. const scope = await env.agent.session(join(root, "interaction-workspace"), "managed");
  25. const creation = env.createWindow(scope).then(() => "unexpected success", String);
  26. await env.events.wait((event) => event.type === "phase" && event.phase === "mounted");
  27. const win = env.windows[0]!;
  28. const ui = (code: string) => win.webContents.executeJavaScript(code);
  29. await ui("window.setAnchors(false, true)");
  30. assert.match(await creation, /handshake timed out/);
  31. await ui(`new Promise(resolve => {
  32. if (!document.querySelector('webview')) return resolve();
  33. const observer = new MutationObserver(() => {
  34. if (!document.querySelector('webview')) { observer.disconnect(); resolve(); }
  35. });
  36. observer.observe(document.body, {subtree: true, childList: true});
  37. })`);
  38. const point = await ui(`(() => {
  39. const button = document.querySelector('[data-testid=plugin-ui-retry]');
  40. const r = button.getBoundingClientRect();
  41. const x = Math.round(r.x + r.width / 2), y = Math.round(r.y + r.height / 2);
  42. window.retryClickCount = 0;
  43. button.addEventListener('click', () => window.retryClickCount++);
  44. return {x, y, hit: document.elementFromPoint(x, y)?.outerHTML};
  45. })()`);
  46. await writeFile(join(root, "retry-hit-test.json"), JSON.stringify(point, null, 2));
  47. await writeFile(htmlPath, goodHtml);
  48. const offset = env.events.history.length;
  49. await clickHostPoint(win, { x: point.x, y: point.y });
  50. assert.equal(await ui("window.retryClickCount"), 1, JSON.stringify(point));
  51. await env.events.wait((event) => event.type === "phase" && event.phase === "running", offset);
  52. await env.evalPage(win, "window.fixtureReady");
  53. assert.equal((await ui("window.managedState()")).views, 1);
  54. assert.equal(env.events.history.filter((event) => event.type === "registered").length, 2);
  55. const guest = env.guests.get(win.webContents.id)!;
  56. await checkPagePointerInput(win, guest);
  57. await ui("window.setAnchors(true, false)");
  58. await checkPagePointerInput(win, guest);
  59. assert.equal(env.guests.get(win.webContents.id)!.id, guest.id);
  60. await writeFile(
  61. join(root, "interaction-results.json"),
  62. JSON.stringify(
  63. {
  64. retryClicks: await ui("window.retryClickCount"),
  65. phase: (await ui("window.managedState()")).phase,
  66. views: (await ui("window.managedState()")).views,
  67. sidebarInput: true,
  68. inlineInput: true,
  69. },
  70. null,
  71. 2,
  72. ),
  73. );
  74. process.stdout.write(
  75. "PASS real handshake timeout accepts mouse retry and restores interactive sidebar/inline guest\n",
  76. );
  77. } finally {
  78. await writeFile(htmlPath, goodHtml);
  79. await env.close();
  80. }
  81. app.exit(0);
  82. })
  83. .catch((error) => {
  84. process.stderr.write(String(error.stack ?? error) + "\n");
  85. app.exit(1);
  86. });