executeGroupToolCallBlock.test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { ToolPresentation } from "@/ToolCallBlocks/ToolPresentation.js";
  2. import { createElement } from "react";
  3. import { renderToStaticMarkup } from "react-dom/server";
  4. import { describe, expect, it } from "vitest";
  5. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  6. import { ExecuteGroupToolCallBlock } from "@/ToolCallBlocks/renderers/execute-group.js";
  7. function renderExecuteGroup(
  8. locale: "zh-CN" | "en-US",
  9. statuses: Array<"pending" | "completed" | "failed" | "stopped" | "in_progress">,
  10. options: { forceOpen?: boolean } = {},
  11. ) {
  12. return renderToStaticMarkup(
  13. createElement(
  14. ZCodeIntlProvider,
  15. { initialLocale: locale },
  16. createElement(ExecuteGroupToolCallBlock, {
  17. nestedToolRenderer: ToolPresentation,
  18. toolCallNode: {
  19. toolCall: {
  20. toolId: "execute:tool-1",
  21. kind: "Execute",
  22. title: "Execute",
  23. input: {},
  24. status:
  25. statuses.includes("in_progress") || statuses.includes("pending")
  26. ? "in_progress"
  27. : "completed",
  28. },
  29. childToolCalls: statuses.map((status, index) => ({
  30. toolCall: {
  31. toolId: `tool-${index + 1}`,
  32. kind: "execute",
  33. title: "Bash",
  34. input: { command: index === 0 ? "pnpm install" : "pnpm test" },
  35. output: status === "failed" ? "failed output" : "ok",
  36. status,
  37. },
  38. childToolCalls: [],
  39. })),
  40. },
  41. workspacePath: "/workspace",
  42. displayModel: {
  43. inlinePreview: { type: "none" },
  44. planResult: null,
  45. viewerSource: null,
  46. viewerLabelId: "codeViewer.viewCode",
  47. showSummaryFileLink: true,
  48. showInput: false,
  49. showOutput: true,
  50. showKind: false,
  51. },
  52. viewerSource: null,
  53. rawFileSummaries: [],
  54. isRunning: statuses.includes("in_progress") || statuses.includes("pending"),
  55. statusLabel:
  56. statuses.includes("in_progress") || statuses.includes("pending") ? "执行中" : "已完成",
  57. childToolList: null,
  58. forceOpen: options.forceOpen ?? false,
  59. canToggle: true,
  60. showIcon: true,
  61. }),
  62. ),
  63. );
  64. }
  65. describe("ExecuteGroupToolCallBlock", () => {
  66. it("shows the latest active command while running", () => {
  67. const html = renderExecuteGroup("zh-CN", ["completed", "in_progress"]);
  68. const enHtml = renderExecuteGroup("en-US", ["completed", "in_progress"]);
  69. expect(html).toContain(">终端</span>");
  70. expect(enHtml).toContain(">Terminal</span>");
  71. expect(html).toContain("正在执行");
  72. expect(html).toContain("pnpm test");
  73. expect(html).not.toContain("2 个命令");
  74. expect(html).toContain(">·</span>");
  75. expect(enHtml).toContain(">Running</span>");
  76. expect(enHtml).toContain(">pnpm test</code>");
  77. });
  78. it("shows only the command count in the expanded running summary", () => {
  79. const html = renderExecuteGroup("en-US", ["completed", "in_progress"], { forceOpen: true });
  80. const summaryEnd = html.indexOf('data-slot="collapsible-content"');
  81. const summary = html.slice(0, summaryEnd);
  82. expect(summary).toContain("2 commands");
  83. expect(summary).not.toContain("Running");
  84. expect(summary).not.toContain("pnpm test");
  85. });
  86. it("prefers an earlier pending command over a later completed command", () => {
  87. const html = renderExecuteGroup("en-US", ["pending", "completed"]);
  88. expect(html).toContain("pnpm install");
  89. expect(html).not.toContain("pnpm test");
  90. });
  91. it("summarizes command and failure counts after completion", () => {
  92. const zhHtml = renderExecuteGroup("zh-CN", ["completed", "failed"]);
  93. const enHtml = renderExecuteGroup("en-US", ["completed", "failed"]);
  94. expect(zhHtml).toContain("2 个命令");
  95. expect(zhHtml).toContain("1 个失败");
  96. expect(enHtml).toContain("2 commands");
  97. expect(enHtml).toContain("1 failed");
  98. expect(enHtml).toContain(">Terminal</span>");
  99. expect(enHtml).toContain(">·</span>");
  100. });
  101. });