plugin-discovery-tool.test.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { describe, expect, it, vi } from "vitest";
  2. import { registerBuiltInTools } from "../src/tool/handlers/index.js";
  3. import { createToolRegistry } from "../src/tool/registry.js";
  4. import {
  5. searchPluginsToolEntry,
  6. suggestPluginInstallToolEntry,
  7. } from "../src/tool/handlers/plugin-discovery.js";
  8. import { PermissionService } from "../src/permission/service.js";
  9. import type { ToolExecutionContext } from "../src/tool/types.js";
  10. describe("plugin discovery tools", () => {
  11. it("is opt-in, with no tools advertised for unsupported clients", () => {
  12. const absent = createToolRegistry();
  13. registerBuiltInTools(absent);
  14. expect(absent.has("SearchPlugins")).toBe(false);
  15. expect(absent.has("SuggestPluginInstall")).toBe(false);
  16. const enabled = createToolRegistry();
  17. registerBuiltInTools(enabled, { includePluginDiscovery: true });
  18. expect(enabled.has("SearchPlugins")).toBe(true);
  19. expect(enabled.has("SuggestPluginInstall")).toBe(true);
  20. });
  21. it("loads both tools on demand through ToolSearch", () => {
  22. expect(searchPluginsToolEntry.metadata.shouldDefer).toBe(true);
  23. expect(suggestPluginInstallToolEntry.metadata.shouldDefer).toBe(true);
  24. });
  25. it("delegates the tool context including cancellation and trace to the injected port", async () => {
  26. const search = vi.fn(async () => ({ plugins: [], stale: false }));
  27. const signal = new AbortController().signal;
  28. const context = {
  29. sessionId: "session",
  30. toolCallId: "call",
  31. traceId: "trace",
  32. abortSignal: signal,
  33. pluginDiscoveryPort: { search },
  34. runtimeScope: "main",
  35. } as unknown as ToolExecutionContext;
  36. await searchPluginsToolEntry.handler({ query: "email" }, context);
  37. expect(search).toHaveBeenCalledWith(
  38. { query: "email" },
  39. expect.objectContaining({ signal, traceId: "trace", sessionId: "session" }),
  40. );
  41. await expect(
  42. searchPluginsToolEntry.handler({ query: "email" }, { ...context, runtimeScope: "subagent" }),
  43. ).rejects.toThrow();
  44. });
  45. it("uses its own confirmation while retaining plan and explicit deny/ask rules", () => {
  46. const service = new PermissionService();
  47. const capability = {
  48. ...suggestPluginInstallToolEntry.metadata,
  49. permission: suggestPluginInstallToolEntry.permission,
  50. };
  51. const context = {
  52. toolName: "SuggestPluginInstall",
  53. input: {},
  54. riskLevel: "medium" as const,
  55. mode: "build" as const,
  56. };
  57. expect(service.checkPermission(context, capability).decision).toBe("allow");
  58. expect(service.checkPermission({ ...context, mode: "plan" }, capability).decision).toBe("deny");
  59. expect(
  60. service.checkPermission(context, capability, { deny: [{ toolName: context.toolName }] })
  61. .decision,
  62. ).toBe("deny");
  63. expect(
  64. service.checkPermission(context, capability, { ask: [{ toolName: context.toolName }] })
  65. .decision,
  66. ).toBe("ask");
  67. expect(suggestPluginInstallToolEntry.timeout).toEqual({ kind: "none" });
  68. });
  69. });