output-files.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. import type { IGenUiService } from "@zcode/services";
  7. export async function prepareOutputFiles(root: string) {
  8. // 回归:打开 Home 一类包含应用数据目录的工作区,固定 Gen UI 输出仍须可读。
  9. const workspacePath = root;
  10. const outputRoot = join(root, "user-data", "visualizations");
  11. const directory = getGenUiOutputDirectory(outputRoot, {
  12. workspacePath,
  13. workspaceIdentity: "ssh:fixture:/workspace",
  14. sessionId: "fixture",
  15. });
  16. await mkdir(directory, { recursive: true });
  17. const path = join(directory, "demo.html");
  18. await copyFile(join(root, "demo.html"), path);
  19. // fork 保留父会话的 HTML 路径;第二个会话应直接读取同一文件,不需要复制产物。
  20. const paths: Record<string, string> = { fixture: path, "second-task": path };
  21. const remoteOutputRoot = join(root, "remote-user-data", "visualizations");
  22. await mkdir(remoteOutputRoot, { recursive: true });
  23. const remotePath = join(remoteOutputRoot, "demo.html");
  24. await copyFile(path, remotePath);
  25. const remotePaths = { fixture: remotePath, "second-task": remotePath };
  26. return { workspacePath, outputRoot, paths, remoteOutputRoot, remotePaths };
  27. }
  28. export async function checkRemoteOutput(
  29. win: BrowserWindow,
  30. desktop: IGenUiService,
  31. remote: IGenUiService,
  32. output: Awaited<ReturnType<typeof prepareOutputFiles>>,
  33. root: string,
  34. waitHost: (predicate: string) => Promise<unknown>,
  35. ) {
  36. const target = {
  37. workspacePath: output.workspacePath,
  38. workspaceIdentity: "ssh:fixture:/workspace",
  39. sessionId: "fixture",
  40. path: output.remotePaths.fixture,
  41. };
  42. // 两端目录必须不同;不能让远端身份的本地文件夹具掩盖错误的服务接线。
  43. await assert.rejects(desktop.readDocument(target), /outside the host output directory/);
  44. await win.loadFile(join(root, "react.html"), { query: { source: "1" } });
  45. await waitHost("window.showReply");
  46. await win.webContents.executeJavaScript("window.showReply('complete')");
  47. await waitHost("document.querySelector('[data-gen-ui-phase=ready]')");
  48. await checkInheritedOutput(win);
  49. const guestId = await win.webContents.executeJavaScript(
  50. "document.querySelector('[data-testid=gen-ui-anchor] webview').getWebContentsId()",
  51. );
  52. const frame = webContents
  53. .fromId(guestId)!
  54. .mainFrame.frames.find((f) => f.url.includes("/instance/"))!;
  55. await frame.executeJavaScript(
  56. "window.zcode.setWidgetState({modelContent:{remote:42},privateContent:{desktop:true}})",
  57. );
  58. assert.deepEqual(await desktop.getState(target), {
  59. modelContent: { remote: 42 },
  60. privateContent: { desktop: true },
  61. });
  62. assert.equal(await remote.getState(target), null);
  63. await win.webContents.executeJavaScript(
  64. "window.showReply('hidden'); window.deleteTask('fixture')",
  65. );
  66. await waitHost("!document.querySelector('webview')");
  67. const before = await win.webContents.executeJavaScript("window.inspectReact().reads");
  68. await win.setContentSize(390, 780);
  69. await win.webContents.executeJavaScript("window.showMobile()");
  70. await waitHost(
  71. "document.querySelector('[role=status]')?.textContent.includes('Open this interactive view in the desktop app')",
  72. );
  73. assert.equal(await win.webContents.executeJavaScript("window.inspectReact().reads"), before);
  74. assert.equal(
  75. await win.webContents.executeJavaScript("document.querySelectorAll('webview').length"),
  76. 0,
  77. );
  78. }
  79. export async function checkInheritedOutput(win: BrowserWindow): Promise<void> {
  80. const guestId = await win.webContents.executeJavaScript(
  81. "document.querySelector('[data-testid=gen-ui-anchor] webview').getWebContentsId()",
  82. );
  83. const frame = webContents
  84. .fromId(guestId)!
  85. .mainFrame.frames.find((value) => value.url.includes("/instance/"))!;
  86. assert.equal(
  87. await frame.executeJavaScript("document.querySelector('h1').textContent"),
  88. "Updated heading",
  89. );
  90. }