Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/prompt/node/test/streamingEdits.spec.ts
13405 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, suite, test } from 'vitest';
7
import { AsyncIterableObject, AsyncIterableSource } from '../../../../util/vs/base/common/async';
8
import { LineFilters, streamLines } from '../streamingEdits';
9
10
suite('streamLinesInCodeBlock', function () {
11
test('no code', async function () {
12
13
const source = new AsyncIterableSource<string>();
14
source.emitOne('Hello');
15
source.emitOne('World');
16
source.resolve();
17
const stream = streamLinesInCodeBlock(source.asyncIterable);
18
const actual = await AsyncIterableObject.toPromise(stream);
19
assert.deepStrictEqual(actual, []);
20
});
21
22
test('emits no lines outside code block', async function () {
23
24
const source = new AsyncIterableSource<string>();
25
const input = [
26
'Hello World',
27
'```py',
28
'# Hello World',
29
'foo',
30
'```',
31
'END',
32
];
33
source.emitOne(input.join('\n'));
34
source.resolve();
35
36
const stream = streamLinesInCodeBlock(source.asyncIterable);
37
const actual = await AsyncIterableObject.toPromise(stream);
38
assert.deepStrictEqual(actual, input.slice(2, 4));
39
});
40
41
test.skip('emits no lines outside code block, N blocks', async function () {
42
43
const source = new AsyncIterableSource<string>();
44
const input = [
45
'Hello World',
46
'```py',
47
'# Hello World',
48
'foo',
49
'```',
50
'MID',
51
'```ts',
52
'type Foo = number',
53
'console.log()',
54
'```',
55
];
56
source.emitOne(input.join('\n'));
57
source.resolve();
58
59
const stream = streamLinesInCodeBlock(source.asyncIterable);
60
const actual = await AsyncIterableObject.toPromise(stream);
61
assert.deepStrictEqual(actual, [input[2], input[3], input[7], input[8]]);
62
});
63
});
64
65
/**
66
* Extract just the lines that are inside a code block.
67
*/
68
function streamLinesInCodeBlock(source: AsyncIterable<string>): AsyncIterableObject<string> {
69
return (
70
streamLines(source)
71
.filter(LineFilters.createCodeBlockFilter())
72
.map(line => line.value)
73
);
74
}
75
76