plugin-runtime-env-agent-projection.test.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import { writeFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import {
  4. TID_SETTINGS_BACK_BUTTON,
  5. TID_SETTINGS_PAGE,
  6. TID_SETTINGS_SECTION_NAV,
  7. TID_SUBAGENT_ROW,
  8. TID_TASK_SETTINGS_BUTTON,
  9. testId,
  10. } from "@zcode/shared";
  11. import {
  12. clearAppData,
  13. clickTestIdByDom,
  14. waitForTestIdByDom,
  15. } from "../../../helpers/desktop-app.js";
  16. import {
  17. ELECTRON_WINDOW_RECORDING_CAPTURE_MODE,
  18. startElectronWindowRecording,
  19. } from "../../../helpers/electron-window-recorder.js";
  20. import {
  21. approveV4Permission,
  22. prepareV4ConversationE2E,
  23. sendV4Prompt,
  24. waitForV4Pane,
  25. } from "../../../helpers/v4-conversation.js";
  26. import {
  27. captureArtifactAdvertisesToolName,
  28. findUpstreamRequestContaining,
  29. } from "../../../helpers/plugin-mcp-skill.js";
  30. import {
  31. addMarketplaceSourceThroughUi,
  32. capturePluginManualReview,
  33. clickPluginControl,
  34. hasPluginControl,
  35. installPluginThroughUi,
  36. openPluginCard,
  37. openPluginSettings,
  38. requirePluginManualReview,
  39. setPluginEnabledThroughUi,
  40. waitForPluginControl,
  41. waitUntilPluginOverview,
  42. } from "../../../helpers/plugin-management-lifecycle.js";
  43. import {
  44. PLUGIN_RUNTIME_ENV_AGENT_BARE_NAME,
  45. PLUGIN_RUNTIME_ENV_AGENT_NAME,
  46. PLUGIN_RUNTIME_ENV_ID,
  47. PLUGIN_RUNTIME_ENV_MARKETPLACE_ID,
  48. PLUGIN_RUNTIME_ENV_RESOLVED_MARKER,
  49. PLUGIN_RUNTIME_ENV_TOOL_NAME,
  50. PLUGIN_RUNTIME_ENV_VARIABLE,
  51. createPluginRuntimeEnvFixture,
  52. } from "../../../helpers/plugin-runtime-env-agent.js";
  53. const CASE_TIMEOUT_MS = 240000;
  54. const VIDEO_ARTIFACT_DIR = join(
  55. process.cwd(),
  56. ".e2e-artifacts",
  57. "plugin-management",
  58. "plm-lc-010",
  59. );
  60. const VIDEO_ARTIFACT_PATH = join(
  61. VIDEO_ARTIFACT_DIR,
  62. "plugin-runtime-env-agent-projection.webm",
  63. );
  64. const VIDEO_MANIFEST_PATH = join(
  65. VIDEO_ARTIFACT_DIR,
  66. "plugin-runtime-env-agent-projection.recording.json",
  67. );
  68. describe("PLM-LC-010 插件 secret env 与 agent 资源投影 E2E", () => {
  69. after(async () => {
  70. await browser.electron.restoreAllMocks();
  71. await clearAppData();
  72. });
  73. it("安装后 MCP 读取任意环境变量且 Subagents 只展示一个规范资源", async function () {
  74. this.timeout(CASE_TIMEOUT_MS);
  75. requirePluginManualReview();
  76. const secret = process.env[PLUGIN_RUNTIME_ENV_VARIABLE]?.trim();
  77. if (!secret) {
  78. throw new Error(
  79. `${PLUGIN_RUNTIME_ENV_VARIABLE} must be injected by the E2E runner`,
  80. );
  81. }
  82. const fixture = await createPluginRuntimeEnvFixture(secret);
  83. const recording = await startElectronWindowRecording({
  84. frameIntervalMs: 250,
  85. outputPath: VIDEO_ARTIFACT_PATH,
  86. preRollDurationMs: 500,
  87. });
  88. try {
  89. await openPluginSettings();
  90. await addMarketplaceSourceThroughUi(fixture.marketplaceRoot);
  91. // 新市场提交后还会异步持久化并刷新列表,必须等弹窗关闭后再操作下层页面。
  92. await waitUntilPluginOverview(
  93. (overview) =>
  94. overview.marketplaces.some(
  95. (marketplace) =>
  96. marketplace.id === PLUGIN_RUNTIME_ENV_MARKETPLACE_ID,
  97. ),
  98. "Plugin runtime env marketplace was not persisted after the UI submission",
  99. 60000,
  100. );
  101. await browser.waitUntil(
  102. async () =>
  103. !(await hasPluginControl("plugin-store-add-source-dialog")),
  104. {
  105. timeout: 60000,
  106. timeoutMsg:
  107. "Plugin runtime env marketplace completed but the source dialog remained open",
  108. },
  109. );
  110. await clickPluginControl("plugin-store-segment-personal");
  111. await openPluginCard(PLUGIN_RUNTIME_ENV_ID);
  112. await waitForPluginControl("plugin-store-component-section", {
  113. componentKind: "mcp",
  114. });
  115. await waitForPluginControl("plugin-store-component-section", {
  116. componentKind: "agent",
  117. });
  118. await installPluginThroughUi(PLUGIN_RUNTIME_ENV_ID);
  119. await setPluginEnabledThroughUi(PLUGIN_RUNTIME_ENV_ID, true, "detail");
  120. await capturePluginManualReview("PLM-LC-010", "installed-mcp-and-agent");
  121. // Plugin 商店是 workspace 主视图而不是设置页分区,subagent 投影断言必须自己打开设置页。
  122. await clickTestIdByDom(TID_TASK_SETTINGS_BUTTON);
  123. await waitForTestIdByDom(TID_SETTINGS_PAGE, { timeout: 15000 });
  124. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "subagents"));
  125. await waitForTestIdByDom(
  126. testId(TID_SUBAGENT_ROW, PLUGIN_RUNTIME_ENV_AGENT_NAME),
  127. {
  128. timeout: 30000,
  129. },
  130. );
  131. const agentRows = await browser.execute(
  132. (canonicalTestId, bareTestId) => ({
  133. bare: document.querySelectorAll(`[data-testid="${bareTestId}"]`)
  134. .length,
  135. canonical: document.querySelectorAll(
  136. `[data-testid="${canonicalTestId}"]`,
  137. ).length,
  138. }),
  139. testId(TID_SUBAGENT_ROW, PLUGIN_RUNTIME_ENV_AGENT_NAME),
  140. testId(TID_SUBAGENT_ROW, PLUGIN_RUNTIME_ENV_AGENT_BARE_NAME),
  141. );
  142. expect(agentRows).toEqual({ bare: 0, canonical: 1 });
  143. await capturePluginManualReview(
  144. "PLM-LC-010",
  145. "single-plugin-agent-resource",
  146. );
  147. await clickTestIdByDom(TID_SETTINGS_BACK_BUTTON);
  148. await prepareV4ConversationE2E();
  149. const runMarker = `E2E_PLUGIN_SECRET_ENV_CASE_${Date.now()}`;
  150. await sendV4Prompt(
  151. `${runMarker}: Call ${PLUGIN_RUNTIME_ENV_TOOL_NAME} once, then reply with exactly upstream-e2e-ok.`,
  152. );
  153. const initialRequest = await findUpstreamRequestContaining(
  154. runMarker,
  155. 60000,
  156. {
  157. advertisedToolName: PLUGIN_RUNTIME_ENV_TOOL_NAME,
  158. },
  159. );
  160. expect(initialRequest).not.toBeNull();
  161. expect(
  162. captureArtifactAdvertisesToolName(
  163. initialRequest?.requestJson,
  164. PLUGIN_RUNTIME_ENV_TOOL_NAME,
  165. ),
  166. ).toBe(true);
  167. expect(await approveV4Permission()).toBe(true);
  168. const finalRequest = await findUpstreamRequestContaining(
  169. PLUGIN_RUNTIME_ENV_RESOLVED_MARKER,
  170. 60000,
  171. );
  172. expect(finalRequest).not.toBeNull();
  173. expect(JSON.stringify(finalRequest?.requestJson)).not.toContain(secret);
  174. await waitForV4Pane(
  175. (snapshot) =>
  176. !snapshot.canStop &&
  177. snapshot.timelineText.includes("upstream-e2e-ok"),
  178. "Plugin secret env case did not visibly reach the deterministic final response",
  179. 60000,
  180. );
  181. await capturePluginManualReview(
  182. "PLM-LC-010",
  183. "mcp-secret-env-resolved",
  184. );
  185. } finally {
  186. const recordingResult = await recording.stop({
  187. tailDurationMs: 1_000,
  188. timeoutMs: 60_000,
  189. });
  190. expect(recordingResult.captureMode).toBe(
  191. ELECTRON_WINDOW_RECORDING_CAPTURE_MODE,
  192. );
  193. expect(recordingResult.frameCount).toBeGreaterThan(1);
  194. expect(recordingResult.captureError).toBeUndefined();
  195. await writeFile(
  196. VIDEO_MANIFEST_PATH,
  197. `${JSON.stringify(
  198. {
  199. caseName: "plugin-runtime-env-agent-projection",
  200. frameCount: recordingResult.frameCount,
  201. video_capture_mode: recordingResult.captureMode,
  202. videoPath: recordingResult.videoPath,
  203. },
  204. null,
  205. 2,
  206. )}\n`,
  207. "utf-8",
  208. );
  209. }
  210. });
  211. });