-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.ts
More file actions
542 lines (467 loc) · 14.4 KB
/
index.test.ts
File metadata and controls
542 lines (467 loc) · 14.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
import { describe, expect, test } from "bun:test";
import {
filterResult,
formatHuman,
parseArgs,
shapeOutput,
type CliOptions,
type NormalizedThread,
type PullRequestResult,
} from "./index";
function stripAnsi(value: string): string {
const escape = String.fromCharCode(27);
const ansiPattern = new RegExp(`${escape}\\[[0-9;]*m`, "g");
return value.replace(ansiPattern, "");
}
const pullRequest: PullRequestResult["pullRequest"] = {
number: 42,
title: "Tighten review CLI output",
url: "https://github.com/octo/repo/pull/42",
state: "OPEN",
isDraft: false,
author: "octocat",
baseRefName: "main",
headRefName: "feature/review-cli",
repository: "octo/repo",
};
const threads: NormalizedThread[] = [
{
id: "thread-1",
isResolved: false,
isOutdated: false,
path: "src/index.ts",
line: 12,
originalLine: 12,
startLine: null,
originalStartLine: null,
diffSide: "RIGHT",
startDiffSide: null,
resolvedBy: null,
url: "https://github.com/octo/repo/pull/42#discussion_r1",
comments: [
{
id: "comment-1",
author: "monalisa",
body: "Please rename this variable.",
bodyText: "Please rename this variable.",
createdAt: "2026-03-25T19:00:00Z",
url: "https://github.com/octo/repo/pull/42#discussion_r1",
path: "src/index.ts",
outdated: false,
line: 12,
originalLine: 12,
startLine: null,
originalStartLine: null,
diffHunk: null,
replyToId: null,
review: {
id: "review-1",
state: "COMMENTED",
submittedAt: "2026-03-25T19:01:00Z",
url: "https://github.com/octo/repo/pull/42/files",
author: "monalisa",
},
},
{
id: "comment-2",
author: "hubot",
body: "Done.",
bodyText: "Done.",
createdAt: "2026-03-25T19:05:00Z",
url: "https://github.com/octo/repo/pull/42#discussion_r2",
path: "src/index.ts",
outdated: false,
line: 12,
originalLine: 12,
startLine: null,
originalStartLine: null,
diffHunk: null,
replyToId: "comment-1",
review: null,
},
],
},
{
id: "thread-2",
isResolved: true,
isOutdated: true,
path: "src/other.ts",
line: 30,
originalLine: 28,
startLine: 29,
originalStartLine: 27,
diffSide: "RIGHT",
startDiffSide: "RIGHT",
resolvedBy: "hubot",
url: "https://github.com/octo/repo/pull/42#discussion_r3",
comments: [
{
id: "comment-3",
author: "hubot",
body: "Looks good now.",
bodyText: "Looks good now.",
createdAt: "2026-03-25T19:10:00Z",
url: "https://github.com/octo/repo/pull/42#discussion_r3",
path: "src/other.ts",
outdated: true,
line: 30,
originalLine: 28,
startLine: 29,
originalStartLine: 27,
diffHunk: null,
replyToId: null,
review: {
id: "review-2",
state: "APPROVED",
submittedAt: "2026-03-25T19:11:00Z",
url: "https://github.com/octo/repo/pull/42/files",
author: "hubot",
},
},
],
},
];
const codeRabbitBody = `_⚠️ Potential issue_ | _🟠 Major_
**Replace \\b at regex start in \`findTextComponent\`.**
<details>
<summary>Suggested fix</summary>
\`\`\`diff
- const pattern = /\\bfunction/;
+ const pattern = /(?:^|[,;{}])function/;
\`\`\`
</details>
<!-- suggestion_start -->
<details>
<summary>🤖 Prompt for AI Agents</summary>
\`\`\`
Verify each finding against the current code and only fix it if needed.
\`\`\`
</details>
<!-- This is an auto-generated comment by CodeRabbit -->`;
const codeRabbitAnalysisBody = `_⚠️ Potential issue_ | _🟡 Minor_
🧩 Analysis chain:
🏁 Script executed:
\`\`\`bash
#!/bin/bash
rg -n 'rebuildBunData' --type=ts -C3
\`\`\`
Repository: Piebald-AI/tweakcc
Length of output: 1352
---
🏁 Script executed:
\`\`\`bash
cat -n src/nativeInstallation.ts | sed -n '753,820p'
\`\`\`
Repository: Piebald-AI/tweakcc
Length of output: 2994
---
Remove unreachable bytecode invalidation code in \`rebuildBunData\`.
The bytecode invalidation logic (lines 800-807) is dead code.
📝 Committable suggestion:
! ‼️ IMPORTANT
! Carefully review the code before committing.
Suggested change:
+ const value = 1;
🤖 Prompt for AI Agents:
Verify each finding against the current code and only fix it if needed.
<!-- This is an auto-generated comment by CodeRabbit -->`;
const codeRabbitPlainSuggestedFixBody = String.raw`_⚠️ Potential issue_ | _🟠 Major_
Missing validation that CJS wrapper replacement succeeded.
Suggested fix:
const replaced = rest.replace(
- /\)\s*$/,
+ /\)\s*$/,
')(exports, require, module, __filename, __dirname)\n'
);
if (replaced === rest) {
throw new Error(
'Failed to add CJS invocation: source does not end with expected wrapper format'
);
}
const callable = pragma + replaced;
Also applies to runtime wrapper construction.
🤖 Prompt for AI Agents:
Verify each finding against the current code and only fix it if needed.
<!-- This is an auto-generated comment by CodeRabbit -->`;
describe("parseArgs", () => {
test("parses positional pr, json, include-resolved, and repeated users", () => {
expect(
parseArgs([
"123",
"--json",
"--include-resolved",
"--filter",
"2",
"--just-reviews",
"--user",
"Monalisa",
"--user=hubot",
]),
).toEqual({
help: false,
json: true,
includeResolved: true,
filter: 2,
justReviews: true,
prNumber: 123,
users: ["Monalisa", "hubot"],
});
});
test("deduplicates users case-insensitively", () => {
expect(parseArgs(["--user", "Monalisa", "--user", "monalisa"]).users).toEqual(["Monalisa"]);
});
test("rejects invalid input", () => {
expect(() => parseArgs(["abc"])).toThrow("PR number must be a positive integer. Received: abc");
expect(() => parseArgs(["1", "2"])).toThrow("Unexpected extra positional argument: 2");
expect(() => parseArgs(["--filter"])).toThrow("Missing value for --filter.");
expect(() => parseArgs(["--filter", "0"])).toThrow(
"--filter must be a positive integer. Received: 0",
);
expect(() => parseArgs(["--user"])).toThrow("Missing value for --user.");
expect(() => parseArgs(["--bogus"])).toThrow("Unknown flag: --bogus");
});
});
describe("filterResult", () => {
test("keeps unresolved threads by default", () => {
const options: CliOptions = {
help: false,
json: false,
includeResolved: false,
justReviews: false,
users: [],
};
const result = filterResult(pullRequest, threads, options);
expect(result.threadCount).toBe(1);
expect(result.commentCount).toBe(2);
expect(result.threads[0]?.id).toBe("thread-1");
});
test("filters comments to selected users and keeps matching threads", () => {
const options: CliOptions = {
help: false,
json: false,
includeResolved: true,
justReviews: false,
users: ["hubot"],
};
const result = filterResult(pullRequest, threads, options);
expect(result.threadCount).toBe(2);
expect(result.commentCount).toBe(2);
expect(result.threads[0]?.comments.map((comment) => comment.author)).toEqual(["hubot"]);
});
test("limits the number of matching review threads", () => {
const result = filterResult(pullRequest, threads, {
help: false,
json: false,
includeResolved: true,
filter: 1,
justReviews: false,
users: [],
});
expect(result.threadCount).toBe(1);
expect(result.commentCount).toBe(2);
expect(result.threads.map((thread) => thread.id)).toEqual(["thread-1"]);
});
});
describe("formatHuman", () => {
test("renders a readable summary", () => {
const result = filterResult(pullRequest, threads, {
help: false,
json: false,
includeResolved: false,
justReviews: false,
users: ["monalisa"],
});
const output = stripAnsi(formatHuman(result));
expect(output).toContain("PR #42: Tighten review CLI output");
expect(output).toContain("Filters: unresolved only; users: monalisa");
expect(output).toContain("[UNRESOLVED] src/index.ts:12");
expect(output).toContain("Please rename this variable.");
});
test("renders suggestion blocks cleanly", () => {
const suggestionResult = filterResult(
pullRequest,
[
{
...threads[0]!,
comments: [
{
...threads[0]!.comments[0]!,
body: "```suggestion\nconst betterName = true;\n```\n\nPlease apply this.",
bodyText:
"Suggested change\n \n const betterName = true;\n\nPlease apply this.",
diffHunk: "@@ -12,1 +12,1 @@\n-const oldName = true;\n+const betterName = true;",
},
],
},
],
{
help: false,
json: false,
includeResolved: false,
justReviews: false,
users: [],
},
);
const output = stripAnsi(formatHuman(suggestionResult));
expect(output).toContain("Suggested change:");
expect(output).toContain("- const oldName = true;");
expect(output).toContain("+ const betterName = true;");
expect(output).toContain("Please apply this.");
});
test("renders an empty state", () => {
const result = filterResult(pullRequest, threads, {
help: false,
json: false,
includeResolved: false,
justReviews: false,
users: ["nobody"],
});
expect(stripAnsi(formatHuman(result))).toContain("No matching review threads found.");
});
test("omits pull request metadata in just-reviews mode", () => {
const result = filterResult(pullRequest, threads, {
help: false,
json: false,
includeResolved: false,
justReviews: true,
users: [],
});
const output = stripAnsi(formatHuman(result));
expect(output).toContain("Review data");
expect(output).not.toContain("PR #42: Tighten review CLI output");
expect(output).not.toContain("Repository:");
});
test("formats CodeRabbit comments without raw html boilerplate", () => {
const result = filterResult(
pullRequest,
[
{
...threads[0]!,
comments: [
{
...threads[0]!.comments[0]!,
author: "coderabbitai",
body: codeRabbitBody,
bodyText: "Potential issue",
},
],
},
],
{
help: false,
json: false,
includeResolved: false,
justReviews: true,
users: [],
},
);
const output = stripAnsi(formatHuman(result));
expect(output).toContain("⚠️ Potential issue | 🟠 Major");
expect(output).toContain("Suggested fix:");
expect(output).toContain("- const pattern = /\\bfunction/;");
expect(output).toContain("+ const pattern = /(?:^|[,;{}])function/;");
expect(output).toContain("🤖 Prompt for AI Agents:");
expect(output).toContain(
"Verify each finding against the current code and only fix it if needed.",
);
expect(output).not.toContain("<details>");
expect(output).not.toContain("<!--");
expect(output).not.toContain("```");
});
test("strips CodeRabbit analysis-chain noise and duplicate boilerplate", () => {
const result = filterResult(
pullRequest,
[
{
...threads[0]!,
comments: [
{
...threads[0]!.comments[0]!,
author: "coderabbitai",
body: codeRabbitAnalysisBody,
bodyText: "Potential issue",
},
],
},
],
{
help: false,
json: false,
includeResolved: false,
justReviews: true,
users: [],
},
);
const output = stripAnsi(formatHuman(result));
expect(output).toContain("⚠️ Potential issue | 🟡 Minor");
expect(output).toContain("Remove unreachable bytecode invalidation code in rebuildBunData.");
expect(output).toContain("The bytecode invalidation logic (lines 800-807) is dead code.");
expect(output).toContain("🤖 Prompt for AI Agents:");
expect(output).toContain(
"Verify each finding against the current code and only fix it if needed.",
);
expect(output).not.toContain("🧩 Analysis chain:");
expect(output).not.toContain("🏁 Script executed:");
expect(output).not.toContain("Repository: Piebald-AI/tweakcc");
expect(output).not.toContain("Length of output: 1352");
expect(output).not.toContain("📝 Committable suggestion:");
expect(output).not.toContain("Carefully review the code before committing.");
});
test("formats plain-text CodeRabbit suggested fix blocks like code diffs", () => {
const result = filterResult(
pullRequest,
[
{
...threads[0]!,
comments: [
{
...threads[0]!.comments[0]!,
author: "coderabbitai",
body: codeRabbitPlainSuggestedFixBody,
bodyText: "Potential issue",
},
],
},
],
{
help: false,
json: false,
includeResolved: false,
justReviews: true,
users: [],
},
);
const output = stripAnsi(formatHuman(result));
expect(output).toContain("Suggested fix:");
expect(output).toContain("const replaced = rest.replace(");
expect(output).toContain("- /\\)\\s*$/,");
expect(output).toContain("+ /\\)\\s*$/,");
expect(output).toContain("__filename");
expect(output).toContain("__dirname");
expect(output).toContain("const callable = pragma + replaced;");
expect(output).toContain("Also applies to runtime wrapper construction.");
expect(output).toContain("Prompt for AI Agents:");
});
});
describe("shapeOutput", () => {
test("drops pull request data in just-reviews mode", () => {
const result = filterResult(pullRequest, threads, {
help: false,
json: true,
includeResolved: false,
justReviews: true,
users: [],
});
expect(shapeOutput(result)).toEqual({
filters: {
includeResolved: false,
justReviews: true,
reviewLimit: null,
users: [],
},
threadCount: 1,
commentCount: 2,
threads: [result.threads[0]!],
});
});
});