conversation-session-plugin-missing-package-workspace.test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { readFile, writeFile, mkdir } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import { clearAppData, clickTestIdByDom, getE2EAppDataPaths } from "#e2e/helpers/desktop-app";
  4. import {
  5. addLifecycleMarketplaceThroughUi,
  6. createPluginLifecycleFixture,
  7. getPluginOverview,
  8. installPluginThroughUi,
  9. openPluginCard,
  10. openPluginSettings,
  11. PLUGIN_LIFECYCLE_ID,
  12. PLUGIN_LIFECYCLE_MARKETPLACE_ID,
  13. PLUGIN_LIFECYCLE_MCP_TOOL_NAME,
  14. PLUGIN_LIFECYCLE_RUNTIME_MARKER,
  15. pluginStoragePath,
  16. waitForPluginCard,
  17. } from "#e2e/helpers/plugin-management-lifecycle";
  18. import {
  19. E2E_REPLY_TOKEN,
  20. getV4ConfigProjection,
  21. prepareV4ConversationE2E,
  22. sendV4Prompt,
  23. startNewV4Draft,
  24. waitForV4AssistantMessageContaining,
  25. } from "#e2e/helpers/v4-conversation";
  26. import { waitForToolCallBlockByToolName } from "#e2e/helpers/conversation-session-tool";
  27. import { waitForUpstreamRequestContaining } from "#e2e/helpers/conversation-session-network";
  28. import { ensureToolCrossProductFullAccessMode } from "#e2e/helpers/conversation-session-tool-cross-product";
  29. import {
  30. captureArtifactAdvertisesToolName,
  31. findUpstreamRequestContaining,
  32. } from "#e2e/helpers/plugin-mcp-skill";
  33. describe("WPL-006 Workspace Plugin missing package E2E", () => {
  34. after(async () => {
  35. await browser.electron.restoreAllMocks();
  36. await clearAppData();
  37. });
  38. it("keeps the project declaration without auto-install, then runs after explicit install", async function () {
  39. this.timeout(300000);
  40. const { workspace } = getE2EAppDataPaths();
  41. const workspaceConfigPath = join(workspace, ".zcode", "config.json");
  42. await mkdir(join(workspace, ".zcode"), { recursive: true });
  43. await writeFile(
  44. workspaceConfigPath,
  45. `${JSON.stringify({ plugins: { enabledPlugins: { [PLUGIN_LIFECYCLE_ID]: true } } }, null, 2)}\n`,
  46. "utf8",
  47. );
  48. const fixture = await createPluginLifecycleFixture("1.0.0");
  49. const installedPluginsPath = pluginStoragePath("installed_plugins.json");
  50. expect(await readOptionalText(installedPluginsPath)).toBeNull();
  51. await openPluginSettings();
  52. await addLifecycleMarketplaceThroughUi(fixture);
  53. await clickTestIdByDom("plugin-store-segment-personal");
  54. await waitForPluginCard(PLUGIN_LIFECYCLE_ID);
  55. const beforeInstall = await getPluginOverview();
  56. expect(beforeInstall.availablePlugins).toEqual(
  57. expect.arrayContaining([
  58. expect.objectContaining({
  59. id: PLUGIN_LIFECYCLE_ID,
  60. installed: false,
  61. marketplace: PLUGIN_LIFECYCLE_MARKETPLACE_ID,
  62. }),
  63. ]),
  64. );
  65. expect(beforeInstall.installedPlugins).not.toEqual(
  66. expect.arrayContaining([expect.objectContaining({ id: PLUGIN_LIFECYCLE_ID })]),
  67. );
  68. expect(await readOptionalText(installedPluginsPath)).toBeNull();
  69. await openPluginCard(PLUGIN_LIFECYCLE_ID);
  70. await installPluginThroughUi(PLUGIN_LIFECYCLE_ID);
  71. const afterInstall = await getPluginOverview();
  72. expect(afterInstall.installedPlugins).toEqual(
  73. expect.arrayContaining([expect.objectContaining({ id: PLUGIN_LIFECYCLE_ID, scope: "user" })]),
  74. );
  75. expect(await readFile(workspaceConfigPath, "utf8")).toContain(PLUGIN_LIFECYCLE_ID);
  76. expect(await readFile(installedPluginsPath, "utf8")).toContain(PLUGIN_LIFECYCLE_ID);
  77. await startNewV4Draft();
  78. await assertLifecycleRuntimeAfterExplicitInstall();
  79. });
  80. });
  81. async function assertLifecycleRuntimeAfterExplicitInstall(): Promise<void> {
  82. await prepareV4ConversationE2E();
  83. await startNewV4Draft();
  84. // 新草稿会恢复初始权限模式;必须在实际发送的草稿上开启完全访问,避免停在工具审批。
  85. await ensureToolCrossProductFullAccessMode();
  86. expect((await getV4ConfigProjection()).mode).toBe("yolo");
  87. const marker = `${PLUGIN_LIFECYCLE_RUNTIME_MARKER}_1.0.0`;
  88. await sendV4Prompt(
  89. `${marker}: Call ${PLUGIN_LIFECYCLE_MCP_TOOL_NAME} exactly once, then reply with exactly ${E2E_REPLY_TOKEN}.`,
  90. );
  91. await waitForUpstreamRequestContaining(marker, 60000);
  92. const requestRecord = await findUpstreamRequestContaining(marker, 60000, {
  93. advertisedToolName: PLUGIN_LIFECYCLE_MCP_TOOL_NAME,
  94. });
  95. expect(requestRecord).not.toBeNull();
  96. if (!requestRecord) return;
  97. expect(
  98. captureArtifactAdvertisesToolName(requestRecord.requestJson, PLUGIN_LIFECYCLE_MCP_TOOL_NAME),
  99. ).toBe(true);
  100. // 工具可能已完成并折叠,使用会展开工作记录的等待器。
  101. await waitForToolCallBlockByToolName(PLUGIN_LIFECYCLE_MCP_TOOL_NAME, 60000);
  102. await waitForV4AssistantMessageContaining(E2E_REPLY_TOKEN);
  103. }
  104. async function readOptionalText(path: string): Promise<string | null> {
  105. try {
  106. return await readFile(path, "utf8");
  107. } catch (error) {
  108. if (
  109. error &&
  110. typeof error === "object" &&
  111. "code" in error &&
  112. (error as { code?: string }).code === "ENOENT"
  113. ) {
  114. return null;
  115. }
  116. throw error;
  117. }
  118. }