toolLayout.test.ts 6.2 KB

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