Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/test/node/editFromDiffGeneration.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
7
import { deepStrictEqual } from 'assert';
8
import { promises as fs, readdirSync } from 'fs';
9
import { suite, test } from 'vitest';
10
import * as path from '../../../util/vs/base/common/path';
11
import { Reporter, createEditsFromPseudoDiff, createEditsFromRealDiff } from '../../prompt/node/editFromDiffGeneration';
12
import { Lines } from '../../prompt/node/editGeneration';
13
import { applyEdits } from '../../prompt/node/intents';
14
15
suite('Real Diff Apply', function () {
16
createTestsFromFixtures(path.join(__dirname, './fixtures/gitdiff'), (original: string, diff: string, expected: string, messages: string[]) => {
17
const reporter = new ReporterImpl();
18
const linesEdits = createEditsFromRealDiff(Lines.fromString(original), Lines.fromString(diff), reporter);
19
const actual = applyEdits(original, linesEdits.map(e => e.toTextEdit()));
20
deepStrictEqual(Lines.fromString(actual), Lines.fromString(expected));
21
deepStrictEqual(reporter.messages, messages);
22
deepStrictEqual(reporter.recovered, []);
23
});
24
});
25
26
suite('Pseudo Diff Apply', function () {
27
createTestsFromFixtures(path.join(__dirname, './fixtures/pseudodiff'), (original: string, diff: string, expected: string, messages: string[]) => {
28
const reporter = new ReporterImpl();
29
const linesEdits = createEditsFromPseudoDiff(Lines.fromString(original), Lines.fromString(diff), reporter);
30
const actual = applyEdits(original, linesEdits.map(e => e.toTextEdit()));
31
deepStrictEqual(Lines.fromString(actual), Lines.fromString(expected));
32
deepStrictEqual(reporter.messages, messages);
33
});
34
});
35
36
class ReporterImpl implements Reporter {
37
public recovered: [number, number][] = [];
38
public messages: string[] = [];
39
40
recovery(originalLine: number, newLine: number) {
41
this.recovered.push([originalLine, newLine]);
42
}
43
warning(message: string) {
44
this.messages.push(message);
45
}
46
}
47
48
49
function createTestsFromFixtures(testDir: string, runTest: (original: string, diff: string, expected: string, messages: string[]) => void) {
50
const entries = readdirSync(testDir);
51
for (const entry of entries) {
52
53
const match = entry.match(/^(\d\d-\w+)-([^.]+)$/);
54
if (match) {
55
test(`${match[1]} - ${match[2].replace(/_/g, ' ')}`, async () => {
56
const expected = await fs.readFile(path.join(testDir, entry), 'utf8');
57
const diff = await fs.readFile(path.join(testDir, `${entry}.diff`), 'utf8');
58
const original = await fs.readFile(path.join(testDir, match[1]), 'utf8');
59
let messages = [];
60
try {
61
messages = JSON.parse(await fs.readFile(path.join(testDir, `${entry}.messages`), 'utf8'));
62
} catch (e) {
63
// ignore
64
}
65
runTest(original, diff, expected, messages);
66
});
67
}
68
}
69
}
70
71