remote-project-actions.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import {
  2. TID_SETTINGS_BACK_BUTTON,
  3. TID_SETTINGS_PAGE,
  4. TID_SETTINGS_SECTION_NAV,
  5. TID_TASK_SETTINGS_BUTTON,
  6. testId,
  7. } from "@zcode/shared/test-ids";
  8. import { clickTestIdByDom, readSettings, waitForTestIdByDom } from "./desktop-app.js";
  9. import {
  10. readRemoteTabSnapshot,
  11. waitForRemoteTab,
  12. waitForSSHDirectoryStep,
  13. } from "./ssh-remote-p0.js";
  14. import { selectTaskOrganizeMode } from "./workspace-agent-warmup.js";
  15. /** 项目平铺后,设备断开属于设置页;按权威 Source 找线路,不借第一个设备操作。 */
  16. export async function disconnectRemoteSource(
  17. sourceId: string,
  18. caseMarker: string,
  19. options: { workspaceIdentities?: readonly string[] } = {},
  20. ): Promise<void> {
  21. await clickRemoteSourceAction(sourceId, caseMarker, ["断开", "Disconnect"]);
  22. await clickTestIdByDom(TID_SETTINGS_BACK_BUTTON, { timeout: 15_000 });
  23. await selectTaskOrganizeMode(["项目", "Project"]);
  24. const identities = options.workspaceIdentities;
  25. if (identities?.length) {
  26. // 根因:旧 case 等 settings 历史投影变成 failed,新项目已离线仍白等 30 秒。
  27. // 当前连接展示归 Source/项目目录,按指定 identity 等实际项目行,不读取退役投影判就绪。
  28. let latest: Array<{ identity: string; status: string | null }> = [];
  29. try {
  30. await browser.waitUntil(
  31. async () => {
  32. latest = await browser.execute(
  33. (identities) =>
  34. identities.map((identity) => ({
  35. identity,
  36. status:
  37. document.querySelector<HTMLElement>(
  38. `[data-project-workspace-identity=${JSON.stringify(identity)}]`,
  39. )?.dataset.connectionState ?? null,
  40. })),
  41. identities,
  42. );
  43. return latest.every((project) => project.status === "offline");
  44. },
  45. { timeout: 30_000, timeoutMsg: `${caseMarker}: 目标项目未离线` },
  46. );
  47. } catch (cause) {
  48. throw new Error(`${caseMarker}: 目标项目未离线;latest=${JSON.stringify(latest)}`, { cause });
  49. }
  50. }
  51. }
  52. export async function connectRemoteSourceFromSettings(
  53. sourceId: string,
  54. caseMarker: string,
  55. ): Promise<void> {
  56. await clickRemoteSourceAction(sourceId, caseMarker, ["连接", "Connect"]);
  57. }
  58. async function clickRemoteSourceAction(
  59. sourceId: string,
  60. caseMarker: string,
  61. labels: string[],
  62. ): Promise<void> {
  63. const settings = await readSettings();
  64. const source = settings.sourceServers?.find((item) => item.sourceServerId === sourceId);
  65. if (!source) throw new Error(`${caseMarker}: 找不到 Source ${sourceId}`);
  66. await clickTestIdByDom(TID_TASK_SETTINGS_BUTTON, { timeout: 15_000 });
  67. await waitForTestIdByDom(TID_SETTINGS_PAGE, { timeout: 15_000 });
  68. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "remoteDevices"), { timeout: 15_000 });
  69. await browser.waitUntil(
  70. () =>
  71. browser.execute(
  72. (name, allowedLabels) => {
  73. const page = document.querySelector('[data-testid="settings-page"]');
  74. // 外层设置内容也用 section;从精确标题取最近设备卡,不能把祖先一起匹配。
  75. const cards = [
  76. ...new Set(
  77. Array.from(page?.querySelectorAll("p") ?? [])
  78. .filter((p) => p.textContent?.trim() === name)
  79. .map((p) => p.closest("section"))
  80. .filter((card): card is HTMLElement => card !== null),
  81. ),
  82. ];
  83. if (cards.length !== 1) return false;
  84. const button = Array.from(cards[0]!.querySelectorAll<HTMLButtonElement>("button")).find(
  85. (item) =>
  86. item.getClientRects().length &&
  87. !item.disabled &&
  88. allowedLabels.includes(item.innerText.trim()),
  89. );
  90. button?.click();
  91. return Boolean(button);
  92. },
  93. source.displayName,
  94. labels,
  95. ),
  96. {
  97. timeout: 30_000,
  98. interval: 200,
  99. timeoutMsg: `${caseMarker}: 目标设备没有可用的 ${labels.join("/")} 入口`,
  100. },
  101. );
  102. }
  103. /** 点击已有项目保留原目录意图;不重新输入线路,也不创建远程无项目聊天。 */
  104. export async function reconnectRemoteProject(identity: string, caseMarker: string): Promise<void> {
  105. const before = await readRemoteTabSnapshot(identity);
  106. if (!before.workspacePath) throw new Error(`${caseMarker}: 找不到保留项目路径`);
  107. await requestRemoteProjectConnection(identity, caseMarker);
  108. await waitForSSHDirectoryStep(caseMarker);
  109. await waitForRemoteTab({
  110. caseMarker,
  111. expectedIdentity: identity,
  112. expectedPath: before.workspacePath,
  113. });
  114. }
  115. /** 故障/升级用例需要观察连接中间态,由调用方决定何时解除 barrier。 */
  116. export async function requestRemoteProjectConnection(
  117. identity: string,
  118. caseMarker: string,
  119. options: { retryFailure?: boolean } = {},
  120. ): Promise<void> {
  121. await selectTaskOrganizeMode(["项目", "Project"]);
  122. const row = $(`[data-project-workspace-identity=${JSON.stringify(identity)}]`);
  123. await row.waitForDisplayed({ timeout: 30_000, timeoutMsg: `${caseMarker}: 项目入口未保留` });
  124. await row.moveTo();
  125. await row.$('[data-testid="virtual-project-menu"]').click();
  126. await $('[data-testid="virtual-project-connect"]').click();
  127. if (!options.retryFailure) return;
  128. // 失败项目先展示原故障,再由用户在原向导点击重试;只打开页面不能算作恢复操作。
  129. await browser.waitUntil(
  130. async () => {
  131. if ((await readRemoteTabSnapshot(identity)).remoteSessionId) return true;
  132. return browser.execute(() => {
  133. const button = Array.from(
  134. document.querySelectorAll<HTMLButtonElement>('[data-testid="ssh-dialog"] button'),
  135. ).find(
  136. (item) =>
  137. /^(重试|重新连接|Retry|Reconnect)$/iu.test(item.innerText.trim()) && !item.disabled,
  138. );
  139. button?.click();
  140. return Boolean(button);
  141. });
  142. },
  143. { timeout: 30_000, timeoutMsg: `${caseMarker}: 原地恢复没有可用重试入口` },
  144. );
  145. }