remoteProjectE2EActions.test.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. disconnectRemoteSource,
  6. reconnectRemoteProject,
  7. } from "./e2e/helpers/remote-project-actions.js";
  8. vi.mock("./e2e/helpers/desktop-app.js", () => ({
  9. clickTestIdByDom: vi.fn(),
  10. waitForTestIdByDom: vi.fn(),
  11. readSettings: async () => ({
  12. sourceServers: [{ sourceServerId: "source-b", displayName: "device-b" }],
  13. }),
  14. }));
  15. vi.mock("./e2e/helpers/ssh-remote-p0.js", () => ({
  16. readRemoteTabSnapshot: async () => ({ workspacePath: "/work" }),
  17. waitForRemoteTab: vi.fn(),
  18. waitForSSHDirectoryStep: vi.fn(),
  19. }));
  20. vi.mock("./e2e/helpers/workspace-agent-warmup.js", () => ({ selectTaskOrganizeMode: vi.fn() }));
  21. afterEach(() => vi.unstubAllGlobals());
  22. function install(document: object) {
  23. vi.stubGlobal("browser", {
  24. execute: async (fn: (...args: unknown[]) => unknown, ...args: unknown[]) =>
  25. runInNewContext(`(${fn.toString()})(...args)`, { document, args }),
  26. waitUntil: async (predicate: () => Promise<boolean>) => {
  27. if (!(await predicate())) throw new Error("入口不可用");
  28. },
  29. });
  30. }
  31. describe("项目导航 E2E 动作归属", () => {
  32. it("断开等待指定项目离线,不能把其它离线项目或旧设置投影当成完成", async () => {
  33. const identity = "remote:server:source-b:/work";
  34. const project = { dataset: { connectionState: "online" } };
  35. const disconnect = {
  36. innerText: "断开",
  37. disabled: false,
  38. getClientRects: () => [1],
  39. click: vi.fn(),
  40. };
  41. const card = { querySelectorAll: () => [disconnect] };
  42. install({
  43. querySelector: (selector: string) => {
  44. if (selector === '[data-testid="settings-page"]')
  45. return { querySelectorAll: () => [{ textContent: "device-b", closest: () => card }] };
  46. if (selector === `[data-project-workspace-identity=${JSON.stringify(identity)}]`)
  47. return project;
  48. return { dataset: { connectionState: "offline" } };
  49. },
  50. });
  51. await expect(
  52. disconnectRemoteSource("source-b", "E2E_SCOPE", { workspaceIdentities: [identity] }),
  53. ).rejects.toThrow("目标项目未离线");
  54. project.dataset.connectionState = "offline";
  55. await expect(
  56. disconnectRemoteSource("source-b", "E2E_SCOPE", { workspaceIdentities: [identity] }),
  57. ).resolves.toBeUndefined();
  58. });
  59. it("相同目录的新草稿只点击指定 Source 的项目,不使用全局任务入口", async () => {
  60. const clicks = [vi.fn(), vi.fn()];
  61. const identities = ["remote:server:a:/work", "remote:server:b:/work"];
  62. vi.stubGlobal("$", (selector: string) => {
  63. const index = identities.findIndex(
  64. (identity) => selector === `[data-project-workspace-identity=${JSON.stringify(identity)}]`,
  65. );
  66. if (index < 0) throw new Error(`未按 Source identity 定位项目:${selector}`);
  67. return {
  68. waitForDisplayed: vi.fn(),
  69. moveTo: vi.fn(),
  70. $: (action: string) => {
  71. expect(action).toBe('[data-testid="virtual-project-create-task"]');
  72. return { click: clicks[index] };
  73. },
  74. };
  75. });
  76. vi.stubGlobal("browser", {
  77. execute: async () => true,
  78. waitUntil: async (predicate: () => Promise<boolean>) => {
  79. expect(await predicate()).toBe(true);
  80. },
  81. });
  82. await startDraftFromProject("/work", { workspaceIdentity: identities[1] });
  83. expect(clicks[0]).not.toHaveBeenCalled();
  84. expect(clicks[1]).toHaveBeenCalledOnce();
  85. });
  86. it("断开匹配的设备,不误点先出现的其他 Source", async () => {
  87. const clicks = [vi.fn(), vi.fn()];
  88. const cards = ["device-a", "device-b"].map((name, i) => ({
  89. querySelectorAll: (selector: string) =>
  90. selector === "p"
  91. ? [{ textContent: name }]
  92. : [{ getClientRects: () => [1], innerText: "断开", click: clicks[i] }],
  93. }));
  94. const paragraphs = cards.map((card, i) => ({
  95. textContent: `device-${i === 0 ? "a" : "b"}`,
  96. closest: () => card,
  97. }));
  98. // SettingsPage 自身有外层 section;不能把包含设备卡的祖先也算成同名设备。
  99. const outer = { querySelectorAll: () => paragraphs };
  100. install({
  101. querySelector: () => ({
  102. querySelectorAll: (selector: string) => (selector === "p" ? paragraphs : [outer, ...cards]),
  103. }),
  104. });
  105. await disconnectRemoteSource("source-b", "E2E_SCOPE");
  106. expect(clicks[0]).not.toHaveBeenCalled();
  107. expect(clicks[1]).toHaveBeenCalledOnce();
  108. });
  109. it("相同路径不同 Source 的项目只重连指定 identity", async () => {
  110. const clicks = [vi.fn(), vi.fn()];
  111. let selected = -1;
  112. // 入口已改为真实 WebDriver 的项目菜单,夹具按权威 identity 选择行。
  113. vi.stubGlobal("$", (selector: string) => {
  114. if (selector === '[data-testid="virtual-project-connect"]')
  115. return { click: () => clicks[selected]() };
  116. const keys = ["remote:server:a:/work", "remote:server:b:/work"];
  117. const index = keys.findIndex(
  118. (key) => selector === `[data-project-workspace-identity=${JSON.stringify(key)}]`,
  119. );
  120. if (index < 0) throw new Error(`未知项目:${selector}`);
  121. return {
  122. waitForDisplayed: vi.fn(),
  123. moveTo: vi.fn(),
  124. $: (menu: string) => {
  125. expect(menu).toBe('[data-testid="virtual-project-menu"]');
  126. return {
  127. click: () => {
  128. selected = index;
  129. },
  130. };
  131. },
  132. };
  133. });
  134. await reconnectRemoteProject("remote:server:b:/work", "E2E_SCOPE");
  135. expect(clicks[0]).not.toHaveBeenCalled();
  136. expect(clicks[1]).toHaveBeenCalledOnce();
  137. });
  138. });