executeGroupToolCallBlock.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 { ExecuteGroupToolCallBlock } from "@/ToolCallBlocks/renderers/execute-group.js";
  6. function renderExecuteGroup(
  7. locale: "zh-CN" | "en-US",
  8. statuses: Array<"pending" | "completed" | "failed" | "stopped" | "in_progress">,
  9. options: { forceOpen?: boolean } = {},
  10. ) {
  11. return renderToStaticMarkup(
  12. createElement(
  13. ZCodeIntlProvider,
  14. { initialLocale: locale },
  15. createElement(ExecuteGroupToolCallBlock, {
  16. toolCallNode: {
  17. toolCall: {
  18. toolId: "execute:tool-1",
  19. kind: "Execute",
  20. title: "Execute",
  21. input: {},
  22. status:
  23. statuses.includes("in_progress") || statuses.includes("pending")
  24. ? "in_progress"
  25. : "completed",
  26. },
  27. childToolCalls: statuses.map((status, index) => ({
  28. toolCall: {
  29. toolId: `tool-${index + 1}`,
  30. kind: "execute",
  31. title: "Bash",
  32. input: { command: index === 0 ? "pnpm install" : "pnpm test" },
  33. output: status === "failed" ? "failed output" : "ok",
  34. status,
  35. },
  36. childToolCalls: [],
  37. })),
  38. },
  39. workspacePath: "/workspace",
  40. displayModel: {
  41. inlinePreview: { type: "none" },
  42. planResult: null,
  43. viewerSource: null,
  44. viewerLabelId: "codeViewer.viewCode",
  45. showSummaryFileLink: true,
  46. showInput: false,
  47. showOutput: true,
  48. showKind: false,
  49. },
  50. viewerSource: null,
  51. rawFileSummaries: [],
  52. isRunning: statuses.includes("in_progress") || statuses.includes("pending"),
  53. statusLabel:
  54. statuses.includes("in_progress") || statuses.includes("pending")
  55. ? "执行中"
  56. : "已完成",
  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(
  76. '<span class="shrink-0 text-foreground-subtle">Running</span>',
  77. );
  78. expect(enHtml).toContain(
  79. '<code class="min-w-0 truncate font-sans">pnpm test</code>',
  80. );
  81. expect(html).toContain(
  82. "relative inline-flex min-w-0 items-center gap-2 overflow-hidden",
  83. );
  84. });
  85. it("shows only the command count in the expanded running summary", () => {
  86. const html = renderExecuteGroup(
  87. "en-US",
  88. ["completed", "in_progress"],
  89. { forceOpen: true },
  90. );
  91. const summaryEnd = html.indexOf('data-slot="collapsible-content"');
  92. const summary = html.slice(0, summaryEnd);
  93. expect(summary).toContain("2 commands");
  94. expect(summary).not.toContain("Running");
  95. expect(summary).not.toContain("pnpm test");
  96. });
  97. it("prefers an earlier pending command over a later completed command", () => {
  98. const html = renderExecuteGroup("en-US", ["pending", "completed"]);
  99. expect(html).toContain("pnpm install");
  100. expect(html).not.toContain("pnpm test");
  101. });
  102. it("summarizes command and failure counts after completion", () => {
  103. const zhHtml = renderExecuteGroup("zh-CN", ["completed", "failed"]);
  104. const enHtml = renderExecuteGroup("en-US", ["completed", "failed"]);
  105. expect(zhHtml).toContain("2 个命令");
  106. expect(zhHtml).toContain("1 个失败");
  107. expect(enHtml).toContain("2 commands");
  108. expect(enHtml).toContain("1 failed");
  109. expect(enHtml).toContain(">Terminal</span>");
  110. expect(enHtml).toContain(">·</span>");
  111. expect(enHtml).not.toContain(
  112. "relative inline-flex min-w-0 items-center overflow-hidden",
  113. );
  114. });
  115. });