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

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