subagent-host-window.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { readFile, readdir, writeFile, rename } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import { fileURLToPath } from "node:url";
  4. export interface HostFaultControl {
  5. tracePaths?: string[];
  6. files?: Array<{
  7. id: string;
  8. path: string;
  9. operation: "readFile" | "readdir" | "writeFile";
  10. action: "hold" | "fail";
  11. once?: boolean;
  12. rejectAfterRelease?: boolean;
  13. }>;
  14. released?: string[];
  15. }
  16. export async function setHostFaults(control: HostFaultControl) {
  17. const path = join(process.env.ZCODE_E2E_ARTIFACT_DIR!, "host-fault-control.json");
  18. await writeFile(path + ".tmp", JSON.stringify(control));
  19. await rename(path + ".tmp", path);
  20. }
  21. export async function readHostBoundary() {
  22. const root = process.env.ZCODE_E2E_ARTIFACT_DIR!;
  23. return (
  24. await Promise.all(
  25. (
  26. await readdir(root)
  27. )
  28. .filter((p) => /^host-boundary-\d+\.jsonl$/u.test(p))
  29. .map(async (p) =>
  30. (await readFile(join(root, p), "utf8"))
  31. .trim()
  32. .split("\n")
  33. .filter(Boolean)
  34. .map(
  35. (line) =>
  36. JSON.parse(line) as {
  37. pid: number;
  38. kind: string;
  39. id?: string;
  40. paths?: string[];
  41. operation?: string;
  42. stack?: string;
  43. readCounts?: Record<string, number>;
  44. },
  45. ),
  46. ),
  47. )
  48. ).flat();
  49. }
  50. export async function waitForHostBoundary(kind: string, id?: string) {
  51. await browser.waitUntil(
  52. async () => (await readHostBoundary()).some((r) => r.kind === kind && (!id || r.id === id)),
  53. { timeout: 15000, timeoutMsg: `缺少 Host 边界事件 ${kind}/${id ?? ""}` },
  54. );
  55. }
  56. /** 测试侧控制 Electron 窗口发现并预加载文件/IPC 故障边界,调用产品已有 activate 路径。 */
  57. export async function openSubagentHostWindow() {
  58. await setHostFaults({});
  59. const handles = await browser.getWindowHandles();
  60. const windowId = await browser.electron.execute(
  61. async (electron, preload, root) => {
  62. const originalWindows = electron.BrowserWindow.getAllWindows;
  63. const originalFork = electron.utilityProcess.fork;
  64. let didFork!: () => void;
  65. const forked = new Promise<void>((resolve) => {
  66. didFork = resolve;
  67. });
  68. const created = new Promise<number>((resolve) =>
  69. electron.app.once("browser-window-created", (_event, window) => {
  70. electron.BrowserWindow.getAllWindows = originalWindows;
  71. resolve(window.id);
  72. }),
  73. );
  74. electron.utilityProcess.fork = (path, args, options) => {
  75. electron.utilityProcess.fork = originalFork;
  76. const child = originalFork(preload, [path, ...(args ?? [])], {
  77. ...options,
  78. env: { ...options?.env, ZCODE_E2E_HOST_FAULT_ROOT: root },
  79. });
  80. didFork();
  81. return child;
  82. };
  83. electron.BrowserWindow.getAllWindows = () => [];
  84. try {
  85. electron.app.emit("activate");
  86. const [id] = await Promise.all([created, forked]);
  87. return id;
  88. } finally {
  89. electron.BrowserWindow.getAllWindows = originalWindows;
  90. electron.utilityProcess.fork = originalFork;
  91. }
  92. },
  93. fileURLToPath(new URL("./subagent-host-faults.cjs", import.meta.url)),
  94. process.env.ZCODE_E2E_ARTIFACT_DIR!,
  95. );
  96. let handle = "";
  97. await browser.waitUntil(
  98. async () => {
  99. handle = (await browser.getWindowHandles()).find((h) => !handles.includes(h)) ?? "";
  100. return Boolean(handle);
  101. },
  102. { timeout: 30000 },
  103. );
  104. await browser.switchToWindow(handle);
  105. await waitForHostBoundary("ready");
  106. return { windowId, handle };
  107. }