changesGroupToolCallBlock.test.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 {
  6. ChangesGroupToolCallBlock,
  7. resolveFileChipAvailableWidth,
  8. resolveResponsiveFileChipCount,
  9. } from "@/ToolCallBlocks/renderers/changes-group.js";
  10. function renderChangesGroup(options: {
  11. running: boolean;
  12. forceOpen?: boolean;
  13. overflow?: boolean;
  14. multiFile?: boolean;
  15. singleFile?: boolean;
  16. unresolvedCount?: number;
  17. }) {
  18. const children = options.unresolvedCount
  19. ? Array.from({ length: options.unresolvedCount }, (_, index) => ({
  20. toolId: `write-unresolved-${index}`,
  21. toolName: "Write",
  22. kind: "Write",
  23. title: "Write",
  24. input: { content: `content-${index}` },
  25. status: "pending",
  26. raw: { v4Status: "inputStreaming", inputPreviewComplete: false },
  27. }))
  28. : [
  29. {
  30. toolId: "write-1",
  31. toolName: "Write",
  32. kind: "Write",
  33. title: "Write",
  34. input: { file_path: "/workspace/src/a.ts", content: "a" },
  35. status: "completed",
  36. },
  37. ...(!options.singleFile ? [{
  38. toolId: "edit-2",
  39. toolName: "Edit",
  40. kind: "Edit",
  41. title: "Edit",
  42. input: {
  43. file_path: "/workspace/src/b.css",
  44. old_string: "b",
  45. new_string: "c",
  46. },
  47. status: options.running ? "in_progress" : "completed",
  48. }] : []),
  49. {
  50. toolId: "edit-3",
  51. toolName: "Edit",
  52. kind: "Edit",
  53. title: "Edit",
  54. input: {
  55. file_path: "src/a.ts",
  56. old_string: "a",
  57. new_string: "aa",
  58. },
  59. raw: {
  60. display: {
  61. kind: "file_diff",
  62. filePath: "/workspace/src/a.ts",
  63. additions: 2,
  64. deletions: 1,
  65. },
  66. },
  67. status: "completed",
  68. },
  69. ...(options.multiFile
  70. ? [
  71. {
  72. toolId: "edit-multi",
  73. toolName: "Edit",
  74. kind: "Edit",
  75. title: "Edit",
  76. input: {},
  77. raw: {
  78. changes: {
  79. "/workspace/src/a.ts": { type: "update" },
  80. "/workspace/src/e.tsx": { type: "update" },
  81. },
  82. },
  83. status: "completed",
  84. },
  85. ]
  86. : []),
  87. ];
  88. if (options.overflow) {
  89. children.push(
  90. {
  91. toolId: "write-4",
  92. toolName: "Write",
  93. kind: "Write",
  94. title: "Write",
  95. input: { file_path: "/workspace/src/c.json", content: "c" },
  96. status: "completed",
  97. },
  98. {
  99. toolId: "write-5",
  100. toolName: "Write",
  101. kind: "Write",
  102. title: "Write",
  103. input: { file_path: "/workspace/src/d.md", content: "d" },
  104. status: "completed",
  105. },
  106. );
  107. }
  108. return renderToStaticMarkup(
  109. createElement(
  110. ZCodeIntlProvider,
  111. { initialLocale: "en-US" },
  112. createElement(ChangesGroupToolCallBlock, {
  113. toolCallNode: {
  114. toolCall: {
  115. toolId: "changes:1",
  116. toolName: "ChangesGroup",
  117. kind: "changesGroup",
  118. title: "Changes",
  119. input: {},
  120. status: options.running ? "in_progress" : "completed",
  121. },
  122. childToolCalls: children.map((toolCall) => ({
  123. toolCall,
  124. childToolCalls: [],
  125. })),
  126. },
  127. workspacePath: "/workspace",
  128. displayModel: {
  129. inlinePreview: { type: "none" },
  130. planResult: null,
  131. viewerSource: null,
  132. viewerLabelId: "codeViewer.viewCode",
  133. showSummaryFileLink: true,
  134. showInput: false,
  135. showOutput: true,
  136. showKind: false,
  137. },
  138. viewerSource: null,
  139. rawFileSummaries: [],
  140. isRunning: options.running,
  141. statusLabel: options.running ? "Running" : "Completed",
  142. childToolList: null,
  143. forceOpen: options.forceOpen ?? false,
  144. canToggle: true,
  145. showIcon: true,
  146. onOpenCodeViewer: () => undefined,
  147. }),
  148. ),
  149. );
  150. }
  151. function parentSummary(html: string) {
  152. return html.slice(0, html.indexOf('data-slot="collapsible-content"'));
  153. }
  154. describe("ChangesGroupToolCallBlock", () => {
  155. it("uses a pencil icon for the Changes group", () => {
  156. const html = renderChangesGroup({ running: false });
  157. expect(html).toContain("lucide-pencil");
  158. expect(html).not.toContain("lucide-files");
  159. });
  160. it("rolls the latest editing file while running and collapsed", () => {
  161. const summary = parentSummary(renderChangesGroup({ running: true }));
  162. expect(summary).toContain("Editing");
  163. expect(summary).toContain("a.ts");
  164. expect(summary).toContain(">src</span>");
  165. expect(summary).toContain("2 files");
  166. expect(summary).toContain('aria-label="+2"');
  167. expect(summary).toContain('aria-label="-1"');
  168. expect(summary).toContain('data-animate-initial="true"');
  169. expect(summary.indexOf("2 files")).toBeLessThan(summary.indexOf("Editing"));
  170. expect(summary).toContain(
  171. "relative inline-flex min-w-0 items-center gap-2 overflow-hidden",
  172. );
  173. });
  174. it("keeps initial diff animation enabled for expanded child edits", () => {
  175. const html = renderChangesGroup({ running: true, forceOpen: true });
  176. const content = html.slice(html.indexOf('data-slot="collapsible-content"'));
  177. expect(content).toContain('aria-label="+2"');
  178. expect(content).toContain('data-animate-initial="true"');
  179. });
  180. it("uses the latest child operation when rolling a Write", () => {
  181. const summary = parentSummary(
  182. renderChangesGroup({ running: true, overflow: true }),
  183. );
  184. expect(summary).toContain("Writing");
  185. expect(summary).toContain("d.md");
  186. expect(summary).not.toContain("Editing");
  187. });
  188. it("shows the tool count before paths arrive", () => {
  189. const summary = parentSummary(
  190. renderChangesGroup({ running: true, unresolvedCount: 3 }),
  191. );
  192. expect(summary).toContain("3 tools");
  193. expect(summary).not.toContain("0 files");
  194. expect(summary).not.toContain("Writing");
  195. });
  196. it("keeps unresolved child actions visible while expanded", () => {
  197. const html = renderChangesGroup({
  198. running: true,
  199. forceOpen: true,
  200. unresolvedCount: 3,
  201. });
  202. const content = html.slice(html.indexOf('data-slot="collapsible-content"'));
  203. expect(content.match(/Writing/gu)).toHaveLength(3);
  204. });
  205. it.each([true, false])(
  206. "shows only the unique file count while expanded (running=%s)",
  207. (running) => {
  208. const summary = parentSummary(
  209. renderChangesGroup({ running, forceOpen: true }),
  210. );
  211. expect(summary).toContain("2 files");
  212. expect(summary).not.toContain("Editing");
  213. expect(summary).not.toContain("a.ts");
  214. },
  215. );
  216. it("shows count and deduplicated file chips when completed and collapsed", () => {
  217. const summary = parentSummary(renderChangesGroup({ running: false }));
  218. expect(summary).toContain("2 files");
  219. expect(summary.match(/>a\.ts<\/span>/gu)).toHaveLength(1);
  220. expect(summary.match(/>b\.css<\/span>/gu)).toHaveLength(1);
  221. expect(summary).toContain("<button");
  222. });
  223. it("shows the file directly without a one-file count when completed and collapsed", () => {
  224. const summary = parentSummary(
  225. renderChangesGroup({ running: false, singleFile: true }),
  226. );
  227. expect(summary).toContain(">a.ts</span>");
  228. expect(summary).not.toContain("1 file");
  229. });
  230. it("renders every completed file before client width measurement", () => {
  231. const summary = parentSummary(
  232. renderChangesGroup({ running: false, overflow: true }),
  233. );
  234. expect(summary).toContain("4 files");
  235. expect(summary).not.toContain("+1");
  236. expect(summary).toContain(">d.md</span>");
  237. });
  238. it("flattens a multi-file Edit before deduplicating across children", () => {
  239. const summary = parentSummary(
  240. renderChangesGroup({ running: false, multiFile: true }),
  241. );
  242. expect(summary).toContain("3 files");
  243. expect(summary.match(/>a\.ts<\/span>/gu)).toHaveLength(1);
  244. expect(summary.match(/>e\.tsx<\/span>/gu)).toHaveLength(1);
  245. });
  246. });
  247. describe("resolveFileChipAvailableWidth", () => {
  248. it("uses the ToolCall boundary instead of the shrink-to-content list width", () => {
  249. expect(
  250. resolveFileChipAvailableWidth({
  251. boundaryRight: 800,
  252. listLeft: 300,
  253. trailingWidth: 24,
  254. }),
  255. ).toBe(476);
  256. });
  257. it("never returns a negative width", () => {
  258. expect(
  259. resolveFileChipAvailableWidth({
  260. boundaryRight: 300,
  261. listLeft: 300,
  262. trailingWidth: 24,
  263. }),
  264. ).toBe(0);
  265. });
  266. });
  267. describe("resolveResponsiveFileChipCount", () => {
  268. it("shows no file chips when the composed available width is zero", () => {
  269. const availableWidth = resolveFileChipAvailableWidth({
  270. boundaryRight: 324,
  271. listLeft: 300,
  272. trailingWidth: 24,
  273. });
  274. expect(availableWidth).toBe(0);
  275. expect(
  276. resolveResponsiveFileChipCount({
  277. availableWidth,
  278. chipWidths: [50, 50],
  279. overflowWidth: 24,
  280. gap: 8,
  281. }),
  282. ).toBe(0);
  283. });
  284. it("keeps every chip when the available width is sufficient", () => {
  285. expect(
  286. resolveResponsiveFileChipCount({
  287. availableWidth: 500,
  288. chipWidths: [50, 50, 50, 50],
  289. overflowWidth: 24,
  290. gap: 8,
  291. }),
  292. ).toBe(4);
  293. });
  294. it("reserves room for +N and keeps as many leading chips as fit", () => {
  295. expect(
  296. resolveResponsiveFileChipCount({
  297. availableWidth: 150,
  298. chipWidths: [50, 50, 50, 50],
  299. overflowWidth: 24,
  300. gap: 8,
  301. }),
  302. ).toBe(2);
  303. });
  304. it("shows only +N when no file chip fits", () => {
  305. expect(
  306. resolveResponsiveFileChipCount({
  307. availableWidth: 20,
  308. chipWidths: [50, 50],
  309. overflowWidth: 24,
  310. gap: 8,
  311. }),
  312. ).toBe(0);
  313. });
  314. });