Skip to content

Commit a46a3de

Browse files
committed
chore: expand handleComment tests for various scenarios including flags for updating and overwriting comments
1 parent dedc3c6 commit a46a3de

2 files changed

Lines changed: 244 additions & 1 deletion

File tree

__tests__/github/handler.test.ts

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,4 +403,247 @@ describe('handleComment', () => {
403403
expect(mockUpdateComment).not.toHaveBeenCalled()
404404
})
405405
})
406+
407+
describe('Existing PR - All flags disabled', () => {
408+
it('should create new comment when comment found and no flags enabled', async () => {
409+
const existingComments = [
410+
{
411+
id: 1,
412+
node_id: 'node1',
413+
url: 'url1',
414+
html_url: 'html1',
415+
body: 'Existing comment with MARKER',
416+
user: null,
417+
created_at: new Date().toISOString(),
418+
updated_at: new Date().toISOString()
419+
}
420+
] as IssueComment[]
421+
422+
mockListComments.mockResolvedValue(existingComments)
423+
424+
await handleComment('owner', 'repo', 1, 'New comment', 'MARKER', {
425+
shouldUpdate: false,
426+
shouldOverwrite: false,
427+
alwaysLatestComment: false
428+
})
429+
430+
expect(mockAddComment).toHaveBeenCalledWith(
431+
'owner',
432+
'repo',
433+
1,
434+
'New comment\nMARKER'
435+
)
436+
expect(mockUpdateComment).not.toHaveBeenCalled()
437+
})
438+
})
439+
440+
describe('Existing PR - updateComment enabled', () => {
441+
it('should update existing comment when updateComment enabled', async () => {
442+
const existingComments = [
443+
{
444+
id: 1,
445+
node_id: 'node1',
446+
url: 'url1',
447+
html_url: 'html1',
448+
body: 'Existing comment with MARKER',
449+
user: null,
450+
created_at: new Date().toISOString(),
451+
updated_at: new Date().toISOString()
452+
}
453+
] as IssueComment[]
454+
455+
mockListComments.mockResolvedValue(existingComments)
456+
457+
await handleComment('owner', 'repo', 1, 'Updated comment', 'MARKER', {
458+
shouldUpdate: true,
459+
shouldOverwrite: false,
460+
alwaysLatestComment: false
461+
})
462+
463+
expect(mockUpdateComment).toHaveBeenCalled()
464+
expect(mockAddComment).not.toHaveBeenCalled()
465+
})
466+
467+
it('should update existing comment when updateComment enabled and alwaysLatest enabled and comment is latest', async () => {
468+
const existingComments = [
469+
{
470+
id: 1,
471+
node_id: 'node1',
472+
url: 'url1',
473+
html_url: 'html1',
474+
body: 'First comment without marker',
475+
user: null,
476+
created_at: new Date().toISOString(),
477+
updated_at: new Date().toISOString()
478+
},
479+
{
480+
id: 2,
481+
node_id: 'node2',
482+
url: 'url2',
483+
html_url: 'html2',
484+
body: 'Latest comment with MARKER',
485+
user: null,
486+
created_at: new Date().toISOString(),
487+
updated_at: new Date().toISOString()
488+
}
489+
] as IssueComment[]
490+
491+
mockListComments.mockResolvedValue(existingComments)
492+
493+
await handleComment('owner', 'repo', 1, 'Updated comment', 'MARKER', {
494+
shouldUpdate: true,
495+
shouldOverwrite: false,
496+
alwaysLatestComment: true
497+
})
498+
499+
expect(mockUpdateComment).toHaveBeenCalled()
500+
expect(mockAddComment).not.toHaveBeenCalled()
501+
})
502+
503+
it('should create new comment when updateComment enabled and alwaysLatest enabled and comment is not latest', async () => {
504+
const existingComments = [
505+
{
506+
id: 1,
507+
node_id: 'node1',
508+
url: 'url1',
509+
html_url: 'html1',
510+
body: 'Comment with MARKER',
511+
user: null,
512+
created_at: new Date().toISOString(),
513+
updated_at: new Date().toISOString()
514+
},
515+
{
516+
id: 2,
517+
node_id: 'node2',
518+
url: 'url2',
519+
html_url: 'html2',
520+
body: 'Latest comment without marker',
521+
user: null,
522+
created_at: new Date().toISOString(),
523+
updated_at: new Date().toISOString()
524+
}
525+
] as IssueComment[]
526+
527+
mockListComments.mockResolvedValue(existingComments)
528+
529+
await handleComment('owner', 'repo', 1, 'New comment', 'MARKER', {
530+
shouldUpdate: true,
531+
shouldOverwrite: false,
532+
alwaysLatestComment: true
533+
})
534+
535+
expect(mockAddComment).toHaveBeenCalledWith(
536+
'owner',
537+
'repo',
538+
1,
539+
'New comment\nMARKER'
540+
)
541+
expect(mockUpdateComment).not.toHaveBeenCalled()
542+
})
543+
})
544+
545+
describe('Existing PR - overwriteComment enabled', () => {
546+
it('should overwrite existing comment when overwriteComment enabled', async () => {
547+
const existingComments = [
548+
{
549+
id: 1,
550+
node_id: 'node1',
551+
url: 'url1',
552+
html_url: 'html1',
553+
body: 'Existing comment with MARKER',
554+
user: null,
555+
created_at: new Date().toISOString(),
556+
updated_at: new Date().toISOString()
557+
}
558+
] as IssueComment[]
559+
560+
mockListComments.mockResolvedValue(existingComments)
561+
562+
await handleComment('owner', 'repo', 1, 'Overwritten comment', 'MARKER', {
563+
shouldUpdate: false,
564+
shouldOverwrite: true,
565+
alwaysLatestComment: false
566+
})
567+
568+
expect(mockUpdateComment).toHaveBeenCalled()
569+
expect(mockAddComment).not.toHaveBeenCalled()
570+
})
571+
572+
it('should overwrite existing comment when overwriteComment enabled and alwaysLatest enabled and comment is latest', async () => {
573+
const existingComments = [
574+
{
575+
id: 1,
576+
node_id: 'node1',
577+
url: 'url1',
578+
html_url: 'html1',
579+
body: 'First comment without marker',
580+
user: null,
581+
created_at: new Date().toISOString(),
582+
updated_at: new Date().toISOString()
583+
},
584+
{
585+
id: 2,
586+
node_id: 'node2',
587+
url: 'url2',
588+
html_url: 'html2',
589+
body: 'Latest comment with MARKER',
590+
user: null,
591+
created_at: new Date().toISOString(),
592+
updated_at: new Date().toISOString()
593+
}
594+
] as IssueComment[]
595+
596+
mockListComments.mockResolvedValue(existingComments)
597+
598+
await handleComment('owner', 'repo', 1, 'Overwritten comment', 'MARKER', {
599+
shouldUpdate: false,
600+
shouldOverwrite: true,
601+
alwaysLatestComment: true
602+
})
603+
604+
expect(mockUpdateComment).toHaveBeenCalled()
605+
expect(mockAddComment).not.toHaveBeenCalled()
606+
})
607+
608+
it('should create new comment when overwriteComment enabled and alwaysLatest enabled and comment is not latest', async () => {
609+
const existingComments = [
610+
{
611+
id: 1,
612+
node_id: 'node1',
613+
url: 'url1',
614+
html_url: 'html1',
615+
body: 'Comment with MARKER',
616+
user: null,
617+
created_at: new Date().toISOString(),
618+
updated_at: new Date().toISOString()
619+
},
620+
{
621+
id: 2,
622+
node_id: 'node2',
623+
url: 'url2',
624+
html_url: 'html2',
625+
body: 'Latest comment without marker',
626+
user: null,
627+
created_at: new Date().toISOString(),
628+
updated_at: new Date().toISOString()
629+
}
630+
] as IssueComment[]
631+
632+
mockListComments.mockResolvedValue(existingComments)
633+
634+
await handleComment('owner', 'repo', 1, 'New comment', 'MARKER', {
635+
shouldUpdate: false,
636+
shouldOverwrite: true,
637+
alwaysLatestComment: true
638+
})
639+
640+
expect(mockAddComment).toHaveBeenCalledWith(
641+
'owner',
642+
'repo',
643+
1,
644+
'New comment\nMARKER'
645+
)
646+
expect(mockUpdateComment).not.toHaveBeenCalled()
647+
})
648+
})
406649
})

badges/coverage.svg

Lines changed: 1 addition & 1 deletion
Loading

0 commit comments

Comments
 (0)