conversation-session-remote-data-sync-device-connect.test.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import { mkdir } from "node:fs/promises";
  2. import { join, resolve } from "node:path";
  3. import {
  4. TID_SETTINGS_PAGE,
  5. TID_SETTINGS_SECTION_NAV,
  6. TID_SSH_HOST_INPUT,
  7. TID_TASK_SETTINGS_BUTTON,
  8. testId,
  9. } from "@zcode/shared/test-ids";
  10. import {
  11. clearAppData,
  12. clickTestIdByDom,
  13. waitForTestIdByDom,
  14. setCurrentElectronRendererContentSize,
  15. } from "#e2e/helpers/desktop-app";
  16. import {
  17. bringRemoteDeviceOnline,
  18. readRemoteDockerContainer,
  19. takeRemoteDeviceOffline,
  20. } from "#e2e/helpers/remote-docker-target";
  21. import {
  22. clickLastEnabledSSHDialogButton,
  23. fillSSHPasswordSettings,
  24. prepareSSHConnectionPage,
  25. readSSHRuntimeConfig,
  26. waitForSSHDirectoryStep,
  27. } from "#e2e/helpers/ssh-remote-p0";
  28. import { prepareV4ConversationE2E } from "#e2e/helpers/v4-conversation";
  29. // DSYNC-05:数据同步页的设备状态与连接(docs/remote/data-sync.md「设备列表与设备同步状态」)。
  30. // 远端为本机 Docker runner(或服务器队列)提供的真实 Linux 容器;离线 / 恢复用 docker stop / start,端口不变。
  31. const CASE_MARKER = "E2E_DSYNC_05_DEVICE_CONNECT";
  32. interface DeviceRowState {
  33. count: number;
  34. sourceServerId: string | null;
  35. state: string | null;
  36. status: string;
  37. connection: string;
  38. actionVariant: string | null;
  39. actionIcon: string;
  40. action: string;
  41. actionDisabled: boolean;
  42. text: string;
  43. }
  44. async function readDeviceRow(): Promise<DeviceRowState> {
  45. return await browser.execute(() => {
  46. const rows = document.querySelectorAll<HTMLElement>(
  47. '[data-testid="remote-sync-device-list"] [data-source-server-id]',
  48. );
  49. const row = rows[0];
  50. const action = row?.querySelector<HTMLButtonElement>(
  51. '[data-testid="remote-sync-device-action"]',
  52. );
  53. return {
  54. count: rows.length,
  55. sourceServerId: row?.dataset.sourceServerId ?? null,
  56. state: row?.dataset.deviceState ?? null,
  57. status:
  58. row?.querySelector('[data-testid="remote-sync-device-status"]')?.textContent?.trim() ?? "",
  59. connection:
  60. row?.querySelector('[data-testid="remote-sync-device-connection"]')?.textContent?.trim() ??
  61. "",
  62. actionVariant: action?.getAttribute("data-variant") ?? null,
  63. actionIcon: action?.querySelector("svg")?.getAttribute("class") ?? "",
  64. action: action?.textContent?.trim() ?? "",
  65. actionDisabled: action?.disabled ?? true,
  66. text: row?.textContent ?? "",
  67. };
  68. });
  69. }
  70. async function waitForDeviceState(state: string, timeoutMsg: string, timeout = 60_000) {
  71. let last: DeviceRowState | undefined;
  72. await browser.waitUntil(
  73. async () => {
  74. last = await readDeviceRow();
  75. return last.state === state;
  76. },
  77. {
  78. timeout,
  79. interval: 500,
  80. timeoutMsg: `${CASE_MARKER}: ${timeoutMsg}(最后状态 ${JSON.stringify(last)})`,
  81. },
  82. );
  83. return last!;
  84. }
  85. /** 数据同步页仍在前台,且没有被连接完成跳到「远程设备」页。 */
  86. async function expectOnDataSyncPage(): Promise<void> {
  87. await waitForTestIdByDom(TID_SETTINGS_PAGE, { timeout: 30_000 });
  88. await waitForTestIdByDom("remote-sync-section", {
  89. timeout: 30_000,
  90. timeoutMsg: `${CASE_MARKER}: 连接完成后没有回到数据同步页`,
  91. });
  92. expect(await $$("[data-source-server-card]").length).toBe(0);
  93. }
  94. /** 深浅主题 × 中英文的设备列表截图,供人工复核状态点在两种主题下可辨识。 */
  95. async function captureDeviceListScreenshots(label: string): Promise<void> {
  96. const artifactDir = process.env.ZCODE_E2E_ARTIFACT_DIR?.trim();
  97. if (!artifactDir) return;
  98. const dir = resolve(artifactDir, "dsync-05");
  99. await mkdir(dir, { recursive: true });
  100. const apply = async (locale: string, theme: string) =>
  101. await browser.execute(
  102. (language, color) => {
  103. const actions = (
  104. window as Window & {
  105. __testActions?: { setLocale(locale: string): void; setTheme(theme: string): void };
  106. }
  107. ).__testActions;
  108. actions?.setLocale(language);
  109. actions?.setTheme(color);
  110. },
  111. locale,
  112. theme,
  113. );
  114. for (const locale of ["zh-CN", "en-US"]) {
  115. for (const theme of ["light", "dark"]) {
  116. await apply(locale, theme);
  117. await browser.executeAsync((done) =>
  118. requestAnimationFrame(() => requestAnimationFrame(() => done())),
  119. );
  120. for (const width of [1200, 480]) {
  121. await setCurrentElectronRendererContentSize(width, 800);
  122. await browser.executeAsync((done) =>
  123. requestAnimationFrame(() => requestAnimationFrame(() => done())),
  124. );
  125. const fits = await browser.execute(() => {
  126. const list = document.querySelector('[data-testid="remote-sync-device-list"]');
  127. const row = list?.querySelector<HTMLElement>("[data-source-server-id]");
  128. return Boolean(row && row.scrollWidth <= row.clientWidth);
  129. });
  130. expect(fits).toBe(true);
  131. await browser.saveScreenshot(join(dir, `${label}-${locale}-${theme}-${width}.png`));
  132. }
  133. }
  134. }
  135. await apply("zh-CN", "light");
  136. await setCurrentElectronRendererContentSize(1200, 800);
  137. }
  138. describe(`${CASE_MARKER}: 数据同步页设备状态与连接`, () => {
  139. let container: string | undefined;
  140. let target: { host: string; port: number } | undefined;
  141. after(async () => {
  142. // 用例中途失败时恢复容器,避免影响同一 runner 的后续 spec。
  143. if (container && target) {
  144. await bringRemoteDeviceOnline(container, target).catch(() => undefined);
  145. }
  146. await browser.electron.restoreAllMocks();
  147. await clearAppData();
  148. });
  149. it("DSYNC-05-01/02 空状态连接设备后留在数据同步页;设备离线显示「未连接」,恢复后点「连接」完成连接回到数据同步页并可打开弹窗", async function () {
  150. this.timeout(20 * 60_000);
  151. const config = readSSHRuntimeConfig(CASE_MARKER);
  152. container = readRemoteDockerContainer(CASE_MARKER);
  153. target = { host: config.host, port: config.port };
  154. await prepareSSHConnectionPage(CASE_MARKER);
  155. await prepareV4ConversationE2E({ skipProvider: true });
  156. // 设置 → 数据与统计 → 数据同步:没有任何设备时显示空状态和「连接远端设备」。
  157. await clickTestIdByDom(TID_TASK_SETTINGS_BUTTON, { timeout: 30_000 });
  158. await waitForTestIdByDom(TID_SETTINGS_PAGE, { timeout: 30_000 });
  159. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "dataSync"), {
  160. timeout: 30_000,
  161. timeoutMsg: `${CASE_MARKER}: 设置页没有数据同步分区`,
  162. });
  163. await waitForTestIdByDom("remote-sync-empty", {
  164. timeout: 30_000,
  165. timeoutMsg: `${CASE_MARKER}: 没有设备时数据同步页没有空状态`,
  166. });
  167. // DSYNC-05-01:空状态按钮以设备管理模式打开连接向导,成功后留在数据同步页,新设备出现在列表中。
  168. await clickTestIdByDom("remote-sync-empty-connect");
  169. await waitForTestIdByDom(TID_SSH_HOST_INPUT, {
  170. timeout: 30_000,
  171. timeoutMsg: `${CASE_MARKER}: 空状态没有打开新增设备表单`,
  172. });
  173. await fillSSHPasswordSettings(config);
  174. await clickLastEnabledSSHDialogButton(CASE_MARKER);
  175. expect(await waitForSSHDirectoryStep(CASE_MARKER)).toBe("closed");
  176. await expectOnDataSyncPage();
  177. const added = await waitForDeviceState("synced", "新设备没有以在线状态出现在列表中");
  178. expect(added.count).toBe(1);
  179. expect(added.text).toContain("SSH");
  180. expect(added.status).toBe("尚未同步");
  181. expect(added.action).toBe("同步配置");
  182. expect(added.connection).toBe("已连接");
  183. expect(added.actionVariant).toBe("default");
  184. expect(added.actionIcon).toContain("lucide-upload");
  185. expect(await $('[data-testid="remote-sync-dialog"]').isExisting()).toBe(false);
  186. // DSYNC-05-02:设备离线后行变为灰点「未连接」+「连接」;状态只取自管理快照。
  187. await takeRemoteDeviceOffline(container);
  188. const offline = await waitForDeviceState(
  189. "disconnected",
  190. "设备离线后设备行没有变为「未连接」",
  191. 5 * 60_000,
  192. );
  193. expect(offline.status).toBe("未连接");
  194. expect(offline.action).toBe("连接");
  195. expect(offline.actionDisabled).toBe(false);
  196. expect(offline.connection).toBe("未连接");
  197. expect(offline.actionVariant).toBe("outline");
  198. expect(offline.actionIcon).toContain("lucide-plug");
  199. await captureDeviceListScreenshots("disconnected");
  200. await bringRemoteDeviceOnline(container, target);
  201. // 恢复后设备仍保持「未连接」,直到用户点「连接」。
  202. expect((await readDeviceRow()).state).toBe("disconnected");
  203. await clickTestIdByDom("remote-sync-device-action");
  204. expect(await waitForSSHDirectoryStep(CASE_MARKER)).toBe("closed");
  205. await expectOnDataSyncPage();
  206. const reconnected = await waitForDeviceState("synced", "连接完成后设备行没有变为「同步」");
  207. expect(reconnected.sourceServerId).toBe(added.sourceServerId);
  208. expect(reconnected.action).toBe("同步配置");
  209. expect(reconnected.connection).toBe("已连接");
  210. expect(await $('[data-testid="remote-sync-dialog"]').isExisting()).toBe(false);
  211. await captureDeviceListScreenshots("synced");
  212. // 连接后可以直接打开该设备的同步弹窗。
  213. await clickTestIdByDom("remote-sync-device-action");
  214. await waitForTestIdByDom("remote-sync-dialog", { timeout: 30_000 });
  215. await waitForTestIdByDom("remote-sync-overview-skills", {
  216. timeout: 60_000,
  217. timeoutMsg: `${CASE_MARKER}: 同步弹窗没有加载概览`,
  218. });
  219. expect(await $('[data-testid="remote-sync-offline"]').isExisting()).toBe(false);
  220. });
  221. });