Path: blob/main/extensions/copilot/test/simulation/setupTests.stest.ts
13389 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*--------------------------------------------------------------------------------------------*/4import * as assert from 'assert';5import * as path from 'path';6import type { ChatResponseFileTree } from 'vscode';7import { createTextDocumentData } from '../../src/util/common/test/shims/textDocument';8import { URI } from '../../src/util/vs/base/common/uri';9import { rubric } from '../base/rubric';10import { ssuite, stest } from '../base/stest';11import { validate } from '../base/validate';12import { discoverScenarios } from '../e2e/scenarioLoader';13import { generateScenarioTestRunner } from '../e2e/scenarioTest';1415ssuite({ title: 'setupTests - recommend', location: 'panel' }, () => {16const scenarioFolder = path.join(__dirname, '..', 'test/scenarios/test-setupTestRecommend');17const scenarios = discoverScenarios(scenarioFolder);18for (const scenario of scenarios) {19stest({ description: scenario[0].json.name }, collection => {20const runner = generateScenarioTestRunner(21scenario.map(tcase => ({22...tcase,23setupCase(accessor, workspace) {24const files = (tcase.json as any).files || [];25for (const file of files) {26workspace.addDocument(createTextDocumentData(27URI.joinPath(workspace.workspaceFolders[0], file),28'',29''30));31}32},33})),34async (accessor, question, answer, rawResponse, turn, scenarioIndex, commands, confirmations) => {35assert.ok(scenario[0].json.keywords, 'expected test case to have keywords');36assert.ok(confirmations.length, 'expected to have a confirmation part');37const err = validate(confirmations[0].buttons!.join(' ').toLowerCase(), scenario[0].json.keywords);38if (err) {39return { success: false, errorMessage: err };40}4142return { success: true, errorMessage: answer };43}44);4546return runner(collection);47});48}49});5051ssuite({ title: 'setupTests - invoke', location: 'panel' }, () => {52const scenarioFolder = path.join(__dirname, '..', 'test/scenarios/test-setupTest');53const scenarios = discoverScenarios(scenarioFolder);54for (const scenario of scenarios) {55stest({ description: scenario[0].json.name }, collection => {56const runner = generateScenarioTestRunner(57scenario.map(tcase => ({58...tcase,59setupCase(accessor, workspace) {60const files = (tcase.json as any).files || [];61for (const file of files) {62workspace.addDocument(createTextDocumentData(63URI.joinPath(workspace.workspaceFolders[0], file),64'',65''66));67}68},69})),70async (accessor, question, answer, rawResponse, turn, scenarioIndex, commands, confirmations, fileTree) => {71const e: {72filePatterns: string[];73installCommandPattern: string;74runCommandPattern: string;75} = (scenario[0].json as any).expectations;7677const files: string[] = [];78const serializeFileTree = (node: ChatResponseFileTree, path: string = ''): void => {79if (node.children) {80node.children.forEach(child => serializeFileTree(child, `${path}${node.name}/`));81} else {82files.push(path + node.name);83}84};85fileTree[0].value.forEach(v => serializeFileTree(v));8687rubric(accessor,88() => {89for (const pattern of e.filePatterns) {90assert.ok(files.some(f => f.match(pattern)), `expected file to match ${pattern}`);91}92},93() => assert.ok(rawResponse.match(e.installCommandPattern), 'expected to have an install command pattern'),94() => assert.ok(rawResponse.match(e.runCommandPattern), 'expected to have a run command pattern'),95);9697return { success: true };98}99);100101return runner(collection);102});103}104});105106107