toolCallBodyFileLinks.test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { createElement, type ReactNode } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it, vi } from "vitest";
  4. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  5. import { ToolCallBody } from "@/ToolCallBlocks/ToolCallBody.js";
  6. import { ToolPresentationCapabilitiesProvider } from "@/ToolCallBlocks/presentationCapabilities.js";
  7. import { buildToolDisplayModel } from "@/lib/toolDisplay.js";
  8. import type { ToolDisplayModel } from "@/lib/toolDisplay.js";
  9. const messageResponseProps = vi.hoisted(() => [] as Array<Record<string, unknown>>);
  10. vi.mock("@/components/ai-elements/message.js", async () => {
  11. const React = await import("react");
  12. return {
  13. MessageResponse: ({ children, ...props }: { children?: ReactNode }) => {
  14. messageResponseProps.push(props);
  15. return React.createElement("div", null, children);
  16. },
  17. };
  18. });
  19. describe("ToolCallBody markdown file links", () => {
  20. it("passes the file-link opener to tool markdown before falling back to code viewer", () => {
  21. const onOpenCodeViewer = vi.fn();
  22. const onOpenFileLink = vi.fn();
  23. const displayModel: ToolDisplayModel = {
  24. inlinePreview: {
  25. type: "text",
  26. source: {
  27. type: "text",
  28. title: "Tool markdown",
  29. content: "See [demo.ts](./src/demo.ts)",
  30. language: "markdown",
  31. },
  32. },
  33. planResult: null,
  34. viewerSource: null,
  35. viewerLabelId: "codeViewer.viewCode",
  36. showSummaryFileLink: false,
  37. showInput: false,
  38. showOutput: false,
  39. showKind: false,
  40. };
  41. renderToStaticMarkup(
  42. createElement(
  43. ZCodeIntlProvider,
  44. { initialLocale: "zh-CN" },
  45. createElement(ToolCallBody, {
  46. childToolList: null,
  47. displayModel,
  48. toolCall: {
  49. toolId: "tool-markdown-link",
  50. kind: "Read",
  51. status: "completed",
  52. },
  53. workspacePath: "/workspace",
  54. onOpenCodeViewer,
  55. onOpenFileLink,
  56. }),
  57. ),
  58. );
  59. expect(messageResponseProps).toHaveLength(1);
  60. expect(messageResponseProps[0]?.onOpenCodeViewer).toBe(onOpenCodeViewer);
  61. expect(messageResponseProps[0]?.onOpenFileLink).toBe(onOpenFileLink);
  62. });
  63. });
  64. it.each(["zh-CN", "en-US"] as const)("无宿主图片能力时 %s 展示静态不可用状态", (locale) => {
  65. const toolCall = {
  66. toolId: "read-image",
  67. kind: "Read",
  68. toolName: "Read",
  69. input: { file_path: "/workspace/chart.png" },
  70. status: "completed" as const,
  71. };
  72. const displayModel = buildToolDisplayModel(toolCall, "/workspace");
  73. expect(displayModel.inlinePreview.type).toBe("image");
  74. const html = renderToStaticMarkup(
  75. createElement(
  76. ZCodeIntlProvider,
  77. { initialLocale: locale },
  78. createElement(
  79. ToolPresentationCapabilitiesProvider,
  80. { value: { readonly: true, resourceUrl: () => undefined } },
  81. createElement(ToolCallBody, {
  82. childToolList: null,
  83. displayModel,
  84. toolCall,
  85. workspacePath: "",
  86. }),
  87. ),
  88. ),
  89. );
  90. expect(html).toContain(locale === "zh-CN" ? "预览不可用" : "Preview unavailable");
  91. expect(html).not.toMatch(/Loading|正在加载/);
  92. });
  93. it("公开图片资源命中时直接渲染图片", () => {
  94. const toolCall = {
  95. toolId: "read-image",
  96. kind: "Read",
  97. toolName: "Read",
  98. input: { file_path: "/workspace/chart.png" },
  99. status: "completed" as const,
  100. };
  101. const html = renderToStaticMarkup(
  102. createElement(
  103. ZCodeIntlProvider,
  104. { initialLocale: "en-US" },
  105. createElement(
  106. ToolPresentationCapabilitiesProvider,
  107. { value: { readonly: true, resourceUrl: () => "https://files.example.com/chart.png" } },
  108. createElement(ToolCallBody, {
  109. childToolList: null,
  110. displayModel: buildToolDisplayModel(toolCall, "/workspace"),
  111. toolCall,
  112. workspacePath: "",
  113. }),
  114. ),
  115. ),
  116. );
  117. expect(html).toContain('src="https://files.example.com/chart.png"');
  118. expect(html).not.toContain("Preview unavailable");
  119. });