| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- import { describe, expect, it } from "vitest";
- import {
- buildTaskChangeSummary,
- buildPerTurnChangeSummaries,
- } from "../src/session/taskChangeSummary.js";
- describe("buildTaskChangeSummary", () => {
- it("按任务最终结果聚合同一文件的多轮改动", () => {
- const summary = buildTaskChangeSummary([
- {
- turnIndex: 0,
- snapshots: [
- {
- path: "/repo/src/a.ts",
- beforeContent: "line1\nline2\n",
- afterContent: "line1\nline2\nline3\n",
- writeCount: 1,
- },
- ],
- },
- {
- turnIndex: 1,
- snapshots: [
- {
- path: "/repo/src/a.ts",
- beforeContent: "line1\nline2\nline3\n",
- afterContent: "line0\nline1\nline2\nline3\n",
- writeCount: 2,
- },
- {
- path: "/repo/src/b.ts",
- beforeContent: null,
- afterContent: "new file\n",
- writeCount: 1,
- },
- ],
- },
- ]);
- expect(summary).toEqual({
- fileCount: 2,
- added: 3,
- removed: 0,
- files: [
- {
- path: "/repo/src/a.ts",
- added: 2,
- removed: 0,
- writeCount: 3,
- lastTurnIndex: 1,
- },
- {
- path: "/repo/src/b.ts",
- added: 1,
- removed: 0,
- writeCount: 1,
- lastTurnIndex: 1,
- },
- ],
- });
- });
- it("文件最终回到原内容时仍保留触达记录", () => {
- const summary = buildTaskChangeSummary([
- {
- turnIndex: 0,
- snapshots: [
- {
- path: "/repo/src/a.ts",
- beforeContent: "same\n",
- afterContent: "changed\n",
- writeCount: 1,
- },
- ],
- },
- {
- turnIndex: 1,
- snapshots: [
- {
- path: "/repo/src/a.ts",
- beforeContent: "changed\n",
- afterContent: "same\n",
- writeCount: 1,
- },
- ],
- },
- ]);
- expect(summary).toEqual({
- fileCount: 1,
- added: 0,
- removed: 0,
- files: [
- {
- path: "/repo/src/a.ts",
- added: 0,
- removed: 0,
- writeCount: 2,
- lastTurnIndex: 1,
- },
- ],
- });
- });
- });
- describe("buildPerTurnChangeSummaries", () => {
- it("returns per-turn summaries independently", () => {
- const summaries = buildPerTurnChangeSummaries([
- {
- turnIndex: 0,
- snapshots: [
- {
- path: "/repo/src/a.ts",
- beforeContent: "line1\nline2\n",
- afterContent: "line1\nline2\nline3\n",
- writeCount: 1,
- },
- ],
- },
- {
- turnIndex: 1,
- snapshots: [
- {
- path: "/repo/src/a.ts",
- beforeContent: "line1\nline2\nline3\n",
- afterContent: "line0\nline1\nline2\nline3\n",
- writeCount: 1,
- },
- {
- path: "/repo/src/b.ts",
- beforeContent: null,
- afterContent: "new file\n",
- writeCount: 1,
- },
- ],
- },
- ]);
- expect(summaries.size).toBe(2);
- const turn0 = summaries.get(0)!;
- expect(turn0.fileCount).toBe(1);
- expect(turn0.added).toBe(1);
- expect(turn0.removed).toBe(0);
- expect(turn0.files).toHaveLength(1);
- expect(turn0.files[0]!.path).toBe("/repo/src/a.ts");
- const turn1 = summaries.get(1)!;
- expect(turn1.fileCount).toBe(2);
- expect(turn1.added).toBe(2);
- expect(turn1.removed).toBe(0);
- });
- it("deduplicates same file changed multiple times within one turn", () => {
- const summaries = buildPerTurnChangeSummaries([
- {
- turnIndex: 0,
- snapshots: [
- {
- path: "/repo/src/a.ts",
- beforeContent: "line1\n",
- afterContent: "line1\nline2\n",
- writeCount: 1,
- },
- {
- path: "/repo/src/a.ts",
- beforeContent: "line1\nline2\n",
- afterContent: "line1\nline2\nline3\n",
- writeCount: 1,
- },
- ],
- },
- ]);
- const turn0 = summaries.get(0)!;
- // Should be 1 file, not 2
- expect(turn0.fileCount).toBe(1);
- expect(turn0.files).toHaveLength(1);
- // Diff should be from original before to final after
- expect(turn0.files[0]!.path).toBe("/repo/src/a.ts");
- expect(turn0.files[0]!.added).toBe(2); // line2 + line3
- expect(turn0.files[0]!.removed).toBe(0);
- expect(turn0.files[0]!.writeCount).toBe(2);
- });
- it("returns empty map for undefined/empty input", () => {
- expect(buildPerTurnChangeSummaries(undefined).size).toBe(0);
- expect(buildPerTurnChangeSummaries([]).size).toBe(0);
- });
- it("skips turns with no actual line changes but still tracks them", () => {
- const summaries = buildPerTurnChangeSummaries([
- {
- turnIndex: 0,
- snapshots: [
- {
- path: "/repo/src/a.ts",
- beforeContent: "same\n",
- afterContent: "same\n",
- writeCount: 1,
- },
- ],
- },
- ]);
- const turn0 = summaries.get(0)!;
- expect(turn0.fileCount).toBe(1);
- expect(turn0.added).toBe(0);
- expect(turn0.removed).toBe(0);
- });
- });
|