Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/test/e2e/semanticSearch.stest.ts
13388 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { ssuite, stest } from '../base/stest';
7
import { discoverScenarios } from './scenarioLoader';
8
import { generateScenarioTestRunner, shouldSkip } from './scenarioTest';
9
10
function getFiles(answer: string): string[] {
11
const regex = /\#\#\s+(.*)\n/g;
12
let match;
13
const titles = [];
14
while ((match = regex.exec(answer)) !== null) {
15
titles.push(match[1].trim());
16
}
17
return [...new Set(titles)];
18
}
19
20
function expectedFileDoesMatch(files: string[], target: string): boolean {
21
return files.some(e => {
22
return e.trim().endsWith(target);
23
});
24
}
25
function assertFilesMatch(expected: string[], actual: string[]) {
26
expected.forEach(e => {
27
if (!expectedFileDoesMatch(actual, e)) {
28
throw Error(`Cannot find match for expected file: ${e}. Instead got the following: \n-${actual.join('\n')}`);
29
}
30
});
31
}
32
33
34
ssuite({ title: 'semanticSearch', location: 'panel' }, (inputPath) => {
35
36
// No default cases checked in at the moment
37
if (!inputPath) {
38
return;
39
}
40
41
const scenariosFolder = inputPath;
42
const scenarios = discoverScenarios(scenariosFolder);
43
for (const scenario of scenarios) {
44
45
const fileName = scenario[0].name;
46
const testName = fileName.substring(0, fileName.indexOf('.'));
47
stest.optional(shouldSkip.bind(undefined, scenario), { description: testName },
48
generateScenarioTestRunner(
49
scenario,
50
async (accessor, question, answer) => {
51
if (scenario[0].json.expectedRetrieval !== undefined) {
52
const expected: string[] = scenario[0].json.expectedRetrieval;
53
const actual = getFiles(answer);
54
55
try {
56
assertFilesMatch(expected, actual);
57
return { success: true, errorMessage: answer };
58
} catch (e) {
59
return { success: false, errorMessage: e.message };
60
}
61
// TODO: incorporate `keywords` into the test.
62
// They should already be on the tests, but to test solely file retrieval first, we can ignore them for now.
63
}
64
return { success: false, errorMessage: 'expectedRetrieval not defined' };
65
}));
66
}
67
});
68
69