toolSearchToolCallBlock.test.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it } from "vitest";
  4. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  5. import type { TaskChatToolCallTreeNode } from "@/lib/toolCallTree.js";
  6. import { resolveToolCallIdentity } from "@/lib/toolIdentity.js";
  7. import { ToolCallBlock } from "@/ToolCallBlocks.js";
  8. type ToolCall = TaskChatToolCallTreeNode["toolCall"];
  9. function renderTool(
  10. input: unknown,
  11. overrides: Partial<ToolCall> = {},
  12. locale: "zh-CN" | "en-US" = "zh-CN",
  13. ) {
  14. return renderToStaticMarkup(
  15. createElement(
  16. ZCodeIntlProvider,
  17. { initialLocale: locale },
  18. createElement(ToolCallBlock, {
  19. toolCallNode: {
  20. toolCall: {
  21. toolId: "search-tools",
  22. toolName: "ToolSearch",
  23. kind: "ToolSearch",
  24. title: "ToolSearch",
  25. input,
  26. output: "RESULT_MUST_NOT_RENDER",
  27. raw: { rawOutput: "RAW_RESULT_MUST_NOT_RENDER" },
  28. status: "completed",
  29. ...overrides,
  30. },
  31. childToolCalls: [],
  32. },
  33. workspacePath: "/workspace",
  34. }),
  35. ),
  36. );
  37. }
  38. describe("ToolSearch summary card", () => {
  39. it.each([
  40. [{ query: "select:TodoWrite" }, "搜索工具:TodoWrite"],
  41. [{ query: "select:Read, TodoWrite,WebFetch" }, "搜索工具:Read, TodoWrite, WebFetch"],
  42. [{ query: "+docs search web" }, "搜索工具:+docs search web"],
  43. [{ query: "" }, "搜索工具"],
  44. [undefined, "搜索工具"],
  45. ])("renders query %j without an expandable result", (input, label) => {
  46. const html = renderTool(input);
  47. expect(html).toContain(label);
  48. expect(html).not.toContain("select:");
  49. expect(html).not.toContain("RESULT_MUST_NOT_RENDER");
  50. expect(html).not.toContain("aria-expanded=");
  51. expect(html).not.toContain("lucide-chevron-right");
  52. });
  53. it("uses English labels and supports kind-only history", () => {
  54. expect(renderTool({ query: "select:TodoWrite" }, { toolName: undefined }, "en-US")).toContain(
  55. "Search tools: TodoWrite",
  56. );
  57. });
  58. it.each(["in_progress", "failed", "stopped"] as const)(
  59. "keeps %s calls non-expandable without hiding failure status",
  60. (status) => {
  61. const html = renderTool({ query: "TodoWrite" }, { status, error: "SEARCH_FAILED" });
  62. expect(html).toContain("搜索工具:TodoWrite");
  63. expect(html).not.toContain("aria-expanded=");
  64. expect(html).not.toContain("RESULT_MUST_NOT_RENDER");
  65. if (status === "failed") expect(html).toContain("失败");
  66. },
  67. );
  68. it("does not change WebSearch or classify ToolSearch as an Explore search", () => {
  69. const html = renderTool(
  70. { query: "TypeScript docs" },
  71. { toolName: "WebSearch", kind: "WebSearch" },
  72. );
  73. expect(html).toContain("查找 TypeScript docs");
  74. expect(html).not.toContain("搜索工具");
  75. expect(resolveToolCallIdentity({ toolName: "ToolSearch" }).family).toBe("unknown");
  76. });
  77. it("does not route other tools by a misleading title", () => {
  78. expect(
  79. renderTool({ query: "TodoWrite" }, { toolName: "OtherTool", kind: "OtherTool" }),
  80. ).not.toContain("搜索工具");
  81. });
  82. });