rows-main.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import { app, protocol } from "electron";
  2. import assert from "node:assert/strict";
  3. import { 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. const [root, profile, node, cli] = process.argv.slice(2) as [string, string, string, string];
  8. app.setPath("userData", profile);
  9. protocol.registerSchemesAsPrivileged([PLUGIN_SANDBOX_PRIVILEGED_SCHEME]);
  10. app.on("window-all-closed", () => {});
  11. app
  12. .whenReady()
  13. .then(async () => {
  14. const env = await managedEnvironment(root, node, cli);
  15. const checks: string[] = [];
  16. const check = async (name: string, run: () => Promise<void>) => {
  17. await run();
  18. checks.push(name);
  19. process.stdout.write(`PASS ${name}\n`);
  20. };
  21. try {
  22. const scope = await env.agent.session(join(root, "rows-workspace"), "rows");
  23. const win = await env.createWindow(scope, "rows");
  24. const ui = (code: string) => win.webContents.executeJavaScript(code);
  25. const page = (code: string) => env.evalPage(win, code);
  26. const state = () => ui("window.rowsState()");
  27. const settle = () =>
  28. ui("new Promise(r => requestAnimationFrame(() => requestAnimationFrame(r)))");
  29. const openRow = async () => {
  30. await ui(`document.querySelector('[data-testid="plugin-ui-open-side-pane"]').click()`);
  31. await settle();
  32. };
  33. const closeSidebar = async () => {
  34. await ui("document.getElementById('close-sidebar').click()");
  35. await settle();
  36. };
  37. const showCard = async () => {
  38. await ui(
  39. `document.querySelector('[data-tool-call-id] [data-testid="plugin-ui-card"]').scrollIntoView({block:'center'})`,
  40. );
  41. await settle();
  42. assert.equal(await ui("window.rowPageVisible()"), true);
  43. };
  44. const expandHistory = async () => {
  45. await ui(
  46. `document.querySelectorAll('button[data-history-open="false"]').forEach(e => e.click())`,
  47. );
  48. await settle();
  49. };
  50. const errors = [
  51. { id: 2, isError: true, fullscreen: true },
  52. { id: 4, isError: true, fullscreen: true },
  53. { id: 6, isError: true, fullscreen: true },
  54. { id: 7, status: "error", fullscreen: true },
  55. { id: 8, status: "cancelled", fullscreen: true },
  56. ];
  57. await check(
  58. "all failures: real timeline rows preserve records but create no App or automatic sidebar",
  59. async () => {
  60. await ui(`window.setRows(${JSON.stringify(errors)}, true)`);
  61. await expandHistory();
  62. assert.equal(
  63. await ui('document.querySelectorAll("[data-tool-call-id]").length'),
  64. errors.length,
  65. );
  66. assert.deepEqual(await state(), {
  67. cards: 0,
  68. placeholders: 0,
  69. opens: 0,
  70. sidebar: false,
  71. views: 0,
  72. owners: [],
  73. });
  74. assert.equal(env.events.history.filter((e) => e.type === "registered").length, 0);
  75. assert.equal(env.agent.events.history.filter((e) => e.type === "read").length, 0);
  76. },
  77. );
  78. await check("explicit widgetSessionId failures also cannot create App anchors", async () => {
  79. await ui(
  80. `window.setRows(${JSON.stringify(errors.map((row) => ({ ...row, widget: true })))}, true)`,
  81. );
  82. await expandHistory();
  83. assert.equal(
  84. await ui('document.querySelectorAll("[data-tool-call-id]").length'),
  85. errors.length,
  86. );
  87. const s = await state();
  88. assert.equal(s.cards, 0);
  89. assert.equal(s.placeholders, 0);
  90. assert.equal(s.opens, 0);
  91. assert.equal(s.views, 0);
  92. assert.equal(env.agent.events.history.filter((e) => e.type === "read").length, 0);
  93. });
  94. const mixed = [{ id: 1 }, errors[0], { id: 3 }, errors[1], { id: 5 }, ...errors.slice(2)];
  95. const attached = new Promise<void>((resolve) =>
  96. win.webContents.once("did-attach-webview", () => resolve()),
  97. );
  98. await ui(`window.setRows(${JSON.stringify(mixed)})`);
  99. await attached;
  100. await ui("window.waitRowsRunning()");
  101. await page("window.fixtureReady");
  102. await expandHistory();
  103. const guestId = env.guests.get(win.webContents.id)!.id;
  104. await check(
  105. "success plus three MCP errors, transport error and cancellation leaves one latest-success anchor",
  106. async () => {
  107. const s = await state();
  108. assert.equal(s.cards, 1);
  109. assert.equal(s.views, 1);
  110. assert.equal(s.opens, 0);
  111. assert.deepEqual(s.owners, ["row-5"]);
  112. await showCard();
  113. assert.equal(
  114. await ui('document.querySelectorAll("[data-tool-call-id]").length'),
  115. mixed.length,
  116. );
  117. await page("document.getElementById('counter').click()");
  118. await page("new Promise(r => requestAnimationFrame(r))");
  119. assert.equal(await page("document.getElementById('counter').textContent"), "1");
  120. },
  121. );
  122. const started = performance.now();
  123. await check(
  124. "20 real row-button inline/sidebar round trips keep one guest, one anchor and React state",
  125. async () => {
  126. for (let i = 0; i < 20; i++) {
  127. await ui(`document.querySelector('[data-testid="plugin-ui-open-side-pane"]').click()`);
  128. await settle();
  129. let s = await state();
  130. assert.equal(s.cards, 0);
  131. assert.equal(s.placeholders, 1);
  132. assert.equal(s.views, 1);
  133. assert.deepEqual(s.owners, ["row-5"]);
  134. await ui("document.getElementById('close-sidebar').click()");
  135. await settle();
  136. s = await state();
  137. assert.equal(s.cards, 1);
  138. assert.equal(s.placeholders, 0);
  139. await showCard();
  140. assert.equal(env.guests.get(win.webContents.id)!.id, guestId);
  141. }
  142. assert.equal(await page("document.getElementById('counter').textContent"), "1");
  143. assert.equal(await page("window.starts"), 1);
  144. assert.equal(env.events.history.filter((e) => e.type === "registered").length, 1);
  145. assert.equal(env.agent.events.history.filter((e) => e.type === "read").length, 1);
  146. assert.equal(env.events.history.filter((e) => e.type === "subscribed").length, 1);
  147. },
  148. );
  149. const switchesMs = performance.now() - started;
  150. await check(
  151. "timeline unmount/remount and later error preserve the good page without fullscreen stealing",
  152. async () => {
  153. await ui("window.mountRows(false)");
  154. await ui("window.mountRows(true)");
  155. await expandHistory();
  156. await ui(
  157. `window.setRows(${JSON.stringify([...mixed, { id: 9, isError: true, fullscreen: true }])})`,
  158. );
  159. await expandHistory();
  160. const s = await state();
  161. assert.equal(s.opens, 20);
  162. assert.equal(s.cards, 1);
  163. assert.deepEqual(s.owners, ["row-5"]);
  164. assert.equal(env.guests.get(win.webContents.id)!.id, guestId);
  165. assert.equal(await page("document.getElementById('counter').textContent"), "1");
  166. },
  167. );
  168. await check(
  169. "new successful fullscreen result still opens once and reuses the existing page",
  170. async () => {
  171. await ui(`window.setRows(${JSON.stringify([...mixed, { id: 10, fullscreen: true }])})`);
  172. await settle();
  173. const s = await state();
  174. assert.equal(s.opens, 21);
  175. assert.equal(s.placeholders, 1);
  176. assert.deepEqual(s.owners, ["row-10"]);
  177. await ui("document.getElementById('close-sidebar').click()");
  178. await settle();
  179. assert.deepEqual((await state()).owners, ["row-10"]);
  180. assert.equal(env.guests.get(win.webContents.id)!.id, guestId);
  181. assert.equal(await page("document.getElementById('counter').textContent"), "1");
  182. assert.equal(env.agent.events.history.filter((e) => e.type === "read").length, 1);
  183. assert.equal(env.events.history.filter((e) => e.type === "registered").length, 1);
  184. },
  185. );
  186. await check(
  187. "closed fullscreen call stays closed after session view unmount and remount",
  188. async () => {
  189. const opens = (await state()).opens;
  190. await ui("window.mountRows(false)");
  191. await ui("window.mountRows(true)");
  192. assert.equal((await state()).sidebar, false);
  193. assert.equal((await state()).opens, opens);
  194. assert.equal(env.guests.get(win.webContents.id)!.id, guestId);
  195. await openRow();
  196. assert.equal((await state()).sidebar, true);
  197. assert.equal((await state()).opens, opens + 1);
  198. },
  199. );
  200. await check(
  201. "calls arriving in an open sidebar are consumed; later new calls still open",
  202. async () => {
  203. await ui(`window.setRows(${JSON.stringify([...mixed, { id: 11, fullscreen: true }])})`);
  204. const opens = (await state()).opens;
  205. await closeSidebar();
  206. await ui("window.mountRows(false)");
  207. await ui("window.mountRows(true)");
  208. assert.equal((await state()).sidebar, false);
  209. assert.equal((await state()).opens, opens);
  210. await ui(`window.setRows(${JSON.stringify([...mixed, { id: 12, fullscreen: true }])})`);
  211. assert.equal((await state()).sidebar, true);
  212. assert.equal((await state()).opens, opens + 1);
  213. assert.equal(env.guests.get(win.webContents.id)!.id, guestId);
  214. await closeSidebar();
  215. },
  216. );
  217. await showCard();
  218. await writeFile(join(root, "rows-inline.png"), (await win.webContents.capturePage()).toPNG());
  219. await writeFile(
  220. join(root, "rows-results.json"),
  221. JSON.stringify({ checks, switchesMs, guestId, state: await state() }, null, 2),
  222. );
  223. } finally {
  224. await env.close();
  225. }
  226. app.exit(0);
  227. })
  228. .catch((error) => {
  229. process.stderr.write(String(error.stack ?? error) + "\n");
  230. app.exit(1);
  231. });