Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/mcp/test/vscode-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 * as fs from 'fs/promises';
7
import path from 'path';
8
import { FetchOptions, IAbortController, IFetcherService, PaginationOptions, Response, WebSocketConnection } from '../../../../platform/networking/common/fetcherService';
9
import { CancellationToken } from '../../../../util/vs/base/common/cancellation';
10
import { Event } from '../../../../util/vs/base/common/event';
11
import { ICommandExecutor } from '../../vscode-node/util';
12
13
type CommandResult = { fileName?: string; stdout?: string; exitCode: number };
14
15
export class FixtureCommandExecutor implements ICommandExecutor {
16
commands: Array<{ command: string; args: string[]; cwd: string }> = [];
17
18
constructor(public readonly fullCommandToResultMap: Map<string, CommandResult> = new Map()) { }
19
20
async executeWithTimeout(command: string, args: string[], cwd: string, timeoutMs?: number, expectZeroExitCode?: boolean, cancellationToken?: CancellationToken): Promise<{ stdout: string; stderr: string; exitCode: number }> {
21
this.commands.push({ command, args, cwd });
22
23
let stdout: string = '';
24
let exitCode: number = 1;
25
if (this.fullCommandToResultMap) {
26
const fullCommand = `${command} ${args.join(' ')}`;
27
const result = this.fullCommandToResultMap.get(fullCommand);
28
if (result) {
29
exitCode = result.exitCode;
30
if (result.fileName) {
31
const filePath = path.join(__dirname, 'fixtures', 'snapshots', result.fileName);
32
stdout = await fs.readFile(filePath, 'utf-8');
33
} else if (result.stdout) {
34
stdout = result.stdout;
35
}
36
}
37
}
38
39
if (expectZeroExitCode && exitCode !== 0) {
40
return Promise.reject(new Error(`Expected zero exit code but got ${exitCode}`));
41
}
42
43
return Promise.resolve({
44
exitCode,
45
stdout,
46
stderr: '',
47
});
48
}
49
}
50
51
export class FixtureFetcherService implements IFetcherService {
52
urls: Array<string> = [];
53
54
constructor(readonly urlToFileNameMap: Map<string, { fileName: string; status: number }> = new Map()) { }
55
56
async fetch(url: string, options: FetchOptions): Promise<Response> {
57
this.urls.push(url);
58
59
const result = this.urlToFileNameMap?.get(url);
60
if (!result) {
61
return Promise.resolve({
62
ok: false,
63
status: 404,
64
json: async () => ({ message: 'Not Found' }),
65
} as Response);
66
} else {
67
const filePath = path.join(__dirname, 'fixtures', 'snapshots', result.fileName);
68
const content = await fs.readFile(filePath, 'utf-8');
69
return Promise.resolve({
70
ok: result.status === 200,
71
status: result.status,
72
text: async () => content,
73
json: async () => JSON.parse(content),
74
} as Response);
75
}
76
}
77
78
async fetchWithPagination<T>(baseUrl: string, options: PaginationOptions<T>): Promise<T[]> {
79
const items: T[] = [];
80
const pageSize = options.pageSize ?? 20;
81
let page = options.startPage ?? 1;
82
let hasNextPage = false;
83
84
do {
85
const url = options.buildUrl(baseUrl, pageSize, page);
86
const response = await this.fetch(url, options);
87
88
if (!response.ok) {
89
// Return what we've collected so far if request fails
90
return items;
91
}
92
93
const data = await response.json();
94
const pageItems = options.getItemsFromResponse(data);
95
items.push(...pageItems);
96
97
hasNextPage = pageItems.length === pageSize;
98
page++;
99
} while (hasNextPage);
100
101
return items;
102
}
103
104
_serviceBrand: undefined;
105
readonly onDidFetch = Event.None;
106
readonly onDidCompleteFetch = Event.None;
107
getUserAgentLibrary(): string { throw new Error('Method not implemented.'); }
108
createWebSocket(_url: string): WebSocketConnection { throw new Error('Method not implemented.'); }
109
disconnectAll(): Promise<unknown> { throw new Error('Method not implemented.'); }
110
makeAbortController(): IAbortController { throw new Error('Method not implemented.'); }
111
isAbortError(e: any): boolean { throw new Error('Method not implemented.'); }
112
isInternetDisconnectedError(e: any): boolean { throw new Error('Method not implemented.'); }
113
isFetcherError(e: any): boolean { throw new Error('Method not implemented.'); }
114
isNetworkProcessCrashedError(e: any): boolean { throw new Error('Method not implemented.'); }
115
getUserMessageForFetcherError(err: any): string { throw new Error('Method not implemented.'); }
116
}
117
118