Skip to content

Commit 993b8c9

Browse files
authored
Merge pull request #1832 from github/koesie10/restructure-varianta-analysis-manager-tests
Restructure variant analysis manager tests
2 parents 79f427c + 0ae7bb8 commit 993b8c9

File tree

1 file changed

+156
-148
lines changed

1 file changed

+156
-148
lines changed

extensions/ql-vscode/src/vscode-tests/cli-integration/remote-queries/variant-analysis-manager.test.ts

Lines changed: 156 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
VariantAnalysis,
4242
VariantAnalysisScannedRepository,
4343
VariantAnalysisScannedRepositoryDownloadStatus,
44+
VariantAnalysisScannedRepositoryState,
4445
VariantAnalysisStatus,
4546
} from "../../../remote-queries/shared/variant-analysis";
4647
import { createTimestampFile } from "../../../helpers";
@@ -365,92 +366,94 @@ describe("Variant Analysis Manager", () => {
365366
});
366367
});
367368

368-
describe("when credentials are invalid", () => {
369-
beforeEach(async () => {
370-
jest
371-
.spyOn(Credentials, "initialize")
372-
.mockResolvedValue(undefined as unknown as Credentials);
373-
});
369+
describe("autoDownloadVariantAnalysisResult", () => {
370+
describe("when credentials are invalid", () => {
371+
beforeEach(async () => {
372+
jest
373+
.spyOn(Credentials, "initialize")
374+
.mockResolvedValue(undefined as unknown as Credentials);
375+
});
374376

375-
it("should return early if credentials are wrong", async () => {
376-
try {
377-
await variantAnalysisManager.autoDownloadVariantAnalysisResult(
378-
scannedRepos[0],
379-
variantAnalysis,
380-
cancellationTokenSource.token,
381-
);
382-
} catch (error: any) {
383-
expect(error.message).toBe("Error authenticating with GitHub");
384-
}
377+
it("should return early if credentials are wrong", async () => {
378+
try {
379+
await variantAnalysisManager.autoDownloadVariantAnalysisResult(
380+
scannedRepos[0],
381+
variantAnalysis,
382+
cancellationTokenSource.token,
383+
);
384+
} catch (error: any) {
385+
expect(error.message).toBe("Error authenticating with GitHub");
386+
}
387+
});
385388
});
386-
});
387389

388-
describe("when credentials are valid", () => {
389-
let arrayBuffer: ArrayBuffer;
390+
describe("when credentials are valid", () => {
391+
let arrayBuffer: ArrayBuffer;
390392

391-
let getVariantAnalysisRepoStub: jest.SpiedFunction<
392-
typeof ghApiClient.getVariantAnalysisRepo
393-
>;
394-
let getVariantAnalysisRepoResultStub: jest.SpiedFunction<
395-
typeof ghApiClient.getVariantAnalysisRepoResult
396-
>;
393+
let getVariantAnalysisRepoStub: jest.SpiedFunction<
394+
typeof ghApiClient.getVariantAnalysisRepo
395+
>;
396+
let getVariantAnalysisRepoResultStub: jest.SpiedFunction<
397+
typeof ghApiClient.getVariantAnalysisRepoResult
398+
>;
397399

398-
beforeEach(async () => {
399-
const mockCredentials = {
400-
getOctokit: () =>
401-
Promise.resolve({
402-
request: jest.fn(),
403-
}),
404-
} as unknown as Credentials;
405-
jest.spyOn(Credentials, "initialize").mockResolvedValue(mockCredentials);
400+
beforeEach(async () => {
401+
const mockCredentials = {
402+
getOctokit: () =>
403+
Promise.resolve({
404+
request: jest.fn(),
405+
}),
406+
} as unknown as Credentials;
407+
jest
408+
.spyOn(Credentials, "initialize")
409+
.mockResolvedValue(mockCredentials);
406410

407-
const sourceFilePath = join(
408-
__dirname,
409-
"../../../../src/vscode-tests/cli-integration/data/variant-analysis-results.zip",
410-
);
411-
arrayBuffer = fs.readFileSync(sourceFilePath).buffer;
411+
const sourceFilePath = join(
412+
__dirname,
413+
"../../../../src/vscode-tests/cli-integration/data/variant-analysis-results.zip",
414+
);
415+
arrayBuffer = fs.readFileSync(sourceFilePath).buffer;
412416

413-
getVariantAnalysisRepoStub = jest.spyOn(
414-
ghApiClient,
415-
"getVariantAnalysisRepo",
416-
);
417-
getVariantAnalysisRepoResultStub = jest.spyOn(
418-
ghApiClient,
419-
"getVariantAnalysisRepoResult",
420-
);
421-
});
417+
getVariantAnalysisRepoStub = jest.spyOn(
418+
ghApiClient,
419+
"getVariantAnalysisRepo",
420+
);
421+
getVariantAnalysisRepoResultStub = jest.spyOn(
422+
ghApiClient,
423+
"getVariantAnalysisRepoResult",
424+
);
425+
});
422426

423-
describe("when the artifact_url is missing", () => {
424-
beforeEach(async () => {
425-
const dummyRepoTask = createMockVariantAnalysisRepoTask();
426-
delete dummyRepoTask.artifact_url;
427+
describe("when the artifact_url is missing", () => {
428+
beforeEach(async () => {
429+
const dummyRepoTask = createMockVariantAnalysisRepoTask();
430+
delete dummyRepoTask.artifact_url;
427431

428-
getVariantAnalysisRepoStub.mockResolvedValue(dummyRepoTask);
429-
getVariantAnalysisRepoResultStub.mockResolvedValue(arrayBuffer);
430-
});
432+
getVariantAnalysisRepoStub.mockResolvedValue(dummyRepoTask);
433+
getVariantAnalysisRepoResultStub.mockResolvedValue(arrayBuffer);
434+
});
431435

432-
it("should not try to download the result", async () => {
433-
await variantAnalysisManager.autoDownloadVariantAnalysisResult(
434-
scannedRepos[0],
435-
variantAnalysis,
436-
cancellationTokenSource.token,
437-
);
436+
it("should not try to download the result", async () => {
437+
await variantAnalysisManager.autoDownloadVariantAnalysisResult(
438+
scannedRepos[0],
439+
variantAnalysis,
440+
cancellationTokenSource.token,
441+
);
438442

439-
expect(getVariantAnalysisRepoResultStub).not.toHaveBeenCalled();
443+
expect(getVariantAnalysisRepoResultStub).not.toHaveBeenCalled();
444+
});
440445
});
441-
});
442446

443-
describe("when the artifact_url is present", () => {
444-
let dummyRepoTask: VariantAnalysisRepoTask;
447+
describe("when the artifact_url is present", () => {
448+
let dummyRepoTask: VariantAnalysisRepoTask;
445449

446-
beforeEach(async () => {
447-
dummyRepoTask = createMockVariantAnalysisRepoTask();
450+
beforeEach(async () => {
451+
dummyRepoTask = createMockVariantAnalysisRepoTask();
448452

449-
getVariantAnalysisRepoStub.mockResolvedValue(dummyRepoTask);
450-
getVariantAnalysisRepoResultStub.mockResolvedValue(arrayBuffer);
451-
});
453+
getVariantAnalysisRepoStub.mockResolvedValue(dummyRepoTask);
454+
getVariantAnalysisRepoResultStub.mockResolvedValue(arrayBuffer);
455+
});
452456

453-
describe("autoDownloadVariantAnalysisResult", () => {
454457
it("should return early if variant analysis is cancelled", async () => {
455458
cancellationTokenSource.cancel();
456459

@@ -626,26 +629,18 @@ describe("Variant Analysis Manager", () => {
626629
});
627630

628631
it("should update the repo state correctly", async () => {
629-
// To set some initial repo states, we need to mock the correct methods so that the repo states are read in.
630-
// The actual tests for these are in rehydrateVariantAnalysis, so we can just mock them here and test that
631-
// the methods are called.
632-
633-
pathExistsStub.mockImplementation(() => true);
634-
// This will read in the correct repo states
635-
readJsonStub.mockImplementation(() =>
636-
Promise.resolve({
637-
[scannedRepos[1].repository.id]: {
638-
repositoryId: scannedRepos[1].repository.id,
639-
downloadStatus:
640-
VariantAnalysisScannedRepositoryDownloadStatus.Succeeded,
641-
},
642-
[scannedRepos[2].repository.id]: {
643-
repositoryId: scannedRepos[2].repository.id,
644-
downloadStatus:
645-
VariantAnalysisScannedRepositoryDownloadStatus.InProgress,
646-
},
647-
}),
648-
);
632+
mockRepoStates({
633+
[scannedRepos[1].repository.id]: {
634+
repositoryId: scannedRepos[1].repository.id,
635+
downloadStatus:
636+
VariantAnalysisScannedRepositoryDownloadStatus.Succeeded,
637+
},
638+
[scannedRepos[2].repository.id]: {
639+
repositoryId: scannedRepos[2].repository.id,
640+
downloadStatus:
641+
VariantAnalysisScannedRepositoryDownloadStatus.InProgress,
642+
},
643+
});
649644

650645
await variantAnalysisManager.rehydrateVariantAnalysis(
651646
variantAnalysis,
@@ -696,78 +691,91 @@ describe("Variant Analysis Manager", () => {
696691
},
697692
);
698693
});
694+
695+
function mockRepoStates(
696+
repoStates: Record<number, VariantAnalysisScannedRepositoryState>,
697+
) {
698+
pathExistsStub.mockImplementation(() => true);
699+
// This will read in the correct repo states
700+
readJsonStub.mockImplementation(() => Promise.resolve(repoStates));
701+
}
699702
});
703+
});
704+
});
700705

701-
describe("enqueueDownload", () => {
702-
it("should pop download tasks off the queue", async () => {
703-
const getResultsSpy = jest.spyOn(
704-
variantAnalysisManager,
705-
"autoDownloadVariantAnalysisResult",
706-
);
706+
describe("enqueueDownload", () => {
707+
beforeEach(async () => {
708+
const mockCredentials = {
709+
getOctokit: () =>
710+
Promise.resolve({
711+
request: jest.fn(),
712+
}),
713+
} as unknown as Credentials;
714+
jest.spyOn(Credentials, "initialize").mockResolvedValue(mockCredentials);
715+
});
707716

708-
await variantAnalysisManager.enqueueDownload(
709-
scannedRepos[0],
710-
variantAnalysis,
711-
cancellationTokenSource.token,
712-
);
713-
await variantAnalysisManager.enqueueDownload(
714-
scannedRepos[1],
715-
variantAnalysis,
716-
cancellationTokenSource.token,
717-
);
718-
await variantAnalysisManager.enqueueDownload(
719-
scannedRepos[2],
720-
variantAnalysis,
721-
cancellationTokenSource.token,
722-
);
717+
it("should pop download tasks off the queue", async () => {
718+
const getResultsSpy = jest
719+
.spyOn(variantAnalysisManager, "autoDownloadVariantAnalysisResult")
720+
.mockResolvedValue(undefined);
723721

724-
expect(variantAnalysisManager.downloadsQueueSize()).toBe(0);
725-
expect(getResultsSpy).toBeCalledTimes(3);
726-
});
727-
});
722+
await variantAnalysisManager.enqueueDownload(
723+
scannedRepos[0],
724+
variantAnalysis,
725+
cancellationTokenSource.token,
726+
);
727+
await variantAnalysisManager.enqueueDownload(
728+
scannedRepos[1],
729+
variantAnalysis,
730+
cancellationTokenSource.token,
731+
);
732+
await variantAnalysisManager.enqueueDownload(
733+
scannedRepos[2],
734+
variantAnalysis,
735+
cancellationTokenSource.token,
736+
);
728737

729-
describe("removeVariantAnalysis", () => {
730-
let removeAnalysisResultsStub: jest.SpiedFunction<
731-
typeof variantAnalysisResultsManager.removeAnalysisResults
732-
>;
733-
let removeStorageStub: jest.SpiedFunction<typeof fs.remove>;
734-
let dummyVariantAnalysis: VariantAnalysis;
738+
expect(variantAnalysisManager.downloadsQueueSize()).toBe(0);
739+
expect(getResultsSpy).toBeCalledTimes(3);
740+
});
741+
});
735742

736-
beforeEach(async () => {
737-
dummyVariantAnalysis = createMockVariantAnalysis({});
743+
describe("removeVariantAnalysis", () => {
744+
let removeAnalysisResultsStub: jest.SpiedFunction<
745+
typeof variantAnalysisResultsManager.removeAnalysisResults
746+
>;
747+
let removeStorageStub: jest.SpiedFunction<typeof fs.remove>;
748+
let dummyVariantAnalysis: VariantAnalysis;
738749

739-
removeAnalysisResultsStub = jest
740-
.spyOn(variantAnalysisResultsManager, "removeAnalysisResults")
741-
.mockReturnValue(undefined);
750+
beforeEach(async () => {
751+
dummyVariantAnalysis = createMockVariantAnalysis({});
742752

743-
removeStorageStub = jest
744-
.spyOn(fs, "remove")
745-
.mockReturnValue(undefined);
746-
});
753+
removeAnalysisResultsStub = jest
754+
.spyOn(variantAnalysisResultsManager, "removeAnalysisResults")
755+
.mockReturnValue(undefined);
747756

748-
it("should remove variant analysis", async () => {
749-
pathExistsStub.mockImplementation(() => true);
750-
await variantAnalysisManager.rehydrateVariantAnalysis(
751-
dummyVariantAnalysis,
752-
);
753-
expect(pathExistsStub).toBeCalledWith(
754-
join(storagePath, dummyVariantAnalysis.id.toString()),
755-
);
756-
expect(variantAnalysisManager.variantAnalysesSize).toBe(1);
757+
removeStorageStub = jest.spyOn(fs, "remove").mockReturnValue(undefined);
758+
});
757759

758-
await variantAnalysisManager.removeVariantAnalysis(
759-
dummyVariantAnalysis,
760-
);
760+
it("should remove variant analysis", async () => {
761+
pathExistsStub.mockImplementation(() => true);
762+
await variantAnalysisManager.rehydrateVariantAnalysis(
763+
dummyVariantAnalysis,
764+
);
765+
expect(pathExistsStub).toBeCalledWith(
766+
join(storagePath, dummyVariantAnalysis.id.toString()),
767+
);
768+
expect(variantAnalysisManager.variantAnalysesSize).toBe(1);
761769

762-
expect(removeAnalysisResultsStub).toBeCalledTimes(1);
763-
expect(removeStorageStub).toBeCalledTimes(1);
764-
expect(variantAnalysisManager.variantAnalysesSize).toBe(0);
765-
});
766-
});
770+
await variantAnalysisManager.removeVariantAnalysis(dummyVariantAnalysis);
771+
772+
expect(removeAnalysisResultsStub).toBeCalledTimes(1);
773+
expect(removeStorageStub).toBeCalledTimes(1);
774+
expect(variantAnalysisManager.variantAnalysesSize).toBe(0);
767775
});
768776
});
769777

770-
describe("when rehydrating a query", () => {
778+
describe("rehydrateVariantAnalysis", () => {
771779
let variantAnalysis: VariantAnalysis;
772780
const variantAnalysisRemovedSpy = jest.fn();
773781
let executeCommandSpy: jest.SpiedFunction<typeof commands.executeCommand>;

0 commit comments

Comments
 (0)