Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/test/node/streaming.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 { expect, it, suite } from 'vitest';
7
import { AsyncIterableSource, timeout } from '../../../util/vs/base/common/async';
8
import { forEachStreamed, replaceStringInStream } from '../../prompts/node/inline/utils/streaming';
9
10
suite('Streaming', () => {
11
it('replaceStringInStream', async () => {
12
const streamSrc = new AsyncIterableSource<string>();
13
14
const resultingStream = replaceStringInStream(streamSrc.asyncIterable, 'aba', 'xxx');
15
const arr = [] as string[];
16
17
forEachStreamed(resultingStream, value => arr.push(value));
18
await timeout(1);
19
expect(arr).toMatchInlineSnapshot(`[]`);
20
arr.length = 0;
21
22
streamSrc.emitOne('12345'); // nothing happens
23
await timeout(1);
24
expect(arr).toMatchInlineSnapshot(`
25
[
26
"12345",
27
]
28
`);
29
arr.length = 0;
30
31
streamSrc.emitOne('1aba234aba5'); // aba's get replaced
32
await timeout(1);
33
expect(arr).toMatchInlineSnapshot(`
34
[
35
"1xxx234xxx5",
36
]
37
`);
38
arr.length = 0;
39
40
streamSrc.emitOne('ab'); // waits for more data
41
await timeout(1);
42
expect(arr).toMatchInlineSnapshot(`[]`);
43
arr.length = 0;
44
45
streamSrc.emitOne('a'); // -> replace
46
await timeout(1);
47
expect(arr).toMatchInlineSnapshot(`
48
[
49
"xxx",
50
]
51
`);
52
arr.length = 0;
53
54
55
streamSrc.emitOne('a'); // waits for more data
56
await timeout(1);
57
expect(arr).toMatchInlineSnapshot(`[]`);
58
arr.length = 0;
59
60
streamSrc.emitOne('a'); // cannot emit this a yet, but the previous one
61
await timeout(1);
62
expect(arr).toMatchInlineSnapshot(`
63
[
64
"a",
65
]
66
`);
67
arr.length = 0;
68
69
streamSrc.emitOne('x'); // flush buffer
70
await timeout(1);
71
expect(arr).toMatchInlineSnapshot(`
72
[
73
"ax",
74
]
75
`);
76
arr.length = 0;
77
});
78
});
79
80