@@ -15,6 +15,7 @@ import { Uri } from 'vscode';
1515import { MockExtensionContext } from '../mocks/mockExtensionContext' ;
1616import { GitHubManager } from '../../authentication/githubServer' ;
1717import { GitHubServerType } from '../../common/authentication' ;
18+ import { CheckState , PullRequestCheckStatus } from '../../github/interface' ;
1819
1920describe ( 'GitHubRepository' , function ( ) {
2021 let sinon : SinonSandbox ;
@@ -52,4 +53,82 @@ describe('GitHubRepository', function () {
5253 // assert(! dotcomRepository.isGitHubDotCom);
5354 } ) ;
5455 } ) ;
56+
57+ describe ( 'deduplicateStatusChecks' , function ( ) {
58+ function createStatus ( overrides : Partial < PullRequestCheckStatus > & { id : string ; context : string } ) : PullRequestCheckStatus {
59+ return {
60+ databaseId : undefined ,
61+ url : undefined ,
62+ avatarUrl : undefined ,
63+ state : CheckState . Success ,
64+ description : null ,
65+ targetUrl : null ,
66+ workflowName : undefined ,
67+ event : undefined ,
68+ isRequired : false ,
69+ isCheckRun : true ,
70+ ...overrides ,
71+ } ;
72+ }
73+
74+ function callDeduplicateStatusChecks ( repo : GitHubRepository , statuses : PullRequestCheckStatus [ ] ) : PullRequestCheckStatus [ ] {
75+ return ( repo as any ) . deduplicateStatusChecks ( statuses ) ;
76+ }
77+
78+ let repo : GitHubRepository ;
79+
80+ beforeEach ( function ( ) {
81+ const url = 'https://github.com/some/repo' ;
82+ const remote = new GitHubRemote ( 'origin' , url , new Protocol ( url ) , GitHubServerType . GitHubDotCom ) ;
83+ const rootUri = Uri . file ( 'C:\\users\\test\\repo' ) ;
84+ repo = new GitHubRepository ( 1 , remote , rootUri , credentialStore , telemetry ) ;
85+ } ) ;
86+
87+ it ( 'keeps checks with different events as separate entries' , function ( ) {
88+ const statuses = [
89+ createStatus ( { id : '1' , context : 'Build Linux / x86-64' , event : 'push' , workflowName : 'Build Linux' } ) ,
90+ createStatus ( { id : '2' , context : 'Build Linux / x86-64' , event : 'pull_request' , workflowName : 'Build Linux' } ) ,
91+ ] ;
92+ const result = callDeduplicateStatusChecks ( repo , statuses ) ;
93+ assert . strictEqual ( result . length , 2 ) ;
94+ } ) ;
95+
96+ it ( 'deduplicates checks with the same name, event, and workflow' , function ( ) {
97+ const statuses = [
98+ createStatus ( { id : '1' , context : 'Build Linux / x86-64' , event : 'push' , workflowName : 'Build Linux' , state : CheckState . Success } ) ,
99+ createStatus ( { id : '2' , context : 'Build Linux / x86-64' , event : 'push' , workflowName : 'Build Linux' , state : CheckState . Success } ) ,
100+ ] ;
101+ const result = callDeduplicateStatusChecks ( repo , statuses ) ;
102+ assert . strictEqual ( result . length , 1 ) ;
103+ assert . strictEqual ( result [ 0 ] . id , '2' ) ; // higher ID preferred
104+ } ) ;
105+
106+ it ( 'keeps checks from different workflows as separate entries' , function ( ) {
107+ const statuses = [
108+ createStatus ( { id : '1' , context : 'build' , event : 'push' , workflowName : 'CI' } ) ,
109+ createStatus ( { id : '2' , context : 'build' , event : 'push' , workflowName : 'Nightly' } ) ,
110+ ] ;
111+ const result = callDeduplicateStatusChecks ( repo , statuses ) ;
112+ assert . strictEqual ( result . length , 2 ) ;
113+ } ) ;
114+
115+ it ( 'prefers pending checks over completed ones during deduplication' , function ( ) {
116+ const statuses = [
117+ createStatus ( { id : '1' , context : 'test' , event : 'push' , workflowName : 'CI' , state : CheckState . Success } ) ,
118+ createStatus ( { id : '2' , context : 'test' , event : 'push' , workflowName : 'CI' , state : CheckState . Pending } ) ,
119+ ] ;
120+ const result = callDeduplicateStatusChecks ( repo , statuses ) ;
121+ assert . strictEqual ( result . length , 1 ) ;
122+ assert . strictEqual ( result [ 0 ] . state , CheckState . Pending ) ;
123+ } ) ;
124+
125+ it ( 'handles status contexts without event or workflowName' , function ( ) {
126+ const statuses = [
127+ createStatus ( { id : '1' , context : 'ci/jenkins' , isCheckRun : false } ) ,
128+ createStatus ( { id : '2' , context : 'ci/travis' , isCheckRun : false } ) ,
129+ ] ;
130+ const result = callDeduplicateStatusChecks ( repo , statuses ) ;
131+ assert . strictEqual ( result . length , 2 ) ;
132+ } ) ;
133+ } ) ;
55134} ) ;
0 commit comments