@@ -11,20 +11,18 @@ import { storagePath } from "../global.helper";
1111import { faker } from "@faker-js/faker" ;
1212import * as ghApiClient from "../../../remote-queries/gh-api/gh-api-client" ;
1313import { createMockVariantAnalysisRepositoryTask } from "../../factories/remote-queries/shared/variant-analysis-repo-tasks" ;
14- import { VariantAnalysisRepositoryTask } from "../../../remote-queries/shared/variant-analysis" ;
14+ import {
15+ VariantAnalysisRepositoryTask ,
16+ VariantAnalysisScannedRepositoryResult ,
17+ } from "../../../remote-queries/shared/variant-analysis" ;
1518
1619jest . setTimeout ( 10_000 ) ;
1720
1821describe ( VariantAnalysisResultsManager . name , ( ) => {
1922 let cli : CodeQLCliServer ;
2023 let variantAnalysisId : number ;
21- let variantAnalysisResultsManager : VariantAnalysisResultsManager ;
2224
2325 beforeEach ( async ( ) => {
24- jest . spyOn ( extLogger , "log" ) . mockResolvedValue ( undefined ) ;
25- jest . spyOn ( fs , "mkdirSync" ) . mockReturnValue ( undefined ) ;
26- jest . spyOn ( fs , "writeFile" ) . mockReturnValue ( undefined ) ;
27-
2826 variantAnalysisId = faker . datatype . number ( ) ;
2927
3028 const extension = await extensions
@@ -33,10 +31,6 @@ describe(VariantAnalysisResultsManager.name, () => {
3331 ) !
3432 . activate ( ) ;
3533 cli = extension . cliServer ;
36- variantAnalysisResultsManager = new VariantAnalysisResultsManager (
37- cli ,
38- extLogger ,
39- ) ;
4034 } ) ;
4135
4236 describe ( "download" , ( ) => {
@@ -49,8 +43,18 @@ describe(VariantAnalysisResultsManager.name, () => {
4943 let dummyRepoTask : VariantAnalysisRepositoryTask ;
5044 let variantAnalysisStoragePath : string ;
5145 let repoTaskStorageDirectory : string ;
46+ let variantAnalysisResultsManager : VariantAnalysisResultsManager ;
5247
5348 beforeEach ( async ( ) => {
49+ jest . spyOn ( extLogger , "log" ) . mockResolvedValue ( undefined ) ;
50+ jest . spyOn ( fs , "mkdirSync" ) . mockReturnValue ( undefined ) ;
51+ jest . spyOn ( fs , "writeFile" ) . mockReturnValue ( undefined ) ;
52+
53+ variantAnalysisResultsManager = new VariantAnalysisResultsManager (
54+ cli ,
55+ extLogger ,
56+ ) ;
57+
5458 dummyRepoTask = createMockVariantAnalysisRepositoryTask ( ) ;
5559
5660 variantAnalysisStoragePath = path . join (
@@ -179,4 +183,184 @@ describe(VariantAnalysisResultsManager.name, () => {
179183 } ) ;
180184 } ) ;
181185 } ) ;
186+
187+ describe ( "loadResults" , ( ) => {
188+ let dummyRepoTask : VariantAnalysisRepositoryTask ;
189+ let variantAnalysisStoragePath : string ;
190+ let repoTaskStorageDirectory : string ;
191+ let variantAnalysisResultsManager : VariantAnalysisResultsManager ;
192+ let onResultLoadedSpy : jest . Mock <
193+ void ,
194+ [ VariantAnalysisScannedRepositoryResult ]
195+ > ;
196+
197+ beforeEach ( ( ) => {
198+ variantAnalysisResultsManager = new VariantAnalysisResultsManager (
199+ cli ,
200+ extLogger ,
201+ ) ;
202+ onResultLoadedSpy = jest . fn ( ) ;
203+ variantAnalysisResultsManager . onResultLoaded ( onResultLoadedSpy ) ;
204+
205+ dummyRepoTask = createMockVariantAnalysisRepositoryTask ( ) ;
206+
207+ variantAnalysisStoragePath = path . join (
208+ storagePath ,
209+ variantAnalysisId . toString ( ) ,
210+ ) ;
211+ repoTaskStorageDirectory =
212+ variantAnalysisResultsManager . getRepoStorageDirectory (
213+ variantAnalysisStoragePath ,
214+ dummyRepoTask . repository . fullName ,
215+ ) ;
216+ } ) ;
217+
218+ afterEach ( async ( ) => {
219+ if ( await fs . pathExists ( variantAnalysisStoragePath ) ) {
220+ await fs . remove ( variantAnalysisStoragePath ) ;
221+ }
222+ } ) ;
223+
224+ describe ( "when results are not downloaded" , ( ) => {
225+ it ( "should reject when results are not cached" , async ( ) => {
226+ await expect (
227+ variantAnalysisResultsManager . loadResults (
228+ variantAnalysisId ,
229+ variantAnalysisStoragePath ,
230+ dummyRepoTask . repository . fullName ,
231+ ) ,
232+ ) . rejects . toThrow ( "Variant analysis results not downloaded" ) ;
233+ } ) ;
234+ } ) ;
235+
236+ describe ( "when the repo task has been written to disk" , ( ) => {
237+ beforeEach ( async ( ) => {
238+ await fs . outputJson (
239+ path . join ( repoTaskStorageDirectory , "repo_task.json" ) ,
240+ dummyRepoTask ,
241+ ) ;
242+ } ) ;
243+
244+ describe ( "when the results are not downloaded" , ( ) => {
245+ it ( "should reject when results are not cached" , async ( ) => {
246+ await expect (
247+ variantAnalysisResultsManager . loadResults (
248+ variantAnalysisId ,
249+ variantAnalysisStoragePath ,
250+ dummyRepoTask . repository . fullName ,
251+ ) ,
252+ ) . rejects . toThrow ( "Missing results file" ) ;
253+ } ) ;
254+ } ) ;
255+
256+ describe ( "when the SARIF results are downloaded" , ( ) => {
257+ beforeEach ( async ( ) => {
258+ await fs . outputJson (
259+ path . join ( repoTaskStorageDirectory , "results/results.sarif" ) ,
260+ await fs . readJson (
261+ path . resolve (
262+ __dirname ,
263+ "../../no-workspace/data/sarif/validSarif.sarif" ,
264+ ) ,
265+ ) ,
266+ ) ;
267+ } ) ;
268+
269+ it ( "should return the results when not cached" , async ( ) => {
270+ await expect (
271+ variantAnalysisResultsManager . loadResults (
272+ variantAnalysisId ,
273+ variantAnalysisStoragePath ,
274+ dummyRepoTask . repository . fullName ,
275+ ) ,
276+ ) . resolves . toHaveProperty ( "interpretedResults" ) ;
277+
278+ expect ( onResultLoadedSpy ) . toHaveBeenCalledTimes ( 1 ) ;
279+ expect ( onResultLoadedSpy ) . toHaveBeenCalledWith (
280+ expect . objectContaining ( {
281+ variantAnalysisId,
282+ repositoryId : dummyRepoTask . repository . id ,
283+ } ) ,
284+ ) ;
285+ } ) ;
286+
287+ it ( "should return the results when cached" , async ( ) => {
288+ // Load into cache
289+ await variantAnalysisResultsManager . loadResults (
290+ variantAnalysisId ,
291+ variantAnalysisStoragePath ,
292+ dummyRepoTask . repository . fullName ,
293+ ) ;
294+
295+ onResultLoadedSpy . mockClear ( ) ;
296+
297+ // Delete the directory so it can't read from disk
298+ await fs . remove ( variantAnalysisStoragePath ) ;
299+
300+ await expect (
301+ variantAnalysisResultsManager . loadResults (
302+ variantAnalysisId ,
303+ variantAnalysisStoragePath ,
304+ dummyRepoTask . repository . fullName ,
305+ ) ,
306+ ) . resolves . toHaveProperty ( "interpretedResults" ) ;
307+
308+ expect ( onResultLoadedSpy ) . toHaveBeenCalledTimes ( 1 ) ;
309+ expect ( onResultLoadedSpy ) . toHaveBeenCalledWith (
310+ expect . objectContaining ( {
311+ variantAnalysisId,
312+ repositoryId : dummyRepoTask . repository . id ,
313+ } ) ,
314+ ) ;
315+ } ) ;
316+
317+ it ( "should not cache when skipCacheStore is given" , async ( ) => {
318+ await variantAnalysisResultsManager . loadResults (
319+ variantAnalysisId ,
320+ variantAnalysisStoragePath ,
321+ dummyRepoTask . repository . fullName ,
322+ {
323+ skipCacheStore : true ,
324+ } ,
325+ ) ;
326+
327+ // Delete the directory so it can't read from disk
328+ await fs . remove ( variantAnalysisStoragePath ) ;
329+
330+ await expect (
331+ variantAnalysisResultsManager . loadResults (
332+ variantAnalysisId ,
333+ variantAnalysisStoragePath ,
334+ dummyRepoTask . repository . fullName ,
335+ ) ,
336+ ) . rejects . toThrow ( "Variant analysis results not downloaded" ) ;
337+
338+ expect ( onResultLoadedSpy ) . not . toHaveBeenCalled ( ) ;
339+ } ) ;
340+
341+ it ( "should use cache when skipCacheStore is given" , async ( ) => {
342+ // Load into cache
343+ await variantAnalysisResultsManager . loadResults (
344+ variantAnalysisId ,
345+ variantAnalysisStoragePath ,
346+ dummyRepoTask . repository . fullName ,
347+ ) ;
348+
349+ // Delete the directory so it can't read from disk
350+ await fs . remove ( variantAnalysisStoragePath ) ;
351+
352+ await expect (
353+ variantAnalysisResultsManager . loadResults (
354+ variantAnalysisId ,
355+ variantAnalysisStoragePath ,
356+ dummyRepoTask . repository . fullName ,
357+ {
358+ skipCacheStore : true ,
359+ } ,
360+ ) ,
361+ ) . resolves . toHaveProperty ( "interpretedResults" ) ;
362+ } ) ;
363+ } ) ;
364+ } ) ;
365+ } ) ;
182366} ) ;
0 commit comments