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

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