remoteProjectE2EActions.test.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import { runInNewContext } from "node:vm";
  2. import { afterEach, describe, expect, it, vi } from "vitest";
  3. import { startDraftFromProject } from "#e2e/helpers/project-sidebar";
  4. import {
  5. connectRemoteSourceFromSettings,
  6. disconnectRemoteSource,
  7. reconnectRemoteProject,
  8. } from "./e2e/helpers/remote-project-actions.js";
  9. vi.mock("./e2e/helpers/desktop-app.js", () => ({
  10. clickTestIdByDom: vi.fn(),
  11. waitForTestIdByDom: vi.fn(),
  12. readSettings: async () => ({
  13. sourceServers: [{ sourceServerId: "source-b", displayName: "device-b" }],
  14. }),
  15. }));
  16. vi.mock("./e2e/helpers/ssh-remote-p0.js", () => ({
  17. readRemoteTabSnapshot: async () => ({ workspacePath: "/work" }),
  18. waitForRemoteTab: vi.fn(),
  19. waitForSSHDirectoryStep: vi.fn(),
  20. }));
  21. vi.mock("./e2e/helpers/workspace-agent-warmup.js", () => ({ selectTaskOrganizeMode: vi.fn() }));
  22. afterEach(() => vi.unstubAllGlobals());
  23. function install(document: object) {
  24. vi.stubGlobal("browser", {
  25. execute: async (fn: (...args: unknown[]) => unknown, ...args: unknown[]) =>
  26. runInNewContext(`(${fn.toString()})(...args)`, { document, args }),
  27. waitUntil: async (predicate: () => Promise<boolean>) => {
  28. if (!(await predicate())) throw new Error("入口不可用");
  29. },
  30. });
  31. }
  32. describe("项目导航 E2E 动作归属", () => {
  33. it.each([
  34. { name: "work", explicit: false },
  35. { name: "work (8)", explicit: true },
  36. ])("项目 $name 按远端身份等待新草稿代次,保留真实显示名", async ({ name, explicit }) => {
  37. const identity = "remote:server:source-b:/work";
  38. const workspace = { draftFocusVersion: 4 };
  39. const click = vi.fn(() => {
  40. workspace.draftFocusVersion++;
  41. });
  42. const input = { closest: () => ({ dataset: { sessionId: "draft" } }) };
  43. const document = {
  44. querySelectorAll: () => [input],
  45. querySelector: () => ({
  46. innerText: name,
  47. querySelector: () => ({ click, disabled: false }),
  48. }),
  49. };
  50. const window = {
  51. __zcodeTabStoreE2E: { getState: () => ({ activeWorkspaceIdentity: identity }) },
  52. __zcodeSessionStoreE2E: { getState: () => ({ workspaces: { [identity]: workspace } }) },
  53. };
  54. vi.stubGlobal("$", () => ({
  55. waitForDisplayed: vi.fn(),
  56. getAttribute: async (attribute: string) =>
  57. attribute === "data-project-name" ? name : identity,
  58. moveTo: vi.fn(),
  59. $: () => ({ click }),
  60. }));
  61. const performActions = vi.fn();
  62. vi.stubGlobal("browser", {
  63. execute: async (fn: (...args: unknown[]) => unknown, ...args: unknown[]) =>
  64. runInNewContext(`(${fn.toString()})(...args)`, { document, window, args }),
  65. waitUntil: async (predicate: () => Promise<boolean>) => {
  66. if (!(await predicate())) throw new Error("远端草稿代次未被识别");
  67. },
  68. performActions,
  69. releaseActions: vi.fn(),
  70. });
  71. await startDraftFromProject("/work", explicit ? { workspaceIdentity: identity } : {});
  72. expect(click).toHaveBeenCalledOnce();
  73. // 项目详情 hover card 会盖住对话区:新建草稿后必须把指针移出项目行。
  74. expect(performActions).toHaveBeenCalledOnce();
  75. expect(workspace.draftFocusVersion).toBe(5);
  76. });
  77. it("项目视图已就绪时不再次切换菜单,避免相邻断开和重连反转菜单状态", async () => {
  78. const { selectTaskOrganizeMode } = await vi.importActual<
  79. typeof import("./e2e/helpers/workspace-agent-warmup.js")
  80. >("./e2e/helpers/workspace-agent-warmup.js");
  81. const click = vi.fn();
  82. vi.stubGlobal("$", (selector: string) => {
  83. if (selector === '[data-testid="project-section"]') return { isDisplayed: async () => true };
  84. return {
  85. click,
  86. waitForClickable: async () => {
  87. throw new Error("重复打开菜单");
  88. },
  89. };
  90. });
  91. await selectTaskOrganizeMode(["项目"]);
  92. expect(click).not.toHaveBeenCalled();
  93. });
  94. it("断开等待指定项目离线,不能把其它离线项目或旧设置投影当成完成", async () => {
  95. const identity = "remote:server:source-b:/work";
  96. const project = { dataset: { connectionState: "online" } };
  97. // RDUX-13:断开在设备行「更多」菜单里,菜单项文案带任务继续提示。
  98. const disconnect = {
  99. textContent: "断开断开后,远端任务会继续运行。",
  100. hasAttribute: () => false,
  101. click: vi.fn(),
  102. };
  103. vi.stubGlobal("$", () => ({ click: vi.fn() }));
  104. install({
  105. querySelectorAll: (selector: string) =>
  106. selector === '[role="menu"] [role="menuitem"]' ? [disconnect] : [],
  107. querySelector: (selector: string) => {
  108. if (selector === `[data-project-workspace-identity=${JSON.stringify(identity)}]`)
  109. return project;
  110. return { dataset: { connectionState: "offline" } };
  111. },
  112. });
  113. await expect(
  114. disconnectRemoteSource("source-b", "E2E_SCOPE", { workspaceIdentities: [identity] }),
  115. ).rejects.toThrow("目标项目未离线");
  116. expect(disconnect.click).toHaveBeenCalled();
  117. project.dataset.connectionState = "offline";
  118. await expect(
  119. disconnectRemoteSource("source-b", "E2E_SCOPE", { workspaceIdentities: [identity] }),
  120. ).resolves.toBeUndefined();
  121. });
  122. it("相同目录的新草稿只点击指定 Source 的项目,不使用全局任务入口", async () => {
  123. const clicks = [vi.fn(), vi.fn()];
  124. const identities = ["remote:server:a:/work", "remote:server:b:/work"];
  125. vi.stubGlobal("$", (selector: string) => {
  126. const index = identities.findIndex(
  127. (identity) => selector === `[data-project-workspace-identity=${JSON.stringify(identity)}]`,
  128. );
  129. if (index < 0) throw new Error(`未按 Source identity 定位项目:${selector}`);
  130. return {
  131. waitForDisplayed: vi.fn(),
  132. getAttribute: async () => "work",
  133. moveTo: vi.fn(),
  134. $: (action: string) => {
  135. expect(action).toBe('[data-testid="virtual-project-create-task"]');
  136. return { click: clicks[index] };
  137. },
  138. };
  139. });
  140. const performActions = vi.fn();
  141. vi.stubGlobal("browser", {
  142. execute: async () => true,
  143. waitUntil: async (predicate: () => Promise<boolean>) => {
  144. expect(await predicate()).toBe(true);
  145. },
  146. performActions,
  147. releaseActions: vi.fn(),
  148. });
  149. await startDraftFromProject("/work", { workspaceIdentity: identities[1] });
  150. expect(performActions).toHaveBeenCalledOnce();
  151. expect(clicks[0]).not.toHaveBeenCalled();
  152. expect(clicks[1]).toHaveBeenCalledOnce();
  153. });
  154. it("断开匹配的设备,不误点先出现的其他 Source", async () => {
  155. const triggers: string[] = [];
  156. // 菜单项渲染在 Portal;归属由打开菜单的设备行决定,必须按 sourceServerId 定位触发按钮。
  157. vi.stubGlobal("$", (selector: string) => ({ click: async () => triggers.push(selector) }));
  158. const disconnect = { textContent: "断开", hasAttribute: () => false, click: vi.fn() };
  159. install({ querySelectorAll: () => [disconnect], querySelector: () => null });
  160. vi.stubGlobal("browser", {
  161. execute: async (fn: (...args: unknown[]) => unknown, ...args: unknown[]) =>
  162. runInNewContext(`(${fn.toString()})(...args)`, {
  163. document: { querySelectorAll: () => [disconnect], querySelector: () => null },
  164. args,
  165. }),
  166. waitUntil: async (predicate: () => Promise<boolean>) => {
  167. if (!(await predicate())) throw new Error("入口不可用");
  168. },
  169. keys: vi.fn(),
  170. });
  171. await disconnectRemoteSource("source-b", "E2E_SCOPE");
  172. expect(triggers).toEqual([
  173. '[data-source-server-card="source-b"] [data-device-row-actions] button[aria-haspopup="menu"]',
  174. ]);
  175. expect(disconnect.click).toHaveBeenCalledOnce();
  176. });
  177. it("连接只点击指定 Source 设备行里的行内主操作", async () => {
  178. const connect = {
  179. innerText: "连接",
  180. disabled: false,
  181. getClientRects: () => [1],
  182. click: vi.fn(),
  183. };
  184. const selectors: string[] = [];
  185. install({
  186. querySelectorAll: (selector: string) => {
  187. selectors.push(selector);
  188. return selector.startsWith('[data-source-server-card="source-b"]') ? [connect] : [];
  189. },
  190. });
  191. await connectRemoteSourceFromSettings("source-b", "E2E_SCOPE");
  192. expect(selectors).toEqual([
  193. '[data-source-server-card="source-b"] [data-device-row-actions] button',
  194. ]);
  195. expect(connect.click).toHaveBeenCalledOnce();
  196. });
  197. it("相同路径不同 Source 的项目只重连指定 identity", async () => {
  198. const clicks = [vi.fn(), vi.fn()];
  199. let selected = -1;
  200. // 入口已改为真实 WebDriver 的项目菜单,夹具按权威 identity 选择行。
  201. vi.stubGlobal("$", (selector: string) => {
  202. if (selector === '[data-testid="virtual-project-connect"]')
  203. return { click: () => clicks[selected]() };
  204. const keys = ["remote:server:a:/work", "remote:server:b:/work"];
  205. const index = keys.findIndex(
  206. (key) => selector === `[data-project-workspace-identity=${JSON.stringify(key)}]`,
  207. );
  208. if (index < 0) throw new Error(`未知项目:${selector}`);
  209. return {
  210. waitForDisplayed: vi.fn(),
  211. moveTo: vi.fn(),
  212. $: (menu: string) => {
  213. expect(menu).toBe('[data-testid="virtual-project-menu"]');
  214. return {
  215. click: () => {
  216. selected = index;
  217. },
  218. };
  219. },
  220. };
  221. });
  222. await reconnectRemoteProject("remote:server:b:/work", "E2E_SCOPE");
  223. expect(clicks[0]).not.toHaveBeenCalled();
  224. expect(clicks[1]).toHaveBeenCalledOnce();
  225. });
  226. });