treemappingActivity.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. import { describe, expect, it } from "vitest";
  2. import { buildTreemappingActivityModel } from "../src/lib/treemappingActivity.js";
  3. import type { TaskChatMessage, TaskChatToolCall } from "../src/lib/taskChatMessageTypes.js";
  4. function message(toolCalls: TaskChatToolCall[]): TaskChatMessage {
  5. return {
  6. id: "assistant-1",
  7. role: "assistant",
  8. content: "",
  9. timestamp: 1,
  10. toolCalls,
  11. };
  12. }
  13. function toolCall(overrides: Partial<TaskChatToolCall>): TaskChatToolCall {
  14. return {
  15. toolId: overrides.toolId ?? "tool-1",
  16. kind: overrides.kind ?? "read",
  17. title: overrides.title,
  18. input: overrides.input ?? {},
  19. output: overrides.output,
  20. raw: overrides.raw,
  21. status: overrides.status ?? "completed",
  22. startedAt: overrides.startedAt ?? 10,
  23. };
  24. }
  25. describe("buildTreemappingActivityModel", () => {
  26. it("derives read, search, edit, write and delete file activity", () => {
  27. const model = buildTreemappingActivityModel(
  28. message([
  29. toolCall({
  30. toolId: "read-1",
  31. kind: "read",
  32. input: { file_path: "/workspace/src/app.ts" },
  33. }),
  34. toolCall({
  35. toolId: "search-1",
  36. kind: "search",
  37. output: { files: ["src/app.ts", "src/util.ts"] },
  38. }),
  39. toolCall({
  40. toolId: "edit-1",
  41. kind: "edit",
  42. raw: {
  43. changes: {
  44. "src/app.ts": {
  45. type: "update",
  46. oldText: "one\n",
  47. newText: "one\ntwo\n",
  48. },
  49. },
  50. },
  51. }),
  52. toolCall({
  53. toolId: "write-1",
  54. kind: "write",
  55. input: { file_path: "src/new.ts", content: "hello\nworld\n" },
  56. }),
  57. toolCall({
  58. toolId: "delete-1",
  59. kind: "delete",
  60. input: { file_path: "src/old.ts", content: "bye\n" },
  61. }),
  62. ]),
  63. "/workspace",
  64. );
  65. const app = model.files.find((file) => file.path === "src/app.ts");
  66. const util = model.files.find((file) => file.path === "src/util.ts");
  67. const written = model.files.find((file) => file.path === "src/new.ts");
  68. const deleted = model.files.find((file) => file.path === "src/old.ts");
  69. expect(app).toMatchObject({ kind: "modified", views: 2, added: 1, removed: 0 });
  70. expect(util).toMatchObject({ kind: "viewed", views: 1 });
  71. expect(written).toMatchObject({ kind: "written", added: 2 });
  72. expect(deleted).toMatchObject({ kind: "deleted", removed: 1 });
  73. });
  74. it("keeps write priority when a file is written then modified", () => {
  75. const model = buildTreemappingActivityModel(
  76. message([
  77. toolCall({
  78. toolId: "write-1",
  79. kind: "write",
  80. input: { file_path: "src/new.ts", content: "hello\n" },
  81. }),
  82. toolCall({
  83. toolId: "edit-1",
  84. kind: "edit",
  85. raw: {
  86. changes: {
  87. "src/new.ts": {
  88. type: "update",
  89. oldText: "hello\n",
  90. newText: "hello\nworld\n",
  91. },
  92. },
  93. },
  94. }),
  95. ]),
  96. "/workspace",
  97. );
  98. expect(model.files[0]).toMatchObject({
  99. path: "src/new.ts",
  100. kind: "written",
  101. added: 2,
  102. removed: 0,
  103. });
  104. });
  105. it("keeps delete priority over write, edit and view for the same file", () => {
  106. const model = buildTreemappingActivityModel(
  107. message([
  108. toolCall({
  109. toolId: "read-1",
  110. kind: "read",
  111. input: { file_path: "src/new.ts" },
  112. }),
  113. toolCall({
  114. toolId: "write-1",
  115. kind: "write",
  116. input: { file_path: "src/new.ts", content: "hello\n" },
  117. }),
  118. toolCall({
  119. toolId: "edit-1",
  120. kind: "edit",
  121. raw: {
  122. changes: {
  123. "src/new.ts": {
  124. type: "update",
  125. oldText: "hello\n",
  126. newText: "hello\nworld\n",
  127. },
  128. },
  129. },
  130. }),
  131. toolCall({
  132. toolId: "delete-1",
  133. kind: "delete",
  134. input: { file_path: "src/new.ts", content: "hello\nworld\n" },
  135. }),
  136. ]),
  137. "/workspace",
  138. );
  139. expect(model.files[0]).toMatchObject({
  140. path: "src/new.ts",
  141. kind: "deleted",
  142. views: 1,
  143. added: 2,
  144. removed: 1,
  145. });
  146. });
  147. it("tracks directory views from ls and search scopes", () => {
  148. const model = buildTreemappingActivityModel(
  149. message([
  150. toolCall({
  151. toolId: "ls-1",
  152. kind: "list",
  153. input: { path: "src/components" },
  154. }),
  155. toolCall({
  156. toolId: "grep-1",
  157. kind: "grep",
  158. input: { directory: "src" },
  159. }),
  160. ]),
  161. "/workspace",
  162. );
  163. expect(model.directories).toEqual(
  164. expect.arrayContaining([
  165. expect.objectContaining({ path: "src/components", views: 1 }),
  166. expect.objectContaining({ path: "src", views: 1 }),
  167. ]),
  168. );
  169. });
  170. it("only consumes structured changes for shell calls", () => {
  171. const model = buildTreemappingActivityModel(
  172. message([
  173. toolCall({
  174. toolId: "shell-1",
  175. kind: "execute",
  176. input: { command: "cat src/ignored.ts && rm src/also-ignored.ts" },
  177. output: "deleted src/also-ignored.ts\nread src/ignored.ts",
  178. }),
  179. toolCall({
  180. toolId: "shell-2",
  181. kind: "execute",
  182. raw: {
  183. rawOutput: {
  184. changes: {
  185. "src/changed.ts": { type: "update", added: 3, removed: 1 },
  186. },
  187. },
  188. },
  189. }),
  190. ]),
  191. "/workspace",
  192. );
  193. expect(model.files.map((file) => file.path)).toEqual(["src/changed.ts"]);
  194. expect(model.files[0]).toMatchObject({ kind: "modified", added: 3, removed: 1 });
  195. });
  196. it("uses ZCode file_diff display stats without treating title=Write as written", () => {
  197. const filePath = "/workspace/src/app/components/TodoList.tsx";
  198. const model = buildTreemappingActivityModel(
  199. message([
  200. toolCall({
  201. toolId: "zcode-edit-1",
  202. kind: "edit",
  203. title: "Write",
  204. input: { file_path: filePath, content: "large content" },
  205. output: {
  206. success: true,
  207. display: {
  208. kind: "file_diff",
  209. filePath,
  210. additions: 541,
  211. deletions: 110,
  212. structuredPatch: [
  213. {
  214. oldStart: 1,
  215. oldLines: 44,
  216. newStart: 1,
  217. newLines: 502,
  218. lines: [
  219. ' "use client";',
  220. "-import { useState } from \"react\";",
  221. "+import { useState, useEffect } from \"react\";",
  222. ],
  223. },
  224. ],
  225. },
  226. },
  227. raw: {
  228. rawOutput: {
  229. success: true,
  230. display: {
  231. kind: "file_diff",
  232. filePath,
  233. additions: 541,
  234. deletions: 110,
  235. structuredPatch: [
  236. {
  237. oldStart: 1,
  238. oldLines: 44,
  239. newStart: 1,
  240. newLines: 502,
  241. lines: [
  242. ' "use client";',
  243. "-import { useState } from \"react\";",
  244. "+import { useState, useEffect } from \"react\";",
  245. ],
  246. },
  247. ],
  248. },
  249. },
  250. },
  251. }),
  252. ]),
  253. "/workspace",
  254. );
  255. expect(model.files).toHaveLength(1);
  256. expect(model.files[0]).toMatchObject({
  257. path: "src/app/components/TodoList.tsx",
  258. kind: "modified",
  259. added: 541,
  260. removed: 110,
  261. });
  262. });
  263. it("uses rawOutput content type as the ZCode write/edit/delete source of truth", () => {
  264. const createPath = "/workspace/src/created.ts";
  265. const updatePath = "/workspace/src/updated.ts";
  266. const deletePath = "/workspace/src/deleted.ts";
  267. const model = buildTreemappingActivityModel(
  268. message([
  269. toolCall({
  270. toolId: "zcode-create-1",
  271. kind: "edit",
  272. title: "Write",
  273. input: { file_path: createPath, content: "one\ntwo\n" },
  274. raw: {
  275. rawOutput: {
  276. content: JSON.stringify({
  277. type: "create",
  278. filePath: createPath,
  279. content: "one\ntwo\n",
  280. }),
  281. display: {
  282. kind: "file_diff",
  283. filePath: createPath,
  284. additions: 2,
  285. deletions: 0,
  286. },
  287. },
  288. },
  289. }),
  290. toolCall({
  291. toolId: "zcode-update-1",
  292. kind: "edit",
  293. title: "Write",
  294. input: { file_path: updatePath, content: "changed\n" },
  295. raw: {
  296. rawOutput: {
  297. content: JSON.stringify({
  298. type: "update",
  299. filePath: updatePath,
  300. content: "changed\n",
  301. }),
  302. display: {
  303. kind: "file_diff",
  304. filePath: updatePath,
  305. additions: 3,
  306. deletions: 1,
  307. },
  308. },
  309. },
  310. }),
  311. toolCall({
  312. toolId: "zcode-delete-1",
  313. kind: "edit",
  314. title: "Write",
  315. input: { file_path: deletePath },
  316. raw: {
  317. rawOutput: {
  318. content: JSON.stringify({
  319. type: "remove",
  320. filePath: deletePath,
  321. }),
  322. display: {
  323. kind: "file_diff",
  324. filePath: deletePath,
  325. additions: 0,
  326. deletions: 4,
  327. },
  328. },
  329. },
  330. }),
  331. ]),
  332. "/workspace",
  333. );
  334. expect(model.files).toEqual(
  335. expect.arrayContaining([
  336. expect.objectContaining({
  337. path: "src/created.ts",
  338. kind: "written",
  339. added: 2,
  340. removed: 0,
  341. }),
  342. expect.objectContaining({
  343. path: "src/updated.ts",
  344. kind: "modified",
  345. added: 3,
  346. removed: 1,
  347. }),
  348. expect.objectContaining({
  349. path: "src/deleted.ts",
  350. kind: "deleted",
  351. added: 0,
  352. removed: 4,
  353. }),
  354. ]),
  355. );
  356. });
  357. });