sshDirectoryFlow.test.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { runInNewContext } from "node:vm";
  2. import { afterEach, describe, expect, it, vi } from "vitest";
  3. import { ServiceChannels } from "@zcode/shared";
  4. import { TID_SETTINGS_SECTION_NAV, TID_TASK_SETTINGS_BUTTON, testId } from "@zcode/shared/test-ids";
  5. import { clickTestIdByDom, clickTestIdByWebDriver } from "#e2e/helpers/desktop-app";
  6. import { callHandoffHost } from "#e2e/helpers/remote-project-handoff-probe";
  7. import { openSSHRemoteDialog, openSSHSourceDirectory } from "#e2e/helpers/ssh-remote-p0";
  8. vi.mock("./e2e/helpers/desktop-app.js", () => ({
  9. clickTestIdByDom: vi.fn(),
  10. clickTestIdByWebDriver: vi.fn(),
  11. hoverTestIdByWebDriver: vi.fn(),
  12. setInputValueByTestIdDom: vi.fn(),
  13. waitForWorkspaceApp: vi.fn(),
  14. waitForTestIdByDom: vi.fn(),
  15. }));
  16. vi.mock("./e2e/helpers/custom-openai-provider-store.js", () => ({}));
  17. vi.mock("./e2e/helpers/v4-conversation.js", () => ({ startNewV4Draft: vi.fn() }));
  18. vi.mock("./e2e/helpers/remote-project-handoff-probe.js", () => ({
  19. installHandoffRpcProbe: vi.fn(),
  20. callHandoffHost: vi.fn(async () => ({
  21. profiles: [
  22. {
  23. transport: "ssh",
  24. lifecycleState: "active",
  25. sourceServerId: "known-source",
  26. target: { kind: "ssh", host: "127.0.0.1", port: 43210, username: "dev" },
  27. },
  28. ],
  29. })),
  30. }));
  31. const target = { host: "127.0.0.1", port: 43210, username: "dev", password: "fixture" };
  32. function button(text: string, disabled = false) {
  33. return { innerText: text, disabled, getClientRects: () => [1], click: vi.fn() };
  34. }
  35. function installBrowserFixture(directoryLabel: string, disabled = false, settingsOpen = false) {
  36. const directoryButton = button(directoryLabel, disabled);
  37. const newConnectionButton = { ...button("新建连接"), textContent: "新建连接" };
  38. const sidebarButton = button("在此设备聊天");
  39. const sidebar = {
  40. innerText: "dev@127.0.0.1:43210 侧栏",
  41. querySelectorAll: () => [sidebarButton],
  42. };
  43. const card = {
  44. innerText: "测试设备 dev@127.0.0.1:43210 已连接",
  45. querySelector: () => (disabled ? null : directoryButton),
  46. querySelectorAll: () => [directoryButton],
  47. };
  48. const dialog = {
  49. querySelectorAll: (selector: string) =>
  50. ["section", "[data-remote-source-server-id]"].includes(selector) ? [card] : [directoryButton],
  51. };
  52. const document = {
  53. querySelector: (selector: string) =>
  54. selector.includes("ssh-dialog")
  55. ? dialog
  56. : selector.includes("settings-page")
  57. ? { querySelectorAll: () => [newConnectionButton] }
  58. : null,
  59. querySelectorAll: (selector: string) =>
  60. selector === "section"
  61. ? [sidebar, card]
  62. : selector === '[role="dialog"] button'
  63. ? [{ textContent: "添加连接", click: vi.fn() }]
  64. : [sidebarButton, directoryButton],
  65. };
  66. vi.stubGlobal("$", (selector: string) => ({
  67. waitForDisplayed: async () => {
  68. if (selector.includes("data-device-id") && disabled) throw new Error("saved device missing");
  69. },
  70. isDisplayed: async () => selector.includes("settings-page") && settingsOpen,
  71. click: selector.includes("data-device-id") ? directoryButton.click : vi.fn(),
  72. }));
  73. vi.stubGlobal("browser", {
  74. // 使用独立 VM 执行真实 browser callback,捕获误用 Node 闭包常量的问题。
  75. execute: async (fn: (...args: unknown[]) => unknown, ...args: unknown[]) =>
  76. runInNewContext(`(${fn.toString()})(...args)`, {
  77. document,
  78. args,
  79. window: { __remoteProjectHandoffRpc: { ready: true } },
  80. }),
  81. waitUntil: async (predicate: () => Promise<boolean>, options: { timeoutMsg: string }) => {
  82. if (!(await predicate())) throw new Error(options.timeoutMsg);
  83. },
  84. });
  85. return { directoryButton, sidebarButton, newConnectionButton };
  86. }
  87. afterEach(() => {
  88. vi.unstubAllGlobals();
  89. vi.clearAllMocks();
  90. });
  91. describe("SSH 项目目录入口", () => {
  92. it("连接向导专用 case 从设置页新建连接,不进入项目创建的 Host 选择路径", async () => {
  93. const { newConnectionButton } = installBrowserFixture("测试设备");
  94. await openSSHRemoteDialog("E2E_SSH_DIRECTORY", { entry: "settings" });
  95. expect(clickTestIdByDom).toHaveBeenCalledWith(TID_TASK_SETTINGS_BUTTON);
  96. expect(clickTestIdByDom).toHaveBeenCalledWith(
  97. testId(TID_SETTINGS_SECTION_NAV, "remoteDevices"),
  98. );
  99. expect(newConnectionButton.click).toHaveBeenCalledOnce();
  100. expect(clickTestIdByWebDriver).not.toHaveBeenCalledWith("virtual-project-add");
  101. });
  102. it("取消独立向导后仍在设置页,可直接重新打开连接,不点击被遮挡的全局设置入口", async () => {
  103. const { newConnectionButton } = installBrowserFixture("测试设备", false, true);
  104. await openSSHRemoteDialog("E2E_SSH_DIRECTORY", { entry: "settings" });
  105. expect(clickTestIdByDom).not.toHaveBeenCalledWith(TID_TASK_SETTINGS_BUTTON);
  106. expect(newConnectionButton.click).toHaveBeenCalledOnce();
  107. });
  108. it("按真实Source身份选择设备,不点击同名侧栏", async () => {
  109. const { directoryButton, sidebarButton } = installBrowserFixture("测试设备");
  110. await openSSHSourceDirectory(target, "E2E_SSH_DIRECTORY");
  111. expect(callHandoffHost).toHaveBeenCalledWith(
  112. ServiceChannels.SourceServerManagement,
  113. "getSnapshot",
  114. );
  115. expect(clickTestIdByWebDriver).toHaveBeenCalledWith("virtual-project-device");
  116. expect(clickTestIdByWebDriver).toHaveBeenCalledWith("virtual-project-browse");
  117. expect(directoryButton.click).toHaveBeenCalledTimes(1);
  118. expect(sidebarButton.click).not.toHaveBeenCalled();
  119. });
  120. it("设备没有进入选择器时报告缺失,不点击同名旧卡片", async () => {
  121. const { directoryButton } = installBrowserFixture("选择其他目录", true);
  122. await expect(openSSHSourceDirectory(target, "E2E_SSH_DIRECTORY")).rejects.toThrow(
  123. "saved device missing",
  124. );
  125. expect(directoryButton.click).not.toHaveBeenCalled();
  126. });
  127. });