taskChangeSummary.test.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. buildTaskChangeSummary,
  4. buildPerTurnChangeSummaries,
  5. } from "../src/session/taskChangeSummary.js";
  6. describe("buildTaskChangeSummary", () => {
  7. it("按任务最终结果聚合同一文件的多轮改动", () => {
  8. const summary = buildTaskChangeSummary([
  9. {
  10. turnIndex: 0,
  11. snapshots: [
  12. {
  13. path: "/repo/src/a.ts",
  14. beforeContent: "line1\nline2\n",
  15. afterContent: "line1\nline2\nline3\n",
  16. writeCount: 1,
  17. },
  18. ],
  19. },
  20. {
  21. turnIndex: 1,
  22. snapshots: [
  23. {
  24. path: "/repo/src/a.ts",
  25. beforeContent: "line1\nline2\nline3\n",
  26. afterContent: "line0\nline1\nline2\nline3\n",
  27. writeCount: 2,
  28. },
  29. {
  30. path: "/repo/src/b.ts",
  31. beforeContent: null,
  32. afterContent: "new file\n",
  33. writeCount: 1,
  34. },
  35. ],
  36. },
  37. ]);
  38. expect(summary).toEqual({
  39. fileCount: 2,
  40. added: 3,
  41. removed: 0,
  42. files: [
  43. {
  44. path: "/repo/src/a.ts",
  45. added: 2,
  46. removed: 0,
  47. writeCount: 3,
  48. lastTurnIndex: 1,
  49. },
  50. {
  51. path: "/repo/src/b.ts",
  52. added: 1,
  53. removed: 0,
  54. writeCount: 1,
  55. lastTurnIndex: 1,
  56. },
  57. ],
  58. });
  59. });
  60. it("文件最终回到原内容时仍保留触达记录", () => {
  61. const summary = buildTaskChangeSummary([
  62. {
  63. turnIndex: 0,
  64. snapshots: [
  65. {
  66. path: "/repo/src/a.ts",
  67. beforeContent: "same\n",
  68. afterContent: "changed\n",
  69. writeCount: 1,
  70. },
  71. ],
  72. },
  73. {
  74. turnIndex: 1,
  75. snapshots: [
  76. {
  77. path: "/repo/src/a.ts",
  78. beforeContent: "changed\n",
  79. afterContent: "same\n",
  80. writeCount: 1,
  81. },
  82. ],
  83. },
  84. ]);
  85. expect(summary).toEqual({
  86. fileCount: 1,
  87. added: 0,
  88. removed: 0,
  89. files: [
  90. {
  91. path: "/repo/src/a.ts",
  92. added: 0,
  93. removed: 0,
  94. writeCount: 2,
  95. lastTurnIndex: 1,
  96. },
  97. ],
  98. });
  99. });
  100. });
  101. describe("buildPerTurnChangeSummaries", () => {
  102. it("returns per-turn summaries independently", () => {
  103. const summaries = buildPerTurnChangeSummaries([
  104. {
  105. turnIndex: 0,
  106. snapshots: [
  107. {
  108. path: "/repo/src/a.ts",
  109. beforeContent: "line1\nline2\n",
  110. afterContent: "line1\nline2\nline3\n",
  111. writeCount: 1,
  112. },
  113. ],
  114. },
  115. {
  116. turnIndex: 1,
  117. snapshots: [
  118. {
  119. path: "/repo/src/a.ts",
  120. beforeContent: "line1\nline2\nline3\n",
  121. afterContent: "line0\nline1\nline2\nline3\n",
  122. writeCount: 1,
  123. },
  124. {
  125. path: "/repo/src/b.ts",
  126. beforeContent: null,
  127. afterContent: "new file\n",
  128. writeCount: 1,
  129. },
  130. ],
  131. },
  132. ]);
  133. expect(summaries.size).toBe(2);
  134. const turn0 = summaries.get(0)!;
  135. expect(turn0.fileCount).toBe(1);
  136. expect(turn0.added).toBe(1);
  137. expect(turn0.removed).toBe(0);
  138. expect(turn0.files).toHaveLength(1);
  139. expect(turn0.files[0]!.path).toBe("/repo/src/a.ts");
  140. const turn1 = summaries.get(1)!;
  141. expect(turn1.fileCount).toBe(2);
  142. expect(turn1.added).toBe(2);
  143. expect(turn1.removed).toBe(0);
  144. });
  145. it("deduplicates same file changed multiple times within one turn", () => {
  146. const summaries = buildPerTurnChangeSummaries([
  147. {
  148. turnIndex: 0,
  149. snapshots: [
  150. {
  151. path: "/repo/src/a.ts",
  152. beforeContent: "line1\n",
  153. afterContent: "line1\nline2\n",
  154. writeCount: 1,
  155. },
  156. {
  157. path: "/repo/src/a.ts",
  158. beforeContent: "line1\nline2\n",
  159. afterContent: "line1\nline2\nline3\n",
  160. writeCount: 1,
  161. },
  162. ],
  163. },
  164. ]);
  165. const turn0 = summaries.get(0)!;
  166. // Should be 1 file, not 2
  167. expect(turn0.fileCount).toBe(1);
  168. expect(turn0.files).toHaveLength(1);
  169. // Diff should be from original before to final after
  170. expect(turn0.files[0]!.path).toBe("/repo/src/a.ts");
  171. expect(turn0.files[0]!.added).toBe(2); // line2 + line3
  172. expect(turn0.files[0]!.removed).toBe(0);
  173. expect(turn0.files[0]!.writeCount).toBe(2);
  174. });
  175. it("returns empty map for undefined/empty input", () => {
  176. expect(buildPerTurnChangeSummaries(undefined).size).toBe(0);
  177. expect(buildPerTurnChangeSummaries([]).size).toBe(0);
  178. });
  179. it("skips turns with no actual line changes but still tracks them", () => {
  180. const summaries = buildPerTurnChangeSummaries([
  181. {
  182. turnIndex: 0,
  183. snapshots: [
  184. {
  185. path: "/repo/src/a.ts",
  186. beforeContent: "same\n",
  187. afterContent: "same\n",
  188. writeCount: 1,
  189. },
  190. ],
  191. },
  192. ]);
  193. const turn0 = summaries.get(0)!;
  194. expect(turn0.fileCount).toBe(1);
  195. expect(turn0.added).toBe(0);
  196. expect(turn0.removed).toBe(0);
  197. });
  198. });