-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.test.ts
More file actions
381 lines (329 loc) · 10.4 KB
/
index.test.ts
File metadata and controls
381 lines (329 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import { describe, expect, it } from "bun:test";
import { parseGitPatch } from "./index.js";
describe("parseGitPatch", () => {
it("parses a single commit patch", () => {
const patch = `From f9ec51d9919f16c09476f51eaa19b818564904b2 Mon Sep 17 00:00:00 2001
From: John Doe <john@example.com>
Date: Wed, 12 Oct 2022 14:38:15 +0200
Subject: [PATCH] My commit message
Some more lines of the commit message.
---
file1.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/file1.txt b/file1.txt
index 24967d3..b37620a 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1,6 +1,6 @@
Line 1
-Line 2
+Line 2 changed
Line 3
Line 4
Line 5
Line 6
--
2.47.1`;
const commits = parseGitPatch(patch);
expect(commits).toHaveLength(1);
const commit = commits[0];
expect(commit.sha).toBe("f9ec51d9919f16c09476f51eaa19b818564904b2");
expect(commit.authorName).toBe("John Doe");
expect(commit.authorEmail).toBe("john@example.com");
expect(commit.date).toBe("Wed, 12 Oct 2022 14:38:15 +0200");
expect(commit.message).toBe(
"My commit message\n\nSome more lines of the commit message.",
);
expect(commit.diff).toBe(`diff --git a/file1.txt b/file1.txt
index 24967d3..b37620a 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1,6 +1,6 @@
Line 1
-Line 2
+Line 2 changed
Line 3
Line 4
Line 5
Line 6
`);
});
it("parses multiple commits patch", () => {
const patch = `From f9ec51d9919f16c09476f51eaa19b818564904b2 Mon Sep 17 00:00:00 2001
From: John Doe <john@example.com>
Date: Wed, 12 Oct 2022 14:38:15 +0200
Subject: [PATCH 1/2] First commit message
Line two of message.
---
file1.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/file1.txt b/file1.txt
index 24967d3..b37620a 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1,6 +1,6 @@
Line 1
-Line 2
+Line 2 changed
Line 3
Line 4
Line 5
Line foo
From 4e9c51d9919f16c09476f51eaa19b818564904b1 Mon Sep 17 00:00:00 2001
From: Jane Smith <jane@example.com>
Date: Thu, 13 Oct 2022 15:38:15 +0200
Subject: [PATCH 1/2] Second commit message
Another line
And another.
---
file2.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/file2.txt b/file2.txt
index 24967d3..b37620a 100644
--- a/file2.txt
+++ b/file2.txt
@@ -1,6 +1,6 @@
Line 1
-Line 2
+Line 2 changed again
Line 3
Line 4
Line 5
Line bar`;
const commits = parseGitPatch(patch);
expect(commits).toHaveLength(2);
const [first, second] = commits;
expect(first.sha).toBe("f9ec51d9919f16c09476f51eaa19b818564904b2");
expect(first.authorName).toBe("John Doe");
expect(first.authorEmail).toBe("john@example.com");
expect(first.date).toBe("Wed, 12 Oct 2022 14:38:15 +0200");
expect(first.message).toBe("First commit message\n\nLine two of message.");
expect(first.diff).toBe(`diff --git a/file1.txt b/file1.txt
index 24967d3..b37620a 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1,6 +1,6 @@
Line 1
-Line 2
+Line 2 changed
Line 3
Line 4
Line 5
Line foo
`);
expect(second.sha).toBe("4e9c51d9919f16c09476f51eaa19b818564904b1");
expect(second.authorName).toBe("Jane Smith");
expect(second.authorEmail).toBe("jane@example.com");
expect(second.date).toBe("Thu, 13 Oct 2022 15:38:15 +0200");
expect(second.message).toBe(
"Second commit message\n\nAnother line\nAnd another.",
);
expect(second.diff).toBe(`diff --git a/file2.txt b/file2.txt
index 24967d3..b37620a 100644
--- a/file2.txt
+++ b/file2.txt
@@ -1,6 +1,6 @@
Line 1
-Line 2
+Line 2 changed again
Line 3
Line 4
Line 5
Line bar
`);
});
it("handles multiline messages without any message lines after subject", () => {
const patch = `From abcdef1111111111111111111111111111111111 Mon Sep 17 00:00:00 2001
From: No Extra <noextra@example.com>
Date: Fri, 14 Oct 2022 10:00:00 +0000
Subject: Just a subject
---
file.txt | 1 +
1 file changed, 1 insertion(+)
diff --git a/file.txt b/file.txt
new file mode 100644
index 0000000..1234567
--- /dev/null
+++ b/file.txt
@@ -0,0 +1 @@
+Hello world
`;
const commits = parseGitPatch(patch);
expect(commits).toHaveLength(1);
const commit = commits[0];
expect(commit.message).toBe("Just a subject");
expect(commit.diff).toContain("diff --git a/file.txt b/file.txt");
});
it("handles no diff scenario (just a commit message)", () => {
const patch = `From abcdef1111111111111111111111111111111111 Mon Sep 17 00:00:00 2001
From: Jane Doe <jane@example.com>
Date: Fri, 14 Oct 2022 10:00:00 +0000
Subject: [PATCH] No diff commit
No changes in this commit.
---
`;
const commits = parseGitPatch(patch);
expect(commits).toHaveLength(1);
const commit = commits[0];
expect(commit.message).toBe("No diff commit\n\nNo changes in this commit.");
expect(commit.diff).toBe("");
});
it("handles malformed input gracefully (no commits)", () => {
const patch = `This is just random text
with no From line at all.
`;
const commits = parseGitPatch(patch);
expect(commits).toHaveLength(0);
});
it("parses a commit with multiple files changed and multiple hunks", () => {
const patch = `From 1234567890abcdef1234567890abcdef12345678 Mon Sep 17 00:00:00 2001
From: Multi Hunk <multihunk@example.com>
Date: Sat, 15 Oct 2022 16:00:00 +0000
Subject: [PATCH] Multiple files and hunks commit
This commit changes multiple files and has multiple hunks in one file.
---
fileA.txt | 3 ++-
fileB.txt | 5 +++++
2 files changed, 7 insertions(+), 1 deletion(-)
diff --git a/fileA.txt b/fileA.txt
index 24967d3..b37620a 100644
--- a/fileA.txt
+++ b/fileA.txt
@@ -1,3 +1,3 @@
Line A1
-Line A2
+Line A2 changed
Line A3
@@ -5,6 +5,7 @@ Line A4
Line A5
+Line A6 new line
diff --git a/fileB.txt b/fileB.txt
new file mode 100644
index 0000000..0abcd12
--- /dev/null
+++ b/fileB.txt
@@ -0,0 +1,5 @@
+Line B1
+Line B2
+Line B3
+Line B4
+Line B5
`;
const commits = parseGitPatch(patch);
expect(commits).toHaveLength(1);
const commit = commits[0];
expect(commit.sha).toBe("1234567890abcdef1234567890abcdef12345678");
expect(commit.authorName).toBe("Multi Hunk");
expect(commit.authorEmail).toBe("multihunk@example.com");
expect(commit.date).toBe("Sat, 15 Oct 2022 16:00:00 +0000");
expect(commit.message).toBe(
"Multiple files and hunks commit\n\nThis commit changes multiple files and has multiple hunks in one file.",
);
// Check that diff contains both files and multiple hunks
expect(commit.diff).toContain("diff --git a/fileA.txt b/fileA.txt");
expect(commit.diff).toContain("+Line A2 changed");
expect(commit.diff).toContain("+Line A6 new line"); // from the second hunk in fileA.txt
expect(commit.diff).toContain("diff --git a/fileB.txt b/fileB.txt");
expect(commit.diff).toContain("new file mode 100644");
expect(commit.diff).toContain("+Line B5");
});
it("parses a commit with a long multi-line message and tricky formatting", () => {
const patch = `From abc123abc123abc123abc123abc123abc123abc1 Mon Sep 17 00:00:00 2001
From: Tricky Author <tricky@example.com>
Date: Mon, 16 Oct 2022 12:34:56 +0000
Subject: [PATCH] This is a long, multi-line commit message
that spans multiple lines,
includes some empty lines,
and lines that might look like diffs but are not.
Some lines have trailing spaces:
And some lines have unusual indentation:
Indented line here
Another line that looks like a patch hunk header but isn't:
@@ -10,5 +10,7 @@ This is not really a diff
Also lines that might contain symbols like +++ or --- in the message body are still part of the message:
+++ Still message line
--- Still message line
At the end of the day, this should all be captured as part of the commit message.
---
fileC.txt | 2 ++
1 file changed, 2 insertions(+)
diff --git a/fileC.txt b/fileC.txt
new file mode 100644
index 0000000..1111111
--- /dev/null
+++ b/fileC.txt
@@ -0,0 +1,2 @@
+Tricky line 1
+Tricky line 2
`;
const commits = parseGitPatch(patch);
expect(commits).toHaveLength(1);
const commit = commits[0];
expect(commit.sha).toBe("abc123abc123abc123abc123abc123abc123abc1");
expect(commit.authorName).toBe("Tricky Author");
expect(commit.authorEmail).toBe("tricky@example.com");
expect(commit.date).toBe("Mon, 16 Oct 2022 12:34:56 +0000");
// Verify that the message includes all expected lines and formatting.
// Be sure it includes lines that look like patch headers but aren't, trailing spaces, and so forth.
const expectedMessage = `This is a long, multi-line commit message
that spans multiple lines,
includes some empty lines,
and lines that might look like diffs but are not.
Some lines have trailing spaces:
And some lines have unusual indentation:
Indented line here
Another line that looks like a patch hunk header but isn't:
@@ -10,5 +10,7 @@ This is not really a diff
Also lines that might contain symbols like +++ or --- in the message body are still part of the message:
+++ Still message line
--- Still message line
At the end of the day, this should all be captured as part of the commit message.`.trim();
expect(commit.message).toBe(expectedMessage);
// Verify the diff
expect(commit.diff).toContain("diff --git a/fileC.txt b/fileC.txt");
expect(commit.diff).toContain("+Tricky line 1");
expect(commit.diff).toContain("+Tricky line 2");
});
it("parses this patch", () => {
const patch = `
From 44c8f979f6b96b9dd8c06a36a5a0cb13a5a67ab6 Mon Sep 17 00:00:00 2001
From: Foo <foo@dexa.ai>
Date: Thu, 19 Dec 2024 20:54:06 +0000
Subject: [PATCH] feat: Some changes
---
src/ai/workers/create-worker.ts | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/src/ai/workers/create-worker.ts b/src/ai/workers/create-worker.ts
index 4328ba4..63af774 100644
--- a/src/ai/workers/create-worker.ts
+++ b/src/ai/workers/create-worker.ts
@@ -1,4 +1,5 @@
-export function createWorker(): void {
- // TODO: implement your worker logic here
- console.log('createWorker called');
+// placeholder to begin.
+
+export function createWorkerPlaceholder() {
+ return 'placeholder';
}
--
2.39.5`;
const expextedDiff = `diff --git a/src/ai/workers/create-worker.ts b/src/ai/workers/create-worker.ts
index 4328ba4..63af774 100644
--- a/src/ai/workers/create-worker.ts
+++ b/src/ai/workers/create-worker.ts
@@ -1,4 +1,5 @@
-export function createWorker(): void {
- // TODO: implement your worker logic here
- console.log('createWorker called');
+// placeholder to begin.
+
+export function createWorkerPlaceholder() {
+ return 'placeholder';
}
`;
const commits = parseGitPatch(patch);
expect(commits).toHaveLength(1);
expect(commits[0].diff).toBe(expextedDiff);
});
});