toolLayout.test.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { createElement } 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 { ToolLayout } from "../src/ToolCallBlocks/ToolLayout.js";
  6. import { handleToolSummaryActionKeyDown } from "../src/ToolCallBlocks/ToolSummaryRow.js";
  7. describe("ToolLayout", () => {
  8. it("renders force-open details inside collapsible content", () => {
  9. const html = renderToStaticMarkup(
  10. createElement(
  11. ZCodeIntlProvider,
  12. { initialLocale: "zh-CN" },
  13. createElement(ToolLayout, {
  14. toolId: "tool-layout-spacing",
  15. icon: createElement("span", { "aria-hidden": true }, "icon"),
  16. forceOpen: true,
  17. kindLabel: "已探索",
  18. primaryText: "1 个搜索",
  19. renderContent: () => createElement("div", { className: "tool-detail" }, "detail"),
  20. }),
  21. ),
  22. );
  23. expect(html).toContain('data-testid="tool-summary-trigger-tool-layout-spacing"');
  24. expect(html).toMatch(/data-slot="collapsible-content"[^>]*>[\s\S]*?<div[^>]*>detail<\/div>/);
  25. });
  26. it("renders force-open content without a collapsible wrapper", () => {
  27. const html = renderToStaticMarkup(
  28. createElement(
  29. ZCodeIntlProvider,
  30. { initialLocale: "zh-CN" },
  31. createElement(ToolLayout, {
  32. toolId: "tool-layout-force-open-spacing",
  33. icon: createElement("span", { "aria-hidden": true }, "icon"),
  34. canToggle: false,
  35. forceOpen: true,
  36. kindLabel: "已探索",
  37. primaryText: "1 个搜索",
  38. renderContent: () => createElement("div", { className: "tool-detail" }, "detail"),
  39. }),
  40. ),
  41. );
  42. expect(html).not.toContain('data-slot="collapsible-content"');
  43. expect(html).toContain('<div class="tool-detail">detail</div>');
  44. });
  45. it("keeps running tool icons static while animating kind labels", () => {
  46. const html = renderToStaticMarkup(
  47. createElement(
  48. ZCodeIntlProvider,
  49. { initialLocale: "zh-CN" },
  50. createElement(ToolLayout, {
  51. toolId: "tool-layout-running-label",
  52. icon: createElement("span", { "aria-hidden": true }, "icon"),
  53. canToggle: false,
  54. isRunning: true,
  55. kindLabel: "读取",
  56. primaryText: "mod.rs",
  57. }),
  58. ),
  59. );
  60. expect(html).toContain("icon");
  61. expect(html).not.toContain("lucide-loader");
  62. expect(html).not.toContain("animate-spin");
  63. expect(html).toContain("读取");
  64. expect(html).toContain("animated-gradient-text");
  65. });
  66. it("does not reserve a summary content gap when an expanded tool has no summary", () => {
  67. const html = renderToStaticMarkup(
  68. createElement(
  69. ZCodeIntlProvider,
  70. { initialLocale: "zh-CN" },
  71. createElement(ToolLayout, {
  72. toolId: "tool-layout-empty-expanded-summary",
  73. icon: createElement("span", { "aria-hidden": true }, "icon"),
  74. forceOpen: true,
  75. kindLabel: "终端",
  76. primaryText: null,
  77. renderContent: () => createElement("div", null, "detail"),
  78. }),
  79. ),
  80. );
  81. expect(html).not.toContain("tool-summary-content");
  82. expect(html).toMatch(
  83. /tool-summary-kind-label[\s\S]*?终端<\/span><svg[^>]*lucide-chevron-right/,
  84. );
  85. });
  86. it("prioritizes edit file content when the conversation container narrows", () => {
  87. const html = renderToStaticMarkup(
  88. createElement(
  89. ZCodeIntlProvider,
  90. { initialLocale: "zh-CN" },
  91. createElement(ToolLayout, {
  92. toolId: "tool-layout-edit-file-priority",
  93. icon: createElement("span", { "aria-hidden": true }, "icon"),
  94. canToggle: false,
  95. kindLabel: "已编辑",
  96. primaryText: "long-file-name.tsx",
  97. prioritizePrimaryText: true,
  98. }),
  99. ),
  100. );
  101. expect(html).toMatch(
  102. /class="[^"]*tool-summary-kind-label[^"]*@max-\[360px\]\/conversation:hidden/,
  103. );
  104. expect(html).toMatch(/class="[^"]*tool-summary-content[^"]*flex-1[^"]*overflow-hidden/);
  105. });
  106. it("renders a summary action without collapsible semantics or inline detail", () => {
  107. const html = renderToStaticMarkup(
  108. createElement(
  109. ZCodeIntlProvider,
  110. { initialLocale: "zh-CN" },
  111. createElement(ToolLayout, {
  112. toolId: "tool-layout-summary-action",
  113. icon: createElement("span", { "aria-hidden": true }, "icon"),
  114. kindLabel: "子智能体",
  115. primaryText: "Explore workspace",
  116. summaryAction: {
  117. ariaLabel: "在右侧打开",
  118. onActivate: vi.fn(),
  119. testId: "subagent-summary-action",
  120. },
  121. renderContent: () => createElement("div", { className: "tool-detail" }, "detail"),
  122. }),
  123. ),
  124. );
  125. expect(html).toContain('data-testid="subagent-summary-action"');
  126. expect(html).toContain('role="button"');
  127. expect(html).toContain('aria-label="在右侧打开"');
  128. expect(html).not.toContain("aria-expanded");
  129. expect(html).not.toContain("lucide-chevron-right");
  130. expect(html).not.toContain("tool-detail");
  131. });
  132. it("activates summary actions from Enter and Space only", () => {
  133. const onActivate = vi.fn();
  134. const preventDefault = vi.fn();
  135. expect(handleToolSummaryActionKeyDown({ key: "Enter", preventDefault }, onActivate)).toBe(true);
  136. expect(handleToolSummaryActionKeyDown({ key: " ", preventDefault }, onActivate)).toBe(true);
  137. expect(handleToolSummaryActionKeyDown({ key: "Escape", preventDefault }, onActivate)).toBe(
  138. false,
  139. );
  140. expect(onActivate).toHaveBeenCalledTimes(2);
  141. expect(preventDefault).toHaveBeenCalledTimes(2);
  142. });
  143. });