toolDisplay.test.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. import { describe, expect, it } from "vitest";
  2. import { buildToolDisplayModel } from "../src/lib/toolDisplay.js";
  3. describe("toolDisplay helpers", () => {
  4. it("renders read tools as inline text previews", () => {
  5. const displayModel = buildToolDisplayModel(
  6. {
  7. toolId: "tool-read",
  8. kind: "read",
  9. title: "读取 src/app.ts",
  10. input: {
  11. path: "src/app.ts",
  12. },
  13. output: {
  14. content: "export const value = 1;\n",
  15. },
  16. status: "completed",
  17. },
  18. "/workspace",
  19. );
  20. expect(displayModel.inlinePreview.type).toBe("text");
  21. expect(displayModel.showSummaryFileLink).toBe(false);
  22. expect(displayModel.showInput).toBe(false);
  23. expect(displayModel.showOutput).toBe(false);
  24. expect(displayModel.showKind).toBe(false);
  25. });
  26. it("renders replace-like tools as inline patches", () => {
  27. const displayModel = buildToolDisplayModel(
  28. {
  29. toolId: "tool-replace",
  30. kind: "replace",
  31. title: "替换 src/app.ts",
  32. input: {
  33. path: "src/app.ts",
  34. old_string: "const a = 1;",
  35. new_string: "const a = 2;",
  36. },
  37. status: "completed",
  38. },
  39. "/workspace",
  40. );
  41. expect(displayModel.inlinePreview.type).toBe("patch");
  42. expect(displayModel.viewerLabelId).toBe("codeViewer.viewDiff");
  43. expect(displayModel.showSummaryFileLink).toBe(true);
  44. expect(displayModel.showInput).toBe(false);
  45. expect(displayModel.showOutput).toBe(false);
  46. });
  47. it("hides successful write tool outputs while showing inline code", () => {
  48. const displayModel = buildToolDisplayModel(
  49. {
  50. toolId: "tool-write",
  51. kind: "write",
  52. title: "写入 src/app.ts",
  53. input: {
  54. path: "src/app.ts",
  55. content: "export const value = 1;\n",
  56. },
  57. output: {
  58. result: "文件已成功写入",
  59. },
  60. status: "completed",
  61. },
  62. "/workspace",
  63. );
  64. expect(displayModel.inlinePreview.type).toBe("text");
  65. expect(displayModel.showInput).toBe(false);
  66. expect(displayModel.showOutput).toBe(false);
  67. expect(displayModel.showKind).toBe(false);
  68. });
  69. it("renders image tools as inline image previews", () => {
  70. const displayModel = buildToolDisplayModel(
  71. {
  72. toolId: "tool-image",
  73. kind: "read",
  74. title: "查看 logo.png",
  75. input: {
  76. path: "assets/logo.png",
  77. },
  78. status: "completed",
  79. },
  80. "/workspace",
  81. );
  82. expect(displayModel.inlinePreview.type).toBe("image");
  83. expect(displayModel.showInput).toBe(false);
  84. expect(displayModel.showOutput).toBe(false);
  85. expect(displayModel.viewerSource?.type).toBe("image");
  86. });
  87. it("renders execute tools showing only output, hiding input and kind", () => {
  88. const displayModel = buildToolDisplayModel(
  89. {
  90. toolId: "tool-exec",
  91. kind: "execute",
  92. title: "运行 npm test",
  93. input: {
  94. command: "npm test",
  95. },
  96. output: "All tests passed.\n",
  97. status: "completed",
  98. },
  99. "/workspace",
  100. );
  101. expect(displayModel.inlinePreview.type).toBe("none");
  102. expect(displayModel.showInput).toBe(false);
  103. expect(displayModel.showOutput).toBe(true);
  104. expect(displayModel.showKind).toBe(false);
  105. });
  106. it("renders execute tools with no output as collapsed", () => {
  107. const displayModel = buildToolDisplayModel(
  108. {
  109. toolId: "tool-exec-2",
  110. kind: "execute",
  111. title: "运行 ls",
  112. input: {
  113. command: "ls",
  114. },
  115. status: "in_progress",
  116. },
  117. "/workspace",
  118. );
  119. expect(displayModel.showInput).toBe(false);
  120. expect(displayModel.showOutput).toBe(false);
  121. expect(displayModel.showKind).toBe(false);
  122. });
  123. it("leaves node repl input and output to the dedicated renderer", () => {
  124. const displayModel = buildToolDisplayModel(
  125. {
  126. toolId: "tool-node-repl-display",
  127. toolName: "js",
  128. kind: "js",
  129. title: "js",
  130. input: {
  131. code: "return 1;",
  132. title: "读取当前状态",
  133. },
  134. output: {
  135. type: "text",
  136. value: "1",
  137. },
  138. status: "completed",
  139. },
  140. "/workspace",
  141. );
  142. expect(displayModel.inlinePreview.type).toBe("none");
  143. expect(displayModel.showSummaryFileLink).toBe(false);
  144. expect(displayModel.showInput).toBe(false);
  145. expect(displayModel.showOutput).toBe(false);
  146. expect(displayModel.showKind).toBe(false);
  147. });
  148. it("renders search tools showing only result payload", () => {
  149. const displayModel = buildToolDisplayModel(
  150. {
  151. toolId: "tool-search",
  152. kind: "search",
  153. title: "搜索 src 下的 TODO",
  154. input: {
  155. query: "TODO",
  156. path: "src",
  157. },
  158. output: {
  159. matches: [
  160. {
  161. path: "src/app.ts",
  162. line: 12,
  163. },
  164. ],
  165. },
  166. status: "completed",
  167. },
  168. "/workspace",
  169. );
  170. expect(displayModel.inlinePreview.type).toBe("none");
  171. expect(displayModel.showSummaryFileLink).toBe(false);
  172. expect(displayModel.showInput).toBe(false);
  173. expect(displayModel.showOutput).toBe(true);
  174. expect(displayModel.showKind).toBe(false);
  175. });
  176. it("keeps search tools collapsed while running", () => {
  177. const displayModel = buildToolDisplayModel(
  178. {
  179. toolId: "tool-search-running",
  180. kind: "grep",
  181. title: "grep TODO",
  182. input: {
  183. pattern: "TODO",
  184. path: "src",
  185. },
  186. status: "in_progress",
  187. },
  188. "/workspace",
  189. );
  190. expect(displayModel.showInput).toBe(false);
  191. expect(displayModel.showOutput).toBe(false);
  192. expect(displayModel.showKind).toBe(false);
  193. });
  194. it("keeps search tool errors visible without showing parameters", () => {
  195. const displayModel = buildToolDisplayModel(
  196. {
  197. toolId: "tool-search-error",
  198. kind: "fetch",
  199. title: "Fetch docs",
  200. input: {
  201. url: "https://example.com",
  202. },
  203. error: "request failed",
  204. status: "failed",
  205. },
  206. "/workspace",
  207. );
  208. expect(displayModel.showInput).toBe(false);
  209. expect(displayModel.showOutput).toBe(true);
  210. expect(displayModel.showKind).toBe(false);
  211. });
  212. it("renders goal tools showing only result payloads", () => {
  213. const displayModel = buildToolDisplayModel(
  214. {
  215. toolId: "tool-goal-update",
  216. toolName: "GoalUpdate",
  217. kind: "GoalUpdate",
  218. title: "GoalUpdate",
  219. input: {
  220. completionEvidence: "测试已通过,文档已更新。",
  221. status: "complete",
  222. },
  223. output: {
  224. goal: {
  225. status: "complete",
  226. },
  227. },
  228. status: "completed",
  229. },
  230. "/workspace",
  231. );
  232. expect(displayModel.inlinePreview.type).toBe("none");
  233. expect(displayModel.showSummaryFileLink).toBe(false);
  234. expect(displayModel.showInput).toBe(false);
  235. expect(displayModel.showOutput).toBe(true);
  236. expect(displayModel.showKind).toBe(false);
  237. });
  238. it("hides failed edit parameters and surfaces nested error payloads", () => {
  239. const displayModel = buildToolDisplayModel(
  240. {
  241. toolId: "tool-edit-error",
  242. kind: "edit",
  243. title: "edit src/App.tsx",
  244. input: {
  245. filePath: "/workspace/src/App.tsx",
  246. oldString: "const a = 1;\n",
  247. newString: "const a = 1;\n",
  248. },
  249. output: {
  250. error: "No changes to apply: oldString and newString are identical.",
  251. },
  252. status: "failed",
  253. },
  254. "/workspace",
  255. );
  256. expect(displayModel.inlinePreview.type).toBe("none");
  257. expect(displayModel.showInput).toBe(false);
  258. expect(displayModel.showOutput).toBe(true);
  259. expect(displayModel.showKind).toBe(false);
  260. });
  261. it("renders plan results from tool output and hides generic payload blocks", () => {
  262. const displayModel = buildToolDisplayModel(
  263. {
  264. toolId: "tool-plan-output",
  265. kind: "switch_mode",
  266. title: "Exited Plan Mode",
  267. input: {
  268. plan: "# 输入里的旧计划",
  269. },
  270. output: {
  271. allowedPrompts: [
  272. {
  273. prompt: "run python3 snake.py to test the game",
  274. tool: "Bash",
  275. },
  276. ],
  277. plan: "# 输出计划\n\n- 渲染 markdown",
  278. planFilePath: "plans/snake.md",
  279. },
  280. status: "completed",
  281. },
  282. "/workspace",
  283. );
  284. expect(displayModel.planResult).toEqual({
  285. plan: "# 输出计划\n\n- 渲染 markdown",
  286. planFilePath: "/workspace/plans/snake.md",
  287. });
  288. expect(displayModel.showInput).toBe(false);
  289. expect(displayModel.showOutput).toBe(false);
  290. expect(displayModel.showKind).toBe(true);
  291. });
  292. it("does not treat plan-like input as tool result", () => {
  293. const displayModel = buildToolDisplayModel(
  294. {
  295. toolId: "tool-plan-input-only",
  296. kind: "switch_mode",
  297. title: "EnterPlanMode",
  298. input: {
  299. plan: "# 只存在于输入中的计划",
  300. },
  301. status: "completed",
  302. },
  303. "/workspace",
  304. );
  305. expect(displayModel.planResult).toBeNull();
  306. expect(displayModel.showInput).toBe(true);
  307. expect(displayModel.showOutput).toBe(false);
  308. expect(displayModel.showKind).toBe(true);
  309. });
  310. });