@@ -9,24 +9,28 @@ import * as ghApiClient from '../../../remote-queries/gh-api/gh-api-client';
99import { VariantAnalysisMonitor } from '../../../remote-queries/variant-analysis-monitor' ;
1010import {
1111 VariantAnalysis as VariantAnalysisApiResponse ,
12+ VariantAnalysisFailureReason ,
1213 VariantAnalysisScannedRepository as ApiVariantAnalysisScannedRepository ,
13- VariantAnalysisFailureReason
1414} from '../../../remote-queries/gh-api/variant-analysis' ;
1515import { createFailedMockApiResponse , createMockApiResponse } from '../../factories/remote-queries/gh-api/variant-analysis-api-response' ;
1616import { VariantAnalysis , VariantAnalysisStatus } from '../../../remote-queries/shared/variant-analysis' ;
1717import { createMockScannedRepos } from '../../factories/remote-queries/gh-api/scanned-repositories' ;
1818import { processFailureReason } from '../../../remote-queries/variant-analysis-processor' ;
1919import { Credentials } from '../../../authentication' ;
2020import { createMockVariantAnalysis } from '../../factories/remote-queries/shared/variant-analysis' ;
21+ import { VariantAnalysisManager } from '../../../remote-queries/variant-analysis-manager' ;
2122
2223describe ( 'Variant Analysis Monitor' , async function ( ) {
2324 this . timeout ( 60000 ) ;
2425
2526 let sandbox : sinon . SinonSandbox ;
27+ let extension : CodeQLExtensionInterface | Record < string , never > ;
2628 let mockGetVariantAnalysis : sinon . SinonStub ;
2729 let cancellationTokenSource : CancellationTokenSource ;
2830 let variantAnalysisMonitor : VariantAnalysisMonitor ;
2931 let variantAnalysis : VariantAnalysis ;
32+ let variantAnalysisManager : VariantAnalysisManager ;
33+ let mockGetDownloadResult : sinon . SinonStub ;
3034
3135 beforeEach ( async ( ) => {
3236 sandbox = sinon . createSandbox ( ) ;
@@ -38,12 +42,15 @@ describe('Variant Analysis Monitor', async function() {
3842 variantAnalysis = createMockVariantAnalysis ( ) ;
3943
4044 try {
41- const extension = await extensions . getExtension < CodeQLExtensionInterface | Record < string , never > > ( 'GitHub.vscode-codeql' ) ! . activate ( ) ;
45+ extension = await extensions . getExtension < CodeQLExtensionInterface | Record < string , never > > ( 'GitHub.vscode-codeql' ) ! . activate ( ) ;
4246 variantAnalysisMonitor = new VariantAnalysisMonitor ( extension . ctx , logger ) ;
4347 } catch ( e ) {
4448 fail ( e as Error ) ;
4549 }
4650
51+ variantAnalysisManager = extension . variantAnalysisManager ;
52+ mockGetDownloadResult = sandbox . stub ( variantAnalysisManager , 'autoDownloadVariantAnalysisResult' ) ;
53+
4754 limitNumberOfAttemptsToMonitor ( ) ;
4855 } ) ;
4956
@@ -112,20 +119,21 @@ describe('Variant Analysis Monitor', async function() {
112119 describe ( 'when the variant analysis is in progress' , async ( ) => {
113120 let mockApiResponse : VariantAnalysisApiResponse ;
114121 let scannedRepos : ApiVariantAnalysisScannedRepository [ ] ;
122+ let succeededRepos : ApiVariantAnalysisScannedRepository [ ] ;
115123
116124 describe ( 'when there are successfully scanned repos' , async ( ) => {
117125 beforeEach ( async function ( ) {
118126 scannedRepos = createMockScannedRepos ( [ 'pending' , 'pending' , 'in_progress' , 'in_progress' , 'succeeded' , 'succeeded' , 'succeeded' ] ) ;
119127 mockApiResponse = createMockApiResponse ( 'completed' , scannedRepos ) ;
120128 mockGetVariantAnalysis = sandbox . stub ( ghApiClient , 'getVariantAnalysis' ) . resolves ( mockApiResponse ) ;
129+ succeededRepos = scannedRepos . filter ( r => r . analysis_status === 'succeeded' ) ;
121130 } ) ;
122131
123132 it ( 'should succeed and return a list of scanned repo ids' , async ( ) => {
124133 const result = await variantAnalysisMonitor . monitorVariantAnalysis ( variantAnalysis , cancellationTokenSource . token ) ;
125- const scannedRepoIds = scannedRepos . filter ( r => r . analysis_status == 'succeeded' ) . map ( r => r . repository . id ) ;
126134
127135 expect ( result . status ) . to . equal ( 'CompletedSuccessfully' ) ;
128- expect ( result . scannedReposDownloaded ) . to . eql ( scannedRepoIds ) ;
136+ expect ( result . scannedReposDownloaded ) . to . eql ( succeededRepos . map ( r => r . repository . id ) ) ;
129137 } ) ;
130138
131139 it ( 'should trigger a download extension command for each repo' , async ( ) => {
@@ -142,6 +150,17 @@ describe('Variant Analysis Monitor', async function() {
142150 expect ( commandSpy . getCall ( index ) . args [ 2 ] ) . to . eq ( mockApiResponse ) ;
143151 } ) ;
144152 } ) ;
153+
154+ it ( 'should download all available results' , async ( ) => {
155+ await variantAnalysisMonitor . monitorVariantAnalysis ( variantAnalysis , cancellationTokenSource . token ) ;
156+
157+ expect ( mockGetDownloadResult ) . to . have . callCount ( succeededRepos . length ) ;
158+
159+ succeededRepos . forEach ( ( succeededRepo , index ) => {
160+ expect ( mockGetDownloadResult . getCall ( index ) . args [ 0 ] ) . to . eq ( succeededRepo ) ;
161+ expect ( mockGetDownloadResult . getCall ( index ) . args [ 1 ] ) . to . eq ( mockApiResponse ) ;
162+ } ) ;
163+ } ) ;
145164 } ) ;
146165
147166 describe ( 'when there are only in progress repos' , async ( ) => {
@@ -159,6 +178,12 @@ describe('Variant Analysis Monitor', async function() {
159178 expect ( result . status ) . to . equal ( 'CompletedSuccessfully' ) ;
160179 expect ( result . scannedReposDownloaded ) . to . eql ( [ ] ) ;
161180 } ) ;
181+
182+ it ( 'should not try to download any repos' , async ( ) => {
183+ await variantAnalysisMonitor . monitorVariantAnalysis ( variantAnalysis , cancellationTokenSource . token ) ;
184+
185+ expect ( mockGetDownloadResult ) . to . not . have . been . called ;
186+ } ) ;
162187 } ) ;
163188
164189 describe ( 'when there are no repos to scan' , async ( ) => {
@@ -174,6 +199,12 @@ describe('Variant Analysis Monitor', async function() {
174199 expect ( result . status ) . to . equal ( 'CompletedSuccessfully' ) ;
175200 expect ( result . scannedReposDownloaded ) . to . eql ( [ ] ) ;
176201 } ) ;
202+
203+ it ( 'should not try to download any repos' , async ( ) => {
204+ await variantAnalysisMonitor . monitorVariantAnalysis ( variantAnalysis , cancellationTokenSource . token ) ;
205+
206+ expect ( mockGetDownloadResult ) . to . not . have . been . called ;
207+ } ) ;
177208 } ) ;
178209 } ) ;
179210 } ) ;
0 commit comments