@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 279 lines 8.13 KB
Raw
1 import { assert, describe, it } from "@effect/vitest";
2
3 import {
4 diffFileSection,
5 parseDiffFilePaths,
6 parseDiffFileRevisions,
7 } from "./diffFileRevisions.ts";
8
9 function patchOf(...lines: ReadonlyArray<string>): string {
10 return `${lines.join("\n")}\n`;
11 }
12
13 describe("parseDiffFileRevisions", () => {
14 it("reads the head id of a changed file off its index line", () => {
15 const revisions = parseDiffFileRevisions(
16 patchOf(
17 "diff --git a/src/a.ts b/src/a.ts",
18 "index 7f2aa0ab6..b4a2a7c9a 100644",
19 "--- a/src/a.ts",
20 "+++ b/src/a.ts",
21 "@@ -1 +1 @@",
22 "-a",
23 "+b",
24 ),
25 );
26
27 assert.deepStrictEqual([...revisions], [["src/a.ts", "b4a2a7c9a"]]);
28 });
29
30 it("names a deletion by the path it had, which is the one still on screen", () => {
31 const revisions = parseDiffFileRevisions(
32 patchOf(
33 "diff --git a/gone.ts b/gone.ts",
34 "deleted file mode 100644",
35 "index 1111111..0000000",
36 "--- a/gone.ts",
37 "+++ /dev/null",
38 "@@ -1 +0,0 @@",
39 "-x",
40 ),
41 );
42
43 assert.deepStrictEqual([...revisions], [["gone.ts", "0000000"]]);
44 });
45
46 it("names a rename by where it moved to", () => {
47 const revisions = parseDiffFileRevisions(
48 patchOf(
49 "diff --git a/old.ts b/new.ts",
50 "similarity index 90%",
51 "rename from old.ts",
52 "rename to new.ts",
53 "index 2222222..3333333 100644",
54 "--- a/old.ts",
55 "+++ b/new.ts",
56 "@@ -1 +1 @@",
57 "-a",
58 "+b",
59 ),
60 );
61
62 assert.deepStrictEqual([...revisions], [["new.ts", "3333333"]]);
63 });
64
65 it("names a rename that changed nothing, which states no paths of its own", () => {
66 const revisions = parseDiffFileRevisions(
67 patchOf(
68 "diff --git a/old.ts b/new.ts",
69 "similarity index 100%",
70 "rename from old.ts",
71 "rename to new.ts",
72 "index 2222222..2222222 100644",
73 ),
74 );
75
76 assert.deepStrictEqual([...revisions], [["new.ts", "2222222"]]);
77 });
78
79 it("keeps a leading U+FEFF in a quoted rename's revision key", () => {
80 const revisions = parseDiffFileRevisions(
81 patchOf(
82 'diff --git a/old.txt "b/\\357\\273\\277note.txt"',
83 "similarity index 100%",
84 "rename from old.txt",
85 'rename to "\\357\\273\\277note.txt"',
86 "index 2222222..2222222 100644",
87 ),
88 );
89
90 assert.deepStrictEqual([...revisions], [["\uFEFFnote.txt", "2222222"]]);
91 });
92
93 it("leaves out a file Bitbucket excluded, which it gives no index line for", () => {
94 const revisions = parseDiffFileRevisions(
95 patchOf(
96 "diff --git a/package.json b/package.json",
97 "index 7f2aa0ab6..b4a2a7c9a 100644",
98 "--- a/package.json",
99 "+++ b/package.json",
100 "@@ -1 +1 @@",
101 '- "x": "1"',
102 '+ "x": "2"',
103 "diff --git a/yarn.lock b/yarn.lock",
104 'File excluded by pattern "yarn.lock"',
105 ),
106 );
107
108 assert.deepStrictEqual([...revisions], [["package.json", "b4a2a7c9a"]]);
109 });
110
111 it("stops reading headers at the first hunk, so content cannot pose as one", () => {
112 const revisions = parseDiffFileRevisions(
113 patchOf(
114 "diff --git a/notes.md b/notes.md",
115 "index aaaaaaa..bbbbbbb 100644",
116 "--- a/notes.md",
117 "+++ b/notes.md",
118 "@@ -1,2 +1,2 @@",
119 "--- a/decoy.ts",
120 "+++ b/decoy.ts",
121 "+index ccccccc..ddddddd 100644",
122 ),
123 );
124
125 assert.deepStrictEqual([...revisions], [["notes.md", "bbbbbbb"]]);
126 });
127
128 it("splits a header whose paths contain the separator, by the sides agreeing", () => {
129 const revisions = parseDiffFileRevisions(
130 patchOf(
131 "diff --git a/one b/two.ts b/one b/two.ts",
132 "index eeeeeee..fffffff 100644",
133 "@@ -1 +1 @@",
134 ),
135 );
136
137 assert.deepStrictEqual([...revisions], [["one b/two.ts", "fffffff"]]);
138 });
139
140 it("reads a name git had to quote, here one holding a tab", () => {
141 const revisions = parseDiffFileRevisions(
142 patchOf(
143 'diff --git "a/we\\tird.ts" "b/we\\tird.ts"',
144 "index 4444444..5555555 100644",
145 '--- "a/we\\tird.ts"',
146 '+++ "b/we\\tird.ts"',
147 "@@ -1 +1 @@",
148 "-a",
149 "+b",
150 ),
151 );
152
153 assert.deepStrictEqual([...revisions], [["we\tird.ts", "5555555"]]);
154 });
155
156 it("rejoins the octal bytes git writes for a name outside ASCII", () => {
157 const revisions = parseDiffFileRevisions(
158 patchOf(
159 'diff --git "a/caf\\303\\251/r\\303\\251sum\\303\\251.ts" "b/caf\\303\\251/r\\303\\251sum\\303\\251.ts"',
160 "index 6666666..7777777 100644",
161 "@@ -1 +1 @@",
162 ),
163 );
164
165 assert.deepStrictEqual([...revisions], [["café/résumé.ts", "7777777"]]);
166 });
167
168 it("splits a rename header where git quoted only the side that needed it", () => {
169 const revisions = parseDiffFileRevisions(
170 patchOf(
171 'diff --git a/old.ts "b/new\\tname.ts"',
172 "similarity index 90%",
173 "rename from old.ts",
174 'rename to "new\\tname.ts"',
175 "index 8888888..9999999 100644",
176 "--- a/old.ts",
177 '+++ "b/new\\tname.ts"',
178 "@@ -1 +1 @@",
179 "-a",
180 "+b",
181 ),
182 );
183
184 assert.deepStrictEqual([...revisions], [["new\tname.ts", "9999999"]]);
185 });
186
187 it("keeps a character from outside the basic plane that git left unescaped", () => {
188 const revisions = parseDiffFileRevisions(
189 patchOf(
190 'diff --git "a/we\\tird-\u{1f680}.ts" "b/we\\tird-\u{1f680}.ts"',
191 "index aaaaaaa..bbbbbbb 100644",
192 "@@ -1 +1 @@",
193 ),
194 );
195
196 assert.deepStrictEqual([...revisions], [["we\tird-\u{1f680}.ts", "bbbbbbb"]]);
197 });
198
199 it("drops the tab git ends a name holding a space with", () => {
200 const revisions = parseDiffFileRevisions(
201 patchOf(
202 "diff --git a/with space.txt b/with space.txt",
203 "index 1111111..6178079 100644",
204 "--- a/with space.txt\t",
205 "+++ b/with space.txt\t",
206 "@@ -1 +1 @@",
207 "-a",
208 "+b",
209 ),
210 );
211
212 assert.deepStrictEqual([...revisions], [["with space.txt", "6178079"]]);
213 });
214
215 it("drops the timestamp other producers of the format write past that tab", () => {
216 const revisions = parseDiffFileRevisions(
217 patchOf(
218 "diff --git a/stamped.ts b/stamped.ts",
219 "index 4444444..5555555 100644",
220 "--- a/stamped.ts\t2024-01-01 00:00:00.000000000 +0000",
221 "+++ b/stamped.ts\t2024-01-02 00:00:00.000000000 +0000",
222 "@@ -1 +1 @@",
223 "-a",
224 "+b",
225 ),
226 );
227
228 assert.deepStrictEqual([...revisions], [["stamped.ts", "5555555"]]);
229 });
230 });
231
232 describe("parseDiffFilePaths", () => {
233 it("names both sides of each file, with no side for an added or deleted one", () => {
234 const paths = parseDiffFilePaths(
235 [
236 "diff --git a/src/a.ts b/src/a.ts",
237 "--- a/src/a.ts",
238 "+++ b/src/a.ts",
239 "@@ -1 +1 @@",
240 "--- a/not/a/header.ts",
241 "diff --git a/added.md b/added.md",
242 "new file mode 100644",
243 "--- /dev/null",
244 "+++ b/added.md",
245 "diff --git a/gone.md b/gone.md",
246 "deleted file mode 100644",
247 "--- a/gone.md",
248 "+++ /dev/null",
249 'diff --git "a/tab\\tname" b/moved.ts',
250 'rename from "tab\\tname"',
251 "rename to moved.ts",
252 "",
253 ].join("\n"),
254 );
255
256 assert.deepStrictEqual(paths, [
257 { oldPath: "src/a.ts", newPath: "src/a.ts" },
258 { oldPath: null, newPath: "added.md" },
259 { oldPath: "gone.md", newPath: null },
260 { oldPath: "tab\tname", newPath: "moved.ts" },
261 ]);
262 });
263 });
264
265 describe("diffFileSection", () => {
266 it("picks a file by where it ends up, so a swapped rename is not read as its partner", () => {
267 const aToB = patchOf("diff --git a/a.ts b/b.ts", "rename from a.ts", "rename to b.ts");
268 const bToA = patchOf("diff --git a/b.ts b/a.ts", "rename from b.ts", "rename to a.ts");
269 const gone = patchOf(
270 "diff --git a/gone.ts b/gone.ts",
271 "deleted file mode 100644",
272 "--- a/gone.ts",
273 "+++ /dev/null",
274 );
275 const patch = `${aToB}${bToA}${gone}`;
276 assert.strictEqual(diffFileSection(patch, "a.ts"), bToA);
277 assert.strictEqual(diffFileSection(patch, "gone.ts"), gone);
278 });
279 });