-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodecommit.test.ts
More file actions
1882 lines (1689 loc) · 55.6 KB
/
codecommit.test.ts
File metadata and controls
1882 lines (1689 loc) · 55.6 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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { beforeEach, describe, expect, it, vi } from "vitest";
import {
closePullRequest,
createClient,
deleteComment,
evaluateApprovalRules,
getApprovalStates,
getBlobContent,
getComments,
getCommit,
getCommitDifferences,
getCommitsForPR,
getMergeConflicts,
getPullRequestDetail,
getReactionsForComment,
getReactionsForComments,
listPullRequests,
listRepositories,
mergePullRequest,
postComment,
postCommentReply,
putReaction,
updateApprovalState,
updateComment,
} from "./codecommit.js";
const mockSend = vi.fn();
const mockClient = {
send: mockSend,
} as any;
beforeEach(() => {
mockSend.mockReset();
});
describe("createClient", () => {
it("returns a CodeCommitClient instance with provided config", () => {
const client = createClient({ profile: "dev", region: "ap-northeast-1" });
expect(client).toBeInstanceOf(Object);
expect(client.send).toBeDefined();
});
});
describe("listRepositories", () => {
it("returns repositories from API response", async () => {
mockSend.mockResolvedValueOnce({
repositories: [
{ repositoryName: "my-service", repositoryId: "1" },
{ repositoryName: "my-frontend", repositoryId: "2" },
],
});
const repos = await listRepositories(mockClient);
expect(repos).toHaveLength(2);
expect(repos[0].repositoryName).toBe("my-service");
});
it("returns empty array when no repositories", async () => {
mockSend.mockResolvedValueOnce({ repositories: undefined });
const repos = await listRepositories(mockClient);
expect(repos).toHaveLength(0);
});
});
describe("listPullRequests", () => {
it("returns pull request summaries with OPEN status", async () => {
mockSend.mockResolvedValueOnce({
pullRequestIds: ["42"],
nextToken: undefined,
});
mockSend.mockResolvedValueOnce({
pullRequest: {
pullRequestId: "42",
title: "fix: login timeout",
authorArn: "arn:aws:iam::123456789012:user/watany",
pullRequestStatus: "OPEN",
creationDate: new Date("2026-02-13T10:00:00Z"),
},
});
const result = await listPullRequests(mockClient, "my-service");
expect(result.pullRequests).toHaveLength(1);
expect(result.pullRequests[0].title).toBe("fix: login timeout");
expect(result.pullRequests[0].status).toBe("OPEN");
});
it("returns MERGED status when mergeMetadata.isMerged is true", async () => {
mockSend.mockResolvedValueOnce({
pullRequestIds: ["40"],
nextToken: undefined,
});
mockSend.mockResolvedValueOnce({
pullRequest: {
pullRequestId: "40",
title: "feat: auth",
authorArn: "arn:aws:iam::123456789012:user/watany",
pullRequestStatus: "CLOSED",
creationDate: new Date("2026-02-11T10:00:00Z"),
pullRequestTargets: [
{
mergeMetadata: {
isMerged: true,
mergedBy: "arn:aws:iam::123456789012:user/taro",
mergeCommitId: "abc123",
mergeOption: "SQUASH_MERGE",
},
},
],
},
});
const result = await listPullRequests(mockClient, "my-service");
expect(result.pullRequests).toHaveLength(1);
expect(result.pullRequests[0].status).toBe("MERGED");
});
it("passes pullRequestStatus OPEN to API by default", async () => {
mockSend.mockResolvedValueOnce({ pullRequestIds: [], nextToken: undefined });
await listPullRequests(mockClient, "my-service");
const sentCommand = mockSend.mock.calls[0][0];
expect(sentCommand.input.pullRequestStatus).toBe("OPEN");
});
it("passes pullRequestStatus CLOSED to API when specified", async () => {
mockSend.mockResolvedValueOnce({ pullRequestIds: [], nextToken: undefined });
await listPullRequests(mockClient, "my-service", undefined, "CLOSED");
const sentCommand = mockSend.mock.calls[0][0];
expect(sentCommand.input.pullRequestStatus).toBe("CLOSED");
});
it("passes pullRequestStatus OPEN to API when specified", async () => {
mockSend.mockResolvedValueOnce({ pullRequestIds: [], nextToken: undefined });
await listPullRequests(mockClient, "my-service", undefined, "OPEN");
const sentCommand = mockSend.mock.calls[0][0];
expect(sentCommand.input.pullRequestStatus).toBe("OPEN");
});
it("returns nextToken when API provides one", async () => {
mockSend.mockResolvedValueOnce({
pullRequestIds: [],
nextToken: "next-page-token",
});
const result = await listPullRequests(mockClient, "my-service");
expect(result.nextToken).toBe("next-page-token");
});
it("returns no nextToken on last page", async () => {
mockSend.mockResolvedValueOnce({
pullRequestIds: [],
nextToken: undefined,
});
const result = await listPullRequests(mockClient, "my-service");
expect(result.nextToken).toBeUndefined();
});
it("returns CLOSED status when mergeMetadata.isMerged is false", async () => {
mockSend.mockResolvedValueOnce({
pullRequestIds: ["35"],
nextToken: undefined,
});
mockSend.mockResolvedValueOnce({
pullRequest: {
pullRequestId: "35",
title: "fix: typos",
authorArn: "arn:aws:iam::123456789012:user/hanako",
pullRequestStatus: "CLOSED",
creationDate: new Date("2026-02-09T10:00:00Z"),
pullRequestTargets: [
{
mergeMetadata: {
isMerged: false,
},
},
],
},
});
const result = await listPullRequests(mockClient, "my-service");
expect(result.pullRequests).toHaveLength(1);
expect(result.pullRequests[0].status).toBe("CLOSED");
});
});
describe("getPullRequestDetail", () => {
it("fetches pull request with differences and comments", async () => {
mockSend.mockResolvedValueOnce({
pullRequest: {
pullRequestId: "42",
title: "fix: login timeout",
pullRequestTargets: [
{
sourceCommit: "abc123",
destinationCommit: "def456",
},
],
},
});
mockSend.mockResolvedValueOnce({
differences: [
{
beforeBlob: { blobId: "b1", path: "src/auth.ts" },
afterBlob: { blobId: "b2", path: "src/auth.ts" },
},
],
});
mockSend.mockResolvedValueOnce({
commentsForPullRequestData: [
{
comments: [
{
authorArn: "arn:aws:iam::123456789012:user/taro",
content: "LGTM",
},
],
},
],
});
const detail = await getPullRequestDetail(mockClient, "42", "my-service");
expect(detail.pullRequest.title).toBe("fix: login timeout");
expect(detail.differences).toHaveLength(1);
expect(detail.commentThreads).toHaveLength(1);
expect(detail.commentThreads[0].location).toBeNull();
expect(detail.commentThreads[0].comments[0].content).toBe("LGTM");
});
it("passes commit IDs and repositoryName to GetCommentsForPullRequest", async () => {
mockSend.mockResolvedValueOnce({
pullRequest: {
pullRequestId: "42",
title: "fix: login timeout",
pullRequestTargets: [
{
sourceCommit: "abc123",
destinationCommit: "def456",
},
],
},
});
mockSend.mockResolvedValueOnce({ differences: [] });
mockSend.mockResolvedValueOnce({ commentsForPullRequestData: [] });
await getPullRequestDetail(mockClient, "42", "my-service");
// Third call is GetCommentsForPullRequestCommand
const commentsCall = mockSend.mock.calls[2][0];
expect(commentsCall.input).toEqual(
expect.objectContaining({
pullRequestId: "42",
repositoryName: "my-service",
afterCommitId: "abc123",
beforeCommitId: "def456",
}),
);
});
it("paginates GetDifferences when NextToken is returned", async () => {
mockSend.mockResolvedValueOnce({
pullRequest: {
pullRequestId: "42",
title: "big PR",
pullRequestTargets: [
{
sourceCommit: "abc123",
destinationCommit: "def456",
},
],
},
});
// Promise.all runs getAllDifferences and getComments concurrently.
// Mock order matches actual call order:
// 1. GetDifferencesCommand (page 1) - from getAllDifferences
// 2. GetCommentsForPullRequestCommand - from getComments (concurrent)
// 3. GetDifferencesCommand (page 2) - from getAllDifferences loop
mockSend.mockResolvedValueOnce({
differences: [
{
beforeBlob: { blobId: "b1", path: "file1.ts" },
afterBlob: { blobId: "b2", path: "file1.ts" },
},
],
NextToken: "page2-token",
});
mockSend.mockResolvedValueOnce({ commentsForPullRequestData: [] });
mockSend.mockResolvedValueOnce({
differences: [
{
beforeBlob: { blobId: "b3", path: "file2.ts" },
afterBlob: { blobId: "b4", path: "file2.ts" },
},
],
NextToken: undefined,
});
const detail = await getPullRequestDetail(mockClient, "42", "my-service");
expect(detail.differences).toHaveLength(2);
expect(detail.differences[0].beforeBlob?.path).toBe("file1.ts");
expect(detail.differences[1].beforeBlob?.path).toBe("file2.ts");
});
});
describe("listPullRequests edge cases", () => {
it("returns empty array when no PR IDs", async () => {
mockSend.mockResolvedValueOnce({
pullRequestIds: undefined,
nextToken: undefined,
});
const result = await listPullRequests(mockClient, "my-service");
expect(result.pullRequests).toHaveLength(0);
});
it("handles PR without full data", async () => {
mockSend.mockResolvedValueOnce({
pullRequestIds: ["99"],
nextToken: "token123",
});
mockSend.mockResolvedValueOnce({
pullRequest: {
pullRequestId: undefined,
title: undefined,
authorArn: undefined,
creationDate: undefined,
},
});
const result = await listPullRequests(mockClient, "my-service");
expect(result.pullRequests).toHaveLength(1);
expect(result.pullRequests[0].title).toBe("(no title)");
expect(result.pullRequests[0].status).toBe("OPEN");
expect(result.nextToken).toBe("token123");
});
it("skips when pullRequest is undefined in response", async () => {
mockSend.mockResolvedValueOnce({
pullRequestIds: ["99"],
nextToken: undefined,
});
mockSend.mockResolvedValueOnce({
pullRequest: undefined,
});
const result = await listPullRequests(mockClient, "my-service");
expect(result.pullRequests).toHaveLength(0);
});
});
describe("getPullRequestDetail edge cases", () => {
it("throws when pullRequest is missing from response", async () => {
mockSend.mockResolvedValueOnce({
pullRequest: undefined,
});
await expect(getPullRequestDetail(mockClient, "42", "my-service")).rejects.toThrow(
"Pull request 42 not found.",
);
});
it("handles pull request without targets", async () => {
mockSend.mockResolvedValueOnce({
pullRequest: {
pullRequestId: "42",
title: "fix: something",
pullRequestTargets: [],
},
});
mockSend.mockResolvedValueOnce({
commentsForPullRequestData: [],
});
const detail = await getPullRequestDetail(mockClient, "42", "my-service");
expect(detail.differences).toHaveLength(0);
expect(detail.commentThreads).toHaveLength(0);
});
it("handles undefined differences in response", async () => {
mockSend.mockResolvedValueOnce({
pullRequest: {
pullRequestId: "42",
pullRequestTargets: [
{
sourceCommit: "abc",
destinationCommit: "def",
},
],
},
});
mockSend.mockResolvedValueOnce({
differences: undefined,
});
mockSend.mockResolvedValueOnce({
commentsForPullRequestData: [],
});
const detail = await getPullRequestDetail(mockClient, "42", "my-service");
expect(detail.differences).toHaveLength(0);
});
it("handles empty comments data", async () => {
mockSend.mockResolvedValueOnce({
pullRequest: {
pullRequestId: "42",
pullRequestTargets: [],
},
});
mockSend.mockResolvedValueOnce({
commentsForPullRequestData: undefined,
});
const detail = await getPullRequestDetail(mockClient, "42", "my-service");
expect(detail.commentThreads).toHaveLength(0);
});
it("handles thread with no comments", async () => {
mockSend.mockResolvedValueOnce({
pullRequest: {
pullRequestId: "42",
pullRequestTargets: [],
},
});
mockSend.mockResolvedValueOnce({
commentsForPullRequestData: [{ comments: undefined }],
});
const detail = await getPullRequestDetail(mockClient, "42", "my-service");
expect(detail.commentThreads).toHaveLength(1);
expect(detail.commentThreads[0].comments).toHaveLength(0);
});
it("defaults filePosition and relativeFileVersion in getPullRequestDetail", async () => {
mockSend.mockResolvedValueOnce({
pullRequest: {
pullRequestId: "42",
pullRequestTargets: [],
},
});
mockSend.mockResolvedValueOnce({
commentsForPullRequestData: [
{
location: {
filePath: "src/x.ts",
filePosition: undefined,
relativeFileVersion: undefined,
},
comments: [{ commentId: "c1", content: "test" }],
},
],
});
const detail = await getPullRequestDetail(mockClient, "42", "my-service");
expect(detail.commentThreads[0].location).toEqual({
filePath: "src/x.ts",
filePosition: 0,
relativeFileVersion: "AFTER",
});
});
it("preserves inline comment location in threads", async () => {
mockSend.mockResolvedValueOnce({
pullRequest: {
pullRequestId: "42",
pullRequestTargets: [],
},
});
mockSend.mockResolvedValueOnce({
commentsForPullRequestData: [
{
location: {
filePath: "src/index.ts",
filePosition: 5,
relativeFileVersion: "BEFORE",
},
comments: [{ commentId: "c1", content: "fix this" }],
},
],
});
const detail = await getPullRequestDetail(mockClient, "42", "my-service");
expect(detail.commentThreads).toHaveLength(1);
expect(detail.commentThreads[0].location).toEqual({
filePath: "src/index.ts",
filePosition: 5,
relativeFileVersion: "BEFORE",
});
});
});
describe("postComment", () => {
it("posts a comment and returns the result", async () => {
const mockComment = {
commentId: "comment-1",
content: "Looks good!",
authorArn: "arn:aws:iam::123456789012:user/watany",
creationDate: new Date("2026-02-13T12:00:00Z"),
};
mockSend.mockResolvedValueOnce({ comment: mockComment });
const result = await postComment(mockClient, {
pullRequestId: "42",
repositoryName: "my-service",
beforeCommitId: "def456",
afterCommitId: "abc123",
content: "Looks good!",
});
expect(result).toEqual(mockComment);
expect(mockSend).toHaveBeenCalledWith(
expect.objectContaining({
input: {
pullRequestId: "42",
repositoryName: "my-service",
beforeCommitId: "def456",
afterCommitId: "abc123",
content: "Looks good!",
},
}),
);
});
it("posts a comment with location parameter", async () => {
const mockComment = {
commentId: "comment-2",
content: "Fix this line",
authorArn: "arn:aws:iam::123456789012:user/watany",
};
mockSend.mockResolvedValueOnce({ comment: mockComment });
const result = await postComment(mockClient, {
pullRequestId: "42",
repositoryName: "my-service",
beforeCommitId: "def456",
afterCommitId: "abc123",
content: "Fix this line",
location: {
filePath: "src/auth.ts",
filePosition: 10,
relativeFileVersion: "AFTER",
},
});
expect(result).toEqual(mockComment);
expect(mockSend).toHaveBeenCalledWith(
expect.objectContaining({
input: {
pullRequestId: "42",
repositoryName: "my-service",
beforeCommitId: "def456",
afterCommitId: "abc123",
content: "Fix this line",
location: {
filePath: "src/auth.ts",
filePosition: 10,
relativeFileVersion: "AFTER",
},
},
}),
);
});
it("posts a comment without location (general comment)", async () => {
const mockComment = {
commentId: "comment-3",
content: "General",
};
mockSend.mockResolvedValueOnce({ comment: mockComment });
await postComment(mockClient, {
pullRequestId: "42",
repositoryName: "my-service",
beforeCommitId: "def456",
afterCommitId: "abc123",
content: "General",
});
expect(mockSend).toHaveBeenCalledWith(
expect.objectContaining({
input: {
pullRequestId: "42",
repositoryName: "my-service",
beforeCommitId: "def456",
afterCommitId: "abc123",
content: "General",
location: undefined,
},
}),
);
});
it("propagates API errors", async () => {
const error = new Error("Access denied");
error.name = "AccessDeniedException";
mockSend.mockRejectedValueOnce(error);
await expect(
postComment(mockClient, {
pullRequestId: "42",
repositoryName: "my-service",
beforeCommitId: "def456",
afterCommitId: "abc123",
content: "test",
}),
).rejects.toThrow("Access denied");
});
});
describe("getComments", () => {
it("returns comment threads from API response", async () => {
mockSend.mockResolvedValueOnce({
commentsForPullRequestData: [
{
comments: [
{
commentId: "comment-1",
content: "First comment",
authorArn: "arn:aws:iam::123456789012:user/alice",
creationDate: new Date("2026-02-13T10:00:00Z"),
},
{
commentId: "comment-2",
content: "Second comment",
authorArn: "arn:aws:iam::123456789012:user/bob",
creationDate: new Date("2026-02-13T11:00:00Z"),
},
],
},
],
});
const threads = await getComments(mockClient, "42");
expect(threads).toHaveLength(1);
expect(threads[0].location).toBeNull();
expect(threads[0].comments).toHaveLength(2);
expect(threads[0].comments[0].content).toBe("First comment");
expect(threads[0].comments[1].content).toBe("Second comment");
});
it("handles empty comments data", async () => {
mockSend.mockResolvedValueOnce({
commentsForPullRequestData: undefined,
});
const threads = await getComments(mockClient, "42");
expect(threads).toHaveLength(0);
});
it("handles thread with no comments", async () => {
mockSend.mockResolvedValueOnce({
commentsForPullRequestData: [{ comments: undefined }],
});
const threads = await getComments(mockClient, "42");
expect(threads).toHaveLength(1);
expect(threads[0].comments).toHaveLength(0);
});
it("returns location for inline comment threads", async () => {
mockSend.mockResolvedValueOnce({
commentsForPullRequestData: [
{
location: {
filePath: "src/auth.ts",
filePosition: 10,
relativeFileVersion: "AFTER",
},
comments: [{ commentId: "c1", content: "inline comment" }],
},
{
comments: [{ commentId: "c2", content: "general comment" }],
},
],
});
const threads = await getComments(mockClient, "42");
expect(threads).toHaveLength(2);
expect(threads[0].location).toEqual({
filePath: "src/auth.ts",
filePosition: 10,
relativeFileVersion: "AFTER",
});
expect(threads[1].location).toBeNull();
});
it("defaults filePosition and relativeFileVersion when missing", async () => {
mockSend.mockResolvedValueOnce({
commentsForPullRequestData: [
{
location: {
filePath: "src/app.ts",
filePosition: undefined,
relativeFileVersion: undefined,
},
comments: [{ commentId: "c1", content: "inline" }],
},
],
});
const threads = await getComments(mockClient, "42");
expect(threads).toHaveLength(1);
expect(threads[0].location).toEqual({
filePath: "src/app.ts",
filePosition: 0,
relativeFileVersion: "AFTER",
});
});
it("treats location without filePath as general comment", async () => {
mockSend.mockResolvedValueOnce({
commentsForPullRequestData: [
{
location: { filePath: undefined, filePosition: 5 },
comments: [{ commentId: "c1", content: "comment" }],
},
],
});
const threads = await getComments(mockClient, "42");
expect(threads).toHaveLength(1);
expect(threads[0].location).toBeNull();
});
it("passes commit IDs and repositoryName when provided", async () => {
mockSend.mockResolvedValueOnce({ commentsForPullRequestData: [] });
await getComments(mockClient, "42", {
repositoryName: "my-service",
afterCommitId: "abc123",
beforeCommitId: "def456",
});
expect(mockSend).toHaveBeenCalledWith(
expect.objectContaining({
input: expect.objectContaining({
pullRequestId: "42",
repositoryName: "my-service",
afterCommitId: "abc123",
beforeCommitId: "def456",
}),
}),
);
});
it("omits commit IDs when only one is provided", async () => {
mockSend.mockResolvedValueOnce({ commentsForPullRequestData: [] });
await getComments(mockClient, "42", {
repositoryName: "my-service",
afterCommitId: "abc123",
});
const callInput = mockSend.mock.calls[0][0].input;
expect(callInput.pullRequestId).toBe("42");
expect(callInput.repositoryName).toBe("my-service");
expect(callInput.afterCommitId).toBeUndefined();
expect(callInput.beforeCommitId).toBeUndefined();
});
it("sorts comments so root comment comes before replies", async () => {
mockSend.mockResolvedValueOnce({
commentsForPullRequestData: [
{
comments: [
{
commentId: "reply-1",
inReplyTo: "root-1",
content: "This is a reply",
authorArn: "arn:aws:iam::123456789012:user/bob",
},
{
commentId: "root-1",
content: "This is the root comment",
authorArn: "arn:aws:iam::123456789012:user/alice",
},
],
},
],
});
const threads = await getComments(mockClient, "42");
expect(threads).toHaveLength(1);
expect(threads[0].comments[0].commentId).toBe("root-1");
expect(threads[0].comments[0].inReplyTo).toBeUndefined();
expect(threads[0].comments[1].commentId).toBe("reply-1");
expect(threads[0].comments[1].inReplyTo).toBe("root-1");
});
it("sorts replies by creationDate within a thread", async () => {
mockSend.mockResolvedValueOnce({
commentsForPullRequestData: [
{
comments: [
{
commentId: "reply-2",
inReplyTo: "root-1",
content: "Second reply",
creationDate: new Date("2026-02-14T12:00:00Z"),
},
{
commentId: "root-1",
content: "Root",
creationDate: new Date("2026-02-14T10:00:00Z"),
},
{
commentId: "reply-1",
inReplyTo: "root-1",
content: "First reply",
creationDate: new Date("2026-02-14T11:00:00Z"),
},
],
},
],
});
const threads = await getComments(mockClient, "42");
expect(threads[0].comments[0].commentId).toBe("root-1");
expect(threads[0].comments[1].commentId).toBe("reply-1");
expect(threads[0].comments[2].commentId).toBe("reply-2");
});
it("handles replies with missing creationDate during sort", async () => {
mockSend.mockResolvedValueOnce({
commentsForPullRequestData: [
{
comments: [
{
commentId: "reply-2",
inReplyTo: "root-1",
content: "Has date",
creationDate: new Date("2026-02-14T12:00:00Z"),
},
{
commentId: "root-1",
content: "Root",
},
{
commentId: "reply-1",
inReplyTo: "root-1",
content: "No date",
},
{
commentId: "reply-3",
inReplyTo: "root-1",
content: "Also no date",
},
],
},
],
});
const threads = await getComments(mockClient, "42");
expect(threads[0].comments[0].commentId).toBe("root-1");
// Replies without creationDate (epoch 0) come before those with dates
expect(threads[0].comments[1].commentId).toBe("reply-1");
expect(threads[0].comments[2].commentId).toBe("reply-3");
expect(threads[0].comments[3].commentId).toBe("reply-2");
});
it("paginates when nextToken is returned", async () => {
mockSend.mockResolvedValueOnce({
commentsForPullRequestData: [
{
comments: [{ commentId: "c1", content: "Page 1 comment" }],
},
],
nextToken: "page2-token",
});
mockSend.mockResolvedValueOnce({
commentsForPullRequestData: [
{
comments: [{ commentId: "c2", content: "Page 2 comment" }],
},
],
nextToken: undefined,
});
const threads = await getComments(mockClient, "42");
expect(threads).toHaveLength(2);
expect(threads[0].comments[0].commentId).toBe("c1");
expect(threads[1].comments[0].commentId).toBe("c2");
expect(mockSend).toHaveBeenCalledTimes(2);
// Second call should include nextToken
expect(mockSend.mock.calls[1][0].input.nextToken).toBe("page2-token");
});
});
describe("postCommentReply", () => {
it("posts a reply with inReplyTo and content", async () => {
const mockReply = {
commentId: "reply-1",
content: "Will fix in next PR",
authorArn: "arn:aws:iam::123456789012:user/watany",
inReplyTo: "comment-1",
};
mockSend.mockResolvedValueOnce({ comment: mockReply });
const result = await postCommentReply(mockClient, {
inReplyTo: "comment-1",
content: "Will fix in next PR",
});
expect(result).toEqual(mockReply);
expect(mockSend).toHaveBeenCalledWith(
expect.objectContaining({
input: {
inReplyTo: "comment-1",
content: "Will fix in next PR",
},
}),
);
});
it("propagates API errors", async () => {
const error = new Error("comment not found");
error.name = "CommentDoesNotExistException";
mockSend.mockRejectedValueOnce(error);
await expect(
postCommentReply(mockClient, {
inReplyTo: "nonexistent",
content: "reply",
}),
).rejects.toThrow("comment not found");
});
});
describe("updateApprovalState", () => {
it("sends Approve command with correct parameters", async () => {
mockSend.mockResolvedValueOnce({});
await updateApprovalState(mockClient, {
pullRequestId: "42",
revisionId: "rev-1",
approvalState: "APPROVE",
});
expect(mockSend).toHaveBeenCalledWith(
expect.objectContaining({
input: {
pullRequestId: "42",
revisionId: "rev-1",
approvalState: "APPROVE",
},
}),
);
});
it("sends Revoke command with correct parameters", async () => {
mockSend.mockResolvedValueOnce({});
await updateApprovalState(mockClient, {
pullRequestId: "42",
revisionId: "rev-1",
approvalState: "REVOKE",
});
expect(mockSend).toHaveBeenCalledWith(
expect.objectContaining({
input: {
pullRequestId: "42",
revisionId: "rev-1",
approvalState: "REVOKE",
},
}),
);
});
it("propagates API errors", async () => {
const error = new Error("Access denied");
error.name = "AccessDeniedException";
mockSend.mockRejectedValueOnce(error);
await expect(
updateApprovalState(mockClient, {
pullRequestId: "42",
revisionId: "rev-1",
approvalState: "APPROVE",
}),
).rejects.toThrow("Access denied");
});
});
describe("getApprovalStates", () => {
it("returns approval list when approvals exist", async () => {
mockSend.mockResolvedValueOnce({
approvals: [{ userArn: "arn:aws:iam::123456789012:user/taro", approvalState: "APPROVE" }],
});
const result = await getApprovalStates(mockClient, {
pullRequestId: "42",
revisionId: "rev-1",
});
expect(result).toHaveLength(1);
expect(result[0].approvalState).toBe("APPROVE");
});
it("returns empty array when no approvals", async () => {
mockSend.mockResolvedValueOnce({ approvals: undefined });
const result = await getApprovalStates(mockClient, {
pullRequestId: "42",
revisionId: "rev-1",
});
expect(result).toHaveLength(0);
});
it("propagates API errors", async () => {
const error = new Error("not found");
error.name = "PullRequestDoesNotExistException";
mockSend.mockRejectedValueOnce(error);
await expect(
getApprovalStates(mockClient, {
pullRequestId: "42",
revisionId: "rev-1",
}),
).rejects.toThrow("not found");