Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/linkify/test/node/util.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 from 'assert';
7
import type { FileStat } from 'vscode';
8
import { NullEnvService } from '../../../../platform/env/common/nullEnvService';
9
import { IFileSystemService } from '../../../../platform/filesystem/common/fileSystemService';
10
import { FileType } from '../../../../platform/filesystem/common/fileTypes';
11
import { IWorkspaceService } from '../../../../platform/workspace/common/workspaceService';
12
import { CancellationToken } from '../../../../util/vs/base/common/cancellation';
13
import { URI } from '../../../../util/vs/base/common/uri';
14
import { PromptReference } from '../../../prompt/common/conversation';
15
import { coalesceParts, LinkifiedPart, LinkifiedText, LinkifyLocationAnchor, LinkifySymbolAnchor } from '../../common/linkifiedText';
16
import { ILinkifyService, LinkifyService } from '../../common/linkifyService';
17
18
const workspace = URI.file('/workspace');
19
20
export function workspaceFile(path: string) {
21
return URI.joinPath(workspace, path);
22
}
23
24
export function createMockFsService(listOfFiles: readonly (string | URI)[]): IFileSystemService {
25
const workspaceEntries = listOfFiles.map(f => URI.isUri(f) ? f : workspaceFile(f));
26
return new class implements Partial<IFileSystemService> {
27
async stat(path: URI): Promise<FileStat> {
28
if (path.path === '/' || path.path === workspace.path) {
29
return { ctime: 0, mtime: 0, size: 0, type: FileType.File };
30
}
31
32
const entry = workspaceEntries.find(f => f.toString() === path.toString() || f.toString() === `${path.toString()}/`);
33
if (!entry) {
34
throw new Error(`File not found: ${path}`);
35
}
36
const isDirectory = entry.path.endsWith('/');
37
return { ctime: 0, mtime: 0, size: 0, type: isDirectory ? FileType.Directory : FileType.File };
38
}
39
} as any;
40
}
41
42
export function createMockWorkspaceService(): IWorkspaceService {
43
return new class implements Partial<IWorkspaceService> {
44
getWorkspaceFolders(): URI[] {
45
return [workspace];
46
}
47
48
getWorkspaceFolder(): URI | undefined {
49
return workspace;
50
}
51
52
getWorkspaceFolderName(): string {
53
return 'workspace';
54
}
55
} as any;
56
}
57
58
export function createTestLinkifierService(...listOfFiles: readonly (string | URI)[]): ILinkifyService {
59
return new LinkifyService(
60
createMockFsService(listOfFiles),
61
createMockWorkspaceService(),
62
NullEnvService.Instance
63
);
64
}
65
66
export async function linkify(linkifer: ILinkifyService, text: string, references: readonly PromptReference[] = []): Promise<LinkifiedText> {
67
const linkifier = linkifer.createLinkifier({ requestId: undefined, references }, []);
68
69
const initial = await linkifier.append(text, CancellationToken.None);
70
const flushed = await linkifier.flush(CancellationToken.None);
71
if (!flushed) {
72
return initial;
73
}
74
75
return {
76
parts: coalesceParts(initial.parts.concat(flushed.parts)),
77
};
78
}
79
80
81
export function assertPartsEqual(actualParts: readonly LinkifiedPart[], expectedParts: readonly LinkifiedPart[]) {
82
assert.strictEqual(actualParts.length, expectedParts.length, `got ${JSON.stringify(actualParts)}`);
83
84
for (let i = 0; i < actualParts.length; i++) {
85
const actual = actualParts[i];
86
const expected = expectedParts[i];
87
if (typeof actual === 'string') {
88
assert.strictEqual(actual, expected);
89
} else if (actual instanceof LinkifyLocationAnchor) {
90
assert(expected instanceof LinkifyLocationAnchor, 'Expected LinkifyLocationAnchor');
91
assert.strictEqual(actual.value.toString(), expected.value.toString());
92
} else {
93
assert(actual instanceof LinkifySymbolAnchor);
94
assert(expected instanceof LinkifySymbolAnchor, 'Expected LinkifySymbolAnchor');
95
assert.strictEqual(actual.symbolInformation.name, expected.symbolInformation.name);
96
}
97
}
98
}
99
100