conversation-session-remote-data-sync-marketplace-plugin-push.test.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import { readFile, writeFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import {
  4. TID_SETTINGS_PAGE,
  5. TID_SETTINGS_SECTION_NAV,
  6. TID_TASK_SETTINGS_BUTTON,
  7. testId,
  8. } from "@zcode/shared/test-ids";
  9. import {
  10. clearAppData,
  11. clickTestIdByDom,
  12. getE2EAppDataPaths,
  13. waitForTestIdByDom,
  14. } from "#e2e/helpers/desktop-app";
  15. import {
  16. addMarketplaceSourceThroughAgent,
  17. createPluginLifecycleFixture,
  18. installMarketplacePluginThroughAgent,
  19. PLUGIN_LIFECYCLE_ID,
  20. PLUGIN_LIFECYCLE_MARKETPLACE_ID,
  21. PLUGIN_LIFECYCLE_NAME,
  22. waitUntilPluginList,
  23. } from "#e2e/helpers/plugin-management-lifecycle";
  24. import {
  25. readRemoteContainerFile,
  26. readRemoteDockerContainer,
  27. } from "#e2e/helpers/remote-docker-target";
  28. import {
  29. connectSSHWorkspace,
  30. prepareSSHConnectionPage,
  31. readSSHRuntimeConfig,
  32. } from "#e2e/helpers/ssh-remote-p0";
  33. import { prepareV4ConversationE2E } from "#e2e/helpers/v4-conversation";
  34. // DSYNC-08:设置 → 数据同步 → 设备 → 推送 marketplace 插件(docs/remote/data-sync.md「插件 · marketplace 插件」)。
  35. // 本机从 Personal Source 的本地目录 marketplace 安装插件;推送时先把 marketplace 清单与插件源码镜像到远端,
  36. // 再在远端 add + install。远端断言用 docker exec 读容器内 ~/.zcode。覆盖旧插件同步弹窗 WPL-011 的场景。
  37. const CASE_MARKER = "E2E_DSYNC_08_MARKETPLACE_PLUGIN_PUSH";
  38. const ROW_KEY = PLUGIN_LIFECYCLE_ID.toLowerCase();
  39. const REMOTE_PLUGIN_STORAGE = "/home/dev/.zcode/cli/plugins";
  40. const REMOTE_CONFIG = "/home/dev/.zcode/cli/config.json";
  41. /** 本机把该插件设为停用(直接写 User config,CLI 每次请求重新读取),用于断言启用状态随推送一致。 */
  42. async function disableLocalPlugin(): Promise<void> {
  43. const configPath = join(getE2EAppDataPaths().storageRoot, "cli", "config.json");
  44. const config = JSON.parse(await readFile(configPath, "utf-8")) as Record<string, unknown>;
  45. const plugins = (config.plugins ?? {}) as Record<string, unknown>;
  46. const enabledPlugins = (plugins.enabledPlugins ?? {}) as Record<string, boolean>;
  47. await writeFile(
  48. configPath,
  49. `${JSON.stringify(
  50. {
  51. ...config,
  52. plugins: {
  53. ...plugins,
  54. enabledPlugins: { ...enabledPlugins, [PLUGIN_LIFECYCLE_ID]: false },
  55. },
  56. },
  57. null,
  58. 2,
  59. )}\n`,
  60. );
  61. }
  62. async function readRowState(key: string) {
  63. return await browser.execute((rowTestId) => {
  64. const row = Array.from(document.querySelectorAll<HTMLButtonElement>("[data-testid]")).find(
  65. (element) => element.dataset.testid === rowTestId,
  66. );
  67. return row
  68. ? {
  69. status: row.dataset.syncStatus ?? null,
  70. checked: row.getAttribute("aria-checked"),
  71. text: row.textContent ?? "",
  72. }
  73. : null;
  74. }, `remote-sync-row-${key}`);
  75. }
  76. async function waitForRowStatus(key: string, status: string, timeoutMsg: string) {
  77. await browser.waitUntil(async () => (await readRowState(key))?.status === status, {
  78. timeout: 120_000,
  79. timeoutMsg: `${CASE_MARKER}: ${timeoutMsg}`,
  80. });
  81. return (await readRowState(key))!;
  82. }
  83. async function readButtonText(buttonTestId: string): Promise<string> {
  84. return await browser.execute(
  85. (id) => document.querySelector(`[data-testid="${id}"]`)?.textContent?.trim() ?? "",
  86. buttonTestId,
  87. );
  88. }
  89. async function readRemoteJson(container: string, path: string): Promise<Record<string, unknown>> {
  90. return JSON.parse(await readRemoteContainerFile(container, path)) as Record<string, unknown>;
  91. }
  92. describe(`${CASE_MARKER}: 数据同步页推送 marketplace 插件`, () => {
  93. after(async () => {
  94. await browser.electron.restoreAllMocks();
  95. await clearAppData();
  96. });
  97. it("DSYNC-08-01 本地目录来源的 marketplace 插件镜像到远端后安装,启用状态与本机一致", async function () {
  98. this.timeout(20 * 60_000);
  99. const config = readSSHRuntimeConfig(CASE_MARKER);
  100. const container = readRemoteDockerContainer(CASE_MARKER);
  101. const workspacePath = config.workspacePaths[0];
  102. await prepareSSHConnectionPage(CASE_MARKER);
  103. await prepareV4ConversationE2E({ skipProvider: true });
  104. // 本机:添加本地目录 marketplace,安装其中一个插件,再停用它。
  105. const fixture = await createPluginLifecycleFixture("1.0.0");
  106. await addMarketplaceSourceThroughAgent(fixture.marketplaceRoot);
  107. await installMarketplacePluginThroughAgent(
  108. PLUGIN_LIFECYCLE_NAME,
  109. PLUGIN_LIFECYCLE_MARKETPLACE_ID,
  110. );
  111. await disableLocalPlugin();
  112. await waitUntilPluginList(
  113. (list) => list.plugins.some((plugin) => plugin.id === PLUGIN_LIFECYCLE_ID && !plugin.enabled),
  114. `${CASE_MARKER}: 本机插件没有变为停用`,
  115. );
  116. await connectSSHWorkspace({ caseMarker: CASE_MARKER, config, workspacePath });
  117. await clickTestIdByDom(TID_TASK_SETTINGS_BUTTON, { timeout: 30_000 });
  118. await waitForTestIdByDom(TID_SETTINGS_PAGE, { timeout: 30_000 });
  119. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "dataSync"), {
  120. timeout: 30_000,
  121. timeoutMsg: `${CASE_MARKER}: 设置页没有数据同步分区`,
  122. });
  123. await waitForTestIdByDom("remote-sync-device-list", {
  124. timeout: 60_000,
  125. timeoutMsg: `${CASE_MARKER}: 数据同步页没有在线设备`,
  126. });
  127. await clickTestIdByDom("remote-sync-device-action", { timeout: 30_000 });
  128. await waitForTestIdByDom("remote-sync-dialog", { timeout: 30_000 });
  129. await clickTestIdByDom("remote-sync-tab-plugins", {
  130. timeout: 30_000,
  131. timeoutMsg: `${CASE_MARKER}: 同步弹窗没有插件分类`,
  132. });
  133. // 只在本机存在的 marketplace 插件显示「推送」并默认勾选。
  134. const row = await waitForRowStatus(ROW_KEY, "push", "marketplace 插件没有显示为「推送」");
  135. expect(row.checked).toBe("true");
  136. expect(row.text).toContain(PLUGIN_LIFECYCLE_MARKETPLACE_ID);
  137. await clickTestIdByDom("remote-sync-push", {
  138. timeout: 30_000,
  139. timeoutMsg: `${CASE_MARKER}: 推送插件按钮不可用`,
  140. });
  141. await browser.waitUntil(async () => (await readButtonText("remote-sync-push")) === "再次同步", {
  142. timeout: 300_000,
  143. timeoutMsg: `${CASE_MARKER}: 推送插件没有完成`,
  144. });
  145. await waitForRowStatus(ROW_KEY, "synced", "推送后 marketplace 插件没有变为「已同步」");
  146. expect(
  147. await browser.execute(() =>
  148. document.querySelector('[data-testid^="remote-sync-row-error-"]'),
  149. ),
  150. ).toBeNull();
  151. // 远端:marketplace 来源是镜像到 ~/.zcode/plugins/marketplace-sources 的副本,插件以 User scope 安装且停用。
  152. const knownMarketplaces = await readRemoteContainerFile(
  153. container,
  154. `${REMOTE_PLUGIN_STORAGE}/known_marketplaces.json`,
  155. );
  156. expect(knownMarketplaces).toContain(PLUGIN_LIFECYCLE_MARKETPLACE_ID);
  157. expect(knownMarketplaces).toContain("/home/dev/.zcode/plugins/marketplace-sources/");
  158. const installed = await readRemoteContainerFile(
  159. container,
  160. `${REMOTE_PLUGIN_STORAGE}/installed_plugins.json`,
  161. );
  162. expect(installed).toContain(PLUGIN_LIFECYCLE_ID);
  163. const remoteConfig = await readRemoteJson(container, REMOTE_CONFIG);
  164. const enabledPlugins = ((remoteConfig.plugins ?? {}) as Record<string, unknown>)
  165. .enabledPlugins as Record<string, boolean> | undefined;
  166. expect(enabledPlugins?.[PLUGIN_LIFECYCLE_ID]).toBe(false);
  167. });
  168. });