plugin-management-personal-lifecycle.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. import { type ZCodePluginComponentKind } from "@zcode/shared";
  2. import { TID_V4_COMPOSER_INPUT } from "@zcode/shared/test-ids";
  3. import { clearAppData, waitForTestIdByDom } from "#e2e/helpers/desktop-app";
  4. import { waitForUpstreamRequestContaining } from "#e2e/helpers/conversation-session-network";
  5. import {
  6. getV4ComposerText,
  7. prepareV4ConversationE2E,
  8. sendV4Prompt,
  9. } from "#e2e/helpers/v4-conversation";
  10. import {
  11. captureArtifactAdvertisesToolName,
  12. findUpstreamRequestContaining,
  13. } from "#e2e/helpers/plugin-mcp-skill";
  14. import {
  15. PLUGIN_LIFECYCLE_CONFIG_VALUE,
  16. PLUGIN_LIFECYCLE_EXAMPLE_PROMPT,
  17. PLUGIN_LIFECYCLE_EXTRA_ID,
  18. PLUGIN_LIFECYCLE_EXTRA_NAME,
  19. PLUGIN_LIFECYCLE_ID,
  20. PLUGIN_LIFECYCLE_MARKETPLACE_ID,
  21. PLUGIN_LIFECYCLE_MCP_TOOL_NAME,
  22. PLUGIN_LIFECYCLE_RUNTIME_MARKER,
  23. addLifecycleMarketplaceThroughUi,
  24. capturePluginManualReview,
  25. clickPluginControl,
  26. configureLifecyclePluginThroughUi,
  27. createPluginLifecycleFixture,
  28. getPluginList,
  29. getPluginOverview,
  30. hasPluginControl,
  31. installMarketplacePluginThroughAgent,
  32. openManageView,
  33. openPluginCard,
  34. openPluginSettings,
  35. requirePluginManualReview,
  36. selectPluginSettingsScope,
  37. setPluginEnabledThroughUi,
  38. uninstallPluginThroughUi,
  39. updateLifecycleExtraPluginFixture,
  40. updatePluginLifecycleFixture,
  41. waitForPluginControl,
  42. waitUntilPluginOverview,
  43. } from "#e2e/helpers/plugin-management-lifecycle";
  44. const LIFECYCLE_TIMEOUT_MS = 300000;
  45. describe("PLM-LC-003 Personal Source 插件主生命周期 E2E", () => {
  46. after(async () => {
  47. await browser.electron.restoreAllMocks();
  48. await clearAppData();
  49. });
  50. it("从来源添加到运行时升级,再取消并确认卸载", async function () {
  51. this.timeout(LIFECYCLE_TIMEOUT_MS);
  52. requirePluginManualReview();
  53. let fixture = await createPluginLifecycleFixture("1.0.0");
  54. await openPluginSettings();
  55. await addLifecycleMarketplaceThroughUi(fixture);
  56. await clickPluginControl("plugin-store-segment-personal");
  57. await openPluginCard(PLUGIN_LIFECYCLE_ID);
  58. for (const kind of [
  59. "mcp",
  60. "skill",
  61. "command",
  62. "agent",
  63. "hook",
  64. ] satisfies ZCodePluginComponentKind[]) {
  65. await waitForPluginControl("plugin-store-component-section", { componentKind: kind }, 30000);
  66. }
  67. await capturePluginManualReview("PLM-LC-003", "uninstalled-five-components");
  68. await clickPluginControl("plugin-store-example-prompt", { pluginId: PLUGIN_LIFECYCLE_ID });
  69. await waitUntilPluginOverview(
  70. (overview) => overview.installedPlugins.some((plugin) => plugin.id === PLUGIN_LIFECYCLE_ID),
  71. "Example Prompt did not install the plugin",
  72. 120000,
  73. );
  74. await waitForPluginControl("plugin-store-item-menu", { pluginId: PLUGIN_LIFECYCLE_ID }, 30000);
  75. expect(await isPluginSettingsVisible()).toBe(true);
  76. await clickPluginControl("plugin-store-example-prompt", { pluginId: PLUGIN_LIFECYCLE_ID });
  77. await waitForTestIdByDom(TID_V4_COMPOSER_INPUT, { timeout: 30000 });
  78. try {
  79. await browser.waitUntil(
  80. async () => {
  81. const composerText = (await getV4ComposerText()) ?? "";
  82. return (
  83. composerText.includes(`plugin://${PLUGIN_LIFECYCLE_ID}`) &&
  84. composerText.endsWith(` ${PLUGIN_LIFECYCLE_EXAMPLE_PROMPT}`)
  85. );
  86. },
  87. {
  88. timeout: 10000,
  89. timeoutMsg: "Example Prompt did not prefill the canonical Plugin reference and prompt",
  90. },
  91. );
  92. } catch (error) {
  93. const evidence = await browser.execute(() => {
  94. interface ComposerEvidenceWorkspace {
  95. activeTaskId: string | null;
  96. composerTextInsertRequest: { requestId: number; text: string } | null;
  97. composerTextInsertVersion: number;
  98. }
  99. const store = (
  100. window as typeof window & {
  101. __zcodeSessionStoreE2E?: {
  102. getState: () => { workspaces: Record<string, ComposerEvidenceWorkspace> };
  103. };
  104. }
  105. ).__zcodeSessionStoreE2E;
  106. const state = store?.getState();
  107. return {
  108. composers: Array.from(
  109. document.querySelectorAll<
  110. HTMLElement & { __zcodeLexicalInputE2E?: { getText: () => string } }
  111. >('[data-testid="v4-composer-input"]'),
  112. ).map(
  113. (element) =>
  114. element.__zcodeLexicalInputE2E?.getText() ??
  115. element.innerText ??
  116. element.textContent ??
  117. "",
  118. ),
  119. workspaces: Object.fromEntries(
  120. Object.entries(state?.workspaces ?? {}).map(([key, workspace]) => [
  121. key,
  122. {
  123. activeTaskId: workspace.activeTaskId,
  124. composerTextInsertRequest: workspace.composerTextInsertRequest,
  125. composerTextInsertVersion: workspace.composerTextInsertVersion,
  126. },
  127. ]),
  128. ),
  129. };
  130. });
  131. throw new Error(`Example Prompt evidence: ${JSON.stringify(evidence)}`, { cause: error });
  132. }
  133. expect(await countCapturedRequestsContaining(PLUGIN_LIFECYCLE_EXAMPLE_PROMPT)).toBe(0);
  134. await openPluginSettings();
  135. await clickPluginControl("plugin-store-segment-personal");
  136. await openPluginCard(PLUGIN_LIFECYCLE_ID);
  137. await configureLifecyclePluginThroughUi(PLUGIN_LIFECYCLE_CONFIG_VALUE);
  138. // 市场菜单会修改当前项目;用户级生命周期必须显式选全局管理范围。
  139. await clickPluginControl("plugin-store-detail-back");
  140. await openManageView();
  141. await selectPluginSettingsScope("user");
  142. await setPluginEnabledThroughUi(PLUGIN_LIFECYCLE_ID, false, "manage");
  143. await setPluginEnabledThroughUi(PLUGIN_LIFECYCLE_ID, true, "manage");
  144. // 第二个可更新插件:一个走行内「更新」,另一个走分组头部「全部更新」,两条真实链路都覆盖。
  145. await installMarketplacePluginThroughAgent(
  146. PLUGIN_LIFECYCLE_EXTRA_NAME,
  147. PLUGIN_LIFECYCLE_MARKETPLACE_ID,
  148. );
  149. fixture = await updatePluginLifecycleFixture(fixture, "2.0.0");
  150. await updateLifecycleExtraPluginFixture(fixture, "2.0.0");
  151. // 市场刷新承担目录更新;管理菜单已只保留启停与卸载,更新使用行内独立按钮。
  152. await clickPluginControl("plugin-store-manage-back");
  153. await clickPluginControl("plugin-store-refresh");
  154. await waitUntilPluginOverview(
  155. (overview) =>
  156. [PLUGIN_LIFECYCLE_ID, PLUGIN_LIFECYCLE_EXTRA_ID].every((pluginId) =>
  157. overview.installedPlugins.some(
  158. (plugin) => plugin.id === pluginId && plugin.updateStatus !== "none",
  159. ),
  160. ),
  161. "Checking updates did not expose lifecycle and extra v2",
  162. 60000,
  163. );
  164. await openManageView();
  165. await selectPluginSettingsScope("user");
  166. // 2026-09-02:检查更新后,已安装行必须直接亮出「可更新」角标与行内「更新」按钮;
  167. // 此前更新入口只藏在「…」菜单和详情页高级区,用户刷新后看不出哪个插件可更新。
  168. await waitForPluginControl("plugin-store-update-badge", { pluginId: PLUGIN_LIFECYCLE_ID });
  169. await waitForPluginControl("plugin-store-card-update", { pluginId: PLUGIN_LIFECYCLE_ID });
  170. await waitForPluginControl("plugin-settings-update-all");
  171. await clickPluginControl("plugin-store-card-update", { pluginId: PLUGIN_LIFECYCLE_EXTRA_ID });
  172. await waitUntilPluginOverview(
  173. (overview) =>
  174. overview.installedPlugins.some(
  175. (plugin) => plugin.id === PLUGIN_LIFECYCLE_EXTRA_ID && plugin.version === "2.0.0",
  176. ),
  177. "Extra plugin did not upgrade to v2 through the inline update button",
  178. 120000,
  179. );
  180. await clickPluginControl("plugin-settings-update-all");
  181. await waitUntilPluginOverview(
  182. (overview) =>
  183. overview.installedPlugins.some(
  184. (plugin) => plugin.id === PLUGIN_LIFECYCLE_ID && plugin.version === "2.0.0",
  185. ),
  186. "Lifecycle plugin did not upgrade to v2 through update-all",
  187. 120000,
  188. );
  189. await browser.waitUntil(async () => !(await hasPluginControl("plugin-settings-update-all")), {
  190. timeout: 15000,
  191. timeoutMsg: "Update-all entry stayed visible after every plugin was updated",
  192. });
  193. expect(
  194. (await getPluginList()).plugins.find((plugin) => plugin.id === PLUGIN_LIFECYCLE_ID)?.version,
  195. ).toBe("2.0.0");
  196. // V4 已无旧 draftSessionId 缓存;用下方新 session 的真实 v2 工具请求验证 runtime 刷新。
  197. await clickPluginControl("plugin-settings-plugin-row", { pluginId: PLUGIN_LIFECYCLE_ID });
  198. await capturePluginManualReview("PLM-LC-003", "updated-v2-detail");
  199. await prepareV4ConversationE2E();
  200. const runMarker = `${PLUGIN_LIFECYCLE_RUNTIME_MARKER}_2.0.0_${Date.now()}`;
  201. await sendV4Prompt(
  202. `${runMarker}: Call ${PLUGIN_LIFECYCLE_MCP_TOOL_NAME} once, then reply with exactly upstream-e2e-ok.`,
  203. );
  204. await waitForUpstreamRequestContaining(runMarker, 60000);
  205. const runtimeRequest = await findUpstreamRequestContaining(runMarker, 60000, {
  206. advertisedToolName: PLUGIN_LIFECYCLE_MCP_TOOL_NAME,
  207. });
  208. expect(runtimeRequest).not.toBeNull();
  209. expect(
  210. captureArtifactAdvertisesToolName(
  211. runtimeRequest?.requestJson,
  212. PLUGIN_LIFECYCLE_MCP_TOOL_NAME,
  213. ),
  214. ).toBe(true);
  215. expect(JSON.stringify(runtimeRequest?.requestJson)).toContain(
  216. `${PLUGIN_LIFECYCLE_RUNTIME_MARKER}_2.0.0`,
  217. );
  218. await openPluginSettings();
  219. await clickPluginControl("plugin-store-segment-personal");
  220. await openPluginCard(PLUGIN_LIFECYCLE_ID);
  221. await uninstallPluginThroughUi(PLUGIN_LIFECYCLE_ID, false);
  222. expect(
  223. (await getPluginOverview()).installedPlugins.some(
  224. (plugin) => plugin.id === PLUGIN_LIFECYCLE_ID,
  225. ),
  226. ).toBe(true);
  227. await uninstallPluginThroughUi(PLUGIN_LIFECYCLE_ID, true);
  228. expect(
  229. (await getPluginOverview()).installedPlugins.some(
  230. (plugin) => plugin.id === PLUGIN_LIFECYCLE_ID,
  231. ),
  232. ).toBe(false);
  233. });
  234. });
  235. async function isPluginSettingsVisible(): Promise<boolean> {
  236. return browser.execute(() =>
  237. Boolean(document.querySelector('[data-testid="plugin-store-root"]')),
  238. );
  239. }
  240. async function countCapturedRequestsContaining(marker: string): Promise<number> {
  241. const capturePath = process.env.E2E_PROVIDER_CAPTURE_PATH?.trim();
  242. if (!capturePath) return 0;
  243. try {
  244. const artifact = JSON.parse(
  245. await (await import("node:fs/promises")).readFile(capturePath, "utf-8"),
  246. ) as {
  247. records?: Array<{ requestJson?: unknown }>;
  248. };
  249. return (artifact.records ?? []).filter((record) =>
  250. JSON.stringify(record.requestJson).includes(marker),
  251. ).length;
  252. } catch {
  253. return 0;
  254. }
  255. }