Merge pull request #1832 from github/koesie10/restructure-varianta-analysis-manager-tests

Restructure variant analysis manager tests
This commit is contained in:
Koen Vlaswinkel
2022-12-08 10:04:19 +01:00
committed by GitHub

View File

@@ -41,6 +41,7 @@ import {
VariantAnalysis, VariantAnalysis,
VariantAnalysisScannedRepository, VariantAnalysisScannedRepository,
VariantAnalysisScannedRepositoryDownloadStatus, VariantAnalysisScannedRepositoryDownloadStatus,
VariantAnalysisScannedRepositoryState,
VariantAnalysisStatus, VariantAnalysisStatus,
} from "../../../remote-queries/shared/variant-analysis"; } from "../../../remote-queries/shared/variant-analysis";
import { createTimestampFile } from "../../../helpers"; import { createTimestampFile } from "../../../helpers";
@@ -365,92 +366,94 @@ describe("Variant Analysis Manager", () => {
}); });
}); });
describe("when credentials are invalid", () => { describe("autoDownloadVariantAnalysisResult", () => {
beforeEach(async () => { describe("when credentials are invalid", () => {
jest
.spyOn(Credentials, "initialize")
.mockResolvedValue(undefined as unknown as Credentials);
});
it("should return early if credentials are wrong", async () => {
try {
await variantAnalysisManager.autoDownloadVariantAnalysisResult(
scannedRepos[0],
variantAnalysis,
cancellationTokenSource.token,
);
} catch (error: any) {
expect(error.message).toBe("Error authenticating with GitHub");
}
});
});
describe("when credentials are valid", () => {
let arrayBuffer: ArrayBuffer;
let getVariantAnalysisRepoStub: jest.SpiedFunction<
typeof ghApiClient.getVariantAnalysisRepo
>;
let getVariantAnalysisRepoResultStub: jest.SpiedFunction<
typeof ghApiClient.getVariantAnalysisRepoResult
>;
beforeEach(async () => {
const mockCredentials = {
getOctokit: () =>
Promise.resolve({
request: jest.fn(),
}),
} as unknown as Credentials;
jest.spyOn(Credentials, "initialize").mockResolvedValue(mockCredentials);
const sourceFilePath = join(
__dirname,
"../../../../src/vscode-tests/cli-integration/data/variant-analysis-results.zip",
);
arrayBuffer = fs.readFileSync(sourceFilePath).buffer;
getVariantAnalysisRepoStub = jest.spyOn(
ghApiClient,
"getVariantAnalysisRepo",
);
getVariantAnalysisRepoResultStub = jest.spyOn(
ghApiClient,
"getVariantAnalysisRepoResult",
);
});
describe("when the artifact_url is missing", () => {
beforeEach(async () => { beforeEach(async () => {
const dummyRepoTask = createMockVariantAnalysisRepoTask(); jest
delete dummyRepoTask.artifact_url; .spyOn(Credentials, "initialize")
.mockResolvedValue(undefined as unknown as Credentials);
getVariantAnalysisRepoStub.mockResolvedValue(dummyRepoTask);
getVariantAnalysisRepoResultStub.mockResolvedValue(arrayBuffer);
}); });
it("should not try to download the result", async () => { it("should return early if credentials are wrong", async () => {
await variantAnalysisManager.autoDownloadVariantAnalysisResult( try {
scannedRepos[0], await variantAnalysisManager.autoDownloadVariantAnalysisResult(
variantAnalysis, scannedRepos[0],
cancellationTokenSource.token, variantAnalysis,
); cancellationTokenSource.token,
);
expect(getVariantAnalysisRepoResultStub).not.toHaveBeenCalled(); } catch (error: any) {
expect(error.message).toBe("Error authenticating with GitHub");
}
}); });
}); });
describe("when the artifact_url is present", () => { describe("when credentials are valid", () => {
let dummyRepoTask: VariantAnalysisRepoTask; let arrayBuffer: ArrayBuffer;
let getVariantAnalysisRepoStub: jest.SpiedFunction<
typeof ghApiClient.getVariantAnalysisRepo
>;
let getVariantAnalysisRepoResultStub: jest.SpiedFunction<
typeof ghApiClient.getVariantAnalysisRepoResult
>;
beforeEach(async () => { beforeEach(async () => {
dummyRepoTask = createMockVariantAnalysisRepoTask(); const mockCredentials = {
getOctokit: () =>
Promise.resolve({
request: jest.fn(),
}),
} as unknown as Credentials;
jest
.spyOn(Credentials, "initialize")
.mockResolvedValue(mockCredentials);
getVariantAnalysisRepoStub.mockResolvedValue(dummyRepoTask); const sourceFilePath = join(
getVariantAnalysisRepoResultStub.mockResolvedValue(arrayBuffer); __dirname,
"../../../../src/vscode-tests/cli-integration/data/variant-analysis-results.zip",
);
arrayBuffer = fs.readFileSync(sourceFilePath).buffer;
getVariantAnalysisRepoStub = jest.spyOn(
ghApiClient,
"getVariantAnalysisRepo",
);
getVariantAnalysisRepoResultStub = jest.spyOn(
ghApiClient,
"getVariantAnalysisRepoResult",
);
}); });
describe("autoDownloadVariantAnalysisResult", () => { describe("when the artifact_url is missing", () => {
beforeEach(async () => {
const dummyRepoTask = createMockVariantAnalysisRepoTask();
delete dummyRepoTask.artifact_url;
getVariantAnalysisRepoStub.mockResolvedValue(dummyRepoTask);
getVariantAnalysisRepoResultStub.mockResolvedValue(arrayBuffer);
});
it("should not try to download the result", async () => {
await variantAnalysisManager.autoDownloadVariantAnalysisResult(
scannedRepos[0],
variantAnalysis,
cancellationTokenSource.token,
);
expect(getVariantAnalysisRepoResultStub).not.toHaveBeenCalled();
});
});
describe("when the artifact_url is present", () => {
let dummyRepoTask: VariantAnalysisRepoTask;
beforeEach(async () => {
dummyRepoTask = createMockVariantAnalysisRepoTask();
getVariantAnalysisRepoStub.mockResolvedValue(dummyRepoTask);
getVariantAnalysisRepoResultStub.mockResolvedValue(arrayBuffer);
});
it("should return early if variant analysis is cancelled", async () => { it("should return early if variant analysis is cancelled", async () => {
cancellationTokenSource.cancel(); cancellationTokenSource.cancel();
@@ -626,26 +629,18 @@ describe("Variant Analysis Manager", () => {
}); });
it("should update the repo state correctly", async () => { it("should update the repo state correctly", async () => {
// To set some initial repo states, we need to mock the correct methods so that the repo states are read in. mockRepoStates({
// The actual tests for these are in rehydrateVariantAnalysis, so we can just mock them here and test that [scannedRepos[1].repository.id]: {
// the methods are called. repositoryId: scannedRepos[1].repository.id,
downloadStatus:
pathExistsStub.mockImplementation(() => true); VariantAnalysisScannedRepositoryDownloadStatus.Succeeded,
// This will read in the correct repo states },
readJsonStub.mockImplementation(() => [scannedRepos[2].repository.id]: {
Promise.resolve({ repositoryId: scannedRepos[2].repository.id,
[scannedRepos[1].repository.id]: { downloadStatus:
repositoryId: scannedRepos[1].repository.id, VariantAnalysisScannedRepositoryDownloadStatus.InProgress,
downloadStatus: },
VariantAnalysisScannedRepositoryDownloadStatus.Succeeded, });
},
[scannedRepos[2].repository.id]: {
repositoryId: scannedRepos[2].repository.id,
downloadStatus:
VariantAnalysisScannedRepositoryDownloadStatus.InProgress,
},
}),
);
await variantAnalysisManager.rehydrateVariantAnalysis( await variantAnalysisManager.rehydrateVariantAnalysis(
variantAnalysis, variantAnalysis,
@@ -696,78 +691,91 @@ describe("Variant Analysis Manager", () => {
}, },
); );
}); });
});
describe("enqueueDownload", () => { function mockRepoStates(
it("should pop download tasks off the queue", async () => { repoStates: Record<number, VariantAnalysisScannedRepositoryState>,
const getResultsSpy = jest.spyOn( ) {
variantAnalysisManager,
"autoDownloadVariantAnalysisResult",
);
await variantAnalysisManager.enqueueDownload(
scannedRepos[0],
variantAnalysis,
cancellationTokenSource.token,
);
await variantAnalysisManager.enqueueDownload(
scannedRepos[1],
variantAnalysis,
cancellationTokenSource.token,
);
await variantAnalysisManager.enqueueDownload(
scannedRepos[2],
variantAnalysis,
cancellationTokenSource.token,
);
expect(variantAnalysisManager.downloadsQueueSize()).toBe(0);
expect(getResultsSpy).toBeCalledTimes(3);
});
});
describe("removeVariantAnalysis", () => {
let removeAnalysisResultsStub: jest.SpiedFunction<
typeof variantAnalysisResultsManager.removeAnalysisResults
>;
let removeStorageStub: jest.SpiedFunction<typeof fs.remove>;
let dummyVariantAnalysis: VariantAnalysis;
beforeEach(async () => {
dummyVariantAnalysis = createMockVariantAnalysis({});
removeAnalysisResultsStub = jest
.spyOn(variantAnalysisResultsManager, "removeAnalysisResults")
.mockReturnValue(undefined);
removeStorageStub = jest
.spyOn(fs, "remove")
.mockReturnValue(undefined);
});
it("should remove variant analysis", async () => {
pathExistsStub.mockImplementation(() => true); pathExistsStub.mockImplementation(() => true);
await variantAnalysisManager.rehydrateVariantAnalysis( // This will read in the correct repo states
dummyVariantAnalysis, readJsonStub.mockImplementation(() => Promise.resolve(repoStates));
); }
expect(pathExistsStub).toBeCalledWith(
join(storagePath, dummyVariantAnalysis.id.toString()),
);
expect(variantAnalysisManager.variantAnalysesSize).toBe(1);
await variantAnalysisManager.removeVariantAnalysis(
dummyVariantAnalysis,
);
expect(removeAnalysisResultsStub).toBeCalledTimes(1);
expect(removeStorageStub).toBeCalledTimes(1);
expect(variantAnalysisManager.variantAnalysesSize).toBe(0);
});
}); });
}); });
}); });
describe("when rehydrating a query", () => { describe("enqueueDownload", () => {
beforeEach(async () => {
const mockCredentials = {
getOctokit: () =>
Promise.resolve({
request: jest.fn(),
}),
} as unknown as Credentials;
jest.spyOn(Credentials, "initialize").mockResolvedValue(mockCredentials);
});
it("should pop download tasks off the queue", async () => {
const getResultsSpy = jest
.spyOn(variantAnalysisManager, "autoDownloadVariantAnalysisResult")
.mockResolvedValue(undefined);
await variantAnalysisManager.enqueueDownload(
scannedRepos[0],
variantAnalysis,
cancellationTokenSource.token,
);
await variantAnalysisManager.enqueueDownload(
scannedRepos[1],
variantAnalysis,
cancellationTokenSource.token,
);
await variantAnalysisManager.enqueueDownload(
scannedRepos[2],
variantAnalysis,
cancellationTokenSource.token,
);
expect(variantAnalysisManager.downloadsQueueSize()).toBe(0);
expect(getResultsSpy).toBeCalledTimes(3);
});
});
describe("removeVariantAnalysis", () => {
let removeAnalysisResultsStub: jest.SpiedFunction<
typeof variantAnalysisResultsManager.removeAnalysisResults
>;
let removeStorageStub: jest.SpiedFunction<typeof fs.remove>;
let dummyVariantAnalysis: VariantAnalysis;
beforeEach(async () => {
dummyVariantAnalysis = createMockVariantAnalysis({});
removeAnalysisResultsStub = jest
.spyOn(variantAnalysisResultsManager, "removeAnalysisResults")
.mockReturnValue(undefined);
removeStorageStub = jest.spyOn(fs, "remove").mockReturnValue(undefined);
});
it("should remove variant analysis", async () => {
pathExistsStub.mockImplementation(() => true);
await variantAnalysisManager.rehydrateVariantAnalysis(
dummyVariantAnalysis,
);
expect(pathExistsStub).toBeCalledWith(
join(storagePath, dummyVariantAnalysis.id.toString()),
);
expect(variantAnalysisManager.variantAnalysesSize).toBe(1);
await variantAnalysisManager.removeVariantAnalysis(dummyVariantAnalysis);
expect(removeAnalysisResultsStub).toBeCalledTimes(1);
expect(removeStorageStub).toBeCalledTimes(1);
expect(variantAnalysisManager.variantAnalysesSize).toBe(0);
});
});
describe("rehydrateVariantAnalysis", () => {
let variantAnalysis: VariantAnalysis; let variantAnalysis: VariantAnalysis;
const variantAnalysisRemovedSpy = jest.fn(); const variantAnalysisRemovedSpy = jest.fn();
let executeCommandSpy: jest.SpiedFunction<typeof commands.executeCommand>; let executeCommandSpy: jest.SpiedFunction<typeof commands.executeCommand>;