output-files.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import { copyFile, mkdir } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import assert from "node:assert/strict";
  4. import { webContents, type BrowserWindow } from "electron";
  5. import { getGenUiOutputDirectory } from "@zcode/shared/node";
  6. export async function prepareOutputFiles(root: string) {
  7. // 回归:打开 Home 一类包含应用数据目录的工作区,固定 Gen UI 输出仍须可读。
  8. const workspacePath = root;
  9. const outputRoot = join(root, "user-data", "visualizations");
  10. const directory = getGenUiOutputDirectory(outputRoot, {
  11. workspacePath,
  12. workspaceIdentity: "ssh:fixture:/workspace",
  13. sessionId: "fixture",
  14. });
  15. await mkdir(directory, { recursive: true });
  16. const path = join(directory, "demo.html");
  17. await copyFile(join(root, "demo.html"), path);
  18. // fork 保留父会话的 HTML 路径;第二个会话应直接读取同一文件,不需要复制产物。
  19. const paths: Record<string, string> = { fixture: path, "second-task": path };
  20. return { workspacePath, outputRoot, paths };
  21. }
  22. export async function checkInheritedOutput(win: BrowserWindow): Promise<void> {
  23. const guestId = await win.webContents.executeJavaScript(
  24. "document.querySelector('[data-testid=gen-ui-anchor] webview').getWebContentsId()",
  25. );
  26. const frame = webContents
  27. .fromId(guestId)!
  28. .mainFrame.frames.find((value) => value.url.includes("/instance/"))!;
  29. assert.equal(
  30. await frame.executeJavaScript("document.querySelector('h1').textContent"),
  31. "Updated heading",
  32. );
  33. }