Path: blob/main/extensions/copilot/src/extension/test/node/extractCodeSnippets.spec.ts
13399 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 assert from 'assert';6import { suite, test } from 'vitest';7import { FileChunk } from '../../../platform/chunking/common/chunk';8import { URI } from '../../../util/vs/base/common/uri';9import { Range } from '../../../util/vs/editor/common/core/range';10import { getSearchResults } from '../../workspaceSemanticSearch/node/semanticSearchTextSearchProvider';1112suite('Extract Code Snippets From Files', () => {1314const uri1 = URI.file('/c:/Users/file1.ts');15const uri2 = URI.file('/c:/Users/file2.ts');1617const fileReader = async (uri: URI): Promise<Uint8Array> => {18if (uri === uri1) {19return Buffer.from(`20const express = require("express");21const patchHandler = require("./patchHandler");22const app = express();23const port = 3001;2425app.get("/", (req, res) => {26console.log('\${new Date()} \${req.method} \${req.path}');27res.send("Hello world!");28});2930const b = [1, 2, 3, 4, 5];3132// This comment shouldn't be included33app.post("/", (req, res) => {34console.log('\${new Date()} \${req.method} \${req.path}');35// Fake comment36// That should be included37res.send("Post");38});3940app.use("/", patchHandler);4142app.listen(port, () => console.log('Example app listening on port \${port}!'));43`);44} else {45return Buffer.from(`46const express = require("express");47const router = express.Router();4849router.patch("/", (req, res) => {50console.log('\${new Date()} \${req.method} \${req.path}');51res.send("Patch");52});5354module.exports = router;55`);56}57};585960test('Return the ranges from the code snippet', async () => {61const range1 = new Range(14, 0, 19, 2);62const range2 = new Range(4, 0, 7, 2);63const fileResults: FileChunk[] = [];64fileResults.push({65file: uri1,66range: range1,67text: `68app.post("/", (req, res) => {69console.log('\${new Date()} \${req.method} \${req.path}');70res.send("Post");71});`,72rawText: undefined,73});74fileResults.push({75file: uri2,76range: range2,77text: `78router.patch("/", (req, res) => {79console.log('\${new Date()} \${req.method} \${req.path}');80res.send("Patch");81});82`,83rawText: undefined,84});8586const results = await getSearchResults(fileReader, fileResults);87assert.strictEqual(results.length, 2);88assert.strictEqual(results[0].ranges[0].sourceRange.start.line, 14);89assert.strictEqual(results[0].ranges[0].sourceRange.end.line, 19);90assert.strictEqual(results[1].ranges[0].sourceRange.start.line, 4);91assert.strictEqual(results[1].ranges[0].sourceRange.end.line, 7);92});93});949596