changesGroupToolCallBlock.test.ts 9.5 KB

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