Path: blob/main/extensions/copilot/src/extension/linkify/test/node/util.ts
13405 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 assert from 'assert';6import type { FileStat } from 'vscode';7import { NullEnvService } from '../../../../platform/env/common/nullEnvService';8import { IFileSystemService } from '../../../../platform/filesystem/common/fileSystemService';9import { FileType } from '../../../../platform/filesystem/common/fileTypes';10import { IWorkspaceService } from '../../../../platform/workspace/common/workspaceService';11import { CancellationToken } from '../../../../util/vs/base/common/cancellation';12import { URI } from '../../../../util/vs/base/common/uri';13import { PromptReference } from '../../../prompt/common/conversation';14import { coalesceParts, LinkifiedPart, LinkifiedText, LinkifyLocationAnchor, LinkifySymbolAnchor } from '../../common/linkifiedText';15import { ILinkifyService, LinkifyService } from '../../common/linkifyService';1617const workspace = URI.file('/workspace');1819export function workspaceFile(path: string) {20return URI.joinPath(workspace, path);21}2223export function createMockFsService(listOfFiles: readonly (string | URI)[]): IFileSystemService {24const workspaceEntries = listOfFiles.map(f => URI.isUri(f) ? f : workspaceFile(f));25return new class implements Partial<IFileSystemService> {26async stat(path: URI): Promise<FileStat> {27if (path.path === '/' || path.path === workspace.path) {28return { ctime: 0, mtime: 0, size: 0, type: FileType.File };29}3031const entry = workspaceEntries.find(f => f.toString() === path.toString() || f.toString() === `${path.toString()}/`);32if (!entry) {33throw new Error(`File not found: ${path}`);34}35const isDirectory = entry.path.endsWith('/');36return { ctime: 0, mtime: 0, size: 0, type: isDirectory ? FileType.Directory : FileType.File };37}38} as any;39}4041export function createMockWorkspaceService(): IWorkspaceService {42return new class implements Partial<IWorkspaceService> {43getWorkspaceFolders(): URI[] {44return [workspace];45}4647getWorkspaceFolder(): URI | undefined {48return workspace;49}5051getWorkspaceFolderName(): string {52return 'workspace';53}54} as any;55}5657export function createTestLinkifierService(...listOfFiles: readonly (string | URI)[]): ILinkifyService {58return new LinkifyService(59createMockFsService(listOfFiles),60createMockWorkspaceService(),61NullEnvService.Instance62);63}6465export async function linkify(linkifer: ILinkifyService, text: string, references: readonly PromptReference[] = []): Promise<LinkifiedText> {66const linkifier = linkifer.createLinkifier({ requestId: undefined, references }, []);6768const initial = await linkifier.append(text, CancellationToken.None);69const flushed = await linkifier.flush(CancellationToken.None);70if (!flushed) {71return initial;72}7374return {75parts: coalesceParts(initial.parts.concat(flushed.parts)),76};77}787980export function assertPartsEqual(actualParts: readonly LinkifiedPart[], expectedParts: readonly LinkifiedPart[]) {81assert.strictEqual(actualParts.length, expectedParts.length, `got ${JSON.stringify(actualParts)}`);8283for (let i = 0; i < actualParts.length; i++) {84const actual = actualParts[i];85const expected = expectedParts[i];86if (typeof actual === 'string') {87assert.strictEqual(actual, expected);88} else if (actual instanceof LinkifyLocationAnchor) {89assert(expected instanceof LinkifyLocationAnchor, 'Expected LinkifyLocationAnchor');90assert.strictEqual(actual.value.toString(), expected.value.toString());91} else {92assert(actual instanceof LinkifySymbolAnchor);93assert(expected instanceof LinkifySymbolAnchor, 'Expected LinkifySymbolAnchor');94assert.strictEqual(actual.symbolInformation.name, expected.symbolInformation.name);95}96}97}9899100