toolPresentationHost.test.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // @vitest-environment jsdom
  2. import { createElement } from "react";
  3. import { render, cleanup, screen } from "@testing-library/react";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import { ToolCallBlock } from "@/ToolCallBlocks.js";
  6. import { PermissionDialog } from "@/PermissionDialog.js";
  7. import { useToolPresentationCapabilities } from "@/ToolCallBlocks/presentationCapabilities.js";
  8. const state = vi.hoisted(() => ({ plugin: false }));
  9. vi.mock("@/hooks/useToolPresentationHostCapabilities.js", () => ({
  10. useToolPresentationHostCapabilities: () => ({ resourceUrl: () => "host-capability" }),
  11. }));
  12. vi.mock("@/plugin-ui/index.js", () => ({
  13. usePluginUiToolBinding: () => undefined,
  14. shouldRenderPluginUi: () => state.plugin,
  15. PluginUiToolCallBlock: () => createElement("p", null, "plugin-surface"),
  16. }));
  17. vi.mock("@/ToolCallBlocks/resolveRenderer.js", () => ({
  18. resolveToolCallRenderer: () => () => createElement("p", null, "special-renderer"),
  19. }));
  20. vi.mock("@/ToolCallBlocks/renderers/fallback.js", () => ({
  21. FallbackToolCallBlock: () =>
  22. createElement(
  23. "p",
  24. null,
  25. useToolPresentationCapabilities().resourceUrl?.("preview") ?? "missing-capability",
  26. ),
  27. }));
  28. vi.mock("@/i18n/IntlProvider.js", () => ({
  29. useZCodeIntl: () => ({ intl: { formatMessage: ({ id }: { id: string }) => id } }),
  30. }));
  31. afterEach(() => {
  32. cleanup();
  33. state.plugin = false;
  34. });
  35. describe("App tool presentation boundaries", () => {
  36. it.each([true, false])("respects the host plugin eligibility decision: %s", (plugin) => {
  37. state.plugin = plugin;
  38. render(
  39. createElement(ToolCallBlock, {
  40. workspacePath: "/workspace",
  41. toolCallNode: {
  42. toolCall: { toolId: "t1", kind: "other", status: "completed" },
  43. childToolCalls: [],
  44. },
  45. }),
  46. );
  47. expect(screen.getByText(plugin ? "plugin-surface" : "special-renderer")).toBeTruthy();
  48. });
  49. it("injects presentation capabilities into direct permission renderers", () => {
  50. render(
  51. createElement(PermissionDialog, {
  52. request: {
  53. type: "permission_request",
  54. taskId: "task",
  55. traceId: "trace",
  56. requestId: "request",
  57. kind: "other",
  58. description: "Preview",
  59. options: [],
  60. },
  61. workspacePath: "/workspace",
  62. onRespond: () => {},
  63. }),
  64. );
  65. expect(screen.getByText("host-capability")).toBeTruthy();
  66. expect(screen.queryByText("plugin-surface")).toBeNull();
  67. });
  68. });