[refactor]: working on unit tests for the `FilePromptContentsProvider`

legomushroom/prompt-snippets/refactor-prompt-parsers
Oleg Solomko 2024-12-19 03:35:12 +00:00 committed by GitHub
parent 83bfc47be5
commit b6270f8f56
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 107 additions and 1 deletions

View File

@ -127,3 +127,5 @@ export function throttle<T>(delay: number, reducer?: IDebounceReducer<T>, initia
};
});
}
export { cancelPreviousCalls } from './decorators/cancelPreviousCalls.js';

View File

@ -9,6 +9,7 @@ import { Range } from '../../../common/core/range.js';
import { VSBuffer } from '../../../../base/common/buffer.js';
import { newWriteableStream } from '../../../../base/common/stream.js';
import { Line } from '../../../common/codecs/linesCodec/tokens/line.js';
import { BaseDecoder } from '../../../../base/common/codecs/baseDecoder.js';
import { NewLine } from '../../../common/codecs/linesCodec/tokens/newLine.js';
import { CarriageReturn } from '../../../common/codecs/linesCodec/tokens/carriageReturn.js';
import { LinesDecoder, TLineToken } from '../../../common/codecs/linesCodec/linesDecoder.js';
@ -159,6 +160,11 @@ suite('LinesDecoder', () => {
"Must return an instance of the original decoder.",
);
assert(
transformed instanceof BaseDecoder,
"Must return an instance of the base decoder.",
);
setTimeout(() => {
stream.write(VSBuffer.fromString('hello\nworld\r\nhow ☁️ are\n\nyou\rdoing\n\n'));

View File

@ -34,7 +34,7 @@ export class FilePromptContentProvider extends PromptContentsProviderBase<FileCh
}
// if file was deleted, forward the event to
// the `getContentsStream` produce an error
// the `getContentsStream()` produce an error
if (event.contains(this.uri, FileChangeType.DELETED)) {
return this.onChangeEmitter.fire(event);
}

View File

@ -15,6 +15,9 @@ import { IModelContentChangedEvent } from '../../../../../editor/common/textMode
* Prompt contents provider for a {@linkcode ITextModel} instance.
*/
export class TextModelContentsProvider extends PromptContentsProviderBase<IModelContentChangedEvent> {
/**
* URI component of the prompt associated with this contents provider.
*/
public readonly uri = this.model.uri;
constructor(

View File

@ -0,0 +1,95 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import assert from 'assert';
import { ReadableStream } from '../../../../../../base/common/stream.js';
import { URI } from '../../../../../../base/common/uri.js';
import { IFileService } from '../../../../../../platform/files/common/files.js';
import { FileService } from '../../../../../../platform/files/common/fileService.js';
import { NullPolicyService } from '../../../../../../platform/policy/common/policy.js';
import { ILogService, NullLogService } from '../../../../../../platform/log/common/log.js';
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../../base/test/common/utils.js';
import { IConfigurationService } from '../../../../../../platform/configuration/common/configuration.js';
import { ConfigurationService } from '../../../../../../platform/configuration/common/configurationService.js';
import { TestInstantiationService } from '../../../../../../platform/instantiation/test/common/instantiationServiceMock.js';
import { VSBuffer } from '../../../../../../base/common/buffer.js';
import { randomInt } from '../../../../../../base/common/numbers.js';
import { FilePromptContentProvider } from '../../../common/promptContentProviders/filePromptContentsProvider.js';
import { Line } from '../../../../../../editor/common/codecs/linesCodec/tokens/line.js';
import { BaseDecoder } from '../../../../../../base/common/codecs/baseDecoder.js';
import { InMemoryFileSystemProvider } from '../../../../../../platform/files/common/inMemoryFilesystemProvider.js';
import { Schemas } from '../../../../../../base/common/network.js';
suite('FilePromptContentsProvider', function () {
const testDisposables = ensureNoDisposablesAreLeakedInTestSuite();
let instantiationService: TestInstantiationService;
setup(async () => {
const nullPolicyService = new NullPolicyService();
const nullLogService = testDisposables.add(new NullLogService());
const nullFileService = testDisposables.add(new FileService(nullLogService));
const nullConfigService = testDisposables.add(new ConfigurationService(
URI.file('/config.json'),
nullFileService,
nullPolicyService,
nullLogService,
));
instantiationService = testDisposables.add(new TestInstantiationService());
const fileSystemProvider = testDisposables.add(new InMemoryFileSystemProvider());
testDisposables.add(nullFileService.registerProvider(Schemas.file, fileSystemProvider));
instantiationService.stub(IFileService, nullFileService);
instantiationService.stub(ILogService, nullLogService);
instantiationService.stub(IConfigurationService, nullConfigService);
});
test('provides contents of a file', async function () {
const fileService = instantiationService.get(IFileService);
const fileName = `file-${randomInt(10000)}.prompt.md`;
const fileUri = URI.file(`/${fileName}`);
if (await fileService.exists(fileUri)) {
await fileService.del(fileUri);
}
await fileService.writeFile(fileUri, VSBuffer.fromString('Hello, world!'));
await new Promise((resolve) => setTimeout(resolve, 5));
const contentsProvider = testDisposables.add(instantiationService.createInstance(
FilePromptContentProvider,
fileUri,
));
let streamOrError: ReadableStream<Line> | Error | undefined;
testDisposables.add(contentsProvider.onContentChanged((event) => {
streamOrError = event;
}));
contentsProvider.start();
await new Promise((resolve) => setTimeout(resolve, 25));
assert(
streamOrError instanceof BaseDecoder,
`Provider must produce a stream of lines, got '${streamOrError}'.`,
);
const stream = testDisposables.add(streamOrError);
const receivedLines = await stream.consumeAll();
assert.strictEqual(
receivedLines.length,
1,
'Must read the correct number of lines from the provider.',
);
const expectedLine = new Line(1, 'Hello, world!');
const receivedLine = receivedLines[0];
assert(
receivedLine.equals(expectedLine),
`Expected to receive '${expectedLine}', got '${receivedLine}'.`,
);
});
});