char-slop/berry

multipurpose self-modifiable agentic harness

git clone https://git.t4t.associates/char-slop/berry

Charlotte Somadd diff extension806cc3c

main
4.2 KiB149 linesraw
1import { assertEquals } from "@std/assert";
2import { narrow, parsePatch } from "./patch.ts";
3
4Deno.test("renames, deletions, additions and mode changes read as what happened to each file", () => {
5  const files = parsePatch([
6    "diff --git a/old.txt b/new.txt",
7    "similarity index 79%",
8    "rename from old.txt",
9    "rename to new.txt",
10    "index f384549..b2f931a 100644",
11    "--- a/old.txt",
12    "+++ b/new.txt",
13    "@@ -1,2 +1,3 @@",
14    " one",
15    " two",
16    "+three",
17    "diff --git a/keep.txt b/keep.txt",
18    "deleted file mode 100644",
19    "index de98044..0000000",
20    "--- a/keep.txt",
21    "+++ /dev/null",
22    "@@ -1 +0,0 @@",
23    "-a",
24    "diff --git a/empty b/empty",
25    "new file mode 100644",
26    "index 0000000..e69de29",
27    "diff --git a/script.sh b/script.sh",
28    "old mode 100644",
29    "new mode 100755",
30    "diff --git a/bin.dat b/bin.dat",
31    "index bdc955b..8835708 100644",
32    "Binary files a/bin.dat and b/bin.dat differ",
33    "",
34  ].join("\n"));
35  assertEquals(
36    files.map(({ path, from, status, modes, binary }) => ({ path, from, status, modes, binary })),
37    [
38      {
39        path: "new.txt",
40        from: "old.txt",
41        status: "renamed",
42        modes: { old: "100644", new: "100644" },
43        binary: false,
44      },
45      {
46        path: "keep.txt",
47        from: null,
48        status: "deleted",
49        modes: { old: "100644", new: null },
50        binary: false,
51      },
52      {
53        path: "empty",
54        from: null,
55        status: "added",
56        modes: { old: null, new: "100644" },
57        binary: false,
58      },
59      {
60        path: "script.sh",
61        from: null,
62        status: "modified",
63        modes: { old: "100644", new: "100755" },
64        binary: false,
65      },
66      {
67        path: "bin.dat",
68        from: null,
69        status: "modified",
70        modes: { old: "100644", new: "100644" },
71        binary: true,
72      },
73    ],
74  );
75  assertEquals(files[0].hunks, [{ old: 1, new: 1, lines: [" one", " two", "+three"] }]);
76  assertEquals(files[1].hunks, [{ old: 1, new: 0, lines: ["-a"] }]);
77  assertEquals(files[2].hunks, []);
78});
79
80Deno.test("hunks keep missing newlines, carriage returns, and lines that look like headers", () => {
81  const [file] = parsePatch([
82    "commit header that comes first",
83    "",
84    "diff --git a/x b/x",
85    "--- a/x",
86    "+++ b/x",
87    "@@ -1,3 +1,3 @@",
88    "-diff --git a/y b/y",
89    "+--- a/y\r",
90    "",
91    " last",
92    "\\ No newline at end of file",
93    "",
94  ].join("\n"));
95  assertEquals(file.hunks, [{
96    old: 1,
97    new: 1,
98    lines: ["-diff --git a/y b/y", "+--- a/y\r", " ", " last", "\\ No newline at end of file"],
99  }]);
100});
101
102Deno.test("unusual paths read as themselves, quoted or not", () => {
103  const files = parsePatch([
104    'diff --git a/a.txt "b/b \\"c\\".txt"',
105    "similarity index 100%",
106    "rename from a.txt",
107    'rename to "b \\"c\\".txt"',
108    'diff --git "a/we ird\\\\t\\"q\\".txt" "b/we ird\\\\t\\"q\\".txt"',
109    "index 45b983b..ab4a981 100644",
110    '--- "a/we ird\\\\t\\"q\\".txt"\t',
111    '+++ "b/we ird\\\\t\\"q\\".txt"\t',
112    "@@ -1 +1 @@",
113    "-hi",
114    "+ho",
115    "diff --git a/ünï cødé.txt b/ünï cødé.txt",
116    "new file mode 100644",
117    "index 0000000..e69de29",
118    'diff --git "a/\\303\\274.txt" "b/\\303\\274.txt"',
119    "old mode 100644",
120    "new mode 100755",
121    "diff --git a/sp ace b/sp ace",
122    "--- a/sp ace\t",
123    "+++ b/sp ace\t",
124    "@@ -1 +1 @@",
125    "-a",
126    "+b",
127  ].join("\n"));
128  assertEquals(files.map(({ path, from }) => [path, from]), [
129    ['b "c".txt', "a.txt"],
130    ['we ird\\t"q".txt', null],
131    ["ünï cødé.txt", null],
132    ["ü.txt", null],
133    ["sp ace", null],
134  ]);
135});
136
137Deno.test("narrowing a long file keeps the changes and their context, at their own line numbers", () => {
138  const lines = Array.from({ length: 30 }, (_, i) => ` ${i + 1}`);
139  lines[4] = "-5";
140  lines.splice(5, 0, "+five");
141  lines[25] = "-25";
142  const [file] = parsePatch(
143    ["diff --git a/f b/f", "--- a/f", "+++ b/f", "@@ -1,30 +1,30 @@", ...lines].join("\n"),
144  );
145  assertEquals(narrow(file, 2).hunks, [
146    { old: 3, new: 3, lines: [" 3", " 4", "-5", "+five", " 6", " 7"] },
147    { old: 23, new: 23, lines: [" 23", " 24", "-25", " 26", " 27"] },
148  ]);
149});