Open History to see the last commit
1import { parsePatchFiles } from "@pierre/diffs/utils/parsePatchFiles";
2import { describe, expect, it } from "vite-plus/test";
3
4import {
5 excerptExpansionOldContents,
6 excerptRows,
7 fileDiffRows,
8 rowsToPatch,
9} from "./reviewGuideTour.js";
10
11const PATCH = `diff --git a/src/tray.ts b/src/tray.ts
12--- a/src/tray.ts
13+++ b/src/tray.ts
14@@ -10,5 +10,6 @@ function tray()
15 const a = 1;
16-const size = pick();
17+const size = "xs";
18+const variant = "tray";
19 const b = 2;
20 const c = 3;
21 const d = 4;
22@@ -40,3 +41,2 @@ function footer()
23 keep();
24-dropped();
25 tail();
26`;
27
28function parse(patch: string) {
29 const file = parsePatchFiles(patch, `test:${patch.length}`)[0]?.files[0];
30 if (file === undefined) throw new Error("patch did not parse");
31 return file;
32}
33
34const rows = fileDiffRows(parse(PATCH));
35
36describe("fileDiffRows", () => {
37 it("numbers both sides through context, deletions and additions", () => {
38 expect(rows.map((row) => [row.kind, row.oldLine, row.newLine])).toEqual([
39 ["context", 10, 10],
40 ["deletion", 11, null],
41 ["addition", null, 11],
42 ["addition", null, 12],
43 ["context", 12, 13],
44 ["context", 13, 14],
45 ["context", 14, 15],
46 ["context", 40, 41],
47 ["deletion", 41, null],
48 ["context", 42, 42],
49 ]);
50 });
51});
52
53describe("excerptRows", () => {
54 it("brings the replaced lines along with an excerpt that starts on an addition", () => {
55 const shown = excerptRows(rows, { side: "new", start: 11, end: 12 });
56 expect(shown.map((row) => row.kind)).toEqual(["deletion", "addition", "addition"]);
57 });
58
59 it("selects old-side lines for a removal and stops at the hunk boundary", () => {
60 const shown = excerptRows(rows, { side: "old", start: 41, end: 41 });
61 expect(shown.map((row) => [row.kind, row.oldLine])).toEqual([["deletion", 41]]);
62 });
63});
64
65describe("rowsToPatch", () => {
66 it("cuts a slice that parses back to the same lines and numbers", () => {
67 const shown = excerptRows(rows, { side: "new", start: 10, end: 13 });
68 const slice = parse(rowsToPatch("src/tray.ts", "src/tray.ts", shown));
69 expect(fileDiffRows(slice).map((row) => [row.kind, row.oldLine, row.newLine])).toEqual(
70 shown.map((row) => [row.kind, row.oldLine, row.newLine]),
71 );
72 });
73
74 it("starts a hunk per source hunk so separate hunks stay separate", () => {
75 const shown = excerptRows(rows, { side: "old", start: 14, end: 42 });
76 const patch = rowsToPatch("src/tray.ts", "src/tray.ts", shown);
77 expect(patch.match(/^@@ /gm)).toHaveLength(2);
78 expect(patch).toContain("@@ -14,1 +15,1 @@");
79 expect(patch).toContain("@@ -40,3 +41,2 @@");
80 });
81});
82
83describe("excerptExpansionOldContents", () => {
84 const newFile = Array.from({ length: 50 }, (_, index) => `line ${index + 1}\n`);
85 newFile.splice(
86 9,
87 6,
88 "const a = 1;\n",
89 'const size = "xs";\n',
90 'const variant = "tray";\n',
91 "const b = 2;\n",
92 "const c = 3;\n",
93 "const d = 4;\n",
94 );
95 newFile.splice(40, 2, "keep();\n", "tail();\n");
96 const newContents = newFile.join("");
97
98 it("puts back only the excerpt's lines and keeps both sides numbered as in the file", () => {
99 const shown = excerptRows(rows, { side: "new", start: 11, end: 12 });
100 const old = excerptExpansionOldContents(newContents, shown);
101 if (old === null) throw new Error("expected contents");
102 const oldLines = old.split("\n");
103 expect(oldLines[10]).toBe("const size = pick();");
104 expect(oldLines[9]).toBe("const a = 1;");
105 expect(oldLines[11]).toBe("const b = 2;");
106 expect(oldLines.length).toBe(newContents.split("\n").length - 1);
107 });
108
109 it("does not try to expand an excerpt that spans hunks", () => {
110 const shown = excerptRows(rows, { side: "old", start: 14, end: 42 });
111 expect(excerptExpansionOldContents(newContents, shown)).toBeNull();
112 });
113});