conversation-session-remote-sidebar-menus.test.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. import {
  2. disconnectRemoteSource,
  3. requestRemoteProjectConnection,
  4. selectRemoteDeviceMenuItem,
  5. } from "#e2e/helpers/remote-project-actions";
  6. import { mkdir } from "node:fs/promises";
  7. import { resolve } from "node:path";
  8. import {
  9. installHandoffRpcProbe,
  10. callHandoffHost,
  11. restoreHandoffProbes,
  12. } from "#e2e/helpers/remote-project-handoff-probe";
  13. import { ServiceChannels } from "@zcode/shared";
  14. import {
  15. TID_TASK_SETTINGS_BUTTON,
  16. TID_SETTINGS_SECTION_NAV,
  17. testId,
  18. TID_SETTINGS_BACK_BUTTON,
  19. TID_SSH_DIALOG,
  20. } from "@zcode/shared/test-ids";
  21. import {
  22. clearAppData,
  23. clickTestIdByDom,
  24. readSettings,
  25. waitForWorkspaceApp,
  26. DEFAULT_WORKSPACE,
  27. } from "#e2e/helpers/desktop-app";
  28. import { prepareV4ConversationE2E } from "#e2e/helpers/v4-conversation";
  29. import {
  30. clickLastEnabledSSHDialogButton,
  31. fillSSHPasswordSettings,
  32. openSSHRemoteDialog,
  33. openSSHSourceDirectory,
  34. readSSHRuntimeConfig,
  35. prepareSSHConnectionPage,
  36. selectSSHRemoteDirectory,
  37. waitForSSHDirectoryStep,
  38. waitForRemoteTab,
  39. } from "#e2e/helpers/ssh-remote-p0";
  40. const CASE_MARKER = "E2E_REMOTE_SIDEBAR_MENUS";
  41. const artifactDir = resolve(
  42. process.env.ZCODE_E2E_ARTIFACT_DIR ?? ".e2e-artifacts/remote-sidebar-menus",
  43. );
  44. async function buttonIn(selector: string, labels: RegExp) {
  45. let found: WebdriverIO.Element | undefined;
  46. await browser.waitUntil(
  47. async () => {
  48. for (const button of await $(selector).$$("button")) {
  49. if ((await button.isDisplayed()) && labels.test((await button.getText()).trim())) {
  50. found = button;
  51. return true;
  52. }
  53. }
  54. return false;
  55. },
  56. { timeout: 10_000, timeoutMsg: `${CASE_MARKER}: missing action ${labels}` },
  57. );
  58. return found!;
  59. }
  60. async function openDeviceSettings(sourceId: string) {
  61. if (!(await $('[data-testid="settings-page"]').isDisplayed())) {
  62. await clickTestIdByDom(TID_TASK_SETTINGS_BUTTON);
  63. }
  64. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "remoteDevices"));
  65. const card = $(`[data-source-server-card="${sourceId}"]`);
  66. await card.waitForDisplayed();
  67. return card;
  68. }
  69. async function waitForDetailsReady() {
  70. await browser.waitUntil(
  71. () =>
  72. browser.execute(() => {
  73. const card = document.querySelector(
  74. '[data-testid="remote-identity-details"][data-state="open"]',
  75. );
  76. return Boolean(
  77. card &&
  78. getComputedStyle(card).opacity === "1" &&
  79. card.getAnimations().every((animation) => animation.playState !== "running"),
  80. );
  81. }),
  82. { timeout: 5000, timeoutMsg: "身份详情未完成展开" },
  83. );
  84. }
  85. async function captureMatrix(state: string, identity: string) {
  86. for (const locale of ["zh-CN", "en-US"] as const) {
  87. for (const theme of ["zai-light", "zai-dark"]) {
  88. await browser.execute(
  89. (lang, appearance) => {
  90. const actions = (
  91. window as Window & {
  92. __testActions?: {
  93. setLocale(value: "zh-CN" | "en-US"): void;
  94. setTheme(value: string): void;
  95. };
  96. }
  97. ).__testActions;
  98. if (!actions) throw new Error("Missing appearance actions");
  99. actions.setLocale(lang);
  100. actions.setTheme(appearance);
  101. },
  102. locale,
  103. theme,
  104. );
  105. for (const width of [1400, 900]) {
  106. await browser.electron.execute((electron, nextWidth) => {
  107. electron.BrowserWindow.getAllWindows()
  108. .find((window) => !window.isDestroyed())
  109. ?.setSize(nextWidth, 900);
  110. }, width);
  111. await browser.executeAsync((done) =>
  112. requestAnimationFrame(() => requestAnimationFrame(() => done())),
  113. );
  114. // Escape 会关闭当前悬停浮层;下一轮必须真正离开再进入,不能把原地 moveTo 当成新的进入事件。
  115. const outside = await browser.execute(() => ({ x: innerWidth - 24, y: 24 }));
  116. await browser.action("pointer").move(outside).perform();
  117. const row = $(`[data-project-workspace-identity="${identity}"]`);
  118. await row.$('[data-testid="virtual-project-toggle"]').moveTo();
  119. const details = $('[data-testid="remote-identity-details"]');
  120. await details.waitForDisplayed();
  121. expect(await details.$$("dt").map((field) => field.getText())).toEqual(
  122. locale === "zh-CN"
  123. ? ["设备", "连接", "地址", "目录"]
  124. : ["Device", "Connection", "Address", "Directory"],
  125. );
  126. expect(await details.$$("button")).toHaveLength(0);
  127. await browser.saveScreenshot(
  128. resolve(artifactDir, `identity-${state}-${locale}-${theme}-${width}.png`),
  129. );
  130. await row.$('[data-testid="virtual-project-menu"]').click();
  131. const menu = $('[role="menu"][data-state="open"]');
  132. await menu.waitForDisplayed();
  133. const geometry = await browser.execute(() => {
  134. const card = document
  135. .querySelector('[role="menu"][data-state="open"]')!
  136. .getBoundingClientRect();
  137. return {
  138. left: card.left,
  139. top: card.top,
  140. right: card.right,
  141. bottom: card.bottom,
  142. width: innerWidth,
  143. height: innerHeight,
  144. };
  145. });
  146. expect(geometry.left).toBeGreaterThanOrEqual(0);
  147. expect(geometry.top).toBeGreaterThanOrEqual(0);
  148. expect(geometry.right).toBeLessThanOrEqual(geometry.width);
  149. expect(geometry.bottom).toBeLessThanOrEqual(geometry.height);
  150. // 新项目模型把设备详情放在设置页,侧栏菜单保留按连接状态提供的项目操作。
  151. expect(await $('[data-testid="virtual-project-connect"]').isExisting()).toBe(
  152. state === "offline",
  153. );
  154. await browser.saveScreenshot(
  155. resolve(artifactDir, `${state}-${locale}-${theme}-${width}.png`),
  156. );
  157. await browser.keys("Escape");
  158. // 显示结束时菜单仍可能保留退出动画和模态指针锁;卸载后才进入下一个菜单。
  159. await menu.waitForExist({ reverse: true });
  160. await browser.action("pointer").move(outside).perform();
  161. await details.waitForExist({ reverse: true });
  162. for (const scrollable of [true, false]) {
  163. // 侧栏归还焦点后可能重开提示;用键盘进入输入框菜单,详情仍由真实鼠标悬停验证。
  164. await browser.execute(() =>
  165. document
  166. .querySelector<HTMLButtonElement>('[data-testid="composer-workspace-trigger"]')!
  167. .focus(),
  168. );
  169. await browser.keys("Enter");
  170. const composerMenu = $('[data-testid="composer-project-menu"]');
  171. await composerMenu.waitForDisplayed();
  172. // 受控搜索框用真实键盘清空,确保下一轮恢复完整列表和滚动条。
  173. await composerMenu.$("input").click();
  174. await browser.keys([process.platform === "darwin" ? "Meta" : "Control", "a"]);
  175. await browser.keys("Backspace");
  176. if (!scrollable)
  177. await composerMenu.$("input").setValue((await row.getAttribute("data-project-name"))!);
  178. const option = composerMenu.$(
  179. `[data-project-id="${await row.getAttribute("data-project-id")}"]`,
  180. );
  181. await option.scrollIntoView();
  182. // 离线选项禁止点击,悬停由外层触发区承接;列表滚动条曾让详情左沿落入菜单下方。
  183. await option.$("..").moveTo();
  184. await details.waitForDisplayed();
  185. await waitForDetailsReady();
  186. const visibility = await browser.execute(() => {
  187. const card = document.querySelector('[data-testid="remote-identity-details"]')!;
  188. const menu = document.querySelector('[data-testid="composer-project-menu"]')!;
  189. const bounds = card.getBoundingClientRect();
  190. const menuBounds = menu.getBoundingClientRect();
  191. const scroller = menu.querySelector('[role="menuitemcheckbox"]')!.parentElement!
  192. .parentElement!;
  193. const points = [
  194. [bounds.left + 2, bounds.top + 16],
  195. [bounds.right - 2, bounds.top + 16],
  196. [bounds.left + 2, bounds.bottom - 16],
  197. [bounds.right - 2, bounds.bottom - 16],
  198. [bounds.left + 2, (bounds.top + bounds.bottom) / 2],
  199. [bounds.right - 2, (bounds.top + bounds.bottom) / 2],
  200. ];
  201. return {
  202. gap:
  203. card.getAttribute("data-side") === "left"
  204. ? menuBounds.left - bounds.right
  205. : bounds.left - menuBounds.right,
  206. scrollable: scroller.scrollHeight > scroller.clientHeight,
  207. overlap:
  208. Math.max(
  209. 0,
  210. Math.min(bounds.right, menuBounds.right) - Math.max(bounds.left, menuBounds.left),
  211. ) *
  212. Math.max(
  213. 0,
  214. Math.min(bounds.bottom, menuBounds.bottom) - Math.max(bounds.top, menuBounds.top),
  215. ),
  216. occluded: points.filter(
  217. ([x, y]) => !card.contains(document.elementFromPoint(x!, y!)),
  218. ),
  219. inViewport:
  220. bounds.left >= 0 &&
  221. bounds.top >= 0 &&
  222. bounds.right <= innerWidth &&
  223. bounds.bottom <= innerHeight,
  224. };
  225. });
  226. expect(visibility.scrollable).toBe(scrollable);
  227. expect(Math.abs(visibility.gap - 4)).toBeLessThan(0.6);
  228. expect(visibility.overlap).toBe(0);
  229. expect(visibility.occluded).toEqual([]);
  230. expect(visibility.inViewport).toBe(true);
  231. await details.moveTo();
  232. await waitForDetailsReady();
  233. await browser.saveScreenshot(
  234. resolve(
  235. artifactDir,
  236. `composer-identity-${state}-${locale}-${theme}-${width}-${scrollable ? "scroll" : "no-scroll"}.png`,
  237. ),
  238. );
  239. await browser.action("pointer").move(outside).perform();
  240. await details.waitForDisplayed({ reverse: true });
  241. await browser.keys("Escape");
  242. await composerMenu.waitForExist({ reverse: true });
  243. }
  244. }
  245. }
  246. }
  247. await browser.execute(() => {
  248. (
  249. window as Window & { __testActions?: { setLocale(value: "zh-CN"): void } }
  250. ).__testActions?.setLocale("zh-CN");
  251. });
  252. }
  253. describe(`${CASE_MARKER}: 侧栏详情和设备菜单`, () => {
  254. afterEach(async () => {
  255. await browser.saveScreenshot(resolve(artifactDir, "last-state.png"));
  256. });
  257. after(async () => {
  258. await restoreHandoffProbes();
  259. await browser.electron.restoreAllMocks();
  260. await clearAppData();
  261. });
  262. it("RDUX-SIDEBAR 在线无项目设备可选目录,详情可定位设置并在原项目重连", async function () {
  263. this.timeout(15 * 60_000);
  264. await mkdir(artifactDir, { recursive: true });
  265. const config = readSSHRuntimeConfig(CASE_MARKER);
  266. const path = config.workspacePaths[0];
  267. await prepareSSHConnectionPage(CASE_MARKER);
  268. await prepareV4ConversationE2E({ skipProvider: true });
  269. // 初始字段与设备操作断言使用中文;不依赖 runner 的默认语言。
  270. await browser.execute(() => {
  271. (
  272. window as Window & { __testActions?: { setLocale(value: "zh-CN"): void } }
  273. ).__testActions!.setLocale("zh-CN");
  274. });
  275. await openSSHRemoteDialog(CASE_MARKER);
  276. await fillSSHPasswordSettings(config);
  277. await clickLastEnabledSSHDialogButton(CASE_MARKER);
  278. await waitForSSHDirectoryStep(CASE_MARKER);
  279. // 新增设备完成后回到原项目草稿;关闭未保存草稿,保留已连接且没有项目成员的设备。
  280. await $('[data-testid="virtual-project-name"]').waitForDisplayed();
  281. await browser.keys("Escape");
  282. await $('[data-testid="virtual-project-name"]').waitForDisplayed({ reverse: true });
  283. const server = (await readSettings()).sourceServers?.[0];
  284. if (!server) throw new Error(`${CASE_MARKER}: connected device not saved`);
  285. expect(await $$("[data-project-workspace-identity]")).toHaveLength(0);
  286. const initialCard = await openDeviceSettings(server.sourceServerId);
  287. expect(await initialCard.getText()).toContain(server.displayName);
  288. await openSSHSourceDirectory(config, CASE_MARKER);
  289. await waitForSSHDirectoryStep(CASE_MARKER);
  290. await selectSSHRemoteDirectory(path, CASE_MARKER);
  291. await waitForWorkspaceApp(path, 120_000);
  292. const tab = await waitForRemoteTab({ caseMarker: CASE_MARKER, expectedPath: path });
  293. const rowSelector = `[data-project-workspace-identity="${tab.workspaceIdentity}"]`;
  294. await installHandoffRpcProbe();
  295. // 通过真实项目服务增加列表项,固定覆盖原生滚动条参与定位的场景。
  296. for (let index = 0; index < 10; index++) {
  297. await callHandoffHost(ServiceChannels.Project, "saveProject", [
  298. { name: `${CASE_MARKER}_SCROLL_${index}`, roots: [] },
  299. ]);
  300. }
  301. await browser.waitUntil(
  302. async () => (await $(rowSelector).getAttribute("data-connection-state")) === "online",
  303. { timeout: 30_000 },
  304. );
  305. const deviceLabel = $(rowSelector).$('[data-testid="project-device"]');
  306. expect(await deviceLabel.getText()).toBe(server.displayName);
  307. await $(rowSelector).$('[data-testid="virtual-project-toggle"]').moveTo();
  308. const details = $('[data-testid="remote-identity-details"]');
  309. await details.waitForDisplayed();
  310. expect(await details.getText()).toContain(server.displayName);
  311. expect(await details.$$("dt").map((field) => field.getText())).toEqual([
  312. "设备",
  313. "连接",
  314. "地址",
  315. "目录",
  316. ]);
  317. expect(await details.$("strong").getText()).toBe(
  318. await $(rowSelector).getAttribute("data-project-name"),
  319. );
  320. expect(await details.getText()).toContain(path);
  321. expect(await details.$$("button")).toHaveLength(0);
  322. await details.moveTo();
  323. expect(await details.isDisplayed()).toBe(true);
  324. // 选中文字曾触发 HoverCard 的选区锁定,移出后仍遮挡界面;真实双击保留复制能力。
  325. await details.$("dd:last-child div").doubleClick();
  326. const selectedPath = await browser.execute(() => window.getSelection()?.toString() ?? "");
  327. expect(selectedPath.trim().length).toBeGreaterThan(0);
  328. expect(path).toContain(selectedPath.trim());
  329. // 浮层可能盖住聊天项目按钮;移到视口右上角,确保鼠标真正离开触发区域和浮层。
  330. const outside = await browser.execute(() => ({ x: innerWidth - 24, y: 24 }));
  331. await browser.action("pointer").move(outside).perform();
  332. await details.waitForDisplayed({ reverse: true });
  333. await $('[data-testid="composer-workspace-trigger"]').click();
  334. const option = $(
  335. `[data-testid="composer-project-menu"] [data-project-host="${await $(rowSelector).getAttribute("data-project-host")}"]`,
  336. );
  337. await option.waitForDisplayed();
  338. expect(await option.getText()).toContain(server.displayName);
  339. await option.moveTo();
  340. await details.waitForDisplayed();
  341. expect(await details.getText()).toContain(path);
  342. await browser.keys("Escape");
  343. await details.waitForDisplayed({ reverse: true });
  344. await browser.keys("Escape");
  345. const card = await openDeviceSettings(server.sourceServerId);
  346. await card.$('button[aria-label="更多"]').click();
  347. await $("[data-device-info]").waitForDisplayed();
  348. await browser.saveScreenshot(resolve(artifactDir, "device-info.png"));
  349. await browser.keys("Escape");
  350. // RDUX-13:编辑在设备行「更多」菜单里。
  351. await selectRemoteDeviceMenuItem(server.sourceServerId, ["编辑", "Edit"], CASE_MARKER);
  352. const editor = $('[role="dialog"][data-slot="dialog-content"]');
  353. await editor.waitForDisplayed();
  354. expect(await editor.getText()).toMatch(/编辑连接|Edit connection/u);
  355. await (
  356. await buttonIn('[role="dialog"][data-slot="dialog-content"]', /^(取消|Cancel)$/u)
  357. ).click();
  358. await editor.waitForDisplayed({ reverse: true });
  359. await browser.saveScreenshot(resolve(artifactDir, "managed-device.png"));
  360. await clickTestIdByDom(TID_SETTINGS_BACK_BUTTON);
  361. await captureMatrix("online", tab.workspaceIdentity!);
  362. await disconnectRemoteSource(server.sourceServerId, CASE_MARKER, {
  363. workspaceIdentities: [tab.workspaceIdentity!],
  364. });
  365. await browser.waitUntil(
  366. async () => (await $(rowSelector).getAttribute("data-connection-state")) === "offline",
  367. { timeout: 30_000 },
  368. );
  369. // 当前远端断线时输入框不可操作;从本地草稿验证菜单内离线项目,覆盖实际可用入口。
  370. const localProject = $('[data-project-name="ZCodeProject"]');
  371. await localProject.moveTo();
  372. await localProject.$('[data-testid="virtual-project-create-task"]').click();
  373. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 30_000);
  374. await captureMatrix("offline", tab.workspaceIdentity!);
  375. await requestRemoteProjectConnection(tab.workspaceIdentity!, CASE_MARKER);
  376. await browser.waitUntil(
  377. async () => (await $(rowSelector).getAttribute("data-connection-state")) === "online",
  378. { timeout: 90_000 },
  379. );
  380. expect(await $(`[data-testid="${TID_SSH_DIALOG}"]`).isDisplayed()).toBe(false);
  381. // 重连仍恢复原远端 tab;菜单回归已切到本地草稿,后台恢复不能夺走本地活动页。
  382. const reconnected = await waitForRemoteTab({
  383. caseMarker: CASE_MARKER,
  384. expectedPath: path,
  385. expectedIdentity: tab.workspaceIdentity!,
  386. requireActive: false,
  387. });
  388. expect(reconnected.workspaceIdentity).toBe(tab.workspaceIdentity);
  389. expect(reconnected.activeWorkspacePath).toBe(DEFAULT_WORKSPACE);
  390. await $(rowSelector).$('[data-testid="virtual-project-menu"]').click();
  391. await browser.keys("Escape");
  392. // hover/焦点提示“更多”是正常状态,不等于菜单残留;Escape 应关闭菜单并归还焦点。
  393. await $('[role="menu"]').waitForDisplayed({ reverse: true });
  394. await browser.waitUntil(
  395. () =>
  396. browser.execute(
  397. (selector) =>
  398. document.activeElement ===
  399. document.querySelector(`${selector} [data-testid="virtual-project-menu"]`),
  400. rowSelector,
  401. ),
  402. { timeout: 5000, timeoutMsg: "关闭项目菜单后焦点未返回触发器" },
  403. );
  404. });
  405. });