managed-main.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. import { checkPermissions } from "./managed-permission-checks.js";
  8. import { checkLifecycle } from "./managed-lifecycle-checks.js";
  9. import { checkManaged } from "./managed-checks.js";
  10. const [root, profile, node, cli] = process.argv.slice(2) as [string, string, string, string];
  11. app.setPath("userData", profile);
  12. protocol.registerSchemesAsPrivileged([PLUGIN_SANDBOX_PRIVILEGED_SCHEME]);
  13. app.on("window-all-closed", () => {});
  14. app
  15. .whenReady()
  16. .then(async () => {
  17. const env = await managedEnvironment(root, node, cli);
  18. const checks: { name: string; ms: number }[] = [];
  19. const check = async (name: string, run: () => Promise<void>) => {
  20. const start = performance.now();
  21. let timer: ReturnType<typeof setTimeout>;
  22. try {
  23. await Promise.race([
  24. run(),
  25. new Promise<never>((_, reject) => {
  26. timer = setTimeout(
  27. () => reject(new Error(`Timed out: ${name}`)),
  28. name.includes("keep-warm") ? 45_000 : 20_000,
  29. );
  30. }),
  31. ]);
  32. } catch (error) {
  33. await writeFile(
  34. join(root, "managed-failure.json"),
  35. JSON.stringify(
  36. {
  37. name,
  38. requests: env.model.requests.history,
  39. approvals: env.agent.approvals.history,
  40. events: env.events.history.slice(-20),
  41. notifications: env.agent.notifications.history.slice(-12),
  42. },
  43. null,
  44. 2,
  45. ),
  46. );
  47. throw error;
  48. } finally {
  49. clearTimeout(timer!);
  50. }
  51. checks.push({ name, ms: performance.now() - start });
  52. process.stdout.write(`PASS ${name}\n`);
  53. };
  54. try {
  55. const scope = await env.agent.session(join(root, "managed-workspace"), "managed");
  56. const started = performance.now();
  57. const win = await env.createWindow(scope);
  58. const coldPageMs = performance.now() - started;
  59. await writeFile(
  60. join(root, "managed-inline.png"),
  61. (await win.webContents.capturePage()).toPNG(),
  62. );
  63. await check(
  64. "production React card and managed page establish one live conversation lease",
  65. async () => {
  66. const state = await win.webContents.executeJavaScript("window.managedState()");
  67. assert.equal(state.phase, "running");
  68. assert.equal(state.views, 1);
  69. assert.equal(state.listeners.frame, 1);
  70. assert.equal(env.events.history.filter((e) => e.type === "subscribed").length, 1);
  71. },
  72. );
  73. await checkManaged(env, win, scope, check);
  74. await checkPermissions(env, root, check);
  75. await checkLifecycle(env, win, scope, check);
  76. await writeFile(
  77. join(root, "managed-storage-identity.json"),
  78. JSON.stringify({ appIdentity: env.handles.get(win.webContents.id).instance.appIdentity }),
  79. );
  80. await writeFile(
  81. join(root, "managed-results.json"),
  82. JSON.stringify({ coldPageMs, checks }, null, 2),
  83. );
  84. } finally {
  85. await env.close();
  86. }
  87. app.exit(0);
  88. })
  89. .catch((error) => {
  90. process.stderr.write(String(error.stack ?? error) + "\n");
  91. app.exit(1);
  92. });