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