Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/test/node/patchEditGeneration.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 { deepStrictEqual } from 'assert';
7
import { promises as fs, readdirSync } from 'fs';
8
import { suite, test } from 'vitest';
9
import type { TextEdit, Uri } from 'vscode';
10
import { FetchStreamSource } from '../../../platform/chat/common/chatMLFetcher';
11
import { PromptPathRepresentationService } from '../../../platform/prompts/common/promptPathRepresentationService';
12
import { CancellationToken } from '../../../util/vs/base/common/cancellation';
13
import * as path from '../../../util/vs/base/common/path';
14
import { URI } from '../../../util/vs/base/common/uri';
15
import { Lines } from '../../prompt/node/editGeneration';
16
import { applyEdits } from '../../prompt/node/intents';
17
import { processPatchResponse } from '../../prompts/node/codeMapper/codeMapper';
18
import { getPatchEditReplyProcessor } from '../../prompts/node/codeMapper/patchEditGeneration';
19
import { TestWorkspaceService } from '../../../platform/test/node/testWorkspaceService';
20
21
const fixturesRootFolder = path.join(__dirname, './fixtures/patch');
22
23
suite('PatchEditGeneration - sync', function () {
24
const entries = readdirSync(fixturesRootFolder);
25
for (const entry of entries) {
26
const fixturesFolder = path.join(fixturesRootFolder, entry);
27
createTestsFromFixtures(fixturesFolder, (data) => {
28
const replyProcessor = getPatchEditReplyProcessor(new PromptPathRepresentationService(new TestWorkspaceService()));
29
const res = replyProcessor.process(data.patch, data.original);
30
const actual = applyEdits(data.original, res.edits);
31
deepStrictEqual(Lines.fromString(actual), Lines.fromString(data.expected));
32
});
33
}
34
});
35
36
suite('PatchEditGeneration - async', function () {
37
const entries = readdirSync(fixturesRootFolder);
38
for (const entry of entries) {
39
const fixturesFolder = path.join(fixturesRootFolder, entry);
40
createTestsFromFixtures(fixturesFolder, async (data) => {
41
const input = new FetchStreamSource();
42
input.update(data.patch, { text: data.patch });
43
44
let actual = data.original;
45
const outputCollector = {
46
textEdit(_target: Uri, edits: TextEdit | TextEdit[]) {
47
actual = applyEdits(actual, Array.isArray(edits) ? edits : [edits]);
48
},
49
notebookEdit() {
50
throw new Error('Unexpected notebook edit');
51
}
52
};
53
const promise = processPatchResponse(URI.parse('test://foo/bar'), data.original, input.stream, outputCollector, CancellationToken.None);
54
input.resolve();
55
await promise;
56
deepStrictEqual(Lines.fromString(actual), Lines.fromString(data.expected));
57
});
58
}
59
});
60
61
function createTestsFromFixtures(fixturesFolder: string, runTest: (data: { [key: string]: string }) => void) {
62
const entries = readdirSync(fixturesFolder);
63
const testsData = new Map<string, { [key: string]: Promise<string> }>();
64
for (const entry of entries) {
65
const match = entry.match(/^([^.]+)\.([^.]+)\.(txt|bin)$/);
66
if (match) {
67
const [, testName, inputName] = match;
68
const content = fs.readFile(path.join(fixturesFolder, entry), 'utf8');
69
let data = testsData.get(testName);
70
if (!data) {
71
data = {};
72
testsData.set(testName, data);
73
}
74
data[inputName] = content;
75
}
76
}
77
for (const testName of testsData.keys()) {
78
test(testName, async () => {
79
const dataWithPromises = testsData.get(testName);
80
const data: { [key: string]: string } = {};
81
for (const key in dataWithPromises) {
82
data[key] = await dataWithPromises[key];
83
}
84
runTest(data);
85
});
86
}
87
}
88
89