11import * as sinon from 'sinon' ;
22import { expect } from 'chai' ;
3- import { CancellationTokenSource , extensions } from 'vscode' ;
3+ import { CancellationTokenSource , commands , Disposable , extensions } from 'vscode' ;
44import { CodeQLExtensionInterface } from '../../../extension' ;
55import { logger } from '../../../logging' ;
66import * as config from '../../../config' ;
@@ -21,6 +21,8 @@ import { createMockVariantAnalysisRepoTask } from '../../factories/remote-querie
2121import { CodeQLCliServer } from '../../../cli' ;
2222import { storagePath } from '../global.helper' ;
2323import { VariantAnalysisResultsManager } from '../../../remote-queries/variant-analysis-results-manager' ;
24+ import { createMockVariantAnalysis } from '../../factories/remote-queries/shared/variant-analysis' ;
25+ import { VariantAnalysis , VariantAnalysisStatus } from '../../../remote-queries/shared/variant-analysis' ;
2426
2527describe ( 'Variant Analysis Manager' , async function ( ) {
2628 let sandbox : sinon . SinonSandbox ;
@@ -165,4 +167,168 @@ describe('Variant Analysis Manager', async function() {
165167 } ) ;
166168 } ) ;
167169 } ) ;
170+
171+ describe ( 'when rehydrating a query' , async ( ) => {
172+ let variantAnalysis : VariantAnalysis ;
173+ let variantAnalysisRemovedFired = false ;
174+ let onVariantAnalysisRemovedListener : Disposable ;
175+ let monitorVariantAnalysisCommandCalled = false ;
176+
177+ beforeEach ( ( ) => {
178+ variantAnalysis = createMockVariantAnalysis ( ) ;
179+
180+ variantAnalysisRemovedFired = false ;
181+ onVariantAnalysisRemovedListener = variantAnalysisManager . onVariantAnalysisRemoved ( ( ) => {
182+ variantAnalysisRemovedFired = true ;
183+ } ) ;
184+
185+ monitorVariantAnalysisCommandCalled = false ;
186+ sandbox . stub ( commands , 'executeCommand' ) . callsFake ( ( command : string ) => {
187+ if ( command === 'codeQL.monitorVariantAnalysis' ) {
188+ monitorVariantAnalysisCommandCalled = true ;
189+ }
190+ return Promise . resolve ( ) ;
191+ } ) ;
192+ } ) ;
193+
194+ afterEach ( ( ) => {
195+ onVariantAnalysisRemovedListener . dispose ( ) ;
196+ } ) ;
197+
198+ describe ( 'when variant analysis record doesn\'t exist' , async ( ) => {
199+ it ( 'should remove the variant analysis' , async ( ) => {
200+ await variantAnalysisManager . rehydrateVariantAnalysis ( variantAnalysis ) ;
201+ expect ( variantAnalysisRemovedFired ) . to . equal ( true ) ;
202+ } ) ;
203+
204+ it ( 'should not trigger a monitoring command' , async ( ) => {
205+ await variantAnalysisManager . rehydrateVariantAnalysis ( variantAnalysis ) ;
206+ expect ( monitorVariantAnalysisCommandCalled ) . to . equal ( false ) ;
207+ } ) ;
208+ } ) ;
209+
210+ describe ( 'when variant analysis record does exist' , async ( ) => {
211+ let variantAnalysisStorageLocation : string ;
212+
213+ beforeEach ( async ( ) => {
214+ variantAnalysisStorageLocation = variantAnalysisManager . getVariantAnalysisStorageLocation ( variantAnalysis . id ) ;
215+ await variantAnalysisManager . prepareStorageDirectory ( variantAnalysis . id ) ;
216+ } ) ;
217+
218+ afterEach ( ( ) => {
219+ fs . rmSync ( variantAnalysisStorageLocation , { recursive : true } ) ;
220+ } ) ;
221+
222+ describe ( 'when the variant analysis is not complete' , async ( ) => {
223+ beforeEach ( ( ) => {
224+ sinon . stub ( variantAnalysisManager , 'isVariantAnalysisComplete' ) . resolves ( false ) ;
225+ } ) ;
226+
227+ it ( 'should not remove the variant analysis' , async ( ) => {
228+ await variantAnalysisManager . rehydrateVariantAnalysis ( variantAnalysis ) ;
229+ expect ( variantAnalysisRemovedFired ) . to . equal ( false ) ;
230+ } ) ;
231+
232+ it ( 'should trigger a monitoring command' , async ( ) => {
233+ await variantAnalysisManager . rehydrateVariantAnalysis ( variantAnalysis ) ;
234+ expect ( monitorVariantAnalysisCommandCalled ) . to . equal ( true ) ;
235+ } ) ;
236+ } ) ;
237+
238+ describe ( 'when the variant analysis is complete' , async ( ) => {
239+ beforeEach ( ( ) => {
240+ sinon . stub ( variantAnalysisManager , 'isVariantAnalysisComplete' ) . resolves ( true ) ;
241+ } ) ;
242+
243+ it ( 'should not remove the variant analysis' , async ( ) => {
244+ await variantAnalysisManager . rehydrateVariantAnalysis ( variantAnalysis ) ;
245+ expect ( variantAnalysisRemovedFired ) . to . equal ( false ) ;
246+ } ) ;
247+
248+ it ( 'should not trigger a monitoring command' , async ( ) => {
249+ await variantAnalysisManager . rehydrateVariantAnalysis ( variantAnalysis ) ;
250+ expect ( monitorVariantAnalysisCommandCalled ) . to . equal ( false ) ;
251+ } ) ;
252+ } ) ;
253+ } ) ;
254+ } ) ;
255+
256+ describe ( 'isVariantAnalysisComplete' , async ( ) => {
257+ let variantAnalysis : VariantAnalysis ;
258+
259+ beforeEach ( ( ) => {
260+ variantAnalysis = createMockVariantAnalysis ( ) ;
261+ } ) ;
262+
263+ describe ( 'when variant analysis status is InProgress' , async ( ) => {
264+ beforeEach ( ( ) => {
265+ variantAnalysis . status = VariantAnalysisStatus . InProgress ;
266+ } ) ;
267+
268+ describe ( 'when scanned repos is undefined' , async ( ) => {
269+ it ( 'should say the variant analysis is not complete' , async ( ) => {
270+ variantAnalysis . scannedRepos = undefined ;
271+ expect ( variantAnalysisManager . isVariantAnalysisComplete ( variantAnalysis ) ) . to . equal ( false ) ;
272+ } ) ;
273+ } ) ;
274+
275+ describe ( 'when scanned repos is non-empty' , async ( ) => {
276+ describe ( 'when not all results are downloaded' , async ( ) => {
277+ it ( 'should say the variant analysis is not complete' , async ( ) => {
278+ sinon . stub ( variantAnalysisResultsManager , 'isVariantAnalysisRepoDownloaded' ) . resolves ( false ) ;
279+ expect ( variantAnalysisManager . isVariantAnalysisComplete ( variantAnalysis ) ) . to . equal ( false ) ;
280+ } ) ;
281+ } ) ;
282+
283+ describe ( 'when all results are downloaded' , async ( ) => {
284+ it ( 'should say the variant analysis is complete' , async ( ) => {
285+ sinon . stub ( variantAnalysisResultsManager , 'isVariantAnalysisRepoDownloaded' ) . resolves ( true ) ;
286+ expect ( variantAnalysisManager . isVariantAnalysisComplete ( variantAnalysis ) ) . to . equal ( true ) ;
287+ } ) ;
288+ } ) ;
289+ } ) ;
290+ } ) ;
291+
292+ for ( const variantAnalysisStatus of [
293+ VariantAnalysisStatus . Succeeded ,
294+ VariantAnalysisStatus . Failed ,
295+ VariantAnalysisStatus . Canceled
296+ ] ) {
297+ describe ( `when variant analysis status is ${ variantAnalysisStatus } ` , async ( ) => {
298+ beforeEach ( ( ) => {
299+ variantAnalysis . status = variantAnalysisStatus ;
300+ } ) ;
301+
302+ describe ( 'when scanned repos is undefined' , async ( ) => {
303+ it ( 'should say the variant analysis is complete' , async ( ) => {
304+ variantAnalysis . scannedRepos = undefined ;
305+ expect ( variantAnalysisManager . isVariantAnalysisComplete ( variantAnalysis ) ) . to . equal ( true ) ;
306+ } ) ;
307+ } ) ;
308+
309+ describe ( 'when scanned repos is empty' , async ( ) => {
310+ it ( 'should say the variant analysis is complete' , async ( ) => {
311+ variantAnalysis . scannedRepos = [ ] ;
312+ expect ( variantAnalysisManager . isVariantAnalysisComplete ( variantAnalysis ) ) . to . equal ( true ) ;
313+ } ) ;
314+ } ) ;
315+
316+ describe ( 'when scanned repos is non-empty' , async ( ) => {
317+ describe ( 'when not all results are downloaded' , async ( ) => {
318+ it ( 'should say the variant analysis is not complete' , async ( ) => {
319+ sinon . stub ( variantAnalysisResultsManager , 'isVariantAnalysisRepoDownloaded' ) . resolves ( false ) ;
320+ expect ( variantAnalysisManager . isVariantAnalysisComplete ( variantAnalysis ) ) . to . equal ( false ) ;
321+ } ) ;
322+ } ) ;
323+
324+ describe ( 'when all results are downloaded' , async ( ) => {
325+ it ( 'should say the variant analysis is complete' , async ( ) => {
326+ sinon . stub ( variantAnalysisResultsManager , 'isVariantAnalysisRepoDownloaded' ) . resolves ( true ) ;
327+ expect ( variantAnalysisManager . isVariantAnalysisComplete ( variantAnalysis ) ) . to . equal ( true ) ;
328+ } ) ;
329+ } ) ;
330+ } ) ;
331+ } ) ;
332+ }
333+ } ) ;
168334} ) ;
0 commit comments