conversation-session-remote-settings-safeguards.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import { mkdir } from "node:fs/promises";
  2. import { resolve } from "node:path";
  3. import { parseRemoteWorkspaceIdentity } from "@zcode/shared";
  4. import {
  5. TID_SETTINGS_PAGE,
  6. TID_SETTINGS_BACK_BUTTON,
  7. TID_SETTINGS_SECTION_NAV,
  8. TID_TASK_SETTINGS_BUTTON,
  9. testId,
  10. } from "@zcode/shared/test-ids";
  11. import { clearAppData, clickTestIdByDom, waitForWorkspaceApp } from "#e2e/helpers/desktop-app";
  12. import { prepareV4ConversationE2E } from "#e2e/helpers/v4-conversation";
  13. import {
  14. clickLastEnabledSSHDialogButton,
  15. fillSSHPasswordSettings,
  16. openSSHRemoteDialog,
  17. readSSHRuntimeConfig,
  18. prepareSSHConnectionPage,
  19. selectSSHRemoteDirectory,
  20. waitForSSHDirectoryStep,
  21. waitForRemoteTab,
  22. } from "#e2e/helpers/ssh-remote-p0";
  23. const CASE_MARKER = "E2E_REMOTE_SETTINGS_SAFEGUARDS";
  24. const deviceName = "RDUX-05 device";
  25. const connectionName = "RDUX-05 connection";
  26. async function section(name: string) {
  27. if (!(await $(`[data-testid="${TID_SETTINGS_PAGE}"]`).isDisplayed()))
  28. await clickTestIdByDom(TID_TASK_SETTINGS_BUTTON);
  29. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, name));
  30. }
  31. async function clickButton(text: string, root = "[data-settings-content-frame]") {
  32. await browser.waitUntil(
  33. async () =>
  34. await browser.execute(
  35. (label, selector) => {
  36. const button = [
  37. ...(document
  38. .querySelector(selector)
  39. ?.querySelectorAll<HTMLElement>('button, [role="menuitem"]') ?? []),
  40. ].find(
  41. (item) =>
  42. item.textContent?.trim() === label && !item.matches(":disabled, [data-disabled]"),
  43. );
  44. if (!button) return false;
  45. button.click();
  46. return true;
  47. },
  48. text,
  49. root,
  50. ),
  51. { timeout: 10_000, timeoutMsg: `${CASE_MARKER}: button ${text}` },
  52. );
  53. }
  54. async function more() {
  55. await $('[data-settings-content-frame] button[aria-label="更多"]').click();
  56. await $('[role="menu"]').waitForDisplayed();
  57. }
  58. describe(`${CASE_MARKER}: 设置防护、连接编辑与机器标注`, () => {
  59. after(async () => {
  60. await browser.electron.restoreAllMocks();
  61. await clearAppData();
  62. });
  63. it("RDUX-05 真实 SSH 设置操作保持设备、连接与项目边界", async function () {
  64. this.timeout(15 * 60_000);
  65. const config = readSSHRuntimeConfig(CASE_MARKER);
  66. const path = config.workspacePaths[0];
  67. // SSH 入口独立运行,先固定桌面视口,确保连接向导从可见的项目侧栏打开。
  68. await browser.electron.execute((electron) =>
  69. electron.BrowserWindow.getAllWindows()
  70. .find((window) => !window.isDestroyed())
  71. ?.setSize(1400, 900),
  72. );
  73. await browser.waitUntil(() => browser.execute(() => innerWidth === 1400));
  74. await prepareSSHConnectionPage(CASE_MARKER);
  75. await prepareV4ConversationE2E({ skipProvider: true });
  76. if (await $(`[data-testid="${TID_SETTINGS_PAGE}"]`).isDisplayed())
  77. await clickTestIdByDom(TID_SETTINGS_BACK_BUTTON);
  78. // 侧栏有独立的折叠状态,恢复尺寸后仍需检查连接入口是否可见。
  79. if (!(await $('[data-testid="project-section"]').isDisplayed())) {
  80. const toggles = await $$(
  81. 'button[aria-label="切换侧边栏"], button[aria-label="Toggle sidebar"]',
  82. );
  83. for (const toggle of toggles) {
  84. if (!(await toggle.isDisplayed())) continue;
  85. await toggle.click();
  86. break;
  87. }
  88. await $('[data-testid="project-section"]').waitForDisplayed();
  89. }
  90. await openSSHRemoteDialog(CASE_MARKER);
  91. await fillSSHPasswordSettings(config);
  92. await clickLastEnabledSSHDialogButton(CASE_MARKER);
  93. await waitForSSHDirectoryStep(CASE_MARKER);
  94. await selectSSHRemoteDirectory(path, CASE_MARKER);
  95. await waitForWorkspaceApp(path, 120_000);
  96. const tab = await waitForRemoteTab({ caseMarker: CASE_MARKER, expectedPath: path });
  97. await browser.execute(() => {
  98. const actions = (window as Window & { __testActions?: { setLocale(locale: "zh-CN"): void } })
  99. .__testActions;
  100. if (!actions) throw new Error("Missing locale actions");
  101. actions.setLocale("zh-CN");
  102. });
  103. await section("remoteDevices");
  104. await $('[data-remote-connection-status="online"]').waitForDisplayed();
  105. // RDUX-13:编辑在设备行「更多」菜单里。
  106. await more();
  107. await clickButton("编辑", '[role="menu"]');
  108. // 菜单退场期间立即发送按键曾丢失连接名前缀;先按真实操作聚焦字段,再校验完整输入。
  109. await $("#edit-connection-name").waitForClickable();
  110. await $("#edit-connection-name").click();
  111. await $("#edit-connection-name").setValue(connectionName);
  112. expect(await $("#edit-connection-name").getValue()).toBe(connectionName);
  113. await $("#edit-device-name").click();
  114. await $("#edit-device-name").setValue(deviceName);
  115. expect(await $("#edit-device-name").getValue()).toBe(deviceName);
  116. expect(await $('[role="dialog"]').getText()).toContain("下次连接时生效");
  117. await clickButton("保存更改", '[role="dialog"]');
  118. await $('[role="dialog"]').waitForDisplayed({ reverse: true });
  119. await browser.waitUntil(
  120. async () => (await $("[data-settings-content-frame]").getText()).includes(deviceName),
  121. { timeout: 10_000 },
  122. );
  123. await more();
  124. await clickButton("编辑", '[role="menu"]');
  125. expect(await $("#edit-connection-name").getValue()).toBe(connectionName);
  126. expect(await $("#edit-device-name").getValue()).toBe(deviceName);
  127. expect(await $('[data-testid="ssh-auth-password"]').getAttribute("aria-pressed")).toBe("true");
  128. const artifactDir = resolve(
  129. process.env.ZCODE_E2E_ARTIFACT_DIR ?? ".e2e-artifacts/remote-settings-safeguards",
  130. );
  131. await mkdir(artifactDir, { recursive: true });
  132. for (const locale of ["zh-CN", "en-US"] as const) {
  133. await browser.execute(
  134. (value) =>
  135. (
  136. window as Window & { __testActions?: { setLocale(value: "zh-CN" | "en-US"): void } }
  137. ).__testActions?.setLocale(value),
  138. locale,
  139. );
  140. for (const theme of ["light", "dark"]) {
  141. await browser.execute(
  142. (value) =>
  143. (
  144. window as Window & { __testActions?: { setTheme(value: string): void } }
  145. ).__testActions?.setTheme(value),
  146. theme,
  147. );
  148. for (const width of [1400, 480]) {
  149. await browser.electron.execute((electron, size) => {
  150. const window = electron.BrowserWindow.getAllWindows().find(
  151. (candidate) => !candidate.isDestroyed(),
  152. );
  153. window?.setMinimumSize(360, 600);
  154. window?.setSize(size, 900);
  155. }, width);
  156. await browser.executeAsync((done) =>
  157. requestAnimationFrame(() => requestAnimationFrame(() => done())),
  158. );
  159. await browser.saveScreenshot(
  160. resolve(artifactDir, `edit-${locale}-${theme}-${width}.png`),
  161. );
  162. }
  163. }
  164. }
  165. await browser.execute(() =>
  166. (
  167. window as Window & { __testActions?: { setLocale(value: "zh-CN"): void } }
  168. ).__testActions?.setLocale("zh-CN"),
  169. );
  170. await clickButton("取消", '[role="dialog"]');
  171. await browser.electron.execute((electron) =>
  172. electron.BrowserWindow.getAllWindows()
  173. .find((candidate) => !candidate.isDestroyed())
  174. ?.setSize(1400, 900),
  175. );
  176. for (const name of ["plugin", "mcp", "skill", "hooks", "subagents"]) {
  177. await section(name);
  178. expect(await $("[data-settings-machine]").isExisting()).toBe(false);
  179. expect(await $("[data-settings-content-frame]").getText()).not.toMatch(
  180. /正在编辑:|Editing:/u,
  181. );
  182. const trigger = $('[data-plugin-scope-trigger="true"]');
  183. // 数据同步已统一各资源的 Scope;子智能体不再有“用户永远是本机”的旧例外。
  184. expect(await trigger.getText()).toContain(deviceName);
  185. const sourceId = parseRemoteWorkspaceIdentity(tab.workspaceIdentity!)?.sourceServerId;
  186. expect(sourceId).toBeTruthy();
  187. expect(await trigger.getAttribute("data-plugin-scope-key")).toBe(`source-user:${sourceId}`);
  188. }
  189. await section("general");
  190. expect(await $("[data-settings-machine]").isExisting()).toBe(false);
  191. expect(await $("[data-settings-content-frame]").getText()).not.toMatch(/正在编辑:|Editing:/u);
  192. await section("remoteDevices");
  193. await more();
  194. await clickButton("在侧边栏中显示", '[role="menu"]');
  195. // 按远程设备设置规格的「菜单和收起」合同验收工作区返回,不依赖侧栏展开、滚动或焦点。
  196. await $(`[data-testid="${TID_SETTINGS_PAGE}"]`).waitForDisplayed({ reverse: true });
  197. await waitForWorkspaceApp(path, 30_000);
  198. await section("remoteDevices");
  199. await more();
  200. await clickButton("移除设备", '[role="menu"]');
  201. await $('[role="dialog"]').waitForDisplayed();
  202. expect(await $('[role="dialog"]').getText()).toContain(path);
  203. expect(await browser.execute(() => document.activeElement?.textContent)).toContain("取消");
  204. await browser.keys("Enter");
  205. await $('[role="dialog"]').waitForDisplayed({ reverse: true });
  206. expect(await $("[data-settings-content-frame]").getText()).toContain(deviceName);
  207. // RDUX-13:断开在「更多」菜单里,任务继续提示常驻在菜单项内,不占设备行。
  208. expect(await $("[data-settings-content-frame]").getText()).not.toContain("远端任务会继续运行");
  209. await more();
  210. const disconnectItem = await browser.execute(() => {
  211. const item = [
  212. ...document.querySelectorAll<HTMLElement>('[role="menu"] [role="menuitem"]'),
  213. ].find((candidate) => candidate.textContent?.trim().startsWith("断开"));
  214. const text = item?.textContent ?? null;
  215. item?.click();
  216. return text;
  217. });
  218. expect(disconnectItem).toContain("远端任务会继续运行");
  219. await $('[data-remote-connection-status="offline"]').waitForDisplayed({ timeout: 20_000 });
  220. expect(await $('[role="dialog"]').isDisplayed()).toBe(false);
  221. await browser.waitUntil(
  222. async () => (await $("[data-settings-content-frame]").getText()).includes("最近在线 刚刚"),
  223. { timeout: 10_000 },
  224. );
  225. });
  226. });