| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { parseTurnDiffFilesFromNumstat } from "./Diffs.ts"; |
| 4 | |
| 5 | describe("parseTurnDiffFilesFromNumstat", () => { |
| 6 | it("returns an empty list when no files changed", () => { |
| 7 | expect(parseTurnDiffFilesFromNumstat("")).toEqual([]); |
| 8 | }); |
| 9 | |
| 10 | it("sorts files and preserves addition and deletion counts", () => { |
| 11 | const numstat = ["0\t2\tsrc/b.ts", "2\t1\ta.txt", ""].join("\0"); |
| 12 | expect(parseTurnDiffFilesFromNumstat(numstat)).toEqual([ |
| 13 | { path: "a.txt", additions: 2, deletions: 1 }, |
| 14 | { path: "src/b.ts", additions: 0, deletions: 2 }, |
| 15 | ]); |
| 16 | }); |
| 17 | |
| 18 | it("preserves both paths for renames and copies", () => { |
| 19 | const numstat = [ |
| 20 | "0\t0\t", |
| 21 | "src/old.ts", |
| 22 | "src/new.ts", |
| 23 | "2\t1\t", |
| 24 | "src/source.ts", |
| 25 | "src/copied.ts", |
| 26 | "1\t0\tother.ts", |
| 27 | "", |
| 28 | ].join("\0"); |
| 29 | |
| 30 | expect(parseTurnDiffFilesFromNumstat(numstat)).toEqual([ |
| 31 | { path: "other.ts", additions: 1, deletions: 0 }, |
| 32 | { path: "src/copied.ts", previousPath: "src/source.ts", additions: 2, deletions: 1 }, |
| 33 | { path: "src/new.ts", previousPath: "src/old.ts", additions: 0, deletions: 0 }, |
| 34 | ]); |
| 35 | }); |
| 36 | |
| 37 | it("keeps binary files and empty files with zero line changes", () => { |
| 38 | const numstat = ["-\t-\timage.png", "0\t0\tempty.txt", ""].join("\0"); |
| 39 | expect(parseTurnDiffFilesFromNumstat(numstat)).toEqual([ |
| 40 | { path: "empty.txt", additions: 0, deletions: 0 }, |
| 41 | { path: "image.png", additions: 0, deletions: 0 }, |
| 42 | ]); |
| 43 | }); |
| 44 | |
| 45 | it("preserves Unicode, tabs, line endings, and spaces in paths", () => { |
| 46 | const path = " café\tline\r\nname.txt "; |
| 47 | const numstat = `3\t2\t\0old\tname\n.txt\0${path}\0`; |
| 48 | |
| 49 | expect(parseTurnDiffFilesFromNumstat(numstat)).toEqual([ |
| 50 | { path, previousPath: "old\tname\n.txt", additions: 3, deletions: 2 }, |
| 51 | ]); |
| 52 | expect(parseTurnDiffFilesFromNumstat(`1\t0\t${path}\0`)).toEqual([ |
| 53 | { path, additions: 1, deletions: 0 }, |
| 54 | ]); |
| 55 | }); |
| 56 | }); |