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

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