Path: blob/main/extensions/copilot/src/extension/test/node/streaming.spec.ts
13399 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { expect, it, suite } from 'vitest';6import { AsyncIterableSource, timeout } from '../../../util/vs/base/common/async';7import { forEachStreamed, replaceStringInStream } from '../../prompts/node/inline/utils/streaming';89suite('Streaming', () => {10it('replaceStringInStream', async () => {11const streamSrc = new AsyncIterableSource<string>();1213const resultingStream = replaceStringInStream(streamSrc.asyncIterable, 'aba', 'xxx');14const arr = [] as string[];1516forEachStreamed(resultingStream, value => arr.push(value));17await timeout(1);18expect(arr).toMatchInlineSnapshot(`[]`);19arr.length = 0;2021streamSrc.emitOne('12345'); // nothing happens22await timeout(1);23expect(arr).toMatchInlineSnapshot(`24[25"12345",26]27`);28arr.length = 0;2930streamSrc.emitOne('1aba234aba5'); // aba's get replaced31await timeout(1);32expect(arr).toMatchInlineSnapshot(`33[34"1xxx234xxx5",35]36`);37arr.length = 0;3839streamSrc.emitOne('ab'); // waits for more data40await timeout(1);41expect(arr).toMatchInlineSnapshot(`[]`);42arr.length = 0;4344streamSrc.emitOne('a'); // -> replace45await timeout(1);46expect(arr).toMatchInlineSnapshot(`47[48"xxx",49]50`);51arr.length = 0;525354streamSrc.emitOne('a'); // waits for more data55await timeout(1);56expect(arr).toMatchInlineSnapshot(`[]`);57arr.length = 0;5859streamSrc.emitOne('a'); // cannot emit this a yet, but the previous one60await timeout(1);61expect(arr).toMatchInlineSnapshot(`62[63"a",64]65`);66arr.length = 0;6768streamSrc.emitOne('x'); // flush buffer69await timeout(1);70expect(arr).toMatchInlineSnapshot(`71[72"ax",73]74`);75arr.length = 0;76});77});787980