Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/test/node/extractCodeSnippets.spec.ts
13399 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 assert from 'assert';
7
import { suite, test } from 'vitest';
8
import { FileChunk } from '../../../platform/chunking/common/chunk';
9
import { URI } from '../../../util/vs/base/common/uri';
10
import { Range } from '../../../util/vs/editor/common/core/range';
11
import { getSearchResults } from '../../workspaceSemanticSearch/node/semanticSearchTextSearchProvider';
12
13
suite('Extract Code Snippets From Files', () => {
14
15
const uri1 = URI.file('/c:/Users/file1.ts');
16
const uri2 = URI.file('/c:/Users/file2.ts');
17
18
const fileReader = async (uri: URI): Promise<Uint8Array> => {
19
if (uri === uri1) {
20
return Buffer.from(`
21
const express = require("express");
22
const patchHandler = require("./patchHandler");
23
const app = express();
24
const port = 3001;
25
26
app.get("/", (req, res) => {
27
console.log('\${new Date()} \${req.method} \${req.path}');
28
res.send("Hello world!");
29
});
30
31
const b = [1, 2, 3, 4, 5];
32
33
// This comment shouldn't be included
34
app.post("/", (req, res) => {
35
console.log('\${new Date()} \${req.method} \${req.path}');
36
// Fake comment
37
// That should be included
38
res.send("Post");
39
});
40
41
app.use("/", patchHandler);
42
43
app.listen(port, () => console.log('Example app listening on port \${port}!'));
44
`);
45
} else {
46
return Buffer.from(`
47
const express = require("express");
48
const router = express.Router();
49
50
router.patch("/", (req, res) => {
51
console.log('\${new Date()} \${req.method} \${req.path}');
52
res.send("Patch");
53
});
54
55
module.exports = router;
56
`);
57
}
58
};
59
60
61
test('Return the ranges from the code snippet', async () => {
62
const range1 = new Range(14, 0, 19, 2);
63
const range2 = new Range(4, 0, 7, 2);
64
const fileResults: FileChunk[] = [];
65
fileResults.push({
66
file: uri1,
67
range: range1,
68
text: `
69
app.post("/", (req, res) => {
70
console.log('\${new Date()} \${req.method} \${req.path}');
71
res.send("Post");
72
});`,
73
rawText: undefined,
74
});
75
fileResults.push({
76
file: uri2,
77
range: range2,
78
text: `
79
router.patch("/", (req, res) => {
80
console.log('\${new Date()} \${req.method} \${req.path}');
81
res.send("Patch");
82
});
83
`,
84
rawText: undefined,
85
});
86
87
const results = await getSearchResults(fileReader, fileResults);
88
assert.strictEqual(results.length, 2);
89
assert.strictEqual(results[0].ranges[0].sourceRange.start.line, 14);
90
assert.strictEqual(results[0].ranges[0].sourceRange.end.line, 19);
91
assert.strictEqual(results[1].ranges[0].sourceRange.start.line, 4);
92
assert.strictEqual(results[1].ranges[0].sourceRange.end.line, 7);
93
});
94
});
95
96