| 1 | import { expect, test, vi } from "vitest"; |
| 2 | import { DEFAULT_GIT_DIFF_LINE_LIMIT, viewDiffImpl } from "./viewDiff"; |
| 3 | |
| 4 | // Mock the getDiffsFromCache function |
| 5 | vi.mock("../../autocomplete/snippets/gitDiffCache", () => ({ |
| 6 | getDiffsFromCache: vi.fn(), |
| 7 | })); |
| 8 | |
| 9 | import { ToolExtras } from "../.."; |
| 10 | import { getDiffsFromCache } from "../../autocomplete/snippets/gitDiffCache"; |
| 11 | const mockExtras = { |
| 12 | fetch: vi.fn() as any, |
| 13 | ide: {} as any, |
| 14 | } as unknown as ToolExtras; |
| 15 | |
| 16 | test("viewDiff should not truncate diffs under line limit", async () => { |
| 17 | // Create a small diff with just a few lines |
| 18 | const smallDiff = [ |
| 19 | "diff --git a/file1.txt b/file1.txt", |
| 20 | "index 1234..5678 100644", |
| 21 | "--- a/file1.txt", |
| 22 | "+++ b/file1.txt", |
| 23 | "@@ -1,3 +1,4 @@", |
| 24 | " line1", |
| 25 | "-line2", |
| 26 | "+line2 modified", |
| 27 | "+line3 added", |
| 28 | ]; |
| 29 | |
| 30 | (getDiffsFromCache as any).mockResolvedValue(smallDiff); |
| 31 | |
| 32 | const result = await viewDiffImpl({}, mockExtras); |
| 33 | |
| 34 | expect(result).toHaveLength(1); // Just the diff, no warning |
| 35 | expect(result[0].name).toBe("Diff"); |
| 36 | expect(result[0].content).toBe(smallDiff.join("\n")); |
| 37 | }); |
| 38 | |
| 39 | test("viewDiff should truncate diffs exceeding line limit", async () => { |
| 40 | // Create a large diff that exceeds the line limit |
| 41 | const largeDiffLines = Array(DEFAULT_GIT_DIFF_LINE_LIMIT + 1000).fill( |
| 42 | "line content", |
| 43 | ); |
| 44 | const largeDiff = [ |
| 45 | "diff --git a/file1.txt b/file1.txt", |
| 46 | "index 1234..5678 100644", |
| 47 | ...largeDiffLines, |
| 48 | ]; |
| 49 | |
| 50 | (getDiffsFromCache as any).mockResolvedValue([largeDiff.join("\n")]); |
| 51 | |
| 52 | const result = await viewDiffImpl({}, mockExtras); |
| 53 | |
| 54 | expect(result).toHaveLength(2); // Diff + warning |
| 55 | |
| 56 | // Count the number of lines in the result |
| 57 | const resultLines = result[0].content.split("\n"); |
| 58 | expect(resultLines.length).toBe(DEFAULT_GIT_DIFF_LINE_LIMIT); |
| 59 | |
| 60 | // Check the warning |
| 61 | expect(result[1].name).toBe("Truncation warning"); |
| 62 | expect(result[1].content).toContain("truncated"); |
| 63 | expect(result[1].content).toContain(`${DEFAULT_GIT_DIFF_LINE_LIMIT} lines`); |
| 64 | }); |
| 65 | |
| 66 | test("viewDiff should handle multiple diffs correctly", async () => { |
| 67 | const DEFAULT_GIT_DIFF_LINE_LIMIT = 5000; |
| 68 | |
| 69 | // Create multiple diffs that together exceed the line limit |
| 70 | const diff1Lines = Array(3000).fill("diff1 line"); |
| 71 | const diff2Lines = Array(3000).fill("diff2 line"); |
| 72 | const diff1 = diff1Lines.join("\n"); |
| 73 | const diff2 = diff2Lines.join("\n"); |
| 74 | |
| 75 | (getDiffsFromCache as any).mockResolvedValue([diff1, diff2]); |
| 76 | |
| 77 | const result = await viewDiffImpl({}, mockExtras); |
| 78 | |
| 79 | expect(result).toHaveLength(2); // Diff + warning |
| 80 | |
| 81 | // Count the number of lines in the result |
| 82 | const resultLines = result[0].content.split("\n"); |
| 83 | expect(resultLines.length).toBe(DEFAULT_GIT_DIFF_LINE_LIMIT); |
| 84 | |
| 85 | // Check the warning |
| 86 | expect(result[1].name).toBe("Truncation warning"); |
| 87 | }); |