subagent-host-window.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. offset?: number;
  41. paths?: string[];
  42. operation?: string;
  43. stack?: string;
  44. readCounts?: Record<string, number>;
  45. },
  46. ),
  47. ),
  48. )
  49. ).flat();
  50. }
  51. export async function waitForHostBoundary(kind: string, id?: string) {
  52. await browser.waitUntil(
  53. async () => (await readHostBoundary()).some((r) => r.kind === kind && (!id || r.id === id)),
  54. { timeout: 15000, timeoutMsg: `缺少 Host 边界事件 ${kind}/${id ?? ""}` },
  55. );
  56. }
  57. /** 测试侧控制 Electron 窗口发现并预加载文件/IPC 故障边界,调用产品已有 activate 路径。 */
  58. export async function openSubagentHostWindow() {
  59. return openFaultInjectedHostWindow(
  60. fileURLToPath(new URL("./subagent-host-faults.cjs", import.meta.url)),
  61. );
  62. }
  63. /** env 只注入新窗口的 Host,例如 ZCODE_E2E_FS_FAULTS,不影响已有窗口。 */
  64. export async function openFaultInjectedHostWindow(
  65. preloadPath: string,
  66. env: Record<string, string> = {},
  67. ) {
  68. await setHostFaults({});
  69. const handles = await browser.getWindowHandles();
  70. const windowId = await browser.electron.execute(
  71. async (electron, preload, root, extraEnv) => {
  72. const originalWindows = electron.BrowserWindow.getAllWindows;
  73. const originalFork = electron.utilityProcess.fork;
  74. let didFork!: () => void;
  75. const forked = new Promise<void>((resolve) => {
  76. didFork = resolve;
  77. });
  78. const created = new Promise<number>((resolve) =>
  79. electron.app.once("browser-window-created", (_event, window) => {
  80. electron.BrowserWindow.getAllWindows = originalWindows;
  81. resolve(window.id);
  82. }),
  83. );
  84. electron.utilityProcess.fork = (path, args, options) => {
  85. electron.utilityProcess.fork = originalFork;
  86. const child = originalFork(preload, [path, ...(args ?? [])], {
  87. ...options,
  88. env: { ...options?.env, ...extraEnv, ZCODE_E2E_HOST_FAULT_ROOT: root },
  89. });
  90. didFork();
  91. return child;
  92. };
  93. electron.BrowserWindow.getAllWindows = () => [];
  94. try {
  95. electron.app.emit("activate");
  96. const [id] = await Promise.all([created, forked]);
  97. return id;
  98. } finally {
  99. electron.BrowserWindow.getAllWindows = originalWindows;
  100. electron.utilityProcess.fork = originalFork;
  101. }
  102. },
  103. preloadPath,
  104. process.env.ZCODE_E2E_ARTIFACT_DIR!,
  105. env,
  106. );
  107. let handle = "";
  108. await browser.waitUntil(
  109. async () => {
  110. handle = (await browser.getWindowHandles()).find((h) => !handles.includes(h)) ?? "";
  111. return Boolean(handle);
  112. },
  113. { timeout: 30000 },
  114. );
  115. await browser.switchToWindow(handle);
  116. await waitForHostBoundary("ready");
  117. return { windowId, handle };
  118. }