11import * as sinon from 'sinon' ;
22import { expect } from 'chai' ;
3- import { CancellationTokenSource , extensions } from 'vscode' ;
3+ import { CancellationTokenSource , commands , extensions } from 'vscode' ;
44import { CodeQLExtensionInterface } from '../../../extension' ;
55import * as config from '../../../config' ;
66
77import * as ghApiClient from '../../../remote-queries/gh-api/gh-api-client' ;
88import { VariantAnalysisMonitor } from '../../../remote-queries/variant-analysis-monitor' ;
99import {
1010 VariantAnalysis as VariantAnalysisApiResponse ,
11+ VariantAnalysisFailureReason ,
1112 VariantAnalysisScannedRepository as ApiVariantAnalysisScannedRepository ,
12- VariantAnalysisFailureReason
1313} from '../../../remote-queries/gh-api/variant-analysis' ;
1414import { createFailedMockApiResponse , createMockApiResponse } from '../../factories/remote-queries/gh-api/variant-analysis-api-response' ;
1515import { VariantAnalysis , VariantAnalysisStatus } from '../../../remote-queries/shared/variant-analysis' ;
1616import { createMockScannedRepos } from '../../factories/remote-queries/gh-api/scanned-repositories' ;
1717import { processFailureReason } from '../../../remote-queries/variant-analysis-processor' ;
1818import { Credentials } from '../../../authentication' ;
1919import { createMockVariantAnalysis } from '../../factories/remote-queries/shared/variant-analysis' ;
20+ import { VariantAnalysisManager } from '../../../remote-queries/variant-analysis-manager' ;
2021
2122describe ( 'Variant Analysis Monitor' , async function ( ) {
2223 this . timeout ( 60000 ) ;
2324
2425 let sandbox : sinon . SinonSandbox ;
26+ let extension : CodeQLExtensionInterface | Record < string , never > ;
2527 let mockGetVariantAnalysis : sinon . SinonStub ;
2628 let cancellationTokenSource : CancellationTokenSource ;
2729 let variantAnalysisMonitor : VariantAnalysisMonitor ;
2830 let variantAnalysis : VariantAnalysis ;
31+ let variantAnalysisManager : VariantAnalysisManager ;
32+ let mockGetDownloadResult : sinon . SinonStub ;
2933
3034 beforeEach ( async ( ) => {
3135 sandbox = sinon . createSandbox ( ) ;
@@ -36,12 +40,15 @@ describe('Variant Analysis Monitor', async function() {
3640 variantAnalysis = createMockVariantAnalysis ( ) ;
3741
3842 try {
39- const extension = await extensions . getExtension < CodeQLExtensionInterface | Record < string , never > > ( 'GitHub.vscode-codeql' ) ! . activate ( ) ;
43+ extension = await extensions . getExtension < CodeQLExtensionInterface | Record < string , never > > ( 'GitHub.vscode-codeql' ) ! . activate ( ) ;
4044 variantAnalysisMonitor = new VariantAnalysisMonitor ( extension . ctx ) ;
4145 } catch ( e ) {
4246 fail ( e as Error ) ;
4347 }
4448
49+ variantAnalysisManager = extension . variantAnalysisManager ;
50+ mockGetDownloadResult = sandbox . stub ( variantAnalysisManager , 'autoDownloadVariantAnalysisResult' ) ;
51+
4552 limitNumberOfAttemptsToMonitor ( ) ;
4653 } ) ;
4754
@@ -110,20 +117,47 @@ describe('Variant Analysis Monitor', async function() {
110117 describe ( 'when the variant analysis is in progress' , async ( ) => {
111118 let mockApiResponse : VariantAnalysisApiResponse ;
112119 let scannedRepos : ApiVariantAnalysisScannedRepository [ ] ;
120+ let succeededRepos : ApiVariantAnalysisScannedRepository [ ] ;
113121
114122 describe ( 'when there are successfully scanned repos' , async ( ) => {
115123 beforeEach ( async function ( ) {
116- scannedRepos = createMockScannedRepos ( [ 'pending' , 'in_progress' , 'succeeded' ] ) ;
124+ scannedRepos = createMockScannedRepos ( [ 'pending' , 'pending' , ' in_progress' , 'in_progress' , 'succeeded' , 'succeeded ', 'succeeded' ] ) ;
117125 mockApiResponse = createMockApiResponse ( 'completed' , scannedRepos ) ;
118126 mockGetVariantAnalysis = sandbox . stub ( ghApiClient , 'getVariantAnalysis' ) . resolves ( mockApiResponse ) ;
127+ succeededRepos = scannedRepos . filter ( r => r . analysis_status === 'succeeded' ) ;
119128 } ) ;
120129
121130 it ( 'should succeed and return a list of scanned repo ids' , async ( ) => {
122131 const result = await variantAnalysisMonitor . monitorVariantAnalysis ( variantAnalysis , cancellationTokenSource . token ) ;
123- const scannedRepoIds = scannedRepos . filter ( r => r . analysis_status == 'succeeded' ) . map ( r => r . repository . id ) ;
124132
125133 expect ( result . status ) . to . equal ( 'CompletedSuccessfully' ) ;
126- expect ( result . scannedReposDownloaded ) . to . eql ( scannedRepoIds ) ;
134+ expect ( result . scannedReposDownloaded ) . to . eql ( succeededRepos . map ( r => r . repository . id ) ) ;
135+ } ) ;
136+
137+ it ( 'should trigger a download extension command for each repo' , async ( ) => {
138+ const succeededRepos = scannedRepos . filter ( r => r . analysis_status === 'succeeded' ) ;
139+ const commandSpy = sandbox . spy ( commands , 'executeCommand' ) ;
140+
141+ await variantAnalysisMonitor . monitorVariantAnalysis ( variantAnalysis , cancellationTokenSource . token ) ;
142+
143+ expect ( commandSpy ) . to . have . callCount ( succeededRepos . length ) ;
144+
145+ succeededRepos . forEach ( ( succeededRepo , index ) => {
146+ expect ( commandSpy . getCall ( index ) . args [ 0 ] ) . to . eq ( 'codeQL.autoDownloadVariantAnalysisResult' ) ;
147+ expect ( commandSpy . getCall ( index ) . args [ 1 ] ) . to . eq ( succeededRepo ) ;
148+ expect ( commandSpy . getCall ( index ) . args [ 2 ] ) . to . eq ( mockApiResponse ) ;
149+ } ) ;
150+ } ) ;
151+
152+ it ( 'should download all available results' , async ( ) => {
153+ await variantAnalysisMonitor . monitorVariantAnalysis ( variantAnalysis , cancellationTokenSource . token ) ;
154+
155+ expect ( mockGetDownloadResult ) . to . have . callCount ( succeededRepos . length ) ;
156+
157+ succeededRepos . forEach ( ( succeededRepo , index ) => {
158+ expect ( mockGetDownloadResult . getCall ( index ) . args [ 0 ] ) . to . eq ( succeededRepo ) ;
159+ expect ( mockGetDownloadResult . getCall ( index ) . args [ 1 ] ) . to . eq ( mockApiResponse ) ;
160+ } ) ;
127161 } ) ;
128162 } ) ;
129163
@@ -142,6 +176,12 @@ describe('Variant Analysis Monitor', async function() {
142176 expect ( result . status ) . to . equal ( 'CompletedSuccessfully' ) ;
143177 expect ( result . scannedReposDownloaded ) . to . eql ( [ ] ) ;
144178 } ) ;
179+
180+ it ( 'should not try to download any repos' , async ( ) => {
181+ await variantAnalysisMonitor . monitorVariantAnalysis ( variantAnalysis , cancellationTokenSource . token ) ;
182+
183+ expect ( mockGetDownloadResult ) . to . not . have . been . called ;
184+ } ) ;
145185 } ) ;
146186
147187 describe ( 'when there are no repos to scan' , async ( ) => {
@@ -157,6 +197,12 @@ describe('Variant Analysis Monitor', async function() {
157197 expect ( result . status ) . to . equal ( 'CompletedSuccessfully' ) ;
158198 expect ( result . scannedReposDownloaded ) . to . eql ( [ ] ) ;
159199 } ) ;
200+
201+ it ( 'should not try to download any repos' , async ( ) => {
202+ await variantAnalysisMonitor . monitorVariantAnalysis ( variantAnalysis , cancellationTokenSource . token ) ;
203+
204+ expect ( mockGetDownloadResult ) . to . not . have . been . called ;
205+ } ) ;
160206 } ) ;
161207 } ) ;
162208 } ) ;
0 commit comments