Skip to content

Commit da754a2

Browse files
Create basic test set up and first test
Co-authored-by: Nora Scheuch <norascheuch@github.com>
1 parent 4a237ba commit da754a2

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import * as fs from 'fs-extra';
2+
import * as path from 'path';
3+
import * as sinon from 'sinon';
4+
import { expect } from 'chai';
5+
6+
import {
7+
ExtensionContext,
8+
Uri,
9+
} from 'vscode';
10+
import { QueryHistoryConfig } from '../../../config';
11+
import { DatabaseManager } from '../../../databases';
12+
import { tmpDir } from '../../../helpers';
13+
import { QueryHistoryManager } from '../../../query-history';
14+
import { DisposableBucket } from '../../disposable-bucket';
15+
import { testDisposeHandler } from '../../test-dispose-handler';
16+
import { walkDirectory } from '../../../helpers';
17+
import { HistoryItemLabelProvider } from '../../../history-item-label-provider';
18+
import { RemoteQueriesManager } from '../../../remote-queries/remote-queries-manager';
19+
import { ResultsView } from '../../../interface';
20+
import { EvalLogViewer } from '../../../eval-log-viewer';
21+
import { QueryRunner } from '../../../queryRunner';
22+
import { VariantAnalysisManager } from '../../../remote-queries/variant-analysis-manager';
23+
24+
/**
25+
* Tests for variant analyses and how they interact with the query history manager.
26+
*/
27+
28+
describe('Variant Analyses and QueryHistoryManager', function() {
29+
const EXTENSION_PATH = path.join(__dirname, '../../../../');
30+
const STORAGE_DIR = Uri.file(path.join(tmpDir.name, 'variant-analysis')).fsPath;
31+
const asyncNoop = async () => {
32+
/** noop */
33+
};
34+
35+
let sandbox: sinon.SinonSandbox;
36+
let qhm: QueryHistoryManager;
37+
let localQueriesResultsViewStub: ResultsView;
38+
let remoteQueriesManagerStub: RemoteQueriesManager;
39+
let variantAnalysisManagerStub: VariantAnalysisManager;
40+
let rawQueryHistory: any;
41+
let disposables: DisposableBucket;
42+
let openRemoteQueryResultsStub: sinon.SinonStub;
43+
let rehydrateVariantAnalysisStub: sinon.SinonStub;
44+
let removeVariantAnalysisStub: sinon.SinonStub;
45+
46+
beforeEach(async function() {
47+
// set a higher timeout since recursive delete below may take a while, expecially on Windows.
48+
this.timeout(120000);
49+
50+
// Since these tests change the state of the query history manager, we need to copy the original
51+
// to a temporary folder where we can manipulate it for tests
52+
await copyHistoryState();
53+
54+
sandbox = sinon.createSandbox();
55+
disposables = new DisposableBucket();
56+
57+
localQueriesResultsViewStub = {
58+
showResults: sandbox.stub()
59+
} as any as ResultsView;
60+
61+
rehydrateVariantAnalysisStub = sandbox.stub();
62+
removeVariantAnalysisStub = sandbox.stub();
63+
openRemoteQueryResultsStub = sandbox.stub();
64+
65+
remoteQueriesManagerStub = {
66+
onRemoteQueryAdded: sandbox.stub(),
67+
onRemoteQueryRemoved: sandbox.stub(),
68+
onRemoteQueryStatusUpdate: sandbox.stub(),
69+
rehydrateRemoteQuery: sandbox.stub(),
70+
openRemoteQueryResults: openRemoteQueryResultsStub
71+
} as any as RemoteQueriesManager;
72+
73+
variantAnalysisManagerStub = {
74+
onVariantAnalysisAdded: sandbox.stub(),
75+
onVariantAnalysisRemoved: sandbox.stub(),
76+
removeRemoteQuery: removeVariantAnalysisStub,
77+
rehydrateVariantAnalysis: rehydrateVariantAnalysisStub
78+
} as any as VariantAnalysisManager;
79+
80+
rawQueryHistory = fs.readJSONSync(path.join(STORAGE_DIR, 'workspace-query-history.json')).queries;
81+
82+
qhm = new QueryHistoryManager(
83+
{} as QueryRunner,
84+
{} as DatabaseManager,
85+
localQueriesResultsViewStub,
86+
remoteQueriesManagerStub,
87+
variantAnalysisManagerStub,
88+
{} as EvalLogViewer,
89+
STORAGE_DIR,
90+
{
91+
globalStorageUri: Uri.file(STORAGE_DIR),
92+
extensionPath: EXTENSION_PATH
93+
} as ExtensionContext,
94+
{
95+
onDidChangeConfiguration: () => new DisposableBucket(),
96+
} as unknown as QueryHistoryConfig,
97+
new HistoryItemLabelProvider({} as QueryHistoryConfig),
98+
asyncNoop
99+
);
100+
disposables.push(qhm);
101+
});
102+
103+
afterEach(function() {
104+
deleteHistoryState();
105+
disposables.dispose(testDisposeHandler);
106+
sandbox.restore();
107+
});
108+
109+
it('should read query history that has variant analysis history items', async () => {
110+
await qhm.readQueryHistory();
111+
112+
expect(rehydrateVariantAnalysisStub).to.have.callCount(2);
113+
expect(rehydrateVariantAnalysisStub.getCall(0).args[0]).to.deep.eq(rawQueryHistory[0].variantAnalysis);
114+
expect(rehydrateVariantAnalysisStub.getCall(0).args[1]).to.deep.eq(rawQueryHistory[0].status);
115+
expect(rehydrateVariantAnalysisStub.getCall(1).args[0]).to.deep.eq(rawQueryHistory[1].variantAnalysis);
116+
expect(rehydrateVariantAnalysisStub.getCall(1).args[1]).to.deep.eq(rawQueryHistory[1].status);
117+
118+
expect(qhm.treeDataProvider.allHistory[0]).to.deep.eq(rawQueryHistory[0]);
119+
expect(qhm.treeDataProvider.allHistory[1]).to.deep.eq(rawQueryHistory[1]);
120+
expect(qhm.treeDataProvider.allHistory.length).to.eq(2);
121+
});
122+
123+
async function copyHistoryState() {
124+
fs.ensureDirSync(STORAGE_DIR);
125+
fs.copySync(
126+
path.join(__dirname, '../data/variant-analysis/'),
127+
path.join(tmpDir.name, 'variant-analysis')
128+
);
129+
130+
// also, replace the files with 'PLACEHOLDER' so that they have the correct directory
131+
for await (const p of walkDirectory(STORAGE_DIR)) {
132+
replacePlaceholder(path.join(p));
133+
}
134+
}
135+
136+
function replacePlaceholder(filePath: string) {
137+
if (filePath.endsWith('.json')) {
138+
const newContents = fs
139+
.readFileSync(filePath, 'utf8')
140+
.replaceAll('PLACEHOLDER', STORAGE_DIR.replaceAll('\\', '/'));
141+
fs.writeFileSync(filePath, newContents, 'utf8');
142+
}
143+
}
144+
145+
function deleteHistoryState() {
146+
fs.rmSync(STORAGE_DIR, {
147+
recursive: true,
148+
force: true,
149+
maxRetries: 10,
150+
retryDelay: 100
151+
});
152+
}
153+
});

0 commit comments

Comments
 (0)